Initial Commit

This commit is contained in:
2022-05-01 01:39:22 -07:00
commit 3a91648f70
12 changed files with 1594 additions and 0 deletions

35
examples/client/main.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import (
"encoding/gob"
"fmt"
"net"
"go.arsenm.dev/lrpc/client"
"go.arsenm.dev/lrpc/codec"
)
func main() {
gob.Register([2]int{})
conn, _ := net.Dial("tcp", "localhost:9090")
c := client.New(conn, codec.Gob)
defer c.Close()
var add int
c.Call("Arith", "Add", [2]int{5, 5}, &add)
var sub int
c.Call("Arith", "Sub", [2]int{5, 5}, &sub)
var mul int
c.Call("Arith", "Mul", [2]int{5, 5}, &mul)
var div int
c.Call("Arith", "Div", [2]int{5, 5}, &div)
fmt.Printf(
"add: %d, sub: %d, mul: %d, div: %d\n",
add, sub, mul, div,
)
}

37
examples/server/main.go Normal file
View File

@@ -0,0 +1,37 @@
package main
import (
"encoding/gob"
"net"
"go.arsenm.dev/lrpc/codec"
"go.arsenm.dev/lrpc/server"
)
type Arith struct{}
func (Arith) Add(ctx *server.Context, in [2]int) int {
return in[0] + in[1]
}
func (Arith) Mul(ctx *server.Context, in [2]int) int {
return in[0] * in[1]
}
func (Arith) Div(ctx *server.Context, in [2]int) int {
return in[0] / in[1]
}
func (Arith) Sub(ctx *server.Context, in [2]int) int {
return in[0] - in[1]
}
func main() {
gob.Register([2]int{})
s := server.New()
s.Register(Arith{})
ln, _ := net.Listen("tcp", ":9090")
s.Serve(ln, codec.Gob)
}