server

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MulanPSL-2.0 Imports: 9 Imported by: 2

Documentation

Index

Constants

View Source
const UDPPacketClipHeaderLen = 8 + 8 + 2 + 2
View Source
const UDPPacketClipLimit = 480

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncReceiver

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

异步接收者 生命周期中会持有2个goroutine 必须由用户代码Close, 才能关闭连接和goroutine

func NewAsyncReceiver added in v0.0.2

func NewAsyncReceiver(ctx *ServerContext, conn Connection) *AsyncReceiver

func (*AsyncReceiver) Close added in v0.0.2

func (r *AsyncReceiver) Close()

func (*AsyncReceiver) Conn added in v0.0.2

func (r *AsyncReceiver) Conn() Connection

func (*AsyncReceiver) Ctx added in v0.0.2

func (r *AsyncReceiver) Ctx() *ServerContext

func (*AsyncReceiver) Recv added in v0.0.2

func (r *AsyncReceiver) Recv() (Message, error)

接收消息. 尝试从接收缓冲区中读一个消息, 缓冲区空时直接返回nil

func (*AsyncReceiver) RecvChan

func (r *AsyncReceiver) RecvChan() <-chan Message

以channel方式接收消息.

func (*AsyncReceiver) Send added in v0.0.2

func (r *AsyncReceiver) Send(msg Message) error

发送消息. 消息将进入发送缓冲区

func (*AsyncReceiver) String added in v0.0.2

func (r *AsyncReceiver) String() string

type Callback

type Callback struct {
	OnDebug func(string)

	OnError func(string, error, Receiver, Message)

	OnNewReceiver func(Receiver)
}

func (*Callback) TrigDebug

func (rc *Callback) TrigDebug(str string)

func (*Callback) TrigError

func (rc *Callback) TrigError(s string, e error, r Receiver, m Message)

func (*Callback) TrigNewReceiver

func (rc *Callback) TrigNewReceiver(r Receiver)

type Connection

type Connection interface {
	ID() uint64
	Remote() string

	Send(*RawMessage) error
	Recv() (*RawMessage, error)

	Close() error
}

连接的抽象封装

type ConnectionConfig

type ConnectionConfig struct {
	TimeoutEachRead  time.Duration //每次读操作的超时时间
	TimeoutEachWrite time.Duration //每次写操作的超时时间
}

type DefaultMessageMux

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

缺省实现的消息路由集

func NewDefaultMessageMux

func NewDefaultMessageMux() *DefaultMessageMux

func (*DefaultMessageMux) Handle

func (mux *DefaultMessageMux) Handle(protoID uint32, h MessageHandler)

func (*DefaultMessageMux) HandleUnknown

func (mux *DefaultMessageMux) HandleUnknown(h MessageHandler)

func (*DefaultMessageMux) Handler

func (mux *DefaultMessageMux) Handler(protoID uint32) MessageHandler

type FuncUDPConnectionSend added in v0.0.2

type FuncUDPConnectionSend func(addr *net.UDPAddr, data []byte) error

UDP消息发送的调用函数原型

type Listener

type Listener interface {
	Ctx() *ServerContext

	Addr() string

	//blocking function. 建议起goroutine
	Accepting(cbNewConn func(Connection))
	Stop()
}

监听器的抽象封装

func NewTCPListener

func NewTCPListener(ctx *ServerContext) (Listener, error)

func NewUDPListener added in v0.0.2

func NewUDPListener(ctx *ServerContext) (Listener, error)

type Message

type Message interface {
	ProtoID() uint32
	String() string
}

抽象消息体

type MessageHandler

type MessageHandler func(Receiver, Message) error

消息响应句柄原型

type MessageMux

type MessageMux interface {
	Handle(uint32, MessageHandler)
	Handler(uint32) MessageHandler

	//如果注册了此方法, 那么所有未被路由命中的消息都将由此函数处理
	HandleUnknown(MessageHandler)
}

消息路由集定义

type MessageProtocol

type MessageProtocol interface {
	Encode(msg Message) ([]byte, error)
	Decode(protoID uint32, bs []byte) (Message, error)

	New(protoID uint32) Message
}

