rpc/client/rwc/websocket/fasthttp/client_rwc_handlers.go
crusader a3d9c8941a ing
2018-03-23 13:42:39 +09:00

78 lines
1.9 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)
crwch.ReadMtx.Lock()
_, r, err := soc.NextReader()
resCodec, err := codec.NewResponse(r)
crwch.ReadMtx.Unlock()
return resCodec, err
}
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params []interface{}, id interface{}) error {
var (
wc io.WriteCloser
wErr error
)
soc := conn.(cwfc.Socket)
crwch.WriteMtx.Lock()
wc, wErr = soc.NextWriter(websocket.TextMessage)
if nil != wErr {
crwch.WriteMtx.Unlock()
return wErr
}
defer func() {
wc.Close()
}()
wErr = codec.WriteRequest(wc, method, params, id)
crwch.WriteMtx.Unlock()
return wErr
}
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")
}
}