Documentation ¶
Index ¶
- Constants
- Variables
- func SetLogger(lg Logger)
- type Codec
- type Context
- func (c *Context) Bind(v interface{}) error
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) DecodeTo(data []byte, v interface{}) error
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Get(key string) (value interface{}, exists bool)
- func (c *Context) GetResponse() *message.Entry
- func (c *Context) Message() *message.Entry
- func (c *Context) MustBind(v interface{})
- func (c *Context) MustDecodeTo(data []byte, v interface{})
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) Remove(key string)
- func (c *Context) Response(id, data interface{}) error
- func (c *Context) Send(id, data interface{}) error
- func (c *Context) SendTo(sess *Session, id, data interface{}) error
- func (c *Context) Session() *Session
- func (c *Context) Set(key string, value interface{})
- func (c *Context) SetResponse(id interface{}, data []byte)
- func (c *Context) Value(key interface{}) interface{}
- type DefaultLogger
- type DefaultPacker
- type HandlerFunc
- type JsonCodec
- type Logger
- type MiddlewareFunc
- type MsgpackCodec
- type Packer
- type ProtobufCodec
- type Router
- type Server
- type ServerOption
- type Session
- type SessionManager
- type SessionOption
Constants ¶
const ( DefaultReqQueueSize = 1024 DefaultRespQueueSize = 1024 )
Variables ¶
var ErrServerStopped = fmt.Errorf("server stopped")
ErrServerStopped is returned when server stopped.
Functions ¶
Types ¶
type Codec ¶
type Codec interface { // Encode encodes data into []byte. // Returns error when error occurred. Encode(v interface{}) ([]byte, error) // Decode decodes data into v. // Returns error when error occurred. Decode(data []byte, v interface{}) error }
Codec is a generic codec for encoding and decoding data.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is a generic context in a message routing. It allows us to pass variables between handler and middlewares. Context implements the context.Context interface.
func (*Context) Done ¶
func (c *Context) Done() <-chan struct{}
Done implements the context.Context Done method.
func (*Context) GetResponse ¶ added in v0.0.6
GetResponse returns response entry of context.
func (*Context) MustBind ¶ added in v0.0.6
func (c *Context) MustBind(v interface{})
MustBind binds the request message's raw data to v. Panics if any error occurred.
func (*Context) MustDecodeTo ¶ added in v0.0.6
MustDecodeTo decodes data to v via codec. Panics if any error occurred.
func (*Context) MustGet ¶ added in v0.0.6
MustGet returns the value from c.storage by key. Panics if key does not exist.
func (*Context) SetResponse ¶ added in v0.0.6
SetResponse sets response entry with id and data.
type DefaultLogger ¶
type DefaultLogger struct {
// contains filtered or unexported fields
}
DefaultLogger is the default logger instance for this package. DefaultLogger uses the built-in log.Logger.
func (*DefaultLogger) Errorf ¶
func (d *DefaultLogger) Errorf(format string, args ...interface{})
Errorf implements Logger Errorf method.
func (*DefaultLogger) Tracef ¶
func (d *DefaultLogger) Tracef(format string, args ...interface{})
Tracef implements Logger Tracef method.
type DefaultPacker ¶
type DefaultPacker struct { // MaxDataSize represents the max size of `data` MaxDataSize int }
DefaultPacker is the default Packer used in session. Treats the packet with the format:
dataSize(4)|id(4)|data(n)
| segment | type | size | remark | | ---------- | ------ | ------- | ----------------------- | | `dataSize` | uint32 | 4 | the size of `data` only | | `id` | uint32 | 4 | | | `data` | []byte | dynamic | | .
func NewDefaultPacker ¶
func NewDefaultPacker() *DefaultPacker
NewDefaultPacker create a *DefaultPacker with initial field value.
type HandlerFunc ¶
HandlerFunc is the function type for handlers.
type JsonCodec ¶
type JsonCodec struct{}
JsonCodec implements the Codec interface. JsonCodec encodes and decodes data in json way.
type Logger ¶
type Logger interface { Errorf(format string, args ...interface{}) Tracef(format string, args ...interface{}) }
Logger is the generic interface for log recording.
var Log Logger = newLogger()
Log is the instance of Logger interface.
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc is the function type for middlewares. A common pattern is like:
var mf MiddlewareFunc = func(next HandlerFunc) HandlerFunc { return func(ctx *Context) error { return next(ctx) } }
type MsgpackCodec ¶ added in v0.0.2
type MsgpackCodec struct{}
MsgpackCodec implements the Codec interface.
func (*MsgpackCodec) Decode ¶ added in v0.0.2
func (m *MsgpackCodec) Decode(data []byte, v interface{}) error
Decode implements the Codec Decode method.
func (*MsgpackCodec) Encode ¶ added in v0.0.2
func (m *MsgpackCodec) Encode(v interface{}) ([]byte, error)
Encode implements the Codec Encode method.
type Packer ¶
type Packer interface { // Pack packs Message into the packet to be written. Pack(entry *message.Entry) ([]byte, error) // Unpack unpacks the message packet from reader, // returns the message.Entry, and error if error occurred. Unpack(reader io.Reader) (*message.Entry, error) }
Packer is a generic interface to pack and unpack message packet.
type ProtobufCodec ¶
type ProtobufCodec struct{}
ProtobufCodec implements the Codec interface.
func (*ProtobufCodec) Decode ¶
func (p *ProtobufCodec) Decode(data []byte, v interface{}) error
Decode implements the Codec Decode method.
func (*ProtobufCodec) Encode ¶
func (p *ProtobufCodec) Encode(v interface{}) ([]byte, error)
Encode implements the Codec Encode method.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is a router for incoming message. Router routes the message to its handler and middlewares.
type Server ¶
type Server struct { Listener net.Listener // Packer is the message packer, will be passed to session. Packer Packer // Codec is the message codec, will be passed to session. Codec Codec // OnSessionCreate is an event hook, will be invoked when session's created. OnSessionCreate func(sess *Session) // OnSessionClose is an event hook, will be invoked when session's closed. OnSessionClose func(sess *Session) // contains filtered or unexported fields }
Server is a server for TCP connections.
func NewServer ¶
func NewServer(opt *ServerOption) *Server
NewServer creates a Server according to opt.
func (*Server) AddRoute ¶
func (s *Server) AddRoute(msgID interface{}, handler HandlerFunc, middlewares ...MiddlewareFunc)
AddRoute registers message handler and middlewares to the router.
func (*Server) NotFoundHandler ¶
func (s *Server) NotFoundHandler(handler HandlerFunc)
NotFoundHandler sets the not-found handler for router.
func (*Server) Serve ¶
Serve starts to listen TCP and keeps accepting TCP connection in a loop. The loop breaks when error occurred, and the error will be returned.
func (*Server) Use ¶
func (s *Server) Use(middlewares ...MiddlewareFunc)
Use registers global middlewares to the router.
type ServerOption ¶
type ServerOption struct { SocketReadBufferSize int // sets the socket read buffer size. SocketWriteBufferSize int // sets the socket write buffer size. SocketSendDelay bool // sets the socket delay or not. ReadTimeout time.Duration // sets the timeout for connection read. WriteTimeout time.Duration // sets the timeout for connection write. Packer Packer // packs and unpacks packet payload, default packer is the packet.DefaultPacker. Codec Codec // encodes and decodes the message data, can be nil. ReqQueueSize int // sets the request channel size of router, DefaultReqQueueSize will be used if < 0. RespQueueSize int // sets the response channel size of session, DefaultRespQueueSize will be used if < 0. DoNotPrintRoutes bool // whether to print registered route handlers to the console. }
ServerOption is the option for Server.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a TCP session.
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager manages all the sessions in application runtime.
func Sessions ¶
func Sessions() *SessionManager
Sessions returns a SessionManager pointer in a singleton way.
func (*SessionManager) Add ¶
func (m *SessionManager) Add(s *Session)
Add adds a session to Sessions. If the ID of s already existed in Sessions, it replaces the value with the s.
func (*SessionManager) Get ¶
func (m *SessionManager) Get(id string) *Session
Get returns a Session when found by the id, returns nil otherwise.
func (*SessionManager) Range ¶
func (m *SessionManager) Range(fn func(id string, sess *Session) (next bool))
Range calls fn sequentially for each id and sess present in the Sessions. If fn returns false, range stops the iteration.
func (*SessionManager) Remove ¶
func (m *SessionManager) Remove(id string)
Remove removes a session from Sessions. Parameter id should be the session's id.
type SessionOption ¶
SessionOption is the extra options for Session.