tcpx

package
v1.2.47 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TCPSocketType tcp连接
	TCPSocketType = iota
	// WebSocketType WebSocket连接
	WebSocketType
)

Variables

View Source
var (
	DefaultBufferSize         = 1024
	DefaultMessageChannelSize = 32
	DefaultReadDeadline       = 15 * time.Second
)

定义的tcp读缓存区大小

Functions

func ByteToBool

func ByteToBool(i byte) bool

func BytesToInt

func BytesToInt(data []byte) int

func BytesToInt16

func BytesToInt16(data []byte) int16

func BytesToInt32

func BytesToInt32(data []byte) int32

func BytesToInt64

func BytesToInt64(data []byte) int64

func BytesToUint16

func BytesToUint16(data []byte) uint16

func BytesToUint32

func BytesToUint32(data []byte) uint32

func BytesToUint64

func BytesToUint64(data []byte) uint64

func Float32bytes

func Float32bytes(float float32) []byte

func Float64frombytes

func Float64frombytes(bytes []byte) float64

func IntToBytes

func IntToBytes(data interface{}) []byte

IntToBytes 整形转换成byte数组

func SetGlobalLogger

func SetGlobalLogger(logger TCPLibraryLogger)

SetGlobalLogger 设置日志对象

Types

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn 自定义连接对象结构体,可以存储tcp或webSocket连接对象

func (*Conn) GetClientID

func (c *Conn) GetClientID() string

GetClientID 获取当前连接id

func (*Conn) GetConnType

func (c *Conn) GetConnType() TCPType

GetConnType 获取连接类型

func (*Conn) SendMessage

func (c *Conn) SendMessage(data []byte) (int, error)

SendMessage 发送消息,参数为自己报文结构体

type Logger

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

Logger 日志记录

func (*Logger) Errorf

func (l *Logger) Errorf(format string, a ...interface{})

Errorf 打印错误 Error

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, a ...interface{})

Fatalf 打印错误 Fatal

func (*Logger) Infof

func (l *Logger) Infof(format string, a ...interface{})

Infof 打印错误 Info

func (*Logger) SetDefaultDebug

func (l *Logger) SetDefaultDebug(debug bool)

SetDefaultDebug 设置是否为debug模式 - 只对默认日志库生效

func (*Logger) Warnf

func (l *Logger) Warnf(format string, a ...interface{})

Warnf 打印错误 Warn

type ServerSocket

type ServerSocket interface {
	Socket
	GetClientID() string // 获取session id生成规则
}

ServerSocket 服务接口,实例化tcp server时传次参数

type Socket

type Socket interface {
	OnConnect(*Conn) error                       // 连接建立时
	OnError(error)                               // 连接发生错误
	OnClose(*Conn, error)                        // 关闭连接时
	OnRecMessage(context.Context, *Conn, []byte) // 接收消息时
}

Socket tcp通讯需要的一些回调函数

type TCPClient

type TCPClient struct {
	*TCPLibrary
	// contains filtered or unexported fields
}

TCPClient tcp客户端

func NewTCPClient

func NewTCPClient(debug bool, socket Socket) (*TCPClient, error)

NewTCPClient 创建一个tcp客户端

func (*TCPClient) Dial

func (c *TCPClient) Dial(conn net.Conn) error

Dial 连接tcp服务端

func (*TCPClient) DialAndStart

func (c *TCPClient) DialAndStart(address string) error

DialAndStart 连接到服务器,并开始读取信息

func (*TCPClient) DialAndStartTLS

func (c *TCPClient) DialAndStartTLS(address string) error

DialAndStartTLS 连接到服务器,并开始读取信息 tls

func (*TCPClient) GetConn

func (c *TCPClient) GetConn() *Conn

GetConn 获取连接对象

func (*TCPClient) Ping

func (c *TCPClient) Ping(v []byte)

Ping 保活

type TCPLibrary

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

TCPLibrary tcp库父类 - 每实例化

func NewTCPLibrary

func NewTCPLibrary(debug bool, socket ServerSocket) (*TCPLibrary, error)

NewTCPLibrary 创建TCPLibrary对象 - 只用于创建服务端对象时使用,客户端直接使用 NewTCPClient

func (*TCPLibrary) CloseForClientID

func (t *TCPLibrary) CloseForClientID(clientID string) error

CloseForClientID 根据clientID关闭连接

func (*TCPLibrary) GetClients

func (t *TCPLibrary) GetClients() *sync.Map

GetClients 获取客户端列表,在自己的业务中使用,使用时切记小心操作

func (*TCPLibrary) NewTCPServer

func (t *TCPLibrary) NewTCPServer() *TCPServer

NewTCPServer 创建一个server实例

func (*TCPLibrary) SendMessageToAll

func (t *TCPLibrary) SendMessageToAll(v []byte) (int, error)

SendMessageToAll 发送给所有客户端 只有服务端可调用

func (*TCPLibrary) SendMessageToClients

func (t *TCPLibrary) SendMessageToClients(v []byte, clientIDs ...string) (sendCount int, err error)

SendMessageToClients 发送数据给指定客户端 返回值,第一个值为发送成功几个连接 只有服务端可调用

func (*TCPLibrary) SetReadBufferSize

func (t *TCPLibrary) SetReadBufferSize(readBufferSize int)

SetReadBufferSize 设置参数 readBufferSize

func (*TCPLibrary) SetReadDeadline

func (t *TCPLibrary) SetReadDeadline(duration time.Duration)

SetReadDeadline 设置参数 readDeadline

func (*TCPLibrary) StopService

func (t *TCPLibrary) StopService() (err error)

StopService 停止服务

type TCPLibraryLogger

type TCPLibraryLogger interface {
	Infof(format string, a ...interface{})
	Warnf(format string, a ...interface{})
	Errorf(format string, a ...interface{})
	Fatalf(format string, a ...interface{})
}

TCPLibraryLogger 日志接口,可以使用第三方日志库,符合接口即可

type TCPServer

type TCPServer struct {
	*TCPLibrary
	// contains filtered or unexported fields
}

TCPServer tcp服务端对象

func (*TCPServer) ListenAndServe

func (tcp *TCPServer) ListenAndServe(address string) error

ListenAndServe 开始tcp监听

func (*TCPServer) ListenAndServeTLS

func (tcp *TCPServer) ListenAndServeTLS(address, certFile, keyFile string) error

ListenAndServeTLS 开始tcp监听 tls

func (*TCPServer) Serve

func (tcp *TCPServer) Serve(listen net.Listener) error

Serve 开启服务

type TCPType

type TCPType = int

TCPType tcp连接类型

Jump to

Keyboard shortcuts

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