File: README.md

package info (click to toggle)
golang-github-cenk-rpc2 0.0~git20160427.0.7ab76d2e88c7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 104 kB
  • sloc: makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,399 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
rpc2
====

[![GoDoc](https://godoc.org/github.com/cenk/rpc2?status.png)](https://godoc.org/github.com/cenk/rpc2)
[![Build Status](https://travis-ci.org/cenk/rpc2.png)](https://travis-ci.org/cenk/rpc2)

rpc2 is a fork of net/rpc package in the standard library.
The main goal is to add bi-directional support to calls.
That means server can call the methods of client.
This is not possible with net/rpc package.
In order to do this it adds a `*Client` argument to method signatures.

Install
--------

    go get github.com/cenk/rpc2

Example server
---------------

```go
type Args struct{ A, B int }
type Reply int

srv := rpc2.NewServer()
srv.Handle("add", func(client *rpc2.Client, args *Args, reply *Reply) error {
        // Reversed call (server to client)
        var rep Reply
        client.Call("mult", Args{2, 3}, &rep)
        fmt.Println("mult result:", rep)

        *reply = Reply(args.A + args.B)
        return nil
})

lis, _ := net.Listen("tcp", "127.0.0.1:5000")
srv.Accept(lis)
```

Example Client
---------------

```go
type Args struct{ A, B int }
type Reply int

conn, _ := net.Dial("tcp", "127.0.0.1:5000")

clt := rpc2.NewClient(conn)
clt.Handle("mult", func(client *rpc2.Client, args *Args, reply *Reply) error {
        *reply = Reply(args.A * args.B)
        return nil
})
go clt.Run()

var rep Reply
clt.Call("add", Args{1, 2}, &rep)
fmt.Println("add result:", rep)
```