From b0aa331648a61fe50a730e796797353a49d70f34 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 1 Dec 2017 17:26:02 +0900 Subject: [PATCH] ing --- server.go | 4 ++-- socket_handler.go | 2 +- socket_handlers.go | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/server.go b/server.go index b2ec746..81d5cdf 100644 --- a/server.go +++ b/server.go @@ -124,13 +124,13 @@ func handleServer(s *server) { var socketID string socketHandler := s.sh.GetSocketHandler() + socketCTX := socketHandler.SocketContext(s.ctx) - if socketID = socketHandler.Handshake(s.ctx, conn); "" == socketID { + if socketID = socketHandler.Handshake(socketCTX, conn); "" == socketID { logging.Logger().Error(fmt.Sprintf("Server: Handshake err addr[%s] %v", conn.RemoteAddr(), conn)) conn.Close() continue } - socketCTX := socketHandler.SocketContext(s.ctx) soc := newSocket(socketHandler, socketCTX, conn, socketID) logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr())) diff --git a/socket_handler.go b/socket_handler.go index 312836c..cd5c348 100644 --- a/socket_handler.go +++ b/socket_handler.go @@ -21,7 +21,7 @@ type SocketHandler interface { Init(serverCTX ServerContext) error // Handshake do handshake client and server // id is identity of client socket. if id is "", disallow connection - Handshake(serverCTX ServerContext, conn net.Conn) (id string) + Handshake(socketCTX SocketContext, conn net.Conn) (id string) // OnConnect invoked when client is connected // If you override ths method, must call // diff --git a/socket_handlers.go b/socket_handlers.go index ddfe153..931c951 100644 --- a/socket_handlers.go +++ b/socket_handlers.go @@ -24,20 +24,20 @@ type SocketHandlers struct { sockets map[string]Socket } +func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext { + return newSocketContext(serverCTX) +} + func (sh *SocketHandlers) Init(serverCTX ServerContext) error { sh.sockets = make(map[string]Socket) return nil } -func (sh *SocketHandlers) Handshake(serverCTX ServerContext, conn net.Conn) (id string) { +func (sh *SocketHandlers) Handshake(socketCTX SocketContext, conn net.Conn) (id string) { return "" } -func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext { - return newSocketContext(serverCTX) -} - func (sh *SocketHandlers) OnConnect(soc Socket) { // no op } @@ -57,6 +57,7 @@ func (sh *SocketHandlers) Destroy() { func (sh *SocketHandlers) GetSocket(id string) Socket { return sh.sockets[id] } + func (sh *SocketHandlers) GetSockets() map[string]Socket { return sh.sockets }