Interface improvement

This commit is contained in:
Matthias Fulz 2020-05-25 03:40:38 +02:00
parent e418f50f2e
commit 668123cccb
1 changed files with 24 additions and 6 deletions

View File

@ -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)