2017-11-26 10:15:51 +00:00
|
|
|
package fasthttp
|
|
|
|
|
|
|
|
import (
|
2018-03-22 13:35:08 +00:00
|
|
|
"io"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-23 09:45:53 +00:00
|
|
|
func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
|
2017-11-28 09:33:52 +00:00
|
|
|
soc := conn.(cwf.Socket)
|
2018-03-23 09:45:53 +00:00
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
_, buf, err := soc.ReadMessage()
|
2018-03-23 07:43:05 +00:00
|
|
|
if nil != err {
|
2018-03-23 10:58:35 +00:00
|
|
|
if websocket.IsUnexpectedCloseError(err) {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2018-03-23 07:43:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
return codec.NewRequestB(buf)
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-23 13:45:48 +00:00
|
|
|
if !requestCodec.HasResponse() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:35:08 +00:00
|
|
|
var (
|
2018-03-23 13:45:48 +00:00
|
|
|
buf []byte
|
2018-03-22 13:35:08 +00:00
|
|
|
wErr error
|
|
|
|
)
|
2017-11-28 09:33:52 +00:00
|
|
|
soc := conn.(cwf.Socket)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
if nil != err {
|
|
|
|
buf, wErr = requestCodec.NewErrorB(500, err)
|
|
|
|
} else {
|
|
|
|
buf, wErr = requestCodec.NewResponseB(result)
|
|
|
|
}
|
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
|
|
|
|
wErr = soc.WriteMessage(websocket.TextMessage, buf)
|
2017-11-28 09:33:52 +00:00
|
|
|
if nil != wErr {
|
2018-03-23 10:58:35 +00:00
|
|
|
if websocket.IsUnexpectedCloseError(wErr) {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2017-11-28 09:33:52 +00:00
|
|
|
return wErr
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
return nil
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 06:31:54 +00:00
|
|
|
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params []interface{}) error {
|
2018-03-22 13:35:08 +00:00
|
|
|
var (
|
2018-03-23 13:45:48 +00:00
|
|
|
buf []byte
|
2018-03-22 13:35:08 +00:00
|
|
|
wErr error
|
|
|
|
)
|
2017-11-28 09:33:52 +00:00
|
|
|
soc := conn.(cwf.Socket)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
buf, wErr = codec.NewNotificationB(method, params)
|
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
|
|
|
|
wErr = soc.WriteMessage(websocket.TextMessage, buf)
|
2017-11-28 09:33:52 +00:00
|
|
|
if nil != wErr {
|
2018-03-23 10:58:35 +00:00
|
|
|
if websocket.IsUnexpectedCloseError(wErr) {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2017-11-28 09:33:52 +00:00
|
|
|
return wErr
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
return nil
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
2017-11-30 04:59:46 +00:00
|
|
|
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) Validate() {
|
|
|
|
srwch.ServletReadWriteCloseHandlers.Validate()
|
|
|
|
}
|