Pārlūkot izejas kodu

update component impl

joe 4 gadi atpakaļ
vecāks
revīzija
c39fc79430
6 mainītis faili ar 21 papildinājumiem un 51 dzēšanām
  1. 1 15
      cpn/client_tcp.go
  2. 1 15
      cpn/client_ws.go
  3. 15 1
      cpn/hub.go
  4. 1 11
      cpn/server_tcp.go
  5. 2 8
      cpn/server_ws.go
  6. 1 1
      ihub.go

+ 1 - 15
cpn/client_tcp.go

@@ -10,16 +10,12 @@ type TcpClient struct {
 	addr string
 }
 
-func NewTcpClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) *TcpClient {
+func NewTcpClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) nnet.IHub {
 	return &TcpClient{
 		Hub: newHub(cf, cb, p),
 	}
 }
 
-func (self *TcpClient) Start() error {
-	return nil
-}
-
 func (self *TcpClient) NewConnection(addr string, id uint64) error {
 	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
 	if err != nil {
@@ -39,13 +35,3 @@ func (self *TcpClient) NewConnection(addr string, id uint64) error {
 	}()
 	return nil
 }
-
-func (self *TcpClient) DoJob(int) {
-
-}
-
-func (self *TcpClient) Stop() error {
-	close(self.chQuit)
-	self.wg.Wait()
-	return nil
-}

+ 1 - 15
cpn/client_ws.go

@@ -12,17 +12,13 @@ type WsClient struct {
 	pos  int //
 }
 
-func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) *WsClient {
+func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) nnet.IHub {
 	return &WsClient{
 		Hub: newHub(cf, cb, p),
 		pos: 0,
 	}
 }
 
-func (self *WsClient) Start() error {
-	return nil
-}
-
 func (self *WsClient) NewConnection(addr string, id uint64) error {
 	conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
 	if err != nil {
@@ -38,13 +34,3 @@ func (self *WsClient) NewConnection(addr string, id uint64) error {
 
 	return nil
 }
-
-func (self *WsClient) DoJob(int) {
-
-}
-
-func (self *WsClient) Stop() error {
-	close(self.chQuit)
-	self.wg.Wait()
-	return nil
-}

+ 15 - 1
cpn/hub.go

@@ -134,4 +134,18 @@ func (self *Hub) GetSessionNum() int {
 
 func (self *Hub) NewConnection(string, uint64) error {
 	return nil
-}
+}
+
+func (self *Hub) Start() error {
+	return nil
+}
+
+func (self *Hub) Stop() error {
+	close(self.chQuit)
+	self.wg.Wait()
+	return nil
+}
+
+func (self *Hub) DoJob(int) {
+
+}

+ 1 - 11
cpn/server_tcp.go

@@ -11,7 +11,7 @@ type TcpServer struct {
 	listener *net.TCPListener
 }
 
-func NewTcpServer(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol, ls *net.TCPListener) *TcpServer {
+func NewTcpServer(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol, ls *net.TCPListener) nnet.IHub {
 	return &TcpServer{
 		Hub:      newHub(cf, cb, p),
 		listener: ls,
@@ -46,13 +46,3 @@ func (self *TcpServer) Start() error {
 	}
 	return nil
 }
-
-func (self *TcpServer) DoJob(int) {
-
-}
-
-func (self *TcpServer) Stop() error {
-	close(self.chQuit)
-	self.wg.Wait() //保证 Start 完全退出
-	return nil
-}

+ 2 - 8
cpn/server_ws.go

@@ -20,7 +20,7 @@ type WsServer struct {
 }
 
 func NewWsServer(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol,
-	addr string, wspath string, routes map[string]CallbackWsPath) *WsServer {
+	addr string, wspath string, routes map[string]CallbackWsPath) nnet.IHub {
 	return &WsServer{
 		Hub:    newHub(cf, cb, p),
 		addr:   addr,
@@ -80,11 +80,5 @@ func (self *WsServer) do_new_session(w http.ResponseWriter, r *http.Request) {
 
 func (self *WsServer) Stop() error {
 	self.svr.Close()
-	close(self.chQuit)
-	self.wg.Wait()
-	return nil
-}
-
-func (self *WsServer) DoJob(int) {
-
+	return self.Hub.Stop()
 }

+ 1 - 1
ihub.go

@@ -14,7 +14,7 @@ var (
 		WriteBufSize:   2048,
 		Timeout:        3 * time.Second,
 		Tick:           10 * time.Second,
-		ReadTimeout:    12 * time.Second,	// should greater than Tick
+		ReadTimeout:    12 * time.Second, // should greater than Tick
 	}
 )