2017-11-10 13:24:10 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-11-13 16:50:22 +00:00
|
|
|
"time"
|
2017-11-10 13:24:10 +00:00
|
|
|
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
|
|
cwf "git.loafle.net/commons_go/websocket_fasthttp"
|
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/config"
|
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/external"
|
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/external/redis"
|
2017-11-14 03:03:16 +00:00
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/internal/server/rpc"
|
2017-11-10 13:24:10 +00:00
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/servlet"
|
|
|
|
oos "git.loafle.net/overflow/overflow_subscriber"
|
|
|
|
oosr "git.loafle.net/overflow/overflow_subscriber/redis"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
|
|
|
cwf.ServerHandlers
|
|
|
|
|
|
|
|
redisSubscriber oos.Subscriber
|
|
|
|
subscribers []oos.SubscriberHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init invoked before the server is started
|
|
|
|
// If you override ths method, must call
|
|
|
|
func (sh *ServerHandlers) Init() error {
|
|
|
|
if err := sh.ServerHandlers.Init(); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
external.ExternalInit()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Listen() (net.Listener, error) {
|
|
|
|
return net.Listen(config.Config.Server.Network, config.Config.Server.Addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnStart() {
|
|
|
|
sh.ServerHandlers.OnStart()
|
|
|
|
|
|
|
|
sh.redisSubscriber = oosr.New(redis.RedisPool.Get())
|
|
|
|
if err := sh.redisSubscriber.Start(); nil != err {
|
2017-11-14 03:03:16 +00:00
|
|
|
logging.Logger().Panic(fmt.Sprintf("Gateway Websocket: Redist Subscriber did not start %v", err))
|
2017-11-10 13:24:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subscriber := range sh.subscribers {
|
|
|
|
sh.redisSubscriber.Subscribe(subscriber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnError(ctx *fasthttp.RequestCtx, status int, reason error) {
|
|
|
|
sh.OnError(ctx, status, reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnStop() {
|
|
|
|
sh.redisSubscriber.Stop()
|
|
|
|
|
|
|
|
external.ExternalDestroy()
|
|
|
|
|
|
|
|
sh.ServerHandlers.OnStop()
|
|
|
|
}
|
|
|
|
|
2017-11-13 16:50:22 +00:00
|
|
|
func (sh *ServerHandlers) RegisterServlet(entryPath string, servletHandler servlet.ServletHandler) {
|
|
|
|
cfg := config.Config.Servlets[entryPath]
|
|
|
|
if nil == cfg {
|
2017-11-14 03:03:16 +00:00
|
|
|
logging.Logger().Panic(fmt.Sprintf("Gateway Websocket: config of entry path[%s] is not exist", entryPath))
|
2017-11-13 16:50:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
servletH := servletHandler.(*servlet.ServletHandlers)
|
|
|
|
|
|
|
|
servletH.MaxMessageSize = cfg.Socket.MaxMessageSize
|
|
|
|
servletH.WriteTimeout = cfg.Socket.WriteTimeout * time.Second
|
|
|
|
servletH.ReadTimeout = cfg.Socket.ReadTimeout * time.Second
|
|
|
|
servletH.PongTimeout = cfg.Socket.PongTimeout * time.Second
|
|
|
|
servletH.PingTimeout = cfg.Socket.PingTimeout * time.Second
|
|
|
|
servletH.PingPeriod = cfg.Socket.PingPeriod * time.Second
|
|
|
|
// servletH.BinaryMessage = cfg.Socket.BinaryMessage
|
|
|
|
|
|
|
|
sh.RegisterSocketHandler(entryPath, servletH)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) RegisterRPCGatewayServlet(entryPath string, servletHandler servlet.RPCGatewayServletHandler) {
|
2017-11-14 03:03:16 +00:00
|
|
|
cfg := config.Config.Servlets[entryPath]
|
|
|
|
if nil == cfg {
|
|
|
|
logging.Logger().Panic(fmt.Sprintf("Gateway Websocket: config of entry path[%s] is not exist", entryPath))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-14 05:57:03 +00:00
|
|
|
rpcSH := rpc.NewRPCGatewayServletHandler()
|
|
|
|
rpcSHs := rpcSH.(*rpc.RPCGatewayServletHandlers)
|
2017-11-14 03:03:16 +00:00
|
|
|
|
2017-11-14 05:57:03 +00:00
|
|
|
rpcSHs.MaxMessageSize = cfg.Socket.MaxMessageSize
|
|
|
|
rpcSHs.WriteTimeout = cfg.Socket.WriteTimeout * time.Second
|
|
|
|
rpcSHs.ReadTimeout = cfg.Socket.ReadTimeout * time.Second
|
|
|
|
rpcSHs.PongTimeout = cfg.Socket.PongTimeout * time.Second
|
|
|
|
rpcSHs.PingTimeout = cfg.Socket.PingTimeout * time.Second
|
|
|
|
rpcSHs.PingPeriod = cfg.Socket.PingPeriod * time.Second
|
2017-11-13 16:50:22 +00:00
|
|
|
|
2017-11-14 05:57:03 +00:00
|
|
|
sh.RegisterSocketHandler(entryPath, rpcSHs)
|
2017-11-10 13:24:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 16:50:22 +00:00
|
|
|
func (sh *ServerHandlers) RegisterSubscriber(subscriberHandler oos.SubscriberHandler) {
|
2017-11-10 13:24:10 +00:00
|
|
|
if nil == sh.subscribers {
|
|
|
|
sh.subscribers = make([]oos.SubscriberHandler, 0)
|
|
|
|
}
|
|
|
|
sh.subscribers = append(sh.subscribers, subscriberHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Validate() {
|
|
|
|
sh.ServerHandlers.Validate()
|
|
|
|
}
|