srpc/client.go

59 lines
937 B
Go
Raw Normal View History

2019-10-22 01:32:56 +02:00
package srpc
2020-02-10 02:53:05 +01:00
import (
"bytes"
"fmt"
"io"
"net"
)
2019-10-22 01:32:56 +02:00
var REQUEST_HEADER_SIZE = int32(8)
type requestHeader struct {
2020-02-10 02:53:05 +01:00
Size int64
}
type request struct {
FuncName string
Payload []byte
}
type Client struct {
ed IEncoderDecoder
conn net.Conn
}
func (client *Client) Call(funcName string, args ...interface{}) (ret []byte) {
var b bytes.Buffer
enc := client.ed.NewEncoder(&b)
for _, a := range args {
enc.Encode(a)
}
payload := b.Bytes()
req := request{funcName, payload}
fmt.Println(req)
enc = client.ed.NewEncoder(client.conn)
enc.Encode(req)
respSize := int64(0)
dec := client.ed.NewDecoder(client.conn)
dec.Decode(&respSize)
fmt.Println(respSize)
respData := make([]byte, respSize)
dec.Decode(&respData)
return respData
}
func (client *Client) NewDecoder(r io.Reader) IDecoder {
return client.ed.NewDecoder(r)
}
func NewClient(conn net.Conn) *Client {
return &Client{NewEncoderDecoder(), conn}
2019-10-22 01:32:56 +02:00
}