server

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const CTX_CONN_KEY = "conn"
View Source
const CTX_SERVER_OBJECT_KEY = "server-object"
View Source
const (
	DefaultPort = 8883
)

Variables

View Source
var ErrServerClosed = errors.New("server closed")

Functions

func AttachToCmdrCommand added in v0.3.0

func AttachToCmdrCommand(tcp cmdr.OptCmd, opts ...CmdrOpt)

func DefaultCommandAction added in v0.2.5

func DefaultCommandAction(cmd *cmdr.Command, args []string, prefixPrefix string, opts ...Opt) (err error)

func New

func New(config *base.Config, opts ...Opt) (serve ServeFunc, obj *Obj, tlsEnabled bool, err error)

func Shutdown

func Shutdown(serverObj *Obj)

Shutdown will close down the server gracefully

Types

type CmdrOpt

type CmdrOpt func(*builder)

func WithCmdrCommandAction

func WithCmdrCommandAction(action CommandAction) CmdrOpt

WithCmdrCommandAction allows a custom CommandAction of yours. The default is DefaultCommandAction, it's enough in most cases. But, if you want, write yours.

func WithCmdrLogger

func WithCmdrLogger(logger log.Logger) CmdrOpt

func WithCmdrNil

func WithCmdrNil() CmdrOpt

func WithCmdrPort

func WithCmdrPort(port int) CmdrOpt

func WithCmdrPrefixPrefix

func WithCmdrPrefixPrefix(prefixPrefix string) CmdrOpt

WithCmdrPrefixPrefix setup a prefix of prefix of tls configs and server/client tcp/udp configs.

For the `prefixPrefix` = "some-here", the section in yaml config file looks like:

```yaml app:

some-here:
  server:
    addr:       # host[:port]
    # The default ports for the whole socket-lib.
    ports:
      default: 1883
      tls: 8883
      websocket: 443
      #sn: 1884   # mqttsn udp mode

    tls:
      enabled: true
      client-auth: false
      cacert: root.pem
      cert: cert.pem
      key: cert.key
      locations:
        - ./ci/certs
        - $CFG_DIR/certs

 cl ient:
    # To run the client with an interactive  mode, set interactive to true. The default is always false.
    # interactive: true

    addr:       # host[:port]
    # The default ports for the whole socket-lib.
    ports:
      default: 1883
      tls: 8883
      websocket: 443
      #sn: 1884   # mqttsn udp mode

    tls:
      enabled: true
      cacert: root.pem
      server-cert: cert.pem
      client-auth: false
      cert: client.pem
      key: client.key
      locations:
        - ./ci/certs
        - $CFG_DIR/certs

```

func WithCmdrServerOptions

func WithCmdrServerOptions(opts ...Opt) CmdrOpt

func WithCmdrServerProtocolInterceptor

func WithCmdrServerProtocolInterceptor(fn protocol.Interceptor) CmdrOpt

func WithCmdrUDPMode

func WithCmdrUDPMode(mode bool) CmdrOpt

type CommandAction

type CommandAction func(cmd *cmdr.Command, args []string, prefixPrefix string, opts ...Opt) (err error)

type Connection

type Connection interface {
	base.Conn
	base.CachedTCPWriter

	// HandleConnection is used by serverObj
	HandleConnection(ctx context.Context)
}

type NewConnectionFunc

type NewConnectionFunc func(ctx context.Context, serverObj *Obj, conn net.Conn) Connection

type Obj

type Obj struct {
	ReadTimeout  time.Duration
	WriteTimeout time.Duration

	log.Logger
	// contains filtered or unexported fields
}

func (*Obj) BaseUri

func (s *Obj) BaseUri() string

func (*Obj) Close

func (s *Obj) Close()

func (*Obj) Config

func (s *Obj) Config() *base.Config

func (*Obj) ListenTo

func (s *Obj) ListenTo(listener net.Listener)

func (*Obj) ProtocolInterceptor

func (s *Obj) ProtocolInterceptor() protocol.Interceptor

func (*Obj) RequestShutdown

func (s *Obj) RequestShutdown()

func (*Obj) Serve

func (s *Obj) Serve(baseCtx context.Context) (err error)

func (*Obj) SetLogger

func (s *Obj) SetLogger(l log.Logger)

func (*Obj) SetNewConnectionFunc

func (s *Obj) SetNewConnectionFunc(fn NewConnectionFunc)

func (*Obj) SetProtocolInterceptor

func (s *Obj) SetProtocolInterceptor(pi protocol.Interceptor)

func (*Obj) WithLogger

func (s *Obj) WithLogger(logger log.Logger) *Obj

func (*Obj) WithProtocolInterceptor

func (s *Obj) WithProtocolInterceptor(pi protocol.Interceptor) *Obj

type Opt

type Opt func(so *Obj)

func WithServerLogger

func WithServerLogger(logger log.Logger) Opt

func WithServerNetworkType

func WithServerNetworkType(typeOfNetwork string) Opt

WithServerNetworkType setups the network type of the listener.

The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".

For TCP networks, if the host in the address parameter is empty or a literal unspecified IP address, Listen listens on all available unicast and anycast IP addresses of the local system. To only use IPv4, use network "tcp4". The address can use a host name, but this is not recommended, because it will create a listener for at most one of the host's IP addresses. If the port in the address parameter is empty or "0", as in "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. The Addr method of Listener can be used to discover the chosen port.

func WithServerNewConnectionFunc

func WithServerNewConnectionFunc(fn NewConnectionFunc) Opt

func WithServerPrefixInConfigFile

func WithServerPrefixInConfigFile(prefix string) Opt

func WithServerProtocolInterceptor

func WithServerProtocolInterceptor(fn protocol.Interceptor) Opt

func WithServerUDPMode

func WithServerUDPMode(mode bool) Opt

type ServeFunc

type ServeFunc func(baseCtx context.Context) error

Jump to

Keyboard shortcuts

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