Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NextID

func NextID() uint64

    NextID returns a unique ID to identify a request-type message.

    func Route

    func Route(route string, handler MsgHandler)

      Route registers a handler for a specified route. The handler map is global and has no mutex protection. All calls to Route should be done before the Server is started.

      func UseLogger

      func UseLogger(logger dex.Logger)

        UseLogger uses a specified Logger to output package logging info.

        Types

        type Link interface {
        	// Done returns a channel that is closed when the link goes down.
        	Done() <-chan struct{}
        	// ID returns a unique ID by which this connection can be identified.
        	ID() uint64
        	// IP returns the IP address of the peer.
        	IP() string
        	// Send sends the msgjson.Message to the peer.
        	Send(msg *msgjson.Message) error
        	// SendError sends the msgjson.Error to the peer, with reference to a
        	// request message ID.
        	SendError(id uint64, rpcErr *msgjson.Error)
        	// Request sends the Request-type msgjson.Message to the client and registers
        	// a handler for the response.
        	Request(msg *msgjson.Message, f func(Link, *msgjson.Message), expireTime time.Duration, expire func()) error
        	// Banish closes the link and quarantines the client.
        	Banish()
        	// Disconnect closes the link.
        	Disconnect()
        	// Authorized should be called from a request handler when the connection
        	// becomes authorized. Request handlers must be run synchronous with other
        	// reads or it will be a data race with the link's input loop.
        	Authorized()
        }

          Link is an interface for a communication channel with an API client. The reference implementation of a Link-satisfying type is the wsLink, which passes messages over a websocket connection.

          type MsgHandler

          type MsgHandler func(Link, *msgjson.Message) *msgjson.Error

            MsgHandler describes a handler for a specific message route.

            func RouteHandler

            func RouteHandler(route string) MsgHandler

              RouteHandler gets the handler registered to the specified route, if it exists.

              type RPCConfig

              type RPCConfig struct {
              	// ListenAddrs are the addresses on which the server will listen.
              	ListenAddrs []string
              	// The location of the TLS keypair files. If they are not already at the
              	// specified location, a keypair with a self-signed certificate will be
              	// generated and saved to these locations.
              	RPCKey  string
              	RPCCert string
              	// AltDNSNames specifies allowable request addresses for an auto-generated
              	// TLS keypair. Changing AltDNSNames does not force the keypair to be
              	// regenerated. To regenerate, delete or move the old files.
              	AltDNSNames []string
              }

                The RPCConfig is the server configuration settings and the only argument to the server's constructor.

                type Server

                type Server struct {
                	// contains filtered or unexported fields
                }

                  Server is a low-level communications hub. It supports websocket clients and an HTTP API.

                  func NewServer

                  func NewServer(cfg *RPCConfig) (*Server, error)

                    A constructor for an Server. The Server handles a map of clients, each with at least 3 goroutines for communications. The server is TLS-only, and will generate a key pair with a self-signed certificate if one is not provided as part of the RPCConfig. The server also maintains a IP-based quarantine to short-circuit to an error response for misbehaving clients, if necessary.

                    func (*Server) Broadcast

                    func (s *Server) Broadcast(msg *msgjson.Message)

                      Broadcast sends a message to all connected clients. The message should be a notification. See msgjson.NewNotification.

                      func (*Server) Run

                      func (s *Server) Run(ctx context.Context)

                        Run starts the server. Run should be called only after all routes are registered.