ing
This commit is contained in:
parent
c977d2c651
commit
5ddd34f39d
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ServletSocketKey = cuc.ContextKey("ServletSocket")
|
ServletSocketKey = cuc.ContextKey("ServletSocket")
|
||||||
ServletContentTypeKey = cuc.ContextKey("ServletContentType")
|
SocketIDKey = cuc.ContextKey("SocketID")
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -2,9 +2,7 @@ package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.loafle.net/commons_go/logging"
|
|
||||||
"git.loafle.net/commons_go/rpc"
|
"git.loafle.net/commons_go/rpc"
|
||||||
crcwf "git.loafle.net/commons_go/rpc/connection/websocket/fasthttp"
|
crcwf "git.loafle.net/commons_go/rpc/connection/websocket/fasthttp"
|
||||||
"git.loafle.net/commons_go/rpc/gateway"
|
"git.loafle.net/commons_go/rpc/gateway"
|
||||||
|
@ -17,23 +15,36 @@ import (
|
||||||
|
|
||||||
func newGatewayRPCServletHandler() GatewayRPCServletHandler {
|
func newGatewayRPCServletHandler() GatewayRPCServletHandler {
|
||||||
sh := &GatewayRPCServletHandlers{}
|
sh := &GatewayRPCServletHandlers{}
|
||||||
sh.RegisterCodec(json.NewServerCodec(), json.Name)
|
sh.RegisterCodec(json.Name, json.NewServerCodec())
|
||||||
return sh
|
return sh
|
||||||
}
|
}
|
||||||
|
|
||||||
type GatewayRPCServletHandlers struct {
|
type GatewayRPCServletHandlers struct {
|
||||||
gateway.ServletHandlers
|
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) {
|
func (sh *GatewayRPCServletHandlers) Invoke(servletCTX rpc.ServletContext, requestCodec protocol.RegistryCodec) (result interface{}, err error) {
|
||||||
if !sh.RPCRegistry.HasMethod(requestCodec.Method()) {
|
md := metadata.Pairs(oogw.GRPCUserIDKey, servletCTX.GetAttribute(oogw.SocketIDKey).(string))
|
||||||
return nil, fmt.Errorf("RPC Servlet Handler: Method[%s] is not exist", requestCodec.Method())
|
|
||||||
}
|
|
||||||
|
|
||||||
md := metadata.Pairs(oogw.GRPCUserIDKey, soc.ID())
|
|
||||||
grpcCTX := metadata.NewOutgoingContext(context.Background(), md)
|
grpcCTX := metadata.NewOutgoingContext(context.Background(), md)
|
||||||
|
|
||||||
|
params, err := requestCodec.Params()
|
||||||
|
if nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
r, err := grpc.Exec(grpcCTX, requestCodec.Method(), params)
|
r, err := grpc.Exec(grpcCTX, requestCodec.Method(), params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -45,7 +56,4 @@ func (sh *GatewayRPCServletHandlers) Invoke(servletCTX rpc.ServletContext, reque
|
||||||
func (sh *GatewayRPCServletHandlers) Validate() {
|
func (sh *GatewayRPCServletHandlers) Validate() {
|
||||||
sh.ServletHandlers.Validate()
|
sh.ServletHandlers.Validate()
|
||||||
|
|
||||||
if nil == sh.RPCRegistry {
|
|
||||||
logging.Logger().Panic(fmt.Sprintf("RPC Servlet Handler: RPC Registry must be specified"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
cRPC "git.loafle.net/commons_go/rpc"
|
cRPC "git.loafle.net/commons_go/rpc"
|
||||||
"git.loafle.net/commons_go/rpc/protocol/json"
|
"git.loafle.net/commons_go/rpc/protocol/json"
|
||||||
cwf "git.loafle.net/commons_go/websocket_fasthttp"
|
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/rpc"
|
||||||
"git.loafle.net/overflow/overflow_gateway_websocket/servlet"
|
"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) {
|
func (sh *RPCGatewayServletHandlers) OnConnect(soc cwf.Socket) {
|
||||||
sh.ServletHandlers.OnConnect(soc)
|
sh.ServletHandlers.OnConnect(soc)
|
||||||
|
|
||||||
|
soc.Context().SetAttribute(oogw.SocketIDKey, soc.ID())
|
||||||
soc.Context().SetAttribute(cRPC.ContentTypeKey, json.Name)
|
soc.Context().SetAttribute(cRPC.ContentTypeKey, json.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user