This commit is contained in:
crusader 2017-11-09 18:47:30 +09:00
parent 68f1a7b519
commit 8ba623111b
3 changed files with 12 additions and 1 deletions

View File

@ -151,6 +151,8 @@ func handleConnection(s *server, soc *Socket, socketHandler SocketHandler) {
defer s.stopWg.Done()
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr()))
socketHandler.OnConnect(soc)
clientStopChan := make(chan struct{})
handleDoneChan := make(chan struct{})
@ -159,11 +161,11 @@ func handleConnection(s *server, soc *Socket, socketHandler SocketHandler) {
select {
case <-s.stopChan:
close(clientStopChan)
soc.Close()
<-handleDoneChan
case <-handleDoneChan:
close(clientStopChan)
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", soc.RemoteAddr()))
socketHandler.OnDisconnect(soc)
soc.Close()
}
}

View File

@ -10,7 +10,9 @@ type SocketHandler interface {
// Handshake do handshake client and server
// id is identity of client socket. if id is nil, disallow connection
Handshake(ctx *fasthttp.RequestCtx) (id interface{}, extensionsHeader *fasthttp.ResponseHeader)
OnConnect(soc *Socket)
Handle(soc *Socket, stopChan <-chan struct{}, doneChan chan<- struct{})
OnDisconnect(soc *Socket)
GetMaxMessageSize() int64
GetWriteTimeout() time.Duration

View File

@ -30,11 +30,18 @@ type SocketHandlers struct {
func (sh *SocketHandlers) Handshake(ctx *fasthttp.RequestCtx) (id interface{}, extensionsHeader *fasthttp.ResponseHeader) {
return nil, nil
}
func (sh *SocketHandlers) OnConnect(soc *Socket) {
// no op
}
func (sh *SocketHandlers) Handle(soc *Socket, stopChan <-chan struct{}, doneChan chan<- struct{}) {
// no op
}
func (sh *SocketHandlers) OnDisconnect(soc *Socket) {
// no op
}
func (sh *SocketHandlers) GetMaxMessageSize() int64 {
return sh.MaxMessageSize
}