Golang RPC library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

45 lines
781 B

package rpc
import (
"sync"
)
type Client struct {
tokenLock sync.Mutex
token string
codec Codec
transport ClientTransport
}
func NewClient(codec Codec, transport ClientTransport) *Client {
return &Client{
codec: codec,
transport: transport,
}
}
func (cl *Client) SetToken(t string) {
cl.tokenLock.Lock()
cl.token = t
cl.tokenLock.Unlock()
}
func (cl *Client) GetToken() string {
cl.tokenLock.Lock()
t := cl.token
cl.tokenLock.Unlock()
return t
}
func (cl *Client) Call(fn string, args, resp interface{}) error {
rawReq, err := EncodeReq(cl.codec, cl.token, fn, args)
if err != nil {
return err
}
rawResp, err := cl.transport.Request(rawReq)
if err != nil {
return err // Not testable.
}
return DecodeResp(cl.codec, rawResp, resp)
}