/
invoke.go
46 lines (35 loc) · 904 Bytes
/
invoke.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package binrpc
import (
"fmt"
"io"
"net"
)
type binRPCClientCodec struct {
c io.ReadWriteCloser
}
func (c *binRPCClientCodec) ReadResponseBody(body interface{}) error {
return nil
}
func (c *binRPCClientCodec) WriteRequest(name string) error {
var methodName = BinRpcString(name)
return methodName.Encode(c.c)
}
func newClientCodec(conn io.ReadWriteCloser) *binRPCClientCodec {
return &binRPCClientCodec{
c: conn,
}
}
// InvokeMethod calls the given RPC method on the given host and port
func InvokeMethod(method string, host string, port string) error {
conn, err := net.Dial("udp", host+":"+port)
defer conn.Close() // nolint
if err != nil {
return fmt.Errorf("failed to connect to kamailio RPC server: %w", err)
}
codec := newClientCodec(conn)
err = codec.WriteRequest(method)
if err != nil {
return fmt.Errorf("failed to invoke RPC method: %w", err)
}
return nil
}