Merge branch 'develop' into local-test
Some checks failed
local-test分支打包构建docker并发布运行 / Docker-Build (push) Failing after 38s

This commit is contained in:
tiger_zhou 2024-04-02 18:25:10 +08:00
commit fddb10af77
2 changed files with 71 additions and 0 deletions

57
third_party/tcp/tcp_client.go vendored Normal file
View File

@ -0,0 +1,57 @@
package tcp
import (
"fmt"
"io"
"log/slog"
"net"
)
type TcpClient struct {
conn *net.TCPConn
handler func(n int, data []byte)
}
func StartTcpClient(rAddr string, handler func(n int, data []byte)) (*TcpClient, error) {
raddr, addErr := net.ResolveTCPAddr("tcp", rAddr)
if addErr != nil {
return nil, addErr
}
conn, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
return nil, err
}
go func() {
for {
data := make([]byte, 1024)
l, err := conn.Read(data)
if err != nil {
if err == io.EOF {
slog.Warn(fmt.Sprintf("TCP客户端[rAddr:%s]断开连接:", rAddr))
break
}
slog.Error(fmt.Sprintf("TCP客户端[rAddr:%s]读取数据异常:", rAddr), err)
}
handler(l, data)
}
}()
return &TcpClient{conn: conn}, nil
}
func (c *TcpClient) Close() {
if c.conn != nil {
c.conn.Close()
c.conn = nil
slog.Info(fmt.Sprintf("TCP客户端[rAddr:%s]关闭连接", c.conn.RemoteAddr().String()))
} else {
slog.Info(fmt.Sprintf("TCP客户端[rAddr:%s]未连接", c.conn.RemoteAddr().String()))
}
}
func (c *TcpClient) Send(data []byte) error {
_, err := c.conn.Write(data)
if err != nil {
slog.Error("udp client send error", "error", err)
return err
}
return nil
}

14
third_party/tcp/tcp_message.go vendored Normal file
View File

@ -0,0 +1,14 @@
package tcp
type TcpMessageCodec interface {
TcpMessageEncoder
TcpMessageDecoder
}
type TcpMessageEncoder interface {
Encode() []byte
}
type TcpMessageDecoder interface {
Decode(data []byte) error
}