This commit is contained in:
crusader 2017-11-27 20:54:15 +09:00
parent 9ea3676888
commit b2bfe5bb2d
8 changed files with 104 additions and 7 deletions

22
client/client.go Normal file
View File

@ -0,0 +1,22 @@
package client
import (
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/protocol"
)
func NewClientHandler(addr string, codec protocol.ClientCodec, notifyRegistry rpc.Registry) ClientHandler {
ch := &ClientHandlers{}
ch.addr = addr
ch.RPCRegistry = notifyRegistry
ch.Codec = codec
return ch
}
type ClientHandlers struct {
client.ClientHandlers
addr string
}

7
client/client_handler.go Normal file
View File

@ -0,0 +1,7 @@
package client
import "git.loafle.net/commons_go/rpc/client"
type ClientHandler interface {
client.ClientHandler
}

View File

@ -0,0 +1,2 @@
package client

View File

@ -0,0 +1,9 @@
package rpc
import (
"git.loafle.net/commons_go/rpc/gateway"
)
type GatewayRPCServletHandler interface {
gateway.ServletHandler
}

View File

@ -0,0 +1,36 @@
package rpc
import (
"fmt"
"git.loafle.net/commons_go/logging"
crcwf "git.loafle.net/commons_go/rpc/connection/websocket/fasthttp"
"git.loafle.net/commons_go/rpc/gateway"
"git.loafle.net/commons_go/rpc/protocol"
)
type GatewayRPCServletHandlers struct {
gateway.ServletHandlers
crcwf.ServletHandlers
}
func (sh *GatewayRPCServletHandlers) Invoke(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())
}
result, err = sh.RPCRegistry.Invoke(requestCodec)
if nil != err {
}
return
}
func (sh *GatewayRPCServletHandlers) Validate() {
sh.ServletHandlers.Validate()
if nil == sh.RPCRegistry {
logging.Logger().Panic(fmt.Sprintf("RPC Servlet Handler: RPC Registry must be specified"))
}
}

View File

@ -4,8 +4,8 @@ import (
cwf "git.loafle.net/commons_go/websocket_fasthttp" cwf "git.loafle.net/commons_go/websocket_fasthttp"
) )
func New(sh ServerHandler) Server { func New(serverCTX ServerContext, sh ServerHandler) Server {
s := cwf.New(sh) s := cwf.New(serverCTX, sh)
return s return s
} }

21
server/server_context.go Normal file
View File

@ -0,0 +1,21 @@
package server
import (
cuc "git.loafle.net/commons_go/util/context"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
)
type ServerContext interface {
cwf.ServerContext
}
type serverContext struct {
cwf.ServerContext
}
func NewServerContext(parent cuc.Context) ServerContext {
sCTX := &serverContext{}
sCTX.ServerContext = cwf.NewServerContext(parent)
return sCTX
}

View File

@ -46,7 +46,7 @@ type ServerHandlers struct {
// Init invoked before the server is started // Init invoked before the server is started
// If you override ths method, must call // If you override ths method, must call
func (sh *ServerHandlers) Init() error { func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
if err := sh.ServerHandlers.Init(); nil != err { if err := sh.ServerHandlers.Init(); nil != err {
return err return err
} }
@ -56,11 +56,11 @@ func (sh *ServerHandlers) Init() error {
return nil return nil
} }
func (sh *ServerHandlers) Listen() (net.Listener, error) { func (sh *ServerHandlers) Listen(serverCTX ServerContext) (net.Listener, error) {
return net.Listen(config.Config.Server.Network, config.Config.Server.Addr) return net.Listen(config.Config.Server.Network, config.Config.Server.Addr)
} }
func (sh *ServerHandlers) OnStart() { func (sh *ServerHandlers) OnStart(serverCTX ServerContext) {
sh.ServerHandlers.OnStart() sh.ServerHandlers.OnStart()
sh.redisSubscriber = oosr.New(redis.RedisPool.Get()) sh.redisSubscriber = oosr.New(redis.RedisPool.Get())
@ -74,11 +74,11 @@ func (sh *ServerHandlers) OnStart() {
} }
} }
func (sh *ServerHandlers) OnError(ctx *fasthttp.RequestCtx, status int, reason error) { func (sh *ServerHandlers) OnError(serverCTX ServerContext, ctx *fasthttp.RequestCtx, status int, reason error) {
sh.OnError(ctx, status, reason) sh.OnError(ctx, status, reason)
} }
func (sh *ServerHandlers) OnStop() { func (sh *ServerHandlers) OnStop(serverCTX ServerContext) {
sh.redisSubscriber.Stop() sh.redisSubscriber.Stop()
external.ExternalDestroy() external.ExternalDestroy()