tcpserver

package
Version: v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSessionReadError

func NewSessionReadError(ctx context.Context, err error) *sessionError

func NewSessionWriteError

func NewSessionWriteError(ctx context.Context, err error, msg sessionMessage) *sessionError

Types

type ContentTyper

type ContentTyper interface {
	TypeCode() byte     // 包类型代码
	TypeString() string // 包类型名称
}

type HandleFunc

type HandleFunc func(ctx context.Context, request Payloader) (response Payloader, err error)

type Options

type Options func(s *serverOptions)

func WithConnHandle

func WithConnHandle(handle TcpConnHandler) Options

func WithDaemon

func WithDaemon() Options

func WithListenAddr

func WithListenAddr(host, port string) Options

func WithZlog

func WithZlog(zlog *zap.Logger) Options

type Packer

type Packer interface {
	//
	Pack(ctx context.Context, rw io.Writer, payload Payloader) error
	//
	UnPack(ctx context.Context, r io.Reader) (Payloader, error)

	Crypto(cryptoer xxcrypto.Cryptoer)
}

消息编解码器

type Payloader

type Payloader interface {
	// 数据类型
	Type() ContentTyper

	// 数据 head
	Head() []byte

	// 数据
	Payload() []byte

	// 整个payload的总长度
	TotalLen() int64

	fmt.Stringer
}

编解码包,根据 reader和writer来实现.,网络层面可以实现 使用超时机制控制的,和结束字符控制的,和长度控制的

type SessionOptions

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

type SessionProperty

type SessionProperty struct {
	RemoteAddr net.Addr
	SessionId  int64
	// contains filtered or unexported fields
}

func ObtainSession

func ObtainSession(ctx context.Context) *SessionProperty

func (*SessionProperty) Get

func (p *SessionProperty) Get(key string) interface{}

func (*SessionProperty) Put

func (p *SessionProperty) Put(key string, value interface{}) error

type SimpleConnHandle

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

func NewSimpleConnHandle

func NewSimpleConnHandle(opts ...SimpleConnHandleOption) *SimpleConnHandle

func (*SimpleConnHandle) Close

func (sh *SimpleConnHandle) Close() error

func (*SimpleConnHandle) Context

func (sh *SimpleConnHandle) Context(ctx context.Context)

func (*SimpleConnHandle) Handle

func (sh *SimpleConnHandle) Handle(conn *net.TCPConn) (err error)

func (*SimpleConnHandle) Valid

func (sh *SimpleConnHandle) Valid() error

type SimpleConnHandleOption

type SimpleConnHandleOption func(opts *simpleConnHandleOptions) error

func WithContext

func WithContext(rootctx context.Context) SimpleConnHandleOption

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) SimpleConnHandleOption

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) SimpleConnHandleOption

type TcpConnHandler

type TcpConnHandler interface {
	Valid() error
	Context(ctx context.Context)
	// Handle(ctx context.Context, conn *net.TCPConn) error
	Handle(conn *net.TCPConn) error
}

需要是并发安全的.

type TcpConnSession

type TcpConnSession interface {
	SessionId() int64
	Close() error
	Ready() error
	Hello() error
	DoWork() error
}

type TcpConnSessionFactorier

type TcpConnSessionFactorier interface {
	CreateSession(conn *net.TCPConn, opts *SessionOptions) TcpConnSession
}

type TcpConnSessionFactory

type TcpConnSessionFactory struct {
	Pack        Packer
	HandleFunc  HandleFunc
	PushFunc    func(ctx context.Context, writeQueue chan<- Payloader)
	HandleError func(ctx context.Context, err error) (payload Payloader, close bool) // 全局错误处理器,如果返回false,将会关闭该连接,返回true则继续处理该连接的请求
}

func (*TcpConnSessionFactory) CreateSession

func (f *TcpConnSessionFactory) CreateSession(conn *net.TCPConn, opts *SessionOptions) TcpConnSession

type TcpServer

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

func (*TcpServer) CreateListen

func (ts *TcpServer) CreateListen() (*net.Listener, error)

func (*TcpServer) Init

func (ts *TcpServer) Init(opts ...Options) error

func (*TcpServer) ListenAddrs

func (ts *TcpServer) ListenAddrs() string

func (*TcpServer) Serve

func (ts *TcpServer) Serve() error

func (*TcpServer) Stop

func (ts *TcpServer) Stop()

func (*TcpServer) Wait

func (ts *TcpServer) Wait() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto