Selaa lähdekoodia

update WsClient && TcpClient

joe 4 vuotta sitten
vanhempi
commit
2bcf443c63
6 muutettua tiedostoa jossa 27 lisäystä ja 12 poistoa
  1. 2 2
      go.mod
  2. 10 1
      ihub.go
  3. 7 4
      internal/client_tcp.go
  4. 7 3
      internal/client_ws.go
  5. 0 2
      iprotocol.go
  6. 1 0
      isession.go

+ 2 - 2
go.mod

@@ -3,6 +3,6 @@ module one.com/nnet
 go 1.15
 
 require (
-	github.com/gorilla/mux v1.8.0 // indirect
-	github.com/gorilla/websocket v1.4.2 // indirect
+	github.com/gorilla/mux v1.8.0
+	github.com/gorilla/websocket v1.4.2
 )

+ 10 - 1
ihub.go

@@ -6,7 +6,16 @@ import (
 )
 
 var (
-// Hub 状态
+	// Hub 状态
+	DefHubConfig = HubConfig{
+		SizeOfSendChan: 1024,
+		SizeOfRecvChan: 1024,
+		ReadBufSize:    2048,
+		WriteBufSize:   2048,
+		Timeout:        3 * time.Second,
+		Tick:           10 * time.Second,
+		ReadTimeout:    3 * time.Second,
+	}
 )
 
 type HubConfig struct {

+ 7 - 4
internal/client_tcp.go

@@ -10,15 +10,18 @@ type TcpClient struct {
 	addr string
 }
 
-func NewTcpClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol, addr string) *TcpClient {
+func NewTcpClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) *TcpClient {
 	return &TcpClient{
 		Hub: newHub(cf, cb, p),
-		addr: addr,
 	}
 }
 
 func (self *TcpClient) Start() error {
-	tcpAddr, err := net.ResolveTCPAddr("tcp", self.addr)
+	return nil
+}
+
+func (self * TcpClient) NewConnection(addr string, id uint64) error {
+	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
 	if err != nil {
 		return err
 	}
@@ -30,10 +33,10 @@ func (self *TcpClient) Start() error {
 	self.wg.Add(1)
 	go func() {
 		ses := newSession(TcpConn{conn}, self)
+		ses.UpdateId(id)
 		ses.Do()
 		self.wg.Done()
 	}()
-
 	return nil
 }
 

+ 7 - 3
internal/client_ws.go

@@ -16,22 +16,26 @@ type WsClient struct {
 	pos   int // 指示当前连接第几个 addr
 }
 
-func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol, addr string) *WsClient {
+func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) *WsClient {
 	return &WsClient{
 		Hub:   newHub(cf, cb, p),
-		addr: addr,
 		pos:   0,
 	}
 }
 
 func (self *WsClient) Start() error {
-	conn, _, err := websocket.DefaultDialer.Dial(self.addr, nil)
+	return nil
+}
+
+func (self *WsClient)NewConnection(addr string, id uint64) error {
+	conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
 	if err != nil {
 		return err
 	}
 	self.wg.Add(1)
 	go func() {
 		ses := newSession(NewWsConn(conn), self)
+		ses.UpdateId(id)
 		ses.Do()
 		self.wg.Done()
 	}()

+ 0 - 2
iprotocol.go

@@ -1,8 +1,6 @@
 package nnet
 
 type IPacket interface {
-	// free the memory if needs
-	Destroy([]byte)
 	// if need to close socket after sending this packet
 	ShouldClose() (bool, int32)
 }

+ 1 - 0
isession.go

@@ -11,6 +11,7 @@ var (
 type ISession interface {
 	Do() // session 开始工作
 
+	SetFrozen(int32)
 	Close(int32) // 停止所有工作
 	IsClosed() bool