From d4ea8c3daf4f213b7710e52fb4ef33a983f3ad4f Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 19 Apr 2018 12:46:54 +0900 Subject: [PATCH] ing --- const.go | 1 + glide.yaml | 3 ++ server/server-handler.go | 13 +++++ servlet/rpc-servlet.go | 100 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 const.go create mode 100644 glide.yaml create mode 100644 server/server-handler.go create mode 100644 servlet/rpc-servlet.go diff --git a/const.go b/const.go new file mode 100644 index 0000000..92b9571 --- /dev/null +++ b/const.go @@ -0,0 +1 @@ +package container diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..fabf7eb --- /dev/null +++ b/glide.yaml @@ -0,0 +1,3 @@ +package: git.loafle.net/overflow/container-go +import: +- package: git.loafle.net/commons/server-go diff --git a/server/server-handler.go b/server/server-handler.go new file mode 100644 index 0000000..1b991db --- /dev/null +++ b/server/server-handler.go @@ -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 +} diff --git a/servlet/rpc-servlet.go b/servlet/rpc-servlet.go new file mode 100644 index 0000000..fb4050a --- /dev/null +++ b/servlet/rpc-servlet.go @@ -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 +}