nnet

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 11 Imported by: 0

README

nnet

简单封装的网络层库,能够任意切换 tcp ws 等通信协议,能够自由使用数据封装格式 专为这个项目制作

Example

see lobby

Todo

  • 能够简单的支持内存池
  • 能够通过支持中间件方式支持加密和压缩
  • Hub 配置方式更灵活

Documentation

Index

Constants

View Source
const (
	// Close Reason 是一个 int32 型数据,这是系统预置的几个代码
	CLOSE_REASON_READ          = 0
	CLOSE_REASON_WRITE         = 0
	CLOSE_REASON_PROTOCOL      = 1
	CLOSE_REASON_READTIMEOUT   = 4  // HEARTBEAT
	CLOSE_REASON_SERVER_CLOSED = 16 // 本服务器关闭
)

关闭原因

Variables

View Source
var (
	ErrExistsAlready          = errors.New("item already exists")
	ErrNotExists              = errors.New("item not exists")
	ErrConnectionReject       = errors.New("connection rejected by logic")
	ErrConnClosing            = errors.New("use of closed network connection")
	ErrWriteBlocking          = errors.New("write packet was blocking")
	ErrReadBlocking           = errors.New("read packet was blocking")
	ErrEmptySlice             = errors.New("the slice is empty")
	ErrSliceOutOfRange        = errors.New("the slice is out of range")
	ErrBufferSizeInsufficient = errors.New("buffer size is too small")
	ErrInterface              = errors.New("interface convertion failed")
)
View Source
var (
	DEF_SESSION_ID     uint64 = 0
	RUBBISH_SESSION_ID uint64 = 1
)

Functions

func Intn

func Intn(a int) int

Types

type CallbackWsPath

type CallbackWsPath func(http.ResponseWriter, *http.Request)

type Hub

type Hub struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*Hub) Callback

func (self *Hub) Callback() ISessionCallback

func (*Hub) ChQuit

func (self *Hub) ChQuit() <-chan struct{}

func (*Hub) Conf

func (self *Hub) Conf() *HubConfig

func (*Hub) DelSession

func (self *Hub) DelSession(id uint64) error

func (*Hub) GetAllSessions

func (self *Hub) GetAllSessions() map[uint64]ISession

func (*Hub) GetSession

func (self *Hub) GetSession(id uint64) (ISession, error)

func (*Hub) GetSessionNum

func (self *Hub) GetSessionNum() int

func (*Hub) PeekSession

func (self *Hub) PeekSession(id uint64) (ISession, error)

func (*Hub) Protocol

func (self *Hub) Protocol() IProtocol

func (*Hub) PutSession

func (self *Hub) PutSession(id uint64, ses ISession) error

func (*Hub) RandSession

func (self *Hub) RandSession() (ISession, error)

func (*Hub) Wg

func (self *Hub) Wg() *sync.WaitGroup

type HubConfig

type HubConfig struct {
	SizeOfSendChan uint32
	SizeOfRecvChan uint32
	ReadBufSize    int
	WriteBufSize   int
	Timeout        time.Duration // 发送等超时
	Tick           time.Duration // 定时回调
	ReadTimeout    time.Duration // 讀超時,如果為0,則無限等待。超時到達,意味著客戶端心跳丟失
}

type IConn

type IConn interface {
	io.ReadWriteCloser
	SetReadDeadline(time.Time) error
	SetWriteDeadline(time.Time) error
	RemoteAddr() net.Addr
	LocalAddr() net.Addr
}

type IHandler added in v0.1.5

type IHandler interface {
	Dispatch(ISession, IPacket) bool
}

type IHub

type IHub interface {
	Lock() // support locker semantics
	Unlock()

	Wg() *sync.WaitGroup        // object
	ChQuit() <-chan struct{}    // 返回一个通道,用于退出 hub 循环
	Conf() *HubConfig           // 返回配置信息
	Callback() ISessionCallback // 返回回调对象
	Protocol() IProtocol        // 返回数据协议

	Start() error // 启动 hub
	Stop() error  // 停止 hub
	DoJob(int)    // 执行 hub 中其他任务

	PutSession(uint64, ISession) error // session 管理,这里的 session 必须基于 id
	DelSession(uint64) error
	GetSession(uint64) (ISession, error)
	PeekSession(uint64) (ISession, error)
	RandSession() (ISession, error)
	GetSessionNum() int
	GetAllSessions() map[uint64]ISession
}

type IMiddleware

type IMiddleware interface {
	BeforeSend([]byte) []byte
	AfterReceived([]byte) []byte
}

type IPacket

