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-31 10:27:26 +00:00
|
|
|
rpcServer "git.loafle.net/overflow/overflow_discovery/server/rpc"
|
2017-10-26 12:55:55 +00:00
|
|
|
)
|
|
|
|
|
2017-10-26 13:18:20 +00:00
|
|
|
func NewServerHandler(addr string, registry rpc.Registry) *ServerHandlers {
|
2017-10-31 10:27:26 +00:00
|
|
|
sh := &ServerHandlers{}
|
2017-10-26 12:55:55 +00:00
|
|
|
sh.Addr = addr
|
2017-10-31 10:27:26 +00:00
|
|
|
sh.rpcServer = rpcServer.New(registry)
|
2017-10-26 12:55:55 +00:00
|
|
|
|
|
|
|
return sh
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
|
|
|
ipc.ServerHandlers
|
|
|
|
|
2017-10-31 10:27:26 +00:00
|
|
|
rpcServer rpcServer.Server
|
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
|
|
|
|
2017-10-27 06:01:09 +00:00
|
|
|
Loop:
|
2017-10-26 13:18:20 +00:00
|
|
|
for {
|
2017-10-31 10:27:26 +00:00
|
|
|
if err := sh.rpcServer.Handle(rwc, rwc); nil != err && sh.IsClientDisconnect(err) {
|
2017-10-27 06:01:09 +00:00
|
|
|
stopChan <- struct{}{}
|
|
|
|
break Loop
|
|
|
|
}
|
2017-10-26 13:18:20 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
2017-10-31 10:27:26 +00:00
|
|
|
rwc.Close()
|
2017-10-26 13:18:20 +00:00
|
|
|
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
|
|
|
|
|
|
|
}
|