package srpc import ( "net" ) type RPCStatus uint8 const ( RPCOK RPCStatus = 0 RPCERR RPCStatus = 1 ) type RPCRequest struct { FuncName string Payload []byte } type RPCResponse struct { Status RPCStatus Error string Payload []byte } type IRPCConn interface { ReceiveRequest() (*RPCRequest, error) ReceiveResponse() (*RPCResponse, error) SendRequest(request *RPCRequest) error SendResponse(response *RPCResponse) error Close() } type TCPConn struct { conn net.Conn ed IEncoderDecoder // interface IRPCConn } func (tc *TCPConn) ReceiveRequest() (ret *RPCRequest, err error) { ret = new(RPCRequest) err = nil dec := tc.ed.NewDecoder(tc.conn) if err = dec.Decode(ret); err != nil { return nil, err } return ret, err } func (tc *TCPConn) ReceiveResponse() (ret *RPCResponse, err error) { ret = new(RPCResponse) err = nil dec := tc.ed.NewDecoder(tc.conn) if err = dec.Decode(ret); err != nil { return nil, err } return ret, err } func (tc *TCPConn) SendRequest(request *RPCRequest) (err error) { err = nil enc := tc.ed.NewEncoder(tc.conn) err = enc.Encode(request) return err } func (tc *TCPConn) SendResponse(response *RPCResponse) (err error) { err = nil enc := tc.ed.NewEncoder(tc.conn) err = enc.Encode(response) return err } func (tc *TCPConn) Close() { tc.conn.Close() } func NewTCPConn(conn net.Conn, ed IEncoderDecoder) *TCPConn { ret := &TCPConn{} ret.conn = conn ret.ed = ed return ret }