rpc/server/rwc/websocket/fasthttp/servlet_handlers.go
crusader 911e6ec1d7 ing
2017-11-29 19:19:25 +09:00

68 lines
1.8 KiB
Go

package fasthttp
import (
"fmt"
"git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/protocol"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
)
func New() rpc.ServletReadWriteCloseHandler {
return &ServletReadWriteCloseHandlers{}
}
type ServletReadWriteCloseHandlers struct {
rpc.ServletReadWriteCloseHandlers
}
func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
logging.Logger().Debug(fmt.Sprintf("ReadRequest: conn %v", conn))
soc := conn.(cwf.Socket)
_, r, err := soc.NextReader()
requestCodec, err := codec.NewRequest(r)
return requestCodec, err
}
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
logging.Logger().Debug(fmt.Sprintf("WriteResponse: conn %v", conn))
soc := conn.(cwf.Socket)
wc, wErr := soc.NextWriter(websocket.TextMessage)
if nil != wErr {
return wErr
}
if nil != err {
if wErr := requestCodec.WriteError(wc, 500, err); nil != wErr {
return wErr
}
} else {
if wErr := requestCodec.WriteResponse(wc, result); nil != wErr {
return wErr
}
}
return nil
}
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params interface{}) error {
logging.Logger().Debug(fmt.Sprintf("WriteNotification: conn %v", conn))
soc := conn.(cwf.Socket)
wc, wErr := soc.NextWriter(websocket.TextMessage)
if nil != wErr {
return wErr
}
if wErr := codec.WriteNotification(wc, method, params); nil != wErr {
return wErr
}
return nil
}