gf-common/tcp/tcpConfig.go

45 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package tcp
import (
"sync"
"time"
"github.com/gogf/gf/v2/net/gtcp"
)
// 消息帧格式4字节长度前缀 + 消息数据
const (
messageLengthPrefixSize = 4 // 消息长度前缀大小4字节
)
// TcpPoolConfig TCP连接池配置
type TcpPoolConfig struct {
BufferSize int `json:"bufferSize"` // 缓冲区大小
MaxConnections int `json:"maxConnections"` // 最大连接数
ConnectTimeout time.Duration `json:"connectTimeout"` // 连接超时时间
ReadTimeout time.Duration `json:"readTimeout"` // 读取超时时间
WriteTimeout time.Duration `json:"writeTimeout"` // 写入超时时间
MaxIdleTime time.Duration `json:"maxIdleTime"` // 最大空闲时间
}
// TcpConnection TCP连接结构
type TcpConnection struct {
Id string `json:"id"` // 连接ID
Address string `json:"address"` // 连接地址
Server gtcp.Conn `json:"server"` // 实际连接
IsActive bool `json:"isActive"` // 是否活跃
LastUsed time.Time `json:"lastUsed"` // 最后使用时间
CreatedAt time.Time `json:"createdAt"` // 创建时间
Mutex sync.RWMutex `json:"-"` // 读写锁
buffer []byte `json:"-"` // 用于存储未处理的字节数据
}
// TcpMessage TCP消息结构
type TcpMessage struct {
Id string `json:"id"` // 消息ID
ConnId string `json:"connId"` // 连接ID
Data []byte `json:"data"` // 消息数据
Timestamp time.Time `json:"timestamp"` // 时间戳
IsSend bool `json:"isSend"` // 是否是发送的消息
}