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) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Get(key string) (interface{}, bool)
- func (c *Context) Message() *message.Entry
- func (c *Context) Response(id interface{}, data interface{}) (*message.Entry, error)
- func (c *Context) Session() *Session
- func (c *Context) Set(key string, value interface{})
- func (c *Context) Value(key interface{}) interface{}
- type DefaultLogger
- type DefaultPacker
- type Error
- 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
- type UnpackError
Constants ¶
const RespKey = "easytcp.router.context.response"
RespKey is the preset key to the response data before encoding.
Variables ¶
var ErrServerStopped = fmt.Errorf("server stopped")
ErrServerStopped is used 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.
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 {
MaxSize int
}
DefaultPacker is the default Packer used in session. DefaultPacker treats the packet with the format:
(size)(id)(data): size: uint32 | took 4 bytes, only the size of `data` id: uint32 | took 4 bytes data: []byte | took `size` bytes
func NewDefaultPacker ¶
func NewDefaultPacker() *DefaultPacker
NewDefaultPacker create a *DefaultPacker with initial field value.
type Error ¶
type Error interface { error Fatal() bool // should return true if the error is fatal, otherwise false. }
Error is a generic interface for error handling.
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 md MiddlewareFunc = func(next HandlerFunc) HandlerFunc { return func(ctx *Context) (packet.Message, 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(msg Message) ([]byte, error) Pack(entry *message.Entry) ([]byte, error) // Unpack unpacks the message packet from reader, // returns the Message interface, 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 pointer 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 keep accepting TCP connection in a loop. Accepting loop will break 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 WriteBufferSize int // sets the writing channel buffer size, 1024 will be used if < 0. ReadBufferSize int // sets the reading channel buffer size, 1024 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 { // Sessions keeps all sessions. // Key is session's ID, value is *Session Sessions sync.Map }
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.
type UnpackError ¶
type UnpackError struct {
Err error
}
UnpackError is the error returned in packer.Unpack.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
Package mock is a generated GoMock package.
|
Package mock is a generated GoMock package. |
test_data
|
|