overflow_gateway_websocket/servlet/rpc_servlet_handlers.go
2017-11-10 22:24:10 +09:00

99 lines
2.1 KiB
Go

package servlet
import (
"context"
"log"
"git.loafle.net/commons_go/rpc/gateway"
"git.loafle.net/commons_go/server"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
oogw "git.loafle.net/overflow/overflow_gateway_websocket"
)
type RPCServletHandlers struct {
ServletHandlers
RPCHandler
}
func (sh *RPCServletHandlers) Init() error {
if err := sh.ServletHandlers.Init(); nil != err {
return err
}
return nil
}
// OnConnect invoked when client is connected
// If you override ths method, must call
func (sh *RPCServletHandlers) OnConnect(soc *cwf.Socket) {
sh.ServletHandlers.OnConnect(soc)
}
func (sh *RPCServletHandlers) Handle(soc *cwf.Socket, stopChan <-chan struct{}, doneChan chan<- struct{}) {
codec, err := sh.rpcServerHandler.GetCodec(sh.ContentType)
if nil != err {
log.Printf("RPC Handle: %v", err)
doneChan <- struct{}{}
return
}
var socConn *cwf.SocketConn
ctx := context.WithValue(context.Background(), oogw.ServletSocketKey, soc)
// conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
if socConn, err = soc.WaitRequest(); nil != err {
doneChan <- struct{}{}
return
}
// // "git.loafle.net/commons_go/websocket_fasthttp/websocket"
// switch socConn.MessageType {
// case websocket.TextMessage:
// case websocket.BinaryMessage:
// }
if err = gateway.Handle(ctx, sh.rpcServerHandler, codec, socConn, socConn); nil != err {
if server.IsClientDisconnect(err) {
doneChan <- struct{}{}
return
}
log.Printf("RPC: %v", err)
}
if err = socConn.Close(); nil != err {
doneChan <- struct{}{}
return
}
select {
case <-stopChan:
return
default:
}
}
}
// OnDisconnect invoked when client is disconnected
// If you override ths method, must call
func (sh *RPCServletHandlers) OnDisconnect(soc *cwf.Socket) {
sh.ServletHandlers.OnDisconnect(soc)
}
// Destroy invoked when server is stopped
// If you override ths method, must call
func (sh *ServletHandlers) Destroy() {
sh.SocketHandlers.Destroy()
}
func (sh *RPCServletHandlers) Validate() {
sh.ServletHandlers.Validate()
}