ktcp

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2022 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const (
	B = 1 << (10 * iota)
	KB
	MB
	GB
	TB
	PB
	EB
)

Byte unit helpers.

Variables

View Source
var ErrServerStopped = errors.New("ktcp: the server has been stopped")

ErrServerStopped is returned when server stopped.

View Source
var ErrSessionClosed = fmt.Errorf("session closed")

ErrSessionClosed is returned when session stopped.

Functions

func NewContext added in v0.0.2

func NewContext() *routerCtx

Types

type CallBack

type CallBack interface {
	OnMessage(c Context)
	OnClose(c *Session)
}

type Context

type Context interface {
	context.Context
	GetSession() *Session
	ForwardHandler(c CallBack)
	GetReqMsg() *message.Message
	Bind(v interface{}) error
	Response() *message.Message
	Send(id uint32, resp interface{}) error
	SendError(id uint32, resp interface{}) error
	Middleware(middleware.Handler) middleware.Handler
	Reset(sess *Session, reqMsg *message.Message)
	AppendToStorage(saver storage.Saver)
	Save() (err error)
}

Context is a generic context in a message routing. It allows us to pass variables between handler and middlewares.

type Handler

type Handler interface {
	CallBack
	OnConnect(s *Session)
}

Handler Server

type Server

type Server struct {
	Listener net.Listener
	Packer   packing.Packer // Packer is the message packer, will be passed to session.
	Codec    encoding.Codec // Codec is the message codec, will be passed to session.
	// contains filtered or unexported fields
}

Server is a server for TCP connections.

func NewServer

func NewServer(handler Handler, opts ...ServerOption) *Server

NewServer creates an TCP server by options.

func (*Server) GracefulStop added in v0.0.2

func (s *Server) GracefulStop()

TODO GracefulStop

func (*Server) Serve

func (s *Server) Serve() error

Serve the TCP server

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) (err error)

Stop the TCP server.

type ServerOption

type ServerOption func(*Server)

ServerOption is an HTTP server option.

func Address

func Address(addr string) ServerOption

Address with server address.

func Logger

func Logger(logger log.Logger) ServerOption

Logger with server logger.

func Middleware

func Middleware(m ...middleware.Middleware) ServerOption

Middleware with service middleware option.

func Network

func Network(network string) ServerOption

Network with server network.

func ReadTimeout

func ReadTimeout(readTimeout time.Duration) ServerOption

ReadTimeout with server timeout.

func WriteTimeout

func WriteTimeout(writeTimeout time.Duration) ServerOption

WriteTimeout with server timeout.

type Session

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

Session is a server side network connection.

func (*Session) Close

func (s *Session) Close()

Close closes the session, but doesn't close the connection.

func (*Session) Codec

func (s *Session) Codec() encoding.Codec

func (*Session) ID

func (s *Session) ID() string

ID returns the session's ID.

func (*Session) Send

func (s *Session) Send(ctx Context) (err error)

Send pushes response message entry to respQueue.

func (*Session) SendMsg added in v0.0.5

func (s *Session) SendMsg(id uint32, data interface{}) (err error)

SendMsg sends message to session.

Directories

Path Synopsis
cmd
proto
Package proto defines the protobuf codec.
Package proto defines the protobuf codec.
example
pb
internal
sync
errgroup
Package errgroup provides synchronization, error propagation, and Context errgroup 包为一组子任务的 goroutine 提供了 goroutine 同步,错误取消功能.
Package errgroup provides synchronization, error propagation, and Context errgroup 包为一组子任务的 goroutine 提供了 goroutine 同步,错误取消功能.

Jump to

Keyboard shortcuts

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