This commit is contained in:
crusader 2017-12-01 17:26:02 +09:00
parent 53a7e28c86
commit b0aa331648
3 changed files with 9 additions and 8 deletions

View File

@ -124,13 +124,13 @@ func handleServer(s *server) {
var socketID string var socketID string
socketHandler := s.sh.GetSocketHandler() 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)) logging.Logger().Error(fmt.Sprintf("Server: Handshake err addr[%s] %v", conn.RemoteAddr(), conn))
conn.Close() conn.Close()
continue continue
} }
socketCTX := socketHandler.SocketContext(s.ctx)
soc := newSocket(socketHandler, socketCTX, conn, socketID) soc := newSocket(socketHandler, socketCTX, conn, socketID)
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr())) logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr()))

View File

@ -21,7 +21,7 @@ type SocketHandler interface {
Init(serverCTX ServerContext) error Init(serverCTX ServerContext) error
// Handshake do handshake client and server // Handshake do handshake client and server
// id is identity of client socket. if id is "", disallow connection // 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 // OnConnect invoked when client is connected
// If you override ths method, must call // If you override ths method, must call
// //

View File

@ -24,20 +24,20 @@ type SocketHandlers struct {
sockets map[string]Socket sockets map[string]Socket
} }
func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext {
return newSocketContext(serverCTX)
}
func (sh *SocketHandlers) Init(serverCTX ServerContext) error { func (sh *SocketHandlers) Init(serverCTX ServerContext) error {
sh.sockets = make(map[string]Socket) sh.sockets = make(map[string]Socket)
return nil 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 "" return ""
} }
func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext {
return newSocketContext(serverCTX)
}
func (sh *SocketHandlers) OnConnect(soc Socket) { func (sh *SocketHandlers) OnConnect(soc Socket) {
// no op // no op
} }
@ -57,6 +57,7 @@ func (sh *SocketHandlers) Destroy() {
func (sh *SocketHandlers) GetSocket(id string) Socket { func (sh *SocketHandlers) GetSocket(id string) Socket {
return sh.sockets[id] return sh.sockets[id]
} }
func (sh *SocketHandlers) GetSockets() map[string]Socket { func (sh *SocketHandlers) GetSockets() map[string]Socket {
return sh.sockets return sh.sockets
} }