rpc/server/rwc/websocket/fasthttp/servlet_handlers.go

68 lines
1.8 KiB
Go
Raw Normal View History

2017-11-26 10:15:51 +00:00
package fasthttp
import (
2017-11-29 10:19:25 +00:00
"fmt"
"git.loafle.net/commons_go/logging"
2017-11-27 16:22:02 +00:00
"git.loafle.net/commons_go/rpc"
2017-11-26 10:15:51 +00:00
"git.loafle.net/commons_go/rpc/protocol"
2017-11-28 09:33:52 +00:00
cwf "git.loafle.net/commons_go/websocket_fasthttp"
2017-11-28 16:19:03 +00:00
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
2017-11-26 10:15:51 +00:00
)
2017-11-29 10:02:33 +00:00
func New() rpc.ServletReadWriteCloseHandler {
return &ServletReadWriteCloseHandlers{}
2017-11-26 10:15:51 +00:00
}
2017-11-29 10:02:33 +00:00
type ServletReadWriteCloseHandlers struct {
rpc.ServletReadWriteCloseHandlers
}
func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
2017-11-29 10:19:25 +00:00
logging.Logger().Debug(fmt.Sprintf("ReadRequest: conn %v", conn))
2017-11-28 09:33:52 +00:00
soc := conn.(cwf.Socket)
_, r, err := soc.NextReader()
2017-11-26 10:15:51 +00:00
requestCodec, err := codec.NewRequest(r)
return requestCodec, err
}
2017-11-29 10:02:33 +00:00
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
2017-11-29 10:19:25 +00:00
logging.Logger().Debug(fmt.Sprintf("WriteResponse: conn %v", conn))
2017-11-28 09:33:52 +00:00
soc := conn.(cwf.Socket)
2017-11-26 10:15:51 +00:00
2017-11-28 09:33:52 +00:00
wc, wErr := soc.NextWriter(websocket.TextMessage)
if nil != wErr {
return wErr
2017-11-26 10:15:51 +00:00
}
if nil != err {
2017-11-28 09:33:52 +00:00
if wErr := requestCodec.WriteError(wc, 500, err); nil != wErr {
return wErr
2017-11-26 10:15:51 +00:00
}
} else {
2017-11-28 09:33:52 +00:00
if wErr := requestCodec.WriteResponse(wc, result); nil != wErr {
return wErr
2017-11-26 10:15:51 +00:00
}
}
2017-11-28 09:33:52 +00:00
return nil
2017-11-26 10:15:51 +00:00
}
2017-11-29 10:02:33 +00:00
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params interface{}) error {
2017-11-29 10:19:25 +00:00
logging.Logger().Debug(fmt.Sprintf("WriteNotification: conn %v", conn))
2017-11-28 09:33:52 +00:00
soc := conn.(cwf.Socket)
2017-11-26 10:15:51 +00:00
2017-11-28 09:33:52 +00:00
wc, wErr := soc.NextWriter(websocket.TextMessage)
if nil != wErr {
return wErr
2017-11-26 10:15:51 +00:00
}
2017-11-29 10:02:33 +00:00
if wErr := codec.WriteNotification(wc, method, params); nil != wErr {
2017-11-28 09:33:52 +00:00
return wErr
2017-11-26 10:15:51 +00:00
}
2017-11-28 09:33:52 +00:00
return nil
2017-11-26 10:15:51 +00:00
}