From 668123cccb38a8a5d12c8b2af8b7a49855cfcc3f Mon Sep 17 00:00:00 2001 From: Matthias Fulz Date: Mon, 25 May 2020 03:40:38 +0200 Subject: [PATCH] Interface improvement --- client.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index f21ebfd..19d1db0 100644 --- a/client.go +++ b/client.go @@ -1,8 +1,8 @@ package srpc import ( + "bytes" "errors" - "fmt" "io" "net" "sync" @@ -108,8 +108,29 @@ func (c *RPCClient) Stop() { c.stopChan = nil } -func (c *RPCClient) Call(request interface{}) (response interface{}, err error) { +func (c *RPCClient) MakeRequest(fctName string, params ...interface{}) (request RPCRequest, err error) { + data := bytes.Buffer{} + enc := c.Ed.NewEncoder(&data) + + for _, param := range params { + if err = enc.Encode(param); err != nil { + return request, err + } + } + + request.FuncName = fctName + request.Payload = data.Bytes() + + return request, nil +} + +func (c *RPCClient) Call(fctName string, params ...interface{}) (response interface{}, err error) { var requestCall *call + var request RPCRequest + + if request, err = c.MakeRequest(fctName, params...); err != nil { + return nil, errors.New("srpc - Client request encode failed: '" + err.Error() + "'") + } for _, e := range c.calls { e.Lock() @@ -129,13 +150,10 @@ func (c *RPCClient) Call(request interface{}) (response interface{}, err error) return nil, errors.New("srpc - Client requests are full") } - fmt.Printf("Sending request with ID %d\n", requestCall.Request.ID) - fmt.Printf("Request %v\n", requestCall.Request.Data) c.callChan <- requestCall select { case <-requestCall.done: - fmt.Printf("Finished request with ID %d\n", requestCall.Request.ID) requestCall.Lock() response = requestCall.Response.Data err = requestCall.Error @@ -164,7 +182,7 @@ func clientHandler(c *RPCClient) { go func() { if conn, err = c.DialHandler(c.Addr); err != nil { if stopping.Load() == nil { - c.LogError("srpc - '%s' cannot estable rpc connection: '%s'\n", c.Addr, err) + c.LogError("srpc - '%s' cannot establish rpc connection: '%s'\n", c.Addr, err) } } close(dialChan)