listener

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConfigError

func NewConfigError(msg string, err error) error

func NewConnectionError

func NewConnectionError(msg string, err error) error

Error constructors for common scenarios

func NewProtocolError

func NewProtocolError(msg string, err error) error

func NewSessionError

func NewSessionError(msg string, err error) error

func NewValidationError

func NewValidationError(field, message string) error

NewValidationError creates a new validation error

func ValidateConfig

func ValidateConfig(config *ServerConfig) error

ValidateConfig validates server configuration

Types

type AnnounceMiddlewareFunc

type AnnounceMiddlewareFunc func(options any, session Session)

type BaseSession

type BaseSession struct {
	ID           string
	ClientAddr   net.Addr
	DataChannel  chan []byte
	LastReceived time.Time
	// contains filtered or unexported fields
}

BaseSession provides common session functionality for all protocol implementations

func NewBaseSession

func NewBaseSession(addr net.Addr, ctx context.Context, logger Logger, config *ServerConfig) *BaseSession

NewBaseSession creates a new base session with the given parameters

func (*BaseSession) Cancel

func (s *BaseSession) Cancel()

Cancel cancels the session's context

func (*BaseSession) Context

func (s *BaseSession) Context() context.Context

Context returns the session's context

func (*BaseSession) Data

func (s *BaseSession) Data() chan []byte

Data returns the channel for receiving data from the client

func (*BaseSession) GetClientAddr

func (s *BaseSession) GetClientAddr() net.Addr

GetClientAddr returns the client's network address

func (*BaseSession) GetLastReceived

func (s *BaseSession) GetLastReceived() time.Time

GetLastReceived returns the timestamp of the last received data

func (*BaseSession) GetSessionID

func (s *BaseSession) GetSessionID() string

GetSessionID returns the unique session identifier

type DefaultLogger

type DefaultLogger struct{}

DefaultLogger provides a basic implementation of the Logger interface

func (*DefaultLogger) Debug

func (l *DefaultLogger) Debug(msg string, keysAndValues ...interface{})

func (*DefaultLogger) Error

func (l *DefaultLogger) Error(msg string, keysAndValues ...interface{})

func (*DefaultLogger) Info

func (l *DefaultLogger) Info(msg string, keysAndValues ...interface{})

func (*DefaultLogger) Warn

func (l *DefaultLogger) Warn(msg string, keysAndValues ...interface{})

type ErrorType

type ErrorType string

Error types for specific error cases

const (
	ErrConnection    ErrorType = "connection"
	ErrConfiguration ErrorType = "configuration"
	ErrProtocol      ErrorType = "protocol"
	ErrSession       ErrorType = "session"
)

type Listener added in v0.0.2

type Listener interface {
	// StartReceiveStream Starts listener for stream transport
	StartListener() error

	// StopReceiveStream Stops listener for stream transport
	StopListener() error

	// SetAnnounceNewSession Sets middleware for announcing a new session
	SetAnnounceNewSession(function AnnounceMiddlewareFunc, options any)

	// GetActiveSessions Get all sessions
	GetActiveSessions() map[string]Session

	// GetSession Get session from ClientAddr (IP:Port)
	GetSession(ClientAddr string) Session
}

Listener defines the interface for streaming TCP and UDP connections

func NewQUIC added in v0.0.5

func NewQUIC(host string, port uint16, ctx context.Context, opts ...ServerOption) (Listener, error)

NewQUIC creates a new QUIC server with the given configuration

func NewUDP

func NewUDP(host string, port uint16, ctx context.Context, opts ...ServerOption) (Listener, error)

NewUDP creates a new UDP server with the given configuration

type Logger

type Logger interface {
	Debug(msg string, keysAndValues ...interface{})
	Info(msg string, keysAndValues ...interface{})
	Warn(msg string, keysAndValues ...interface{})
	Error(msg string, keysAndValues ...interface{})
}

Logger defines the interface for logging operations

type NetworkError

type NetworkError struct {
	Type    ErrorType
	Message string
	Err     error
}

NetworkError represents a network-related error with context

func (*NetworkError) Error

func (e *NetworkError) Error() string

func (*NetworkError) Unwrap

func (e *NetworkError) Unwrap() error

type QUICConfig added in v0.0.9

type QUICConfig struct {
	TLSConfig  *tls.Config
	QUICConfig *quic.Config
	Delimiter  []byte
}

QUICConfig holds QUIC-specific configuration

