tower

package module
v0.0.7-0...-1ed2a47 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2022 License: MIT Imports: 11 Imported by: 0

README

GoDoc Coverage Status Test Test

Tower

Tower is a tcp connection manager server.

Document

[文档] [Doc]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BootStraper

type BootStraper interface {
	Listen()                                              // start server
	Stop()                                                // stop server
	GetConnMgr() ConnManager                              // get connection manager
	SetOnConnStart(func(conn Connectioner))               // set hook func when client connect server
	SetOnConnClose(func(conn Connectioner))               // set hook func when client disconnect server
	CallOnConnStart(conn Connectioner)                    // call OnConnStart hook func
	CallOnConnClose(conn Connectioner)                    // call OnConnStop hook func
	SetLogging(Logger)                                    // set logging
	AddRoute(msgId uint32, handleFunc func(ctx *Context)) // add route
	Logging() Logger                                      // logging
	GetConfig() *Config                                   // get server global config
}

func NewBootStrap

func NewBootStrap(config *Config) BootStraper

type Config

type Config struct {
	Name             string // server name
	IP               string // server listen ip
	IPVersion        string // ip version
	Port             int    // server listen port
	MaxPacketSize    uint32 // server accept max packet size
	MaxConn          int    // server accept max connection count
	WorkerPoolSize   uint32 // work pool
	MaxWorkerTaskLen uint32 // 业务工作Worker对应负责的任务队列最大任务存储数量
	MaxMsgChanLen    uint32 // SendBuffMsg发送消息的缓冲最大长度
}

func NewConfig

func NewConfig() *Config

NewConfig new config instance use default config option

type ConnManage

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

func NewConnManage

func NewConnManage() *ConnManage

func (*ConnManage) Add

func (c *ConnManage) Add(conn Connectioner)

Add add connection

func (*ConnManage) ClearConn

func (c *ConnManage) ClearConn()

ClearConn stop all connections, then delete them

func (*ConnManage) Get

func (c *ConnManage) Get(connID uint32) (Connectioner, error)

Get get connection by connection id

func (*ConnManage) Len

func (c *ConnManage) Len() int

Len get connections' count

func (*ConnManage) Remove

func (c *ConnManage) Remove(conn Connectioner)

Remove delete connection

type ConnManager

type ConnManager interface {
	Add(conn Connectioner)                   // add connection
	Remove(conn Connectioner)                // delete connection
	Get(connID uint32) (Connectioner, error) // get connection by connection id
	Len() int                                // get connections' count
	ClearConn()                              // stop all connections, then delete them
}

type Connection

type Connection struct {
	Server BootStraper
	Conn   *net.TCPConn
	ConnID uint32

	sync.RWMutex // RWLock
	// contains filtered or unexported fields
}

func (*Connection) GetConnID

func (c *Connection) GetConnID() uint32

func (*Connection) GetProperty

func (c *Connection) GetProperty(key string) (interface{}, error)

func (*Connection) GetTCPConnection

func (c *Connection) GetTCPConnection() *net.TCPConn

func (*Connection) RemoteAddr

func (c *Connection) RemoteAddr() net.Addr

func (*Connection) RemoveProperty

func (c *Connection) RemoveProperty(key string)

func (*Connection) SendBuffMsg

func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error

func (*Connection) SendMsg

func (c *Connection) SendMsg(msgId uint32, data []byte) error

func (*Connection) SetProperty

func (c *Connection) SetProperty(key string, value interface{})

func (*Connection) Start

func (c *Connection) Start()

func (*Connection) Stop

func (c *Connection) Stop()

type Connectioner

type Connectioner interface {
	Start()                                      // start connection work
	Stop()                                       // stop and close connection
	GetTCPConnection() *net.TCPConn              // 从当前连接获取原始的socket TCPConn
	GetConnID() uint32                           // get connection's id
	RemoteAddr() net.Addr                        // get remote client addr info
	SendMsg(msgId uint32, data []byte) error     // 直接将Message数据发送数据给远程的TCP客户端(无缓冲)
	SendBuffMsg(msgId uint32, data []byte) error // 直接将Message数据发送给远程的TCP客户端(有缓冲)
	SetProperty(key string, value interface{})   // set connection's property
	GetProperty(key string) (interface{}, error) // get connection's property
	RemoveProperty(key string)                   // delete connection's property
}

func NewConnection

func NewConnection(server BootStraper, conn *net.TCPConn, connID uint32, route router) Connectioner

NewConnection get new connection instance

type Context

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

func (*Context) GetConnection

func (ctx *Context) GetConnection() Connectioner

func (*Context) GetMsgData

func (ctx *Context) GetMsgData() []byte

func (*Context) GetMsgId

func (ctx *Context) GetMsgId() uint32

type DataPack

type DataPack struct{}

func NewDataPack

func NewDataPack() *DataPack

NewDataPack data pack instance initialization

func (*DataPack) GetHeadLen

func (dp *DataPack) GetHeadLen() uint32

GetHeadLen get pack head's length

func (*DataPack) Pack

func (dp *DataPack) Pack(msg *Message) ([]byte, error)

Pack pack data, compress data

func (*DataPack) Unpack

func (dp *DataPack) Unpack(binaryData []byte) (*Message, error)

Unpack unpack data, uncompress data

type LogLevel

type LogLevel int
const (
	Silent LogLevel = iota + 1
	Error
	Warn
	Info
	Debug
)

type LogWrite

type LogWrite interface {
	Printf(string, ...interface{})
}

type Logger

type Logger interface {
	LogMode(lvl LogLevel) Logger
	Debug(string, ...interface{})
	Info(string, ...interface{})
	Warn(string, ...interface{})
	Error(string, ...interface{})
}

func NewLogger

func NewLogger(w LogWrite, lvl LogLevel) Logger

type Message

type Message struct {
	DataLen uint32 // message's length
	Id      uint32 // message's id
	Data    []byte // message's content
}

func NewMsgPackage

func NewMsgPackage(id uint32, data []byte) *Message

NewMsgPackage create a message package instance

func (*Message) GetData

func (msg *Message) GetData() []byte

GetData get message content

func (*Message) GetDataLen

func (msg *Message) GetDataLen() uint32

GetDataLen get message data's length

func (*Message) GetMsgId

func (msg *Message) GetMsgId() uint32

GetMsgId

func (*Message) SetData

func (msg *Message) SetData(data []byte)

SetData set message's data content

func (*Message) SetDataLen

func (msg *Message) SetDataLen(len uint32)

SetDataLen

func (*Message) SetMsgId

func (msg *Message) SetMsgId(msgId uint32)

SetMsgId

Jump to

Keyboard shortcuts

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