srpc/client.go

140 lines
2.7 KiB
Go
Raw Normal View History

2019-10-22 01:32:56 +02:00
package srpc
2020-02-10 02:53:05 +01:00
import (
2020-02-24 17:03:24 +01:00
//"bytes"
//"errors"
//"fmt"
2020-02-10 02:53:05 +01:00
"io"
"net"
2020-02-18 03:03:02 +01:00
"sync"
2020-02-24 17:03:24 +01:00
//"time"
2020-02-10 02:53:05 +01:00
)
2020-02-24 17:03:24 +01:00
//func (client *Client) Call(funcName string, args ...interface{}) (ret []byte, err error) {
//var b bytes.Buffer
//enc := client.ed.NewEncoder(&b)
//for _, a := range args {
//enc.Encode(a)
//}
//payload := b.Bytes()
//req := RPCRequest{funcName, payload}
//client.RLock()
//defer client.RUnlock()
//if err = client.conn.Send(&req); err != nil {
//return nil, err
//}
//var call *RPCCall
//if call, err = client.conn.Receive(); err != nil {
//return nil, err
//}
//var response *RPCResponse
//var ok bool
//if response, ok = call.Payload.(*RPCResponse); !ok {
//return nil, errors.New("srpc - Expected response")
//}
//if response.Status != RPCOK {
//err = errors.New("srpc - Response contained error: '" + response.Error + "'")
//}
//return response.Payload, err
//}
//func (client *Client) Close() {
//client.Lock()
//defer client.Unlock()
//client.conn.Send(new(RPCClose))
//client.running = false
//}
//func (client *Client) NewDecoder(r io.Reader) IDecoder {
//return client.ed.NewDecoder(r)
//}
//func NewClient(conn net.Conn) *Client {
//ret := &Client{sync.RWMutex{}, NewEncoderDecoder(), NewNetConn(conn, NewEncoderDecoder()), true}
//go func() {
//for {
//fmt.Println("heartbeat")
//ret.Lock()
//if ret.running == false {
//ret.Unlock()
//return
//}
//ret.conn.Send(new(RPCHeartbeat))
//if _, err := ret.conn.Receive(); err != nil {
//ret.conn.Send(new(RPCClose))
//ret.Unlock()
//return
//}
//fmt.Println("got heartbeat")
//ret.Unlock()
//time.Sleep(15 * time.Second)
//}
//}()
//return ret
//}
type ResponseDataHandlerFunc func() (response interface{})
type DialHandlerFunc func(addr string) (conn io.ReadWriteCloser, err error)
2020-02-10 02:53:05 +01:00
2020-02-24 17:03:24 +01:00
func unixDial(addr string) (conn io.ReadWriteCloser, err error) {
if conn, err = net.Dial("unix", addr); err != nil {
return nil, err
2020-02-10 02:53:05 +01:00
}
2020-02-24 17:03:24 +01:00
return conn, nil
}
2020-02-10 02:53:05 +01:00
2020-02-24 17:03:24 +01:00
type RPCClient struct {
Addr string
LogError LogErrorFunc
ConnectHander ConnectHandlerFunc
ResponseDataHandler ResponseDataHandlerFunc
DialHandler DialHandlerFunc
Ed IEncoderDecoder
stopChan chan struct{}
stopWg sync.WaitGroup
}
2020-02-18 03:03:02 +01:00
2020-02-24 17:03:24 +01:00
func (c *RPCClient) Start() (err error) {
if c.LogError == nil {
c.LogError = logError
}
2020-02-10 02:53:05 +01:00
2020-02-24 17:03:24 +01:00
if c.stopChan != nil {
return errors.New("srpc - Client already running")
}
2020-02-24 17:03:24 +01:00
c.stopChan = make(chan struct{})
2020-02-10 02:53:05 +01:00
2020-02-24 17:03:24 +01:00
if c.ResponseDataHandler == nil {
return errors.New("srpc - Client needs a ResponseDataHandler")
2020-02-18 03:03:02 +01:00
}
2020-02-24 17:03:24 +01:00
if c.DialHandler == nil {
return errors.New("srpc - Client needs a DialHandler")
2020-02-14 01:51:30 +01:00
}
2020-02-24 17:03:24 +01:00
c.stopWg.Add(1)
return nil
2020-02-10 02:53:05 +01:00
}
2020-02-24 17:03:24 +01:00
func (c *RPCClient) Stop() {
if c.stopChan == nil {
return
2020-02-19 01:36:18 +01:00
}
2020-02-24 17:03:24 +01:00
close(c.stopChan)
c.stopWg.Wait()
c.stopChan = nil
2020-02-19 01:36:18 +01:00
}