server

package
v0.0.0-...-055e899 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	ServerStatusInitial int32 = iota
	ServerStatusStarted
	ServerStatusStopped
)

Variables

View Source
var (
	NilError = &StatusError{Code: StatusOK, message: "OK"}
)

Functions

func NewContextFromSession

func NewContextFromSession(parent context.Context, sess *Session) context.Context

Types

type ConnectionHandler

type ConnectionHandler struct {
	Handler     HandlerFunc     // Connection handler
	SessManager *SessionManager // Session manager
	Codec       *proto.Codec    // Protocol codec
}

func NewConnectionHandler

func NewConnectionHandler(
	h HandlerFunc, mgr *SessionManager, codec *proto.Codec) *ConnectionHandler

func (*ConnectionHandler) Handle

func (ch *ConnectionHandler) Handle(l net.Listener, conn net.Conn)

handleConnection handles new accepted connection from net listener.

type ContextKey

type ContextKey string
const (
	CtxKeySession ContextKey = "session"
)

type Error

type Error interface {
	error
	Status() int32
}

Error represents a handler error. It provides methods for a server status code and embeds the built-in error interface.

type HandlerFunc

type HandlerFunc func(context.Context, *Message) *Message

type Message

type Message struct {
	*proto.Message
	Error error
}

func NewMessage

func NewMessage(msg *proto.Message) *Message

func NewMessageWithError

func NewMessageWithError(err error) *Message

func (*Message) ProtoMessage

func (m *Message) ProtoMessage() *proto.Message

ProtoMessage adapts into a protobuf response message.

type Server

type Server struct {
	*ConnectionHandler // Connection handler
	// contains filtered or unexported fields
}

func NewTCPServer

func NewTCPServer(addr string, ch *ConnectionHandler) (srv *Server, err error)

func NewUDPServer

func NewUDPServer(addr string, ch *ConnectionHandler) (srv *Server, err error)

func (*Server) Close

func (srv *Server) Close() error

func (*Server) Serve

func (srv *Server) Serve() error

Serve always returns a non-nil error and closes l. After closed, the returned error is errServerClosed.

type Session

type Session struct {
	ID   string   // Session ID
	Conn net.Conn // Underlying network connection
	// contains filtered or unexported fields
}

func NewSession

func NewSession(conn net.Conn) *Session

func SessionFromContext

func SessionFromContext(ctx context.Context) (sess *Session, ok bool)

func (*Session) Close

func (s *Session) Close() error

func (*Session) LastActive

func (s *Session) LastActive() time.Time

func (*Session) Refresh

func (s *Session) Refresh()

type SessionManager

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

func NewSessionManager

func NewSessionManager() *SessionManager

func (*SessionManager) Add

func (m *SessionManager) Add(sess *Session)

func (*SessionManager) Count

func (m *SessionManager) Count() int

func (*SessionManager) ListAll

func (m *SessionManager) ListAll() []*Session

func (*SessionManager) Start

func (m *SessionManager) Start()

Start function to start checking for timeouts periodically

func (*SessionManager) Stop

func (m *SessionManager) Stop()

Stop function to stop checking for timeouts

func (*SessionManager) Terminate

func (m *SessionManager) Terminate(sess *Session) error

func (*SessionManager) TerminateAll

func (m *SessionManager) TerminateAll(ctx context.Context) (err error)

type SessionTerminatedEvent

type SessionTerminatedEvent struct {
	Sess *Session
}

type StatusCode

type StatusCode = int32
const (
	StatusOK StatusCode = iota
	StatusInternalServerError
	StatusBadRequest
)

type StatusError

type StatusError struct {
	Code int32
	Err  error
	// contains filtered or unexported fields
}

StatusError represents an error with an associated server status code.

func NewBadRequestError

func NewBadRequestError(err error) *StatusError

func NewInternalServerError

func NewInternalServerError(err error) *StatusError

func (*StatusError) Error

func (se *StatusError) Error() string

Allows StatusError to satisfy the error interface.

func (*StatusError) Status

func (se *StatusError) Status() int32

Returns our server status code.

Jump to

Keyboard shortcuts

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