消息协议集

type MessageServeAsync

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

消息服务异步型

func NewMessageServeAsync

func NewMessageServeAsync(r *AsyncReceiver, mux MessageMux) *MessageServeAsync

func (*MessageServeAsync) Mux

func (ms *MessageServeAsync) Mux() MessageMux

func (*MessageServeAsync) Processing

func (ms *MessageServeAsync) Processing(ctx context.Context)

blocking. 建议加goroutine, 并使用context.WithCancel 持续地响应消息

func (*MessageServeAsync) Receiver

func (ms *MessageServeAsync) Receiver() *AsyncReceiver

type MessageServeSync

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

消息服务同步型

func NewMessageServeSync

func NewMessageServeSync(r *SyncReceiver, mux MessageMux) *MessageServeSync

func (*MessageServeSync) Mux

func (ms *MessageServeSync) Mux() MessageMux

func (*MessageServeSync) Receiver

func (ms *MessageServeSync) Receiver() *SyncReceiver

func (*MessageServeSync) RecvOnce

func (ms *MessageServeSync) RecvOnce() error

阻塞等待地响应一个消息

type RawMessage

type RawMessage struct {
	ProtoID uint32
	Payload []byte
}

T-L-V消息体

type RawUDPPacket added in v0.0.2

type RawUDPPacket struct {
	Addr   *net.UDPAddr
	Packet []byte
}

原始的UDP报文, 由远端地址和字节块组成

type Receiver

type Receiver interface {
	Ctx() *ServerContext
	Conn() Connection

	Close()

	String() string

	Send(Message) error
	Recv() (Message, error)
}

接收者抽象定义

type Server

type Server struct {
	Ctx *ServerContext

	Listener        Listener
	CBNewConnection func(Connection)
	// contains filtered or unexported fields
}

服务实例

func (*Server) Close

func (s *Server) Close()

服务退出

type ServerBuilder

type ServerBuilder struct {
	Ctx *ServerContext

	//选用的监听器实现
	NewListener func(*ServerContext) (Listener, error)

	//选用的接收者实现
	NewReceiver func(*ServerContext, Connection) Receiver
}

服务构造器, 用于配置具体实现的组合方案

func (*ServerBuilder) NewServer

func (b *ServerBuilder) NewServer() (*Server, error)

从服务构造器构造服务

type ServerContext

type ServerContext struct {
	MessageProtocol MessageProtocol

	CB *Callback

	TCPListenerConfig   *TCPListenerConfig
	TCPConnectionConfig *TCPConnectionConfig
	UDPListenerConfig   *UDPListenerConfig
	UDPConnectionConfig *UDPConnectionConfig
}

服务上下文 包含服务内部各组件的共享资源

type SyncReceiver

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

同步接收者 必须由用户代码Close, 才能关闭连接ss

func NewSyncReceiver added in v0.0.2

func NewSyncReceiver(ctx *ServerContext, conn Connection) *SyncReceiver

func (*SyncReceiver) Close added in v0.0.2

func (r *SyncReceiver) Close()

func (*SyncReceiver) Conn added in v0.0.2

func (r *SyncReceiver) Conn() Connection

func (*SyncReceiver) Ctx added in v0.0.2

func (r *SyncReceiver) Ctx() *ServerContext

func (*SyncReceiver) Recv added in v0.0.2

func (r *SyncReceiver) Recv() (Message, error)

阻塞同步的接收一个消息

func (*SyncReceiver) Send added in v0.0.2

func (r *SyncReceiver) Send(msg Message) error

阻塞同步的发送一个消息

func (*SyncReceiver) String added in v0.0.2

func (r *SyncReceiver) String() string

type TCPConnection

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

连接的TCP封装

func NewTCPConnection

func NewTCPConnection(cfg *TCPConnectionConfig, conn *net.TCPConn, id uint64) *TCPConnection

func (*TCPConnection) Close

func (tc *TCPConnection) Close() error

func (*TCPConnection) ID

func (tc *TCPConnection) ID() uint64

