connector

package
v0.0.0-...-2bfb83d Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrExceedMaxClients = errors.New("ppcserver: exceed maximum number of clients")
)

Functions

func ExceedMaxClients

func ExceedMaxClients() bool

func MaxClients

func MaxClients() int

func NumClients

func NumClients() int

func SetMaxClients

func SetMaxClients(v int32)

func StartClient

func StartClient(ctx context.Context, transport Transport) error

StartClient creates a new Client with ClientStateConnected as the initial state,

Types

type Client

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

Client represents a Client connection to a server.

func (*Client) Close

func (c *Client) Close() (err error)

Close first mutates Client to the ClientStateClosed state, then closes the underlying transport connection with the peer. Close does nothing if the Client's state is already ClientStateClosed.

func (*Client) State

func (c *Client) State() ClientState

State returns the current state of the Client.

func (*Client) Write

func (c *Client) Write(data []byte) error

type ClientState

type ClientState uint8

ClientState represents the state of a Client instance, uint8 is used for save memory usage.

const (
	// ClientStateConnected represents a new connection that is waiting for the auth message from the peer.
	// A Client instance begins at this state and then transition to either ClientStateAuthorized or ClientStateClosed.
	ClientStateConnected ClientState = iota
	ClientStateAuthorized
	// ClientStateClosed represents a closed connection. This is a terminal state.
	// After entering this state, a Client instance will not receive any message and can not send any message.
	ClientStateClosed
)

type EncodingType

type EncodingType string

EncodingType represents client connection transport encoding format.

const (
	// EncodingTypeJSON represents that data will transport in JSON format.
	EncodingTypeJSON EncodingType = "json"
	// EncodingTypeProtobuf represents that data will transport in Protobuf format.
	EncodingTypeProtobuf EncodingType = "protobuf"
)

type Option

type Option func(o *Options)

Option is a function to apply various configurations to customize a connector Component.

func WithAddr

func WithAddr(a string) Option

WithAddr is an Option to set the TCP address for the server to listen on.

func WithHTTPServeMux

func WithHTTPServeMux(mux *http.ServeMux) Option

WithHTTPServeMux is an Option to set a custom http.ServeMux, will also update Server.Handler to mux if Options.Server is not nil.

func WithHTTPServer

func WithHTTPServer(s *http.Server) Option

WithHTTPServer is an Option to set a custom http.Server, will also force overwrite Server.Addr to Options.Addr if Options.Addr is not nil, will also force overwrite Server.Handler to Options.ServeMux if Options.ServeMux is not nil.

func WithMaxMessageSize

func WithMaxMessageSize(s int64) Option

WithMaxMessageSize is an Option to set maximum message size in bytes allowed from client.

func WithTLSCertAndKey

func WithTLSCertAndKey(certFile, keyFile string) Option

WithTLSCertAndKey is an Option to set the path to TLS certificate file with its matching private key. WebsocketConnector will start the http.Server with ListenAndServeTLS that expects HTTPS connections, when either certFile or keyFile is not an empty string.

func WithWebsocketPath

func WithWebsocketPath(p string) Option

WithWebsocketPath is an Option to set the URL path for accepting WebSocket connections.

func WithWebsocketUpgrader

func WithWebsocketUpgrader(upgrader *websocket.Upgrader) Option

WithWebsocketUpgrader is an Option to set a custom websocket.Upgrader.

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) Option

WithWriteTimeout is an Option to set the maximum time of one write message operation to complete.

type Options

type Options struct {
	// WriteTimeout is the maximum time of write message operation.
	// Slow client will be disconnected.
	// Default is 1 second if not set via WithWriteTimeout.
	WriteTimeout time.Duration

	// MaxMessageSize is the maximum allowed message size in bytes received from the client.
	// Default is 4096 bytes (4KB) if not set via WithMaxMessageSize.
	MaxMessageSize int64

	// Addr optionally specifies the TCP address for the server to listen on,
	// in the form "host:port". If empty, ":http" (port 80) is used.
	// See net.Dial for details of the address format.
	Addr string

	// WebsocketPath is the URL path to accept WebSocket connections.
	// This option only applies to WebsocketConnector.
	// Default is "/" if not set via WithWebsocketPath.
	WebsocketPath string

	// TLSCertFile is the path to TLS cert file.
	// This option only applies to WebsocketConnector.
	TLSCertFile string

	// TLSKeyFile is the path to TLS key file.
	// This option only applies to WebsocketConnector.
	TLSKeyFile string

	ServeMux *http.ServeMux

	Server *http.Server

	Upgrader *websocket.Upgrader
}

Options hold the configurable parts of a connector Component.

type Transport

type Transport interface {
	// ProtocolType should return the protocol type of the transport.
	ProtocolType() TransportProtocolType
	// NetConn should return the internal net.Conn of the connection.
	NetConn() net.Conn
	//
	Read() ([]byte, error)
	// Write should write single data into a connection.
	Write([]byte) error
	// Close should close the underlying network connection.
	Close() error
}

Transport abstracts a connection transport between server and client.

type TransportProtocolType

type TransportProtocolType string

TransportProtocolType describes the protocol type name of the connection transport between server and client, currently only supports "websocket".

const (
	TransportProtocolTypeWebsocket TransportProtocolType = "websocket"
)

type WebsocketConnector

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

WebsocketConnector accepts WebSocket client connections, responsible for sending and receiving data with a WebSocket client.

func NewWebsocketConnector

func NewWebsocketConnector(opts ...Option) *WebsocketConnector

NewWebsocketConnector creates a new WebsocketConnector.

func (*WebsocketConnector) Shutdown

func (c *WebsocketConnector) Shutdown(ctx context.Context) error

func (*WebsocketConnector) Start

func (c *WebsocketConnector) Start(ctx context.Context) error

Start starts an HTTP server for serving the WebSocket connection requests and block until the server is closed. A ctx (which will cancel when the server is shutting down) is required for gracefully shutting down the HTTP server and actively closing all the WebSocket connections.

Jump to

Keyboard shortcuts

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