68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package socket
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"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"
|
|
)
|
|
|
|
func New(address string) client.ClientReadWriteCloseHandler {
|
|
return &ClientReadWriteCloseHandlers{
|
|
Address: address,
|
|
}
|
|
}
|
|
|
|
type ClientReadWriteCloseHandlers struct {
|
|
client.ClientReadWriteCloseHandlers
|
|
|
|
Address string
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Connect(clientCTX client.ClientContext) (interface{}, error) {
|
|
return nil, fmt.Errorf("RPC Client RWC Handler: ClientHandlers method[Connect] is not implement")
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
|
|
if nil == conn {
|
|
return nil, io.EOF
|
|
}
|
|
|
|
soc := conn.(server.Socket)
|
|
resCodec, err := codec.NewResponse(soc)
|
|
|
|
return resCodec, err
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error {
|
|
if nil == conn {
|
|
return io.EOF
|
|
}
|
|
|
|
soc := conn.(server.Socket)
|
|
|
|
if wErr := codec.WriteRequest(soc, method, params); nil != wErr {
|
|
return wErr
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Disconnect(clientCTX client.ClientContext, conn interface{}) {
|
|
if nil == conn {
|
|
return
|
|
}
|
|
|
|
soc := conn.(server.Socket)
|
|
soc.Close()
|
|
}
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Validate() {
|
|
if "" == crwch.Address {
|
|
logging.Logger().Panic("RPC Client RWC Handler: Address must be specified")
|
|
}
|
|
}
|