rpc/server/rwc/websocket/fasthttp/servlet_rwc_handlers.go
crusader 22eb6d7703 ing
2018-03-26 23:43:43 +09:00

92 lines
2.1 KiB
Go

package fasthttp
import (
"io"
"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) {
soc := conn.(cwf.Socket)
_, buf, err := soc.ReadMessage()
if nil != err {
if websocket.IsUnexpectedCloseError(err) {
logging.Logger().Errorf("RPC: %v", err)
}
return nil, io.EOF
}
return codec.NewRequest(buf)
}
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
if !requestCodec.HasResponse() {
return nil
}
var (
buf []byte
wErr error
)
soc := conn.(cwf.Socket)
if nil != err {
buf, wErr = requestCodec.NewError(500, err)
} else {
buf, wErr = requestCodec.NewResponse(result)
}
if nil != wErr {
return wErr
}
wErr = soc.WriteMessage(websocket.TextMessage, buf)
if nil != wErr {
if websocket.IsUnexpectedCloseError(wErr) {
logging.Logger().Errorf("RPC: %v", wErr)
}
return io.EOF
}
return nil
}
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params []interface{}) error {
var (
buf []byte
wErr error
)
soc := conn.(cwf.Socket)
buf, wErr = codec.NewNotification(method, params)
if nil != wErr {
return wErr
}
wErr = soc.WriteMessage(websocket.TextMessage, buf)
if nil != wErr {
if websocket.IsUnexpectedCloseError(wErr) {
logging.Logger().Errorf("RPC: %v", wErr)
}
return io.EOF
}
return nil
}
func (srwch *ServletReadWriteCloseHandlers) Validate() {
srwch.ServletReadWriteCloseHandlers.Validate()
}