rpc/client/rwc/websocket/fasthttp/client_rwc_handlers.go

79 lines
2.0 KiB
Go
Raw Normal View History

2017-11-29 09:55:24 +00:00
package fasthttp
import (
2018-03-22 13:35:08 +00:00
"io"
2017-11-30 05:34:15 +00:00
"git.loafle.net/commons_go/logging"
2017-11-29 09:55:24 +00:00
"git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/protocol"
2017-11-30 05:34:15 +00:00
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client"
2017-12-05 07:48:49 +00:00
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
2017-11-29 09:55:24 +00:00
)
2017-11-30 05:34:15 +00:00
func New(socketBuilder cwfc.SocketBuilder) client.ClientReadWriteCloseHandler {
return &ClientReadWriteCloseHandlers{
SocketBuilder: socketBuilder,
}
2017-11-29 10:02:33 +00:00
}
2017-11-29 09:55:24 +00:00
type ClientReadWriteCloseHandlers struct {
client.ClientReadWriteCloseHandlers
2017-11-30 05:34:15 +00:00
SocketBuilder cwfc.SocketBuilder
}
func (crwch *ClientReadWriteCloseHandlers) Connect(clientCTX client.ClientContext) (interface{}, error) {
return cwfc.NewSocket(crwch.SocketBuilder, clientCTX)
2017-11-29 09:55:24 +00:00
}
2018-03-23 07:43:05 +00:00
func (crwch *ClientReadWriteCloseHandlers) NewDecoder(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}) (interface{}, error) {
2017-11-30 06:11:11 +00:00
soc := conn.(cwfc.Socket)
2018-03-22 13:35:08 +00:00
_, r, err := soc.NextReader()
2018-03-23 07:43:05 +00:00
if nil != err {
return nil, err
}
return codec.NewDecoder(r), nil
}
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX client.ClientContext, codec protocol.ClientCodec, decoder interface{}) (protocol.ClientResponseCodec, error) {
resCodec, err := codec.NewResponse(decoder)
2017-11-29 09:55:24 +00:00
return resCodec, err
}
2018-03-20 06:31:54 +00:00
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params []interface{}, id interface{}) error {
2018-03-22 13:35:08 +00:00
var (
wc io.WriteCloser
wErr error
)
2017-11-30 06:11:11 +00:00
soc := conn.(cwfc.Socket)
2017-11-29 09:55:24 +00:00
2018-03-22 13:35:08 +00:00
wc, wErr = soc.NextWriter(websocket.TextMessage)
2017-11-29 09:55:24 +00:00
if nil != wErr {
return wErr
}
2017-11-29 11:17:01 +00:00
defer func() {
wc.Close()
}()
2017-11-29 09:55:24 +00:00
2018-03-22 13:35:08 +00:00
wErr = codec.WriteRequest(wc, method, params, id)
return wErr
2017-11-29 09:55:24 +00:00
}
2017-11-30 03:36:03 +00:00
func (crwch *ClientReadWriteCloseHandlers) Disconnect(clientCTX client.ClientContext, conn interface{}) {
2017-11-30 06:11:11 +00:00
soc := conn.(cwfc.Socket)
2017-11-29 09:55:24 +00:00
soc.Close()
}
func (crwch *ClientReadWriteCloseHandlers) Validate() {
2017-11-30 04:59:46 +00:00
crwch.ClientReadWriteCloseHandlers.Validate()
2017-11-30 05:34:15 +00:00
if nil == crwch.SocketBuilder {
2018-03-21 11:26:36 +00:00
logging.Logger().Panicf("RPC Client: SocketBuilder must be specified")
2017-11-30 05:34:15 +00:00
}
2017-11-29 09:55:24 +00:00
}