type QUICServer added in v0.0.5

type QUICServer struct {
	*ServerConfig
	// contains filtered or unexported fields
}

QUICServer implements a QUIC streaming server with session management

func (*QUICServer) GetActiveSessions added in v0.0.5

func (q *QUICServer) GetActiveSessions() map[string]Session

func (*QUICServer) GetSession added in v0.0.5

func (q *QUICServer) GetSession(ClientAddr string) Session

func (*QUICServer) SetAnnounceNewSession added in v0.0.5

func (q *QUICServer) SetAnnounceNewSession(function AnnounceMiddlewareFunc, options any)

func (*QUICServer) StartListener added in v0.0.5

func (q *QUICServer) StartListener() error

StartListener begins accepting QUIC connections

func (*QUICServer) StopListener added in v0.0.5

func (q *QUICServer) StopListener() error

StopListener gracefully shuts down the QUIC server

type QUICSession added in v0.0.5

type QUICSession struct {
	*BaseSession
	// contains filtered or unexported fields
}

QUICSession represents an active QUIC connection

func (*QUICSession) CloseSession added in v0.0.5

func (s *QUICSession) CloseSession()

func (*QUICSession) GetLastRecieved added in v0.0.5

func (s *QUICSession) GetLastRecieved() time.Time

GetLastRecieved maintains backward compatibility with the Session interface

func (*QUICSession) SendToClient added in v0.0.5

func (s *QUICSession) SendToClient(data []byte) error

type ServerConfig

type ServerConfig struct {
	MaxLength      uint32
	BufferSize     int
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	Logger         Logger
	MaxConnections int
	ProtocolConfig interface{} // Protocol-specific configuration
}

ServerConfig holds common configuration for all protocol servers

type ServerOption

type ServerOption func(*ServerConfig)

ServerOption defines a function type for configuring server options

func WithBufferSize

func WithBufferSize(size int) ServerOption

WithBufferSize sets the channel buffer size

func WithLogger

func WithLogger(logger Logger) ServerOption

WithLogger sets the logger implementation

func WithMaxConnections

func WithMaxConnections(max int) ServerOption

WithMaxConnections sets the maximum number of concurrent connections

func WithMaxLength

func WithMaxLength(length uint32) ServerOption

WithMaxLength sets the maximum message length

func WithQUICConfig added in v0.0.9

func WithQUICConfig(quicConfig *quic.Config) ServerOption

WithQUICConfig sets the QUIC configuration

func WithQUICDelimiter added in v0.0.9

func WithQUICDelimiter(delimiter []byte) ServerOption

WithQUICDelimiter sets the delimiter for QUIC messages

func WithTLSConfig added in v0.0.9

func WithTLSConfig(tlsConfig *tls.Config) ServerOption

WithTLSConfig sets the TLS configuration for QUIC

func WithTimeouts

func WithTimeouts(read, write time.Duration) ServerOption

WithTimeouts sets read and write timeouts

func WithWebSocketBufferSizes

func WithWebSocketBufferSizes(readSize, writeSize int) ServerOption

WithWebSocketBufferSizes sets the WebSocket read and write buffer sizes

func WithWebSocketPath

func WithWebSocketPath(path string) ServerOption

WithWebSocketPath sets the WebSocket endpoint path

type Session

type Session interface {
	//Sends to client
	SendToClient(data []byte) error
	//Receive Data channel
	Data() (DataFromClient chan []byte)
	//Close Session
	CloseSession()

	//Get Session UUID
	GetSessionID() string
	//Get Client Addr
	GetClientAddr() net.Addr
	//Get Last Recieved
	GetLastRecieved() time.Time
}

This is a session interface for managing the active connection of the streaming protocol

type TCPServer

type TCPServer struct {
	*ServerConfig
	// contains filtered or unexported fields
}

TCPServer implements a TCP streaming server with enhanced session management

func NewTCP

func NewTCP(host string, port uint16, ctx context.Context, opts ...ServerOption) (*TCPServer, error)

NewTCP creates a new TCP server with the given configuration

func (*TCPServer) GetActiveSessions

func (t *TCPServer) GetActiveSessions() map[string]Session

func (*TCPServer) GetSession

func (t *TCPServer) GetSession(ClientAddr string) Session

func (*TCPServer) SetAnnounceNewSession

func (t *TCPServer) SetAnnounceNewSession(function AnnounceMiddlewareFunc, options any)

func (*TCPServer) StartListener

