orbit

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidVersion defines the error if the version of both peers do not match
	// during the version exchange.
	ErrIncompatibleVersion = errors.New("invalid version")

	// ErrOpenTimeout defines the error if the opening of a stream timeouts.
	ErrOpenTimeout = errors.New("open timeout")

	// ErrClosed defines the error if a stream is unexpectedly closed.
	ErrClosed = errors.New("closed")
)

Functions

This section is empty.

Types

type AcceptStreamFunc

type AcceptStreamFunc func(net.Conn) error

The AcceptStreamFunc type describes the function that is called whenever a new connection is requested on a peer. It must then handle the new connection, if it could be set up correctly.

type AuthFunc

type AuthFunc func(net.Conn) (value interface{}, err error)

The AuthFunc type describes the function that is used during the authentication phase of the session initialization. It may use the given connection to perform some kind of data exchange between the client and the server. It can return some arbitrary data that will be saved to the session. It must return a non nil error, if the authentication did fail.

type Config

type Config struct {
	// Codec used to encode and decode orbit messages.
	// Defaults to msgpack.
	Codec codec.Codec

	// KeepAliveInterval is how often to perform the keep alive.
	// Default to 30 seconds.
	KeepAliveInterval time.Duration

	// Logger is used to pass in the logger to be used.
	// Uses a default logger to os.Stderr.
	Logger *log.Logger

	// AuthFunc authenticates the session connection if defined.
	// It gets called right after the version byte has been exchanged
	// between client and server. Therefore, not much resources are wasted
	// in case the authentication fails.
	AuthFunc AuthFunc
}

type Init

type Init struct {
	AcceptStreams InitAcceptStreams
	Control       InitControl
	Signaler      InitSignaler
}

The Init type is used during the initialization of the orbit session and contains the definition to accept streams and define exactly one control and one signaler.

type InitAcceptStreams

type InitAcceptStreams map[string]AcceptStreamFunc

The InitAcceptStreams type is a map of AcceptStreamFunc, where the key is the id of the stream and the value the func that is used to accept it.

type InitControl

type InitControl struct {
	Funcs  control.Funcs
	Config *control.Config
}

The InitControl type is used to initialize one control. It contains the functions that the remote peer can call and a config.

type InitControls

type InitControls map[string]InitControl

The InitControls type is a map, where the key is the id of a control and the value the associated InitControl.

type InitMany

type InitMany struct {
	AcceptStreams InitAcceptStreams
	Controls      InitControls
	Signalers     InitSignalers
}

The Init type is used during the initialization of the orbit session and contains the definition to accept streams and define many controls and many signalers.

type InitSignal

type InitSignal struct {
	ID     string
	Filter signaler.FilterFunc
}

The InitSignal type is used to initialize one signal. It contains the id of the signal and a filter for it.

type InitSignaler

type InitSignaler struct {
	Signals []InitSignal
	Config  *control.Config
}

The InitSignaler type is used to initialize one signaler. It contains the signals that can be triggered and a config.

type InitSignalers

type InitSignalers map[string]InitSignaler

The InitSignalers type is a map, where the key is the id of a signaler and the value the associated InitSignaler.

type Server

type Server struct {
	closer.Closer
	// contains filtered or unexported fields
}

Server implements a simple orbit server. It listens with serverWorkers many routines for incoming connections.

func NewServer

func NewServer(ln net.Listener, config *ServerConfig) *Server

NewServer creates a new orbit server. A listener is required the server will use to listen for incoming connections. A config can be provided, where every property of it that has not been set will be initialized with a default value. That makes it possible to overwrite only the interesting properties for the caller.

func NewServerWithCloser

func NewServerWithCloser(ln net.Listener, config *ServerConfig, cl closer.Closer) *Server

NewServerWithCloser creates a new orbit server just like NewServer() does, but you can provide your own closer for it.

func (*Server) Listen

func (l *Server) Listen() error

Listen listens for new socket connections, which it passes to the new connection channel that is read by the server workers. This method is blocking.

func (*Server) NewSessionChan

func (l *Server) NewSessionChan() <-chan *Session

NewSessionChan returns the channel for new incoming sessions.

func (*Server) Session

func (l *Server) Session(id string) (s *Session)

