2018-04-19 03:46:54 +00:00
|
|
|
package servlet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2018-04-19 13:46:49 +00:00
|
|
|
"git.loafle.net/commons/logging-go"
|
2018-04-19 03:46:54 +00:00
|
|
|
crp "git.loafle.net/commons/rpc-go/protocol"
|
|
|
|
crr "git.loafle.net/commons/rpc-go/registry"
|
|
|
|
"git.loafle.net/commons/server-go"
|
|
|
|
css "git.loafle.net/commons/server-go/socket"
|
|
|
|
cssn "git.loafle.net/commons/server-go/socket/net"
|
2018-04-19 14:07:14 +00:00
|
|
|
"git.loafle.net/overflow/container-go"
|
2018-04-19 03:46:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RPCServlet interface {
|
|
|
|
cssn.Servlet
|
|
|
|
}
|
|
|
|
|
|
|
|
type RPCServlets struct {
|
|
|
|
cssn.Servlets
|
|
|
|
|
2018-04-19 14:07:14 +00:00
|
|
|
RPCServerCodec crp.ServerCodec
|
|
|
|
RPCInvoker crr.RPCInvoker
|
|
|
|
RPCWriteChan <-chan *container.RPCNotification
|
2018-04-19 03:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCServlets) Handshake(servletCtx server.ServletCtx, conn net.Conn) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCServlets) OnConnect(servletCtx server.ServletCtx, conn css.Conn) {
|
|
|
|
s.Servlets.OnConnect(servletCtx, conn)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCServlets) OnDisconnect(servletCtx server.ServletCtx) {
|
|
|
|
s.Servlets.OnDisconnect(servletCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCServlets) Handle(servletCtx server.ServletCtx,
|
|
|
|
stopChan <-chan struct{}, doneChan chan<- struct{},
|
|
|
|
readChan <-chan []byte, writeChan chan<- []byte) {
|
|
|
|
defer func() {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
src crp.ServerRequestCodec
|
|
|
|
reply interface{}
|
|
|
|
replyBuff []byte
|
|
|
|
err error
|
|
|
|
)
|
2018-04-19 14:07:14 +00:00
|
|
|
|
2018-04-19 13:51:36 +00:00
|
|
|
go s.handleRPCWrite(stopChan, writeChan)
|
2018-04-19 03:46:54 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg, ok := <-readChan:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// grpc exec method call
|
2018-04-19 14:07:14 +00:00
|
|
|
src, err = s.RPCServerCodec.NewRequest(msg)
|
2018-04-19 03:46:54 +00:00
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
reply, err = s.RPCInvoker.Invoke(src)
|
|
|
|
replyBuff, err = src.NewResponse(reply, err)
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
s.writeError(src, writeChan, crp.E_INTERNAL, "", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
writeChan <- replyBuff
|
2018-04-19 13:51:36 +00:00
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCServlets) handleRPCWrite(stopChan <-chan struct{}, writeChan chan<- []byte) {
|
2018-04-19 14:07:14 +00:00
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
err error
|
|
|
|
)
|
2018-04-19 13:51:36 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-04-19 14:07:14 +00:00
|
|
|
case noti, ok := <-s.RPCWriteChan:
|
2018-04-19 13:46:49 +00:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-04-19 14:07:14 +00:00
|
|
|
buf, err = s.RPCServerCodec.NewNotification(noti.Method, noti.Params)
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:01:11 +00:00
|
|
|
writeChan <- buf
|
2018-04-19 03:46:54 +00:00
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCServlets) writeError(src crp.ServerRequestCodec, writeChan chan<- []byte, code crp.ErrorCode, message string, data interface{}) {
|
|
|
|
if !src.HasResponse() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pErr := &crp.Error{
|
|
|
|
Code: code,
|
|
|
|
Message: message,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := src.NewResponse(nil, pErr)
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeChan <- buf
|
|
|
|
}
|