rpc/client/rwc/socket/client_rwc_handlers.go

75 lines
1.8 KiB
Go
Raw Normal View History

2017-11-29 09:55:24 +00:00
package socket
import (
2017-11-30 09:35:23 +00:00
"git.loafle.net/commons_go/logging"
2018-03-23 13:45:48 +00:00
"git.loafle.net/commons_go/rpc"
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 09:35:23 +00:00
csc "git.loafle.net/commons_go/server/client"
2017-11-29 09:55:24 +00:00
)
2017-11-30 09:35:23 +00:00
func New(socketBuilder csc.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 09:35:23 +00:00
SocketBuilder csc.SocketBuilder
}
func (crwch *ClientReadWriteCloseHandlers) Connect(clientCTX client.ClientContext) (interface{}, error) {
return csc.NewSocket(crwch.SocketBuilder, clientCTX)
2017-11-29 09:55:24 +00:00
}
2018-03-23 09:45:53 +00:00
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
2018-03-23 13:45:48 +00:00
var (
buf []byte
rErr error
)
2017-11-30 09:35:23 +00:00
soc := conn.(csc.Socket)
2018-03-22 13:35:08 +00:00
2018-03-23 13:45:48 +00:00
buf, rErr = rpc.TCPReadData(soc)
if nil != rErr {
return nil, rErr
}
2018-03-23 17:32:44 +00:00
return codec.NewResponse(buf)
2017-11-29 09:55:24 +00:00
}
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 (
2018-03-23 13:45:48 +00:00
buf []byte
2018-03-22 13:35:08 +00:00
wErr error
)
2017-11-30 09:35:23 +00:00
soc := conn.(csc.Socket)
2017-11-29 09:55:24 +00:00
2018-03-23 17:32:44 +00:00
buf, wErr = codec.NewRequest(method, params, id)
2018-03-23 13:45:48 +00:00
if nil != wErr {
return wErr
}
wErr = rpc.TCPWriteData(soc, buf)
if nil != wErr {
return wErr
}
2017-11-29 09:55:24 +00:00
2018-03-23 13:45:48 +00:00
return nil
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 09:35:23 +00:00
soc := conn.(csc.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 09:35:23 +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 09:35:23 +00:00
}
2017-11-29 09:55:24 +00:00
}