type IPacket interface {
	// serialize to binary format to be sent
	Serialize() []byte
	// free the memory if needs
	Destroy([]byte)
	// if need to close socket after sending this packet
	ShouldClose() (bool, int32)
}

type IProtocol

type IProtocol interface {
	// parse by raw data to a packet
	ReadPacket(conn IConn) (IPacket, error)
}

type ISession

type ISession interface {
	Do() // session 开始工作

	Close(int32) // 停止所有工作
	IsClosed() bool

	Write(IPacket, time.Duration) error
	AWrite(IPacket, time.Duration) error // 异步发送

	GetData() interface{} // 辅助数据
	SetData(interface{})

	UpdateId(uint64) //更新ID
	Id() uint64
	SetId(uint64)

	GetRawConn() IConn
}

type ISessionCallback

type ISessionCallback interface {
	OnClosed(ISession, int32)
	OnConnected(ISession) (bool, int32)
	OnMessage(ISession, IPacket) bool
	OnHeartbeat(ISession) bool
}

type Packet

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

func NewPacket

func NewPacket(data []byte, params ...int32) *Packet

func (*Packet) Destroy

func (self *Packet) Destroy(b []byte)

func (*Packet) Msg added in v0.1.4

func (self *Packet) Msg() []byte

func (*Packet) Serialize

func (self *Packet) Serialize() []byte

func (*Packet) ShouldClose

func (self *Packet) ShouldClose() (bool, int32)

type Protocol

type Protocol struct {
	BufSize int
}

func NewProtocol

func NewProtocol(buf_size int) *Protocol

func (*Protocol) ReadPacket

func (self *Protocol) ReadPacket(conn IConn) (IPacket, error)

type Session

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

长连接

func (*Session) AWrite

func (self *Session) AWrite(pkt IPacket, timeout time.Duration) (err error)

public 异步写入

func (*Session) Close

func (self *Session) Close(reason int32)

func (*Session) Do

func (self *Session) Do()

func (*Session) GetData

func (self *Session) GetData() interface{}

func (*Session) GetRawConn

func (self *Session) GetRawConn() IConn

func (*Session) Id

func (self *Session) Id() uint64

func (*Session) IsClosed

func (self *Session) IsClosed() bool

func (*Session) SetData

func (self *Session) SetData(data interface{})

func (*Session) SetId

func (self *Session) SetId(id uint64)

func (*Session) UpdateId

func (self *Session) UpdateId(id uint64)

func (*Session) Write

func (self *Session) Write(pkt IPacket, timeout time.Duration) error

type TCPClient

type TCPClient struct {
	*Hub
	// contains filtered or unexported fields
}

func NewTCPClient

func NewTCPClient(hc *HubConfig, cb ISessionCallback, p IProtocol, addr string) *TCPClient

func (*TCPClient) DoJob

func (self *TCPClient) DoJob(p int)

func (*TCPClient) Start

func (self *TCPClient) Start() error

func (*TCPClient) Stop

func (self *TCPClient) Stop() error

type TcpConn

type TcpConn struct {
	*net.TCPConn
}

type TcpServer

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

func NewTcpServer

func NewTcpServer(cf *HubConfig, cb ISessionCallback, p IProtocol, ls *net.TCPListener) *TcpServer

func (*TcpServer) DoJob

func (self *TcpServer) DoJob(int)

func (*TcpServer) Start

func (self *TcpServer) Start() error

func (*TcpServer) Stop

func (self *TcpServer) Stop() error

type WsClient

type WsClient struct {
	*Hub
	// contains filtered or unexported fields
}

客户端组

func NewWsClient

func NewWsClient(cf *HubConfig, cb ISessionCallback, p IProtocol, addr string) *WsClient

func (*WsClient) DoJob

func (self *WsClient) DoJob(int)

func (*WsClient) Start

func (self *WsClient) Start() error

func (*WsClient) Stop

func (self *WsClient) Stop() error

type WsConn

type WsConn struct {
	*websocket.Conn
}

func NewWsConn

func NewWsConn(c *websocket.Conn) *WsConn

func (*WsConn) Read

func (self *WsConn) Read(p []byte) (int, error)

func (*WsConn) Write

func (self *WsConn) Write(p []byte) (int, error)

type WsServer

type WsServer struct {
	*Hub
	// contains filtered or unexported fields
}

func NewWsServer

func NewWsServer(cf *HubConfig, cb ISessionCallback, p IProtocol, addr string, ws_path string, routes map[string]CallbackWsPath) *WsServer

func (*WsServer) DoJob

func (self *WsServer) DoJob(int)

func (*WsServer) Start

func (self *WsServer) Start() error

func (*WsServer) Stop

func (self *WsServer) Stop() error

Jump to

Keyboard shortcuts

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