From 5ddd34f39d209cf94d0691692c14381b26357f69 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 28 Nov 2017 19:40:45 +0900 Subject: [PATCH] ing --- constants.go | 4 +-- .../rpc/gateway_rpc_servlet_handlers.go | 32 ++++++++++++------- .../rpc/rpc_gateway_servlet_handlers.go | 2 ++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/constants.go b/constants.go index 5925672..5f9090b 100644 --- a/constants.go +++ b/constants.go @@ -5,8 +5,8 @@ import ( ) var ( - ServletSocketKey = cuc.ContextKey("ServletSocket") - ServletContentTypeKey = cuc.ContextKey("ServletContentType") + ServletSocketKey = cuc.ContextKey("ServletSocket") + SocketIDKey = cuc.ContextKey("SocketID") ) const ( diff --git a/internal/server/rpc/gateway_rpc_servlet_handlers.go b/internal/server/rpc/gateway_rpc_servlet_handlers.go index e62b7de..397c5ee 100644 --- a/internal/server/rpc/gateway_rpc_servlet_handlers.go +++ b/internal/server/rpc/gateway_rpc_servlet_handlers.go @@ -2,9 +2,7 @@ package rpc import ( "context" - "fmt" - "git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/rpc" crcwf "git.loafle.net/commons_go/rpc/connection/websocket/fasthttp" "git.loafle.net/commons_go/rpc/gateway" @@ -17,23 +15,36 @@ import ( func newGatewayRPCServletHandler() GatewayRPCServletHandler { sh := &GatewayRPCServletHandlers{} - sh.RegisterCodec(json.NewServerCodec(), json.Name) + sh.RegisterCodec(json.Name, json.NewServerCodec()) return sh } type GatewayRPCServletHandlers struct { gateway.ServletHandlers - crcwf.ServletHandlers + rpcIO crcwf.ServletHandlers +} + +func (sh *GatewayRPCServletHandlers) GetRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) { + return sh.rpcIO.GetRequest(servletCTX, codec, conn) +} + +func (sh *GatewayRPCServletHandlers) SendResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error { + return sh.rpcIO.SendResponse(servletCTX, conn, requestCodec, result, err) +} + +func (sh *GatewayRPCServletHandlers) SendNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, args ...interface{}) error { + return sh.rpcIO.SendNotification(servletCTX, conn, codec, method, args...) } func (sh *GatewayRPCServletHandlers) Invoke(servletCTX rpc.ServletContext, requestCodec protocol.RegistryCodec) (result interface{}, err error) { - if !sh.RPCRegistry.HasMethod(requestCodec.Method()) { - return nil, fmt.Errorf("RPC Servlet Handler: Method[%s] is not exist", requestCodec.Method()) - } - - md := metadata.Pairs(oogw.GRPCUserIDKey, soc.ID()) + md := metadata.Pairs(oogw.GRPCUserIDKey, servletCTX.GetAttribute(oogw.SocketIDKey).(string)) grpcCTX := metadata.NewOutgoingContext(context.Background(), md) + params, err := requestCodec.Params() + if nil != err { + return nil, err + } + r, err := grpc.Exec(grpcCTX, requestCodec.Method(), params) if err != nil { return nil, err @@ -45,7 +56,4 @@ func (sh *GatewayRPCServletHandlers) Invoke(servletCTX rpc.ServletContext, reque func (sh *GatewayRPCServletHandlers) Validate() { sh.ServletHandlers.Validate() - if nil == sh.RPCRegistry { - logging.Logger().Panic(fmt.Sprintf("RPC Servlet Handler: RPC Registry must be specified")) - } } diff --git a/internal/server/rpc/rpc_gateway_servlet_handlers.go b/internal/server/rpc/rpc_gateway_servlet_handlers.go index 5a81f09..62791c8 100644 --- a/internal/server/rpc/rpc_gateway_servlet_handlers.go +++ b/internal/server/rpc/rpc_gateway_servlet_handlers.go @@ -8,6 +8,7 @@ import ( cRPC "git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc/protocol/json" cwf "git.loafle.net/commons_go/websocket_fasthttp" + oogw "git.loafle.net/overflow/overflow_gateway_websocket" "git.loafle.net/overflow/overflow_gateway_websocket/rpc" "git.loafle.net/overflow/overflow_gateway_websocket/servlet" ) @@ -57,6 +58,7 @@ func (sh *SocketHandlers) SocketContext(serverCTX cwf.ServerContext) cwf.SocketC func (sh *RPCGatewayServletHandlers) OnConnect(soc cwf.Socket) { sh.ServletHandlers.OnConnect(soc) + soc.Context().SetAttribute(oogw.SocketIDKey, soc.ID()) soc.Context().SetAttribute(cRPC.ContentTypeKey, json.Name) }