Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions znet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/url"
"sync"
"time"

"github.com/aceld/zinx/zdecoder"
Expand All @@ -26,6 +27,8 @@ type Client struct {
version string
// Connection instance 连接实例
conn ziface.IConnection
// Connection instance 连接实例的锁,保证可见性
connMux sync.Mutex
// Hook function called on connection start 该client的连接创建时Hook函数
onConnStart func(conn ziface.IConnection)
// Hook function called on connection stop 该client的连接断开时的Hook函数
Expand Down Expand Up @@ -121,6 +124,7 @@ func (c *Client) Restart() {
}

// Create a raw socket and get net.Conn (创建原始Socket,得到net.Conn)
var connect ziface.IConnection
switch c.version {
case "websocket":
wsAddr := fmt.Sprintf("ws://%s:%d", c.Ip, c.Port)
Expand All @@ -137,8 +141,7 @@ func (c *Client) Restart() {
return
}
// Create Connection object
c.conn = newWsClientConn(c, wsConn)

connect = newWsClientConn(c, wsConn)
default:
var conn net.Conn
var err error
Expand Down Expand Up @@ -166,19 +169,20 @@ func (c *Client) Restart() {
}
}
// Create Connection object
c.conn = newClientConn(c, conn)
connect = newClientConn(c, conn)
}

zlog.Ins().InfoF("[START] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", c.conn.LocalAddr(), c.conn.RemoteAddr())
zlog.Ins().InfoF("[START] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", c.Conn().LocalAddr(), c.Conn().RemoteAddr())
// HeartBeat detection
if c.hc != nil {
// Bind connection and heartbeat detector after connection is successfully established
// (创建连接成功,绑定连接与心跳检测器)
c.hc.BindConn(c.conn)
c.hc.BindConn(connect)
}
c.setConn(connect)

// Start connection
go c.conn.Start()
go connect.Start()

select {
case <-c.exitChan:
Expand Down Expand Up @@ -237,8 +241,8 @@ func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *ziface
}

func (c *Client) Stop() {
zlog.Ins().InfoF("[STOP] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", c.conn.LocalAddr(), c.conn.RemoteAddr())
c.conn.Stop()
zlog.Ins().InfoF("[STOP] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", c.Conn().LocalAddr(), c.Conn().RemoteAddr())
c.Conn().Stop()
c.exitChan <- struct{}{}
close(c.exitChan)
close(c.ErrChan)
Expand All @@ -249,9 +253,17 @@ func (c *Client) AddRouter(msgID uint32, router ziface.IRouter) {
}

func (c *Client) Conn() ziface.IConnection {
c.connMux.Lock()
defer c.connMux.Unlock()
return c.conn
}

func (c *Client) setConn(con ziface.IConnection) {
c.connMux.Lock()
defer c.connMux.Unlock()
c.conn = con
}

func (c *Client) SetOnConnStart(hookFunc func(ziface.IConnection)) {
c.onConnStart = hookFunc
}
Expand Down
Loading