Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server[T any] struct { // contains filtered or unexported fields }
Server represents the bmux TCP server instance. It manages routers, middleware, and the underlying event engine.
T is a generic type parameter representing the connection context type.
Usage:
ctxFactory := func() *MyContext { return &MyContext{} }
extractLen := func(c gnet.Conn, buf []byte) (headLen, totalLen int) { ... }
extractID := func(c gnet.Conn, head []byte) int { ... }
server := bmux.New(ctxFactory, extractLen, extractID, nil)
server.LoadRouter(myRouters)
server.LoadMiddleware(myMiddleware)
server.Start()
The server handles connections using gnet for high-performance async I/O.
func New ¶
func New[T any]( contextFactory func() *T, extractLength engine.ExtractLengthFunc[T], extractMsgID engine.ExtractMsgIDFunc[T], override *config.Config, opts ...Option[T], ) *Server[T]
New creates a new bmux Server instance with the given context factory, length extractor, message ID extractor, optional config override, and options.
It validates required arguments and loads configuration.
Example:
ctxFactory := func() *MyContext { return &MyContext{} }
extractLen := func(c gnet.Conn, buf []byte) (headLen, totalLen int) { ... }
extractID := func(c gnet.Conn, head []byte) int { ... }
server := bmux.New(ctxFactory, extractLen, extractID, nil)
The server is ready to have routers and middleware loaded before starting.
func (*Server[T]) LoadMiddleware ¶
func (s *Server[T]) LoadMiddleware(middleware []middleware.Middleware)
LoadMiddleware appends global middleware to the server.
Middleware applied here runs for all routes.
Example:
server.LoadMiddleware([]middleware.Middleware{myMiddleware})
func (*Server[T]) LoadRouter ¶
LoadRouter appends one or more routers to the server.
Routers contain groups of routes and their associated middleware.
Example:
myRouter := router.NewRouter(true, routes, middleware, opts...)
server.LoadRouter([]router.Router{myRouter})