This commit is contained in:
crusader 2018-04-19 12:46:54 +09:00
parent aca40a2160
commit d4ea8c3daf
4 changed files with 117 additions and 0 deletions

1
const.go Normal file
View File

@ -0,0 +1 @@
package container

3
glide.yaml Normal file
View File

@ -0,0 +1,3 @@
package: git.loafle.net/overflow/container-go
import:
- package: git.loafle.net/commons/server-go

13
server/server-handler.go Normal file
View File

@ -0,0 +1,13 @@
package server
import (
cssn "git.loafle.net/commons/server-go/socket/net"
)
type ServerHandler interface {
cssn.ServerHandler
}
type ServerHandlers struct {
cssn.ServerHandlers
}

100
servlet/rpc-servlet.go Normal file
View File

@ -0,0 +1,100 @@
package servlet
import (
"net"
logging "git.loafle.net/commons/logging-go"
crp "git.loafle.net/commons/rpc-go/protocol"
crpj "git.loafle.net/commons/rpc-go/protocol/json"
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"
)
type RPCServlet interface {
cssn.Servlet
}
type RPCServlets struct {
cssn.Servlets
RPCInvoker crr.RPCInvoker
}
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
)
sc := crpj.NewServerCodec()
for {
select {
case msg, ok := <-readChan:
if !ok {
return
}
// grpc exec method call
src, err = sc.NewRequest(msg)
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
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
}