2 Star 0 Fork 0

纸喵软件/zmlink

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client.go 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
倒霉狐狸 提交于 2022-12-09 13:59 . code style
package zmlink
import (
"net"
"sync"
)
type Client struct {
clientID string
Type string // tcp4/udp
Address string // listen address
linkEventListener LinkEventListener
link *Link
sync.Mutex // conneceted 锁
}
func NewLinkClient(clientID, address string, linkEventListener LinkEventListener) *Client {
l := &Client{
clientID: clientID,
Type: "tcp",
Address: address,
linkEventListener: linkEventListener,
}
return l
}
func (c *Client) Start() error {
tcpAddr, err := net.ResolveTCPAddr(c.Type, c.Address)
if err != nil {
return err
}
conn, err := net.DialTCP(c.Type, nil, tcpAddr)
if err != nil {
return err
}
c.link = newLink(conn, c)
c.link.SetClientID(c.clientID)
err = c.link.run()
if err != nil {
return err
}
// 注册帧识别
if err := c.link.sendRegister(); err != nil {
c.link.Close()
return err
}
c.link.fireStatusEvent(StatusReady)
return nil
}
// GetLink 获取连接
func (c *Client) GetLink() *Link {
if c.link.status == StatusReady {
return c.link
}
return nil
}
func (c *Client) OnStream(link *Link, payload Payload, dir StreamDir) {
c.linkEventListener.OnStream(link, payload, dir)
}
func (c *Client) OnStatus(link *Link, statusType StatusType) {
if statusType == StatusClosed {
c.reconnect()
}
c.linkEventListener.OnStatus(link, statusType)
}
func (c *Client) reconnect() {
for {
if err := c.Start(); err == nil {
break
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zhimiao/zmlink.git
git@gitee.com:zhimiao/zmlink.git
zhimiao
zmlink
zmlink
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385