tcp

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//PackMaxSize 定义单个通信包最大字节数
	PackMaxSize = uint32(6553500)
	//定义包头flag
	PackHdrFlag = uint16(0xAABB)
)

Variables

This section is empty.

Functions

func EncodeDataHdr

func EncodeDataHdr(hdr DataHdr) ([]byte, error)

EncodeDataHdr 将数据包头信息序列化为网络数据 +++++++++++++++++++++++++++++++++++++---------------------------- | 2B flag | 2B chanid |4B data length | (n) data | +++++++++++++++++++++++++++++++++++++----------------------------

func ReadData

func ReadData(conn net.Conn, datalen int) ([]byte, error)

ReadData 从连接读取数据

func ReadDataHdr

func ReadDataHdr(conn net.Conn) (data []byte, err error)

ReadDataHdr 读取包头数据

func WriteData

func WriteData(conn net.Conn, data []byte, chanid uint16) (bool, error)

Send : TCP 发送数据接口 Params : data 用户数据缓冲区

ChanID 数据通道标识(用户自定义用途)

Types

type AcceptCbFunc

type AcceptCbFunc func(ts *TcpServer, conn net.Conn)

type CloseCbFunc

type CloseCbFunc func(ts *TcpServer, conn net.Conn)

type DataHdr

type DataHdr struct {
	Flag    uint16
	ChanID  uint16
	DataLen uint32
}

DataHdr 数据包头定义

func DecodeDataHdr

func DecodeDataHdr(hdrdata []byte) (hdr DataHdr, err error)

DecodeDataHdr 将网络数据反序列化为数据包头 +++++++++++++++++++++++++++++++++++++---------------------------- | 2B flag | 2B chanid |4B data length | (n) data | +++++++++++++++++++++++++++++++++++++----------------------------

func MakeDataHdr

func MakeDataHdr(ChanID uint16, DataLen uint32) DataHdr

MakeDataHdr 生成一个数据包头结构对象

type ReadCbFunc

type ReadCbFunc func(ts *TcpServer, conn net.Conn, data []byte, channel uint16)

type TcpClient

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

TcpClient TCP客户端结构体定义

func (*TcpClient) Close

func (tc *TcpClient) Close()

Close : 关闭连接

func (*TcpClient) Connect

func (tc *TcpClient) Connect(addr string, args ...interface{}) error

Connect : 连接函数,目标服务器地址类型"192.168.1.105:9981" args... 第1个参数是bool类型,是否将地址解析为TCP6格式(默认false)

func (*TcpClient) Recv

func (tc *TcpClient) Recv() (Data []byte, ChanID uint16, DataLen int, err error)

Recv : 接收数据 return Data, Type, DataLen and err

func (*TcpClient) Send

func (tc *TcpClient) Send(data []byte, ChanID uint16) (bool, error)

Send : TCP 发送数据接口 Params : data 用户数据缓冲区

Type 数据包标志(用户自定义用途)

type TcpServer

type TcpServer struct {
	OnAccept AcceptCbFunc //收到连接通知回调
	OnRead   ReadCbFunc   //消息接收完毕回调
	OnClose  CloseCbFunc  //连接断开回调
	// contains filtered or unexported fields
}

TcpServer TCP服务对象

func CreateTcpServer

func CreateTcpServer(cbAccept AcceptCbFunc, cbClose CloseCbFunc, cbRead ReadCbFunc) (ts *TcpServer)

CreateTcpServer 创建TCP服务 return ts TCP服务对象

func (*TcpServer) Close

func (ts *TcpServer) Close(conn net.Conn)

Close 客户端连接关闭

func (*TcpServer) Join

func (ts *TcpServer) Join(conn net.Conn)

Join 新连接加入管理map

func (*TcpServer) KickConn

func (ts *TcpServer) KickConn(conn net.Conn)

KickConn 强制断开客户端连接

func (*TcpServer) Listen

func (ts *TcpServer) Listen(addr string) bool

Listen 监听并接收客户端连接 addr参数 1、非加密模式服务监听URL "tcp://192.168.1.105:8899"监听指定IP端口或"tcp://:8899"监听本机所有IP端口

2、加密模式服务监听URL "tls://192.168.1.105:8899" 监听指定IP端口或"tls://:8899"监听本机所有IP端口

return conn接收的连接,err 错误信息

func (*TcpServer) Loop

func (ts *TcpServer) Loop()

Loop 阻塞线程死循环

func (*TcpServer) ReadConn

func (ts *TcpServer) ReadConn(conn net.Conn)

ReadConn 从客户端读取数据

func (*TcpServer) Send

func (ts *TcpServer) Send(conn net.Conn, data []byte, ChanID uint16) (bool, error)

Send 向指定客户端连接发送数据

func (*TcpServer) Stop

func (ts *TcpServer) Stop()

Stop 强制停止TCP服务

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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