rpc/server/server_handlers.go
crusader 00dff62f10 ing
2017-11-02 15:39:30 +09:00

68 lines
1.4 KiB
Go

package server
import (
"log"
"net"
"git.loafle.net/commons_go/server"
)
type ServerHandlers struct {
server.ServerHandlers
RPCServerHandler RPCServerHandler
}
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
}
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)
}
select {
case <-stopChan:
return
default:
}
}
}
func (sh *ServerHandlers) Validate() {
sh.ServerHandlers.Validate()
if nil == sh.RPCServerHandler {
panic("RPCServerHandler must be specified.")
}
}