Session obtains a session by its ID. Returns nil if not found.

func (*Server) Sessions

func (l *Server) Sessions() []*Session

Sessions returns a list of all currently connected sessions.

type ServerConfig

type ServerConfig struct {
	// Embed the standard config that both clients and servers share.
	*Config

	// The number of goroutines that handle incoming connections on the server.
	NewConnNumberWorkers int
	// The size of the channel on which new connections are passed to the
	// server workers.
	// Should not be less than NewConnNumberWorkers.
	NewConnChanSize int
	// The size of the channel on which new server sessions are passed into,
	// so that a user of this package can read them from it.
	// Should not be less than NewConnNumberWorkers.
	NewSessionChanSize int
}

type Session

type Session struct {
	closer.Closer

	// Value is a custom value which can be set. In case the config contains
	// a valid AuthFunc, the Value will be set to the return value of it.
	Value interface{}
	// contains filtered or unexported fields
}

The Session type describes a orbit session that is used on both the client and server side, so in general for peers. It contains its underlying connection to the remote peer and may accept new incoming connections by defining AcceptStreamFuncs.

func ClientSession

func ClientSession(conn net.Conn, config *Config) (s *Session, err error)

ClientSession is used to initialize a new client-side session. A config can be provided, where every property of it that has not been set will be initialized with a default value. That makes it possible to overwrite only the interesting properties for the caller. When the connection to the server has been established, one byte containing the version of the client is sent to the server. In case the versions do not match, the server will close the connection. As part of the session setup, the auth func of the config is called, if it has been defined.

func ClientSessionWithCloser

func ClientSessionWithCloser(conn net.Conn, config *Config, cl closer.Closer) (s *Session, err error)

ClientSessionWithCloser initializes a new client-side session, just like ClientSession() does, but allows to hand in an own closer.

func ServerSession

func ServerSession(conn net.Conn, config *Config) (s *Session, err error)

ServerSession is used to initialize a new server-side session. A config can be provided, where every property of it that has not been set will be initialized with a default value. That makes it possible to overwrite only the interesting properties for the caller. When the connection has been established, the server waits for the API version byte of the client. If the versions do not match, the server immediately closes the connection. As part of the session setup, the auth func of the config is called, if it has been defined.

func ServerSessionWithCloser

func ServerSessionWithCloser(conn net.Conn, config *Config, cl closer.Closer) (s *Session, err error)

ServerSessionWithCloser initializes a new server-side session, just as ServerSession() does, but allows to hand in an own closer.

func (*Session) AcceptStream

func (s *Session) AcceptStream(channel string, f AcceptStreamFunc)

AcceptStream registers the given accept handler for the specific channel.

func (*Session) ID

func (s *Session) ID() string

ID returns the session ID. This must be set manually.

func (*Session) Init

func (s *Session) Init(opts *Init) (
	control *control.Control,
	signaler *signaler.Signaler,
	err error,
)

Init initializes the session by using InitMany(), but only defining one control and one signaler. If no more than one control/signaler are needed, this is the more convenient method to call. Ready() must be called manually for the control and signaler afterwards.

func (*Session) InitMany

func (s *Session) InitMany(opts *InitMany) (
	controls map[string]*control.Control,
	signalers map[string]*signaler.Signaler,
	err error,
)

InitMany initializes this session. Pass nil to just start accepting streams. Ready() must be called manually for all controls and signaler afterwards.

func (*Session) IsClient

func (s *Session) IsClient() bool

IsClient returns whether this session is a client connection.

func (*Session) IsServer

func (s *Session) IsServer() bool

IsServer returns whether this session is a server connection.

func (*Session) LocalAddr

func (s *Session) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*Session) OpenStream

func (s *Session) OpenStream(channel string) (stream net.Conn, err error)

OpenStream performs the same task as OpenStreamTimeout, but uses the default write timeout openStreamWriteTimeout.

func (*Session) OpenStreamTimeout

func (s *Session) OpenStreamTimeout(channel string, timeout time.Duration) (stream net.Conn, err error)

OpenStreamTimeout opens a new stream with the given channel ID. Expires after the timeout and returns ErrOpenTimeout.

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*Session) SetID

func (s *Session) SetID(id string)

SetID sets the session ID.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL