cbufftcpserver

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 9 Imported by: 0

README

cbufftcpserver

tcp server frame

  • notice server and client events via event callback handler.
  • could define your own package pack/unpack protocol.
  • each client with have it's own read and write goroutine, write messages will store in the send queue(use buffer) before acctually write into system tcp send buffer.
  • server will handler client connection session, upper layer will only have to process business session layer's management.

tcp server架构

  • 通过事件回调handler向外反馈server和client事件
  • 自定义消息的拆解包协议
  • 每个客户端有独立的读写go协程,待发送消息会先存入发送队列(使用切片),再写入系统tcp发送缓冲区
  • 服务端将管理client连接,上层应用仅需按协议处理session层的内容
accepting connection:
+------------+    +------------+    +----------------+
|            |    |            |    |                |
| tcp server |--->| accept     |--->| add to client  |
|            |    | connection |    |   connections  |
|            |    |            |    |   management   |
+------------+    +------------+    +----------------+
                                            |
                                            |
+------------+    +-------------------+     |
|            |    |                   |     |
| your own   |<---| OnNewConnection() | <---+
|  process   |    | callback          |  
|            |    |                   | 
+------------+    +-------------------+

in client life time:
read tcp datas
+------------+    +-----------------------+    +-----------------+
| read       |    | unpack packet payload |    | OnReceiveData() |
| connection |--->| use your own protocol |--->| callback        |
+------------+    +-----------------------+    +-----------------+

send tcp datas
+------------+    +-----------+    +------------+
| your own   |    | server    |    | client's   |
| app send   |--->| send func |--->| send queue |
+------------+    +-----------+    +------------+
                                            |
                                            |
   +--------------+     +--------------+    |
   | client's send |    | pack packet  |    |
   | loop write    |<---| use your own |<---+
   | []byte to sys |    |  protocol    | 
   | tcp send buff |    +--------------+
   +---------------+                        

Documentation

Overview

Package cbufftcpserver tcp server

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// AsyncReceive after recieve a whole package, the receive callback will go sync or async
	AsyncReceive bool
}

Config extra config

type CtcpsvrSt

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

CtcpsvrSt tcp server instance

func NewTCPSvr

func NewTCPSvr(eventCb EventHandler, cnf Config) *CtcpsvrSt

NewTCPSvr new ctcpserver object

func (*CtcpsvrSt) CloseClient

func (t *CtcpsvrSt) CloseClient(clientID uint64, reason string)

CloseClient close one client

func (*CtcpsvrSt) CloseClients

func (t *CtcpsvrSt) CloseClients(clientIDs []uint64, reason string)

CloseClients close clients

func (*CtcpsvrSt) SendToAllClients

func (t *CtcpsvrSt) SendToAllClients(msg interface{}) error

SendToAllClients send to all clients

func (*CtcpsvrSt) SendToClient

func (t *CtcpsvrSt) SendToClient(clientID uint64, msg interface{}) error

SendToClient sent to client

func (*CtcpsvrSt) StartServer

func (t *CtcpsvrSt) StartServer(iPort uint16) error

StartServer start server

func (*CtcpsvrSt) StopServer

func (t *CtcpsvrSt) StopServer() error

StopServer top server

type EventHandler

type EventHandler interface {
	// new connections event
	OnNewConnection(clientID uint64, clientIP string, clientAddr string)
	// disconnected event
	OnDisconnected(clientID uint64, clientIP string, clientAddr string)
	// receive data event
	OnReceiveData(clientID uint64, clientIP string, clientAddr string, pPacks []interface{})
	// data already sended event
	OnSendedData(clientID uint64, clientIP string, clientAddr string, msg interface{}, bysSended []byte, length int)
	// error
	OnError(msg string, err error)
	// error
	OnCliError(clientID uint64, clientIP string, clientAddr string, msg string, err error)
	// error
	OnCliErrorStr(clientID uint64, clientIP string, clientAddr string, msg string)

	// data protocol
	ProtocolIF
}

EventHandler server callback control handler

type ProtocolIF

type ProtocolIF interface {
	// pack message into the []byte to be written
	Pack(clientID uint64, cliIP string, cliAddr string, msg interface{}) ([]byte, error)

	// depack the message packages from read []byte
	Depack(clientID uint64, cliIP string, cliAddr string, rawData []byte) ([]byte, []interface{})
}

ProtocolIF interface to self define pack and depack

type ServerStatus

type ServerStatus int

ServerStatus server status

const (
	// ServerStatusClosed closed
	ServerStatusClosed ServerStatus = 0
	// ServerStatusStarting starting
	ServerStatusStarting ServerStatus = 1
	// ServerStatusRunning running
	ServerStatusRunning ServerStatus = 2
	// ServerStatusStopping stopping
	ServerStatusStopping ServerStatus = 3
)

Jump to

Keyboard shortcuts

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