tcpclient

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 6 Imported by: 1

README

cchantcpclient

tcp cleint frame

  • notice 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 channel) before acctually write into system tcp send buffer.
  • client will handler client connection session, upper layer will only have to process business session layer's management.

tcp client架构

  • 通过事件回调handler向外反馈client事件
  • 自定义消息的拆解包协议
  • 每个客户端有独立的读写go协程,待发送消息会先存入发送队列(使用channel),再写入系统tcp发送缓冲区
  • 客户端将管理client连接,上层应用仅需按协议处理session层的内容
accepting connection:
+------------+    +------------+    +----------------+
|            |    |            |    |                |
| tcp client |--->| establish  |--->| set client     |
|            |    | connection |    |   connection  |
|            |    |            |    |   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

Index

Constants

View Source
const (
	// DefaultSendBuffSize default send buff size
	DefaultSendBuffSize = 1 * 10000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// tcp send buff size
	SendBuffsize int

	// after recieve a whole package, the receive callback will go async or sync
	AsyncReceive bool

	// after data write to tcp sys buff, do OnSendedData() call back require
	RequireSendedCb bool
	// sended callback if async or sync
	AsyncSended bool
}

Config extra config

func DefaultConfig

func DefaultConfig() Config

DefaultConfig default Config

type CtcpCli

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

CtcpCli tcp client

func New

func New(eventCb EventHandler, cnf Config) *CtcpCli

New new tcp client

func (*CtcpCli) Close

func (cli *CtcpCli) Close()

Close close connection

func (*CtcpCli) ConnectToServer

func (cli *CtcpCli) ConnectToServer(ip string, port uint16) error

ConnectToServer connect to remote server

func (*CtcpCli) ConnectToServer_Timeout added in v0.1.3

func (cli *CtcpCli) ConnectToServer_Timeout(ip string, port uint16, timeout time.Duration) error

ConnectToServer connect to remote server

func (*CtcpCli) SendToServer

func (cli *CtcpCli) SendToServer(msg interface{}) (busy bool, retErr error)

SendToServer send message to server

@return busy bool : true -- buff is full, you may need to try again

type EventHandler

type EventHandler interface {
	// new connections event
	OnNewConnection(serverIP string, serverPort uint16)
	// disconnected event
	OnDisconnected(serverIP string, serverPort uint16)
	// receive data event
	OnReceiveData(serverIP string, serverPort uint16, pPacks []interface{})
	// data already sended event
	OnSendedData(serverIP string, serverPort uint16, msg interface{}, bysSended []byte, length int)
	// event
	OnEvent(msg string)
	// error
	OnError(msg string, err error)
	// error
	OnErrorStr(msg string)

	// data protocol
	ProtocolIF
}

EventHandler client callback control handler

type ProtocolIF

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

	// depack the message packages from read []byte
	Depack(rawData []byte) ([]byte, []interface{})
}

ProtocolIF interface to self define pack and depack

Jump to

Keyboard shortcuts

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