Skip to content

Commit 76304d8

Browse files
committed
add license and readme
1 parent 8b5237b commit 76304d8

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Max Kotliar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# TCP based JSON RPC 1.0 Client\Server
2+
3+
The package combines TCP connections management from (valyala/fasthttp)[https://github.com/valyala/fasthttp] and
4+
Go's standard [jsonrpc codec](https://pkg.go.dev/net/rpc/jsonrpc).
5+
6+
Here is a simple example. A server wishes to export an object of type Arith:
7+
8+
```go
9+
package server
10+
11+
import "errors"
12+
13+
type Args struct {
14+
A, B int
15+
}
16+
17+
type Quotient struct {
18+
Quo, Rem int
19+
}
20+
21+
type Arith int
22+
23+
func (t *Arith) Multiply(args *Args, reply *int) error {
24+
*reply = args.A * args.B
25+
return nil
26+
}
27+
28+
func (t *Arith) Divide(args *Args, quo *Quotient) error {
29+
if args.B == 0 {
30+
return errors.New("divide by zero")
31+
}
32+
quo.Quo = args.A / args.B
33+
quo.Rem = args.A % args.B
34+
return nil
35+
}
36+
```
37+
38+
The server calls:
39+
```go
40+
package main
41+
42+
import (
43+
"context"
44+
"log"
45+
"net"
46+
47+
"github.com/makasim/jsonrpc"
48+
49+
"server"
50+
)
51+
52+
func main() {
53+
s := &jsonrpc.Server{
54+
Receivers: []interface{}{
55+
&server.Arith{},
56+
},
57+
}
58+
59+
if err := s.ListenAndServe(":1234"); err != nil {
60+
log.Fatalf("serve: %s", err)
61+
}
62+
}
63+
64+
65+
```
66+
67+
68+
At this point, clients can see a service "Arith" with methods "Arith.Multiply" and "Arith.Divide".
69+
To invoke one, a client does a remote call:
70+
71+
Client:
72+
```go
73+
package main
74+
75+
import (
76+
"context"
77+
"log"
78+
79+
"github.com/makasim/jsonrpc"
80+
81+
"server"
82+
)
83+
84+
func main() {
85+
ctx := context.Background()
86+
args := &server.Args{7,8}
87+
var reply int
88+
89+
c := &jsonrpc.Client{}
90+
91+
err := c.Call(ctx, "127.0.0.1:9999", "FooService.Func", args, &reply)
92+
if err != nil {
93+
log.Fatal("call error:", err)
94+
}
95+
96+
log.Println(reply)
97+
}
98+
```

0 commit comments

Comments
 (0)