Interface improvement
This commit is contained in:
parent
e418f50f2e
commit
668123cccb
30
client.go
30
client.go
|
@ -1,8 +1,8 @@
|
||||||
package srpc
|
package srpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -108,8 +108,29 @@ func (c *RPCClient) Stop() {
|
||||||
c.stopChan = nil
|
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 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 {
|
for _, e := range c.calls {
|
||||||
e.Lock()
|
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")
|
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
|
c.callChan <- requestCall
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-requestCall.done:
|
case <-requestCall.done:
|
||||||
fmt.Printf("Finished request with ID %d\n", requestCall.Request.ID)
|
|
||||||
requestCall.Lock()
|
requestCall.Lock()
|
||||||
response = requestCall.Response.Data
|
response = requestCall.Response.Data
|
||||||
err = requestCall.Error
|
err = requestCall.Error
|
||||||
|
@ -164,7 +182,7 @@ func clientHandler(c *RPCClient) {
|
||||||
go func() {
|
go func() {
|
||||||
if conn, err = c.DialHandler(c.Addr); err != nil {
|
if conn, err = c.DialHandler(c.Addr); err != nil {
|
||||||
if stopping.Load() == 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)
|
close(dialChan)
|
||||||
|
|
Loading…
Reference in New Issue