Documentation ¶
Overview ¶
Package wirenet provides a streaming Bidirectional TCP server. Useful for NAT traversal.
Index ¶
- Constants
- Variables
- func DefaultRetryPolicy(min, max time.Duration, attemptNum int) time.Duration
- func LoadCertificates(name string, certPath string) (*tls.Config, error)
- type ErrorHandler
- type Handler
- type Identification
- type OpError
- type Option
- func WithConnectHook(hook func(io.Closer)) Option
- func WithErrorHandler(h ErrorHandler) Option
- func WithIdentification(id Identification, token Token) Option
- func WithKeepAlive(flag bool) Option
- func WithKeepAliveInterval(interval time.Duration) Option
- func WithLogWriter(w io.Writer) Option
- func WithReadWriteTimeouts(read, write time.Duration) Option
- func WithRetryMax(n int) Option
- func WithRetryPolicy(rp RetryPolicy) Option
- func WithRetryWait(min, max time.Duration) Option
- func WithSessionCloseHook(hook SessionHook) Option
- func WithSessionCloseTimeout(dur time.Duration) Option
- func WithSessionOpenHook(hook SessionHook) Option
- func WithTLS(conf *tls.Config) Option
- func WithTokenValidator(v TokenValidator) Option
- type RetryPolicy
- type Session
- type SessionHook
- type Sessions
- type ShutdownError
- type Stream
- type Token
- type TokenValidator
- type Wire
Constants ¶
const ( DefaultKeepAliveInterval = 15 * time.Second DefaultEnableKeepAlive = true DefaultReadTimeout = 10 * time.Second DefaultWriteTimeout = 10 * time.Second DefaultAcceptBacklog = 256 DefaultSessionCloseTimeout = 5 * time.Second DefaultRetryMax = 100 DefaultRetryWaitMax = 60 * time.Second DefaultRetryWaitMin = 5 * time.Second )
const (
BufSize = 1 << 10
)
Variables ¶
var ( // ErrStreamClosed is returned when named stream is closed. ErrStreamClosed = errors.New("wirenet: read/write on closed stream") // ErrStreamHandlerNotFound is returned when stream handler not found. ErrStreamHandlerNotFound = errors.New("wirenet: stream handler not found") // ErrSessionNotFound is returned when session not found. ErrSessionNotFound = errors.New("wirenet: session not found") // ErrWireClosed is returned when Wire is closed client or server side. ErrWireClosed = errors.New("wirenet: wire closed") // ErrAddrEmpty is returned when addr is empty. See Mount(), Join(), Hub(). ErrAddrEmpty = errors.New("wirenet: listener address is empty") // ErrSessionClosed is returned when session is closed. ErrSessionClosed = errors.New("wirenet: session closed") // ErrUnknownCertificateName is returned when certificate name is empty. See LoadCertificates(). ErrUnknownCertificateName = errors.New("wirenet: unknown certificate name") )
Functions ¶
func DefaultRetryPolicy ¶
Types ¶
type ErrorHandler ¶
ErrorHandler is used for error logging.
type Identification ¶
type Identification []byte
type OpError ¶
type Option ¶
type Option func(*wire)
func WithConnectHook ¶
func WithErrorHandler ¶
func WithErrorHandler(h ErrorHandler) Option
func WithIdentification ¶
func WithIdentification(id Identification, token Token) Option
func WithKeepAlive ¶
func WithKeepAliveInterval ¶
func WithLogWriter ¶
func WithReadWriteTimeouts ¶
func WithRetryMax ¶
func WithRetryPolicy ¶
func WithRetryPolicy(rp RetryPolicy) Option
func WithRetryWait ¶
func WithSessionCloseHook ¶
func WithSessionCloseHook(hook SessionHook) Option
func WithSessionCloseTimeout ¶
func WithSessionOpenHook ¶
func WithSessionOpenHook(hook SessionHook) Option
func WithTokenValidator ¶
func WithTokenValidator(v TokenValidator) Option
type RetryPolicy ¶
RetryPolicy retry policy is used when there is no connection to the server. Used only on the client side. The default is DefaultRetryPolicy, but you can write your own policy.
type Session ¶
type Session interface { // ID returns an unique session id. ID() uuid.UUID // IsClosed returns a true flag if the session is closed, otherwise returns a false flag. IsClosed() bool // Close closes gracefully shutdown the all active streams. Close() error // StreamNames returns a list of open stream names. StreamNames() []string // OpenStream opens a named stream and returns it. // After the named stream is successfully opened, an authentication frame is sent. OpenStream(name string) (Stream, error) // Identification returns some information specified by the user on the client side using WithIdentification(). Identification() Identification // CloseWire closes gracefully shutdown the server without interrupting any active connections. CloseWire() error }
Session represents a connection between a client and a server. Each session can have from one to N named streams. Each named stream is a payload processing (file transfer, video transfer, etc.).
type SessionHook ¶
type SessionHook func(Session)
SessionHook is used when opening or closing a session, and each session interception is performed in a separate goroutine.
type Sessions ¶
Sessions represents a map of active sessions. The key is session id and session is value.
type ShutdownError ¶
type ShutdownError struct {
Errors []error
}
ShutdownError is the error type with errors that occurred when closing a sessions. The error is used only when closing a wired connection.
func NewShutdownError ¶
func NewShutdownError() *ShutdownError
NewShutdownError constructs a new ShutdownError.
func (*ShutdownError) Add ¶
func (e *ShutdownError) Add(er error)
Add adds an error to the container.
func (*ShutdownError) Error ¶
func (e *ShutdownError) Error() string
Error returns a list of all errors in a string representation. Each error is separated by a symbol \n.
func (*ShutdownError) IsFilled ¶
func (e *ShutdownError) IsFilled() bool
IsFilled returns a true flag if the container is full, otherwise returns a false flag.
type Stream ¶
type Stream interface { // ID returns an unique stream id. ID() uuid.UUID // Session returns the instance of the session to which the stream. Session() Session // Name returns a name of stream. Name() string // IsClosed returns a true flag if the stream is closed, otherwise returns a false flag. IsClosed() bool // Reader returns a reader. Reader() io.ReadCloser // Writer returns a writer. Writer() io.WriteCloser io.Closer io.ReaderFrom io.WriterTo }
Stream is a named stream for streaming data. The size of the transmitted data is unlimited since the transfer is carried out in chunks. Default chunk size is 1024 bytes.
type TokenValidator ¶
type TokenValidator func(streamName string, id Identification, token Token) error
TokenValidator used to validate the token if the token is set on the client side with WithIdentification(). Token and identifier can be any sequence of bytes.
type Wire ¶
type Wire interface { // Sessions returns a list of active sessions. Sessions() Sessions // Session returns the session by UUID. Session(sessionID uuid.UUID) (Session, error) // Stream registers the handler for the given name. // If a named stream already exists, stream overwrite. Stream(name string, h Handler) // Close gracefully shutdown the server without interrupting any active connections. Close() error // Connect creates a new connection. // If the wire is on the client-side, then used dial(). // If the wire is on the server-side, then used listener(). Connect() error }
Wire is used to initialize a server or client side connection.
func Hub ¶
Hub constructs a new connection point with the given addr and Options as the server side.