cbufftcpclient

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: 7 Imported by: 0

README

cbufftcpclient

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 buffer) 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协程,待发送消息会先存入发送队列(使用切片),再写入系统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

Overview

Package cbufftcpclient tcp client

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config extra 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) DumpSendBuffer

func (cli *CtcpCli) DumpSendBuffer() *list.List

DumpSendBuffer dump send buffer out, session's send buffer will replace with empty one

func (*CtcpCli) SendBuffToServer

func (cli *CtcpCli) SendBuffToServer(buff *list.List) error

SendBuffToServer send buff message to server

func (*CtcpCli) SendPriorToServer

func (cli *CtcpCli) SendPriorToServer(msg interface{}) error

SendPriorToServer send prior message to server

func (*CtcpCli) SendPrioresToServer

func (cli *CtcpCli) SendPrioresToServer(msgs []interface{}) error

SendPrioresToServer send prior message to server

func (*CtcpCli) SendToServer

func (cli *CtcpCli) SendToServer(msg interface{}) error

SendToServer send message to server

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)
	// 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