func (t *TCPServer) StartListener() error

StartListener begins accepting TCP connections

func (*TCPServer) StopListener

func (t *TCPServer) StopListener() error

StopListener gracefully shuts down the TCP server

type TCPSession

type TCPSession struct {
	*BaseSession
	// contains filtered or unexported fields
}

TCPSession represents an active TCP connection with enhanced management capabilities

func (*TCPSession) CloseSession

func (s *TCPSession) CloseSession()

CloseSession closes the TCP session and cleans up resources

func (*TCPSession) GetLastRecieved

func (s *TCPSession) GetLastRecieved() time.Time

GetLastRecieved maintains backward compatibility with the Session interface

func (*TCPSession) SendToClient

func (s *TCPSession) SendToClient(data []byte) error

SendToClient sends data to the TCP client with length prefix

type UDPServer

type UDPServer struct {
	*ServerConfig
	// contains filtered or unexported fields
}

UDPServer implements a UDP streaming server with enhanced session management

func (*UDPServer) GetActiveSessions

func (u *UDPServer) GetActiveSessions() map[string]Session

GetActiveSessions returns all active sessions

func (*UDPServer) GetSession

func (u *UDPServer) GetSession(ClientAddr string) Session

GetSession returns a specific session by client address

func (*UDPServer) SetAnnounceNewSession

func (u *UDPServer) SetAnnounceNewSession(function AnnounceMiddlewareFunc, options any)

SetAnnounceNewSession sets the middleware for announcing new sessions

func (*UDPServer) StartListener

func (u *UDPServer) StartListener() error

StartListener begins accepting UDP packets

func (*UDPServer) StopListener

func (u *UDPServer) StopListener() error

StopListener gracefully shuts down the UDP server

type UDPSession

type UDPSession struct {
	*BaseSession
	// contains filtered or unexported fields
}

UDPSession represents an active UDP connection

func (*UDPSession) CloseSession

func (s *UDPSession) CloseSession()

CloseSession closes the UDP session and cleans up resources

func (*UDPSession) GetLastRecieved

func (s *UDPSession) GetLastRecieved() time.Time

GetLastRecieved implements the Session interface

func (*UDPSession) SendToClient

func (s *UDPSession) SendToClient(data []byte) error

SendToClient sends data to the UDP client

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

Validation errors

func (*ValidationError) Error

func (e *ValidationError) Error() string

type WebSocketConfig

type WebSocketConfig struct {
	ReadBufferSize  int
	WriteBufferSize int
	Path            string
}

Protocol-specific configurations

type WebSocketServer

type WebSocketServer struct {
	*ServerConfig
	// contains filtered or unexported fields
}

WebSocketServer implements a WebSocket streaming server with enhanced session management

func NewWebSocket

func NewWebSocket(host string, port uint16, ctx context.Context, opts ...ServerOption) (*WebSocketServer, error)

NewWebSocket creates a new WebSocket server with the given configuration

func (*WebSocketServer) GetActiveSessions

func (w *WebSocketServer) GetActiveSessions() map[string]Session

GetActiveSessions returns all active sessions

func (*WebSocketServer) GetSession

func (w *WebSocketServer) GetSession(ClientAddr string) Session

GetSession returns a specific session by client address

func (*WebSocketServer) SetAnnounceNewSession

func (w *WebSocketServer) SetAnnounceNewSession(function AnnounceMiddlewareFunc, options any)

SetAnnounceNewSession sets the middleware for announcing new sessions

func (*WebSocketServer) StartListener

func (w *WebSocketServer) StartListener() error

StartListener begins accepting WebSocket connections

func (*WebSocketServer) StopListener

func (w *WebSocketServer) StopListener() error

StopListener gracefully shuts down the WebSocket server

type WebSocketSession

type WebSocketSession struct {
	*BaseSession

	ClientConn *websocket.Conn
	// contains filtered or unexported fields
}

WebSocketSession represents an active WebSocket connection

func (*WebSocketSession) CloseSession

func (s *WebSocketSession) CloseSession()

CloseSession closes the WebSocket session and cleans up resources

func (*WebSocketSession) GetLastRecieved

func (s *WebSocketSession) GetLastRecieved() time.Time

GetLastRecieved implements the Session interface

func (*WebSocketSession) SendToClient

func (s *WebSocketSession) SendToClient(data []byte) error

SendToClient sends data to the WebSocket client

Jump to

Keyboard shortcuts

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