2017-11-29 18:55:24 +09:00
|
|
|
package fasthttp
|
|
|
|
|
|
|
|
import (
|
2018-03-22 22:35:08 +09:00
|
|
|
"io"
|
|
|
|
|
2017-11-30 14:34:15 +09:00
|
|
|
"git.loafle.net/commons_go/logging"
|
2017-11-29 18:55:24 +09:00
|
|
|
"git.loafle.net/commons_go/rpc/client"
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
2017-11-30 14:34:15 +09:00
|
|
|
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client"
|
2017-12-05 16:48:49 +09:00
|
|
|
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
|
2017-11-29 18:55:24 +09:00
|
|
|
)
|
|
|
|
|
2017-11-30 14:34:15 +09:00
|
|
|
func New(socketBuilder cwfc.SocketBuilder) client.ClientReadWriteCloseHandler {
|
|
|
|
return &ClientReadWriteCloseHandlers{
|
|
|
|
SocketBuilder: socketBuilder,
|
|
|
|
}
|
2017-11-29 19:02:33 +09:00
|
|
|
}
|
|
|
|
|
2017-11-29 18:55:24 +09:00
|
|
|
type ClientReadWriteCloseHandlers struct {
|
|
|
|
client.ClientReadWriteCloseHandlers
|
2017-11-30 14:34:15 +09:00
|
|
|
|
|
|
|
SocketBuilder cwfc.SocketBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Connect(clientCTX client.ClientContext) (interface{}, error) {
|
|
|
|
|
|
|
|
return cwfc.NewSocket(crwch.SocketBuilder, clientCTX)
|
2017-11-29 18:55:24 +09:00
|
|
|
}
|
|
|
|
|
2018-03-23 18:45:53 +09:00
|
|
|
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
|
2017-11-30 15:11:11 +09:00
|
|
|
soc := conn.(cwfc.Socket)
|
2018-03-23 18:45:53 +09:00
|
|
|
|
2018-03-23 22:45:48 +09:00
|
|
|
_, buf, err := soc.ReadMessage()
|
2018-03-23 16:43:05 +09:00
|
|
|
if nil != err {
|
2018-03-23 19:58:35 +09:00
|
|
|
if websocket.IsUnexpectedCloseError(err) {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2018-03-23 16:43:05 +09:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-24 02:32:44 +09:00
|
|
|
return codec.NewResponse(buf)
|
2017-11-29 18:55:24 +09:00
|
|
|
}
|
|
|
|
|
2018-03-20 15:31:54 +09:00
|
|
|
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params []interface{}, id interface{}) error {
|
2018-03-22 22:35:08 +09:00
|
|
|
var (
|
2018-03-23 22:45:48 +09:00
|
|
|
buf []byte
|
2018-03-22 22:35:08 +09:00
|
|
|
wErr error
|
|
|
|
)
|
2017-11-30 15:11:11 +09:00
|
|
|
soc := conn.(cwfc.Socket)
|
2017-11-29 18:55:24 +09:00
|
|
|
|
2018-03-24 02:32:44 +09:00
|
|
|
buf, wErr = codec.NewRequest(method, params, id)
|
2018-03-23 22:45:48 +09:00
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
|
|
|
|
wErr = soc.WriteMessage(websocket.TextMessage, buf)
|
2017-11-29 18:55:24 +09:00
|
|
|
if nil != wErr {
|
2018-03-23 19:58:35 +09:00
|
|
|
if websocket.IsUnexpectedCloseError(wErr) {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2017-11-29 18:55:24 +09:00
|
|
|
return wErr
|
|
|
|
}
|
2018-03-22 22:35:08 +09:00
|
|
|
|
2018-03-23 22:45:48 +09:00
|
|
|
return nil
|
2017-11-29 18:55:24 +09:00
|
|
|
}
|
|
|
|
|
2017-11-30 12:36:03 +09:00
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Disconnect(clientCTX client.ClientContext, conn interface{}) {
|
2017-11-30 15:11:11 +09:00
|
|
|
soc := conn.(cwfc.Socket)
|
2017-11-29 18:55:24 +09:00
|
|
|
soc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Validate() {
|
2017-11-30 13:59:46 +09:00
|
|
|
crwch.ClientReadWriteCloseHandlers.Validate()
|
2017-11-30 14:34:15 +09:00
|
|
|
|
|
|
|
if nil == crwch.SocketBuilder {
|
2018-03-21 20:26:36 +09:00
|
|
|
logging.Logger().Panicf("RPC Client: SocketBuilder must be specified")
|
2017-11-30 14:34:15 +09:00
|
|
|
}
|
|
|
|
|
2017-11-29 18:55:24 +09:00
|
|
|
}
|