2017-10-26 12:55:55 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"git.loafle.net/commons_go/rpc"
|
|
|
|
"git.loafle.net/commons_go/server/ipc"
|
|
|
|
)
|
|
|
|
|
2017-10-26 13:18:20 +00:00
|
|
|
func NewServerHandler(addr string, registry rpc.Registry) *ServerHandlers {
|
2017-10-26 12:55:55 +00:00
|
|
|
sh := &ServerHandlers{
|
|
|
|
registry: registry,
|
|
|
|
}
|
|
|
|
sh.Addr = addr
|
|
|
|
|
|
|
|
return sh
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
|
|
|
ipc.ServerHandlers
|
|
|
|
|
2017-10-26 13:18:20 +00:00
|
|
|
registry rpc.Registry
|
|
|
|
workersChan chan struct{}
|
2017-10-26 12:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Handle(remoteAddr string, rwc io.ReadWriteCloser, stopChan chan struct{}) {
|
2017-10-26 13:18:20 +00:00
|
|
|
contentType := "json"
|
|
|
|
|
2017-10-27 06:01:09 +00:00
|
|
|
Loop:
|
2017-10-26 13:18:20 +00:00
|
|
|
for {
|
2017-10-27 06:01:09 +00:00
|
|
|
if err := sh.registry.Invoke(contentType, rwc, rwc, nil, nil); nil != err && sh.IsClientDisconnect(err) {
|
|
|
|
stopChan <- struct{}{}
|
|
|
|
break Loop
|
|
|
|
}
|
2017-10-26 13:18:20 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
2017-10-27 06:01:09 +00:00
|
|
|
default:
|
2017-10-26 13:18:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-26 12:55:55 +00:00
|
|
|
|
|
|
|
}
|