2017-10-31 09:25:44 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-11-01 07:25:29 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-01 06:17:32 +00:00
|
|
|
"git.loafle.net/commons_go/server"
|
2017-10-31 09:25:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
2017-11-01 06:17:32 +00:00
|
|
|
server.ServerHandlers
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-01 07:25:29 +00:00
|
|
|
RPCServerHandler RPCServerHandler
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 07:25:29 +00:00
|
|
|
func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) {
|
|
|
|
contentType := sh.RPCServerHandler.GetContentType(conn)
|
|
|
|
codec, err := sh.RPCServerHandler.getCodec(contentType)
|
|
|
|
if nil != err {
|
|
|
|
log.Printf("RPC Handle: %v", err)
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
return
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 07:25:29 +00:00
|
|
|
for {
|
|
|
|
sh.RPCServerHandler.OnPreRead(conn)
|
|
|
|
// Create a new codec request.
|
|
|
|
codecReq, errNew := codec.NewRequest(conn)
|
|
|
|
if nil != errNew {
|
|
|
|
if sh.IsClientDisconnect(errNew) {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("RPC Handle: %v", errNew)
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sh.RPCServerHandler.OnPostRead(conn)
|
|
|
|
|
|
|
|
result, err := sh.RPCServerHandler.invoke(codecReq)
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
sh.RPCServerHandler.OnPreWriteError(conn, err)
|
|
|
|
codecReq.WriteError(conn, 400, err)
|
|
|
|
sh.RPCServerHandler.OnPostWriteError(conn, err)
|
|
|
|
} else {
|
|
|
|
sh.RPCServerHandler.OnPreWriteResult(conn, result)
|
|
|
|
codecReq.WriteResponse(conn, result)
|
|
|
|
sh.RPCServerHandler.OnPostWriteResult(conn, result)
|
|
|
|
}
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-01 07:25:29 +00:00
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 09:25:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Validate() {
|
2017-11-01 06:17:32 +00:00
|
|
|
sh.ServerHandlers.Validate()
|
|
|
|
|
2017-11-01 07:25:29 +00:00
|
|
|
if nil == sh.RPCServerHandler {
|
|
|
|
panic("RPCServerHandler must be specified.")
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
}
|