80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package fasthttp
|
|
|
|
import (
|
|
"io"
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
"git.loafle.net/commons_go/rpc/client"
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client"
|
|
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
|
|
)
|
|
|
|
func New(socketBuilder cwfc.SocketBuilder) client.ClientReadWriteCloseHandler {
|
|
return &ClientReadWriteCloseHandlers{
|
|
SocketBuilder: socketBuilder,
|
|
}
|
|
}
|
|
|
|
type ClientReadWriteCloseHandlers struct {
|
|
client.ClientReadWriteCloseHandlers
|
|
|
|
SocketBuilder cwfc.SocketBuilder
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Connect(clientCTX client.ClientContext) (interface{}, error) {
|
|
|
|
return cwfc.NewSocket(crwch.SocketBuilder, clientCTX)
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
|
|
soc := conn.(cwfc.Socket)
|
|
|
|
_, buf, err := soc.ReadMessage()
|
|
if nil != err {
|
|
if !websocket.IsUnexpectedCloseError(err) {
|
|
logging.Logger().Errorf("RPC: %v", err)
|
|
}
|
|
return nil, io.EOF
|
|
}
|
|
|
|
return codec.NewResponse(buf)
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params []interface{}, id interface{}) error {
|
|
var (
|
|
buf []byte
|
|
wErr error
|
|
)
|
|
soc := conn.(cwfc.Socket)
|
|
|
|
buf, wErr = codec.NewRequest(method, params, id)
|
|
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 (crwch *ClientReadWriteCloseHandlers) Disconnect(clientCTX client.ClientContext, conn interface{}) {
|
|
soc := conn.(cwfc.Socket)
|
|
soc.Close()
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Validate() {
|
|
crwch.ClientReadWriteCloseHandlers.Validate()
|
|
|
|
if nil == crwch.SocketBuilder {
|
|
logging.Logger().Panicf("RPC Client: SocketBuilder must be specified")
|
|
}
|
|
|
|
}
|