rpc/client/rwc/socket/client_handlers.go

68 lines
1.5 KiB
Go
Raw Normal View History

2017-11-29 09:55:24 +00:00
package socket
import (
"fmt"
2017-11-29 11:17:01 +00:00
"io"
2017-11-29 09:55:24 +00:00
"git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/protocol"
"git.loafle.net/commons_go/server"
)
2017-11-29 10:02:33 +00:00
func New(address string) client.ClientReadWriteCloseHandler {
return &ClientReadWriteCloseHandlers{
Address: address,
}
}
2017-11-29 09:55:24 +00:00
type ClientReadWriteCloseHandlers struct {
client.ClientReadWriteCloseHandlers
Address string
}
func (crwch *ClientReadWriteCloseHandlers) Connect() (interface{}, error) {
return nil, fmt.Errorf("RPC Client RWC Handler: ClientHandlers method[Connect] is not implement")
}
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
2017-11-29 11:17:01 +00:00
if nil == conn {
return nil, io.EOF
}
2017-11-29 09:55:24 +00:00
soc := conn.(server.Socket)
resCodec, err := codec.NewResponse(soc)
return resCodec, err
}
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error {
2017-11-29 11:17:01 +00:00
if nil == conn {
return io.EOF
}
2017-11-29 09:55:24 +00:00
soc := conn.(server.Socket)
if wErr := codec.WriteRequest(soc, method, params); nil != wErr {
return wErr
}
return nil
}
func (crwch *ClientReadWriteCloseHandlers) Disconnect(conn interface{}) {
2017-11-29 11:17:01 +00:00
if nil == conn {
return
}
2017-11-29 09:55:24 +00:00
soc := conn.(server.Socket)
soc.Close()
}
func (crwch *ClientReadWriteCloseHandlers) Validate() {
if "" == crwch.Address {
logging.Logger().Panic("RPC Client RWC Handler: Address must be specified")
}
}