func (*TCPConnection) Recv

func (tc *TCPConnection) Recv() (*RawMessage, error)

不能并发调用

func (*TCPConnection) Remote

func (tc *TCPConnection) Remote() string

func (*TCPConnection) Send

func (tc *TCPConnection) Send(msg *RawMessage) error

不能并发调用

type TCPConnectionConfig

type TCPConnectionConfig struct {
	*ConnectionConfig
}

type TCPListener

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

监听器的TCP封装 监听构造的连接封装为TCPConnection

func (*TCPListener) Accepting

func (l *TCPListener) Accepting(cbNewConn func(Connection))

blocking. 监听等待循环

func (*TCPListener) Addr

func (l *TCPListener) Addr() string

func (*TCPListener) Ctx

func (l *TCPListener) Ctx() *ServerContext

func (*TCPListener) Stop

func (l *TCPListener) Stop()

type TCPListenerConfig

type TCPListenerConfig struct {
	ListenAddress string
}

type UDPConnection added in v0.0.2

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

连接的UDP封装

func NewUDPConnection added in v0.0.2

func NewUDPConnection(cfg *UDPConnectionConfig, id uint64, addr *net.UDPAddr, fSend FuncUDPConnectionSend) *UDPConnection

func (*UDPConnection) Close added in v0.0.2

func (uc *UDPConnection) Close() error

func (*UDPConnection) ID added in v0.0.2

func (uc *UDPConnection) ID() uint64

func (*UDPConnection) IsClosed added in v0.0.2

func (uc *UDPConnection) IsClosed() bool

func (*UDPConnection) LastRecvTs added in v0.0.2

func (uc *UDPConnection) LastRecvTs() int64

最近一次接到消息的时间. 接到分片就算更新.

func (*UDPConnection) PushPacketClip added in v0.0.2

func (uc *UDPConnection) PushPacketClip(clip *UDPPacketClip) error

推入一个消息分片

func (*UDPConnection) Recv added in v0.0.2

func (uc *UDPConnection) Recv() (*RawMessage, error)

阻塞的接收消息

func (*UDPConnection) Remote added in v0.0.2

func (uc *UDPConnection) Remote() string

func (*UDPConnection) Send added in v0.0.2

func (uc *UDPConnection) Send(msg *RawMessage) error

不能并发调用

type UDPConnectionConfig added in v0.0.2

type UDPConnectionConfig struct {
	*ConnectionConfig

	CacheClipLimit int
}

type UDPListener added in v0.0.2

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

监听器的UDP封装

func (*UDPListener) Accepting added in v0.0.2

func (l *UDPListener) Accepting(cbNewConn func(Connection))

blocking. 监听等待循环

func (*UDPListener) Addr added in v0.0.2

func (l *UDPListener) Addr() string

func (*UDPListener) Ctx added in v0.0.2

func (l *UDPListener) Ctx() *ServerContext

func (*UDPListener) Stop added in v0.0.2

func (l *UDPListener) Stop()

type UDPListenerConfig added in v0.0.2

type UDPListenerConfig struct {
	ListenAddress string
	ConnReadCnt   int
	ConnExpireS   int64
}

type UDPPacket added in v0.0.2

type UDPPacket struct {
	Addr   *net.UDPAddr
	Packet []byte
}

一个UDP消息体. 这是经过分片聚合后的消息体, .Packet的长度可以超过MTU

type UDPPacketClip added in v0.0.2

type UDPPacketClip struct {
	ConnID    uint64
	Addr      *net.UDPAddr
	PacketSeq uint64
	ClipLen   uint16
	ClipSeq   uint16
	Payload   []byte
}

UDP报文头顺序: uint64 连接ID uint64 消息序号 uint16 分片数 uint16 分片序号 [0, ~)

func (*UDPPacketClip) Decode added in v0.0.2

func (clip *UDPPacketClip) Decode(bs []byte) error

func (*UDPPacketClip) Encode added in v0.0.2

func (clip *UDPPacketClip) Encode() ([]byte, error)

Directories

Path Synopsis
sample

Jump to

Keyboard shortcuts

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