-
Notifications
You must be signed in to change notification settings - Fork 136
/
server.go
58 lines (47 loc) · 1.16 KB
/
server.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
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"time"
example "github.com/rpcxio/rpcx-examples"
"github.com/smallnest/rpcx/server"
)
var (
addr = flag.String("addr", "localhost:8972", "server address")
)
var clientConn net.Conn
var connected = false
type Arith int
func (t *Arith) Mul(ctx context.Context, args *example.Args, reply *example.Reply) error {
clientConn = ctx.Value(server.RemoteConnContextKey).(net.Conn)
reply.C = args.A * args.B
connected = true
return nil
}
func main() {
flag.Parse()
ln, _ := net.Listen("tcp", ":9981")
go http.Serve(ln, nil)
s := server.NewServer()
//s.RegisterName("Arith", new(example.Arith), "")
s.Register(new(Arith), "")
go s.Serve("tcp", *addr)
for !connected {
time.Sleep(time.Second)
}
fmt.Printf("start to send messages to %s\n", clientConn.RemoteAddr().String())
for {
if clientConn != nil {
err := s.SendMessage(clientConn, "test_service_path", "test_service_method", nil, []byte("abcde"))
if err != nil {
fmt.Printf("failed to send messsage to %s: %v\n", clientConn.RemoteAddr().String(), err)
clientConn = nil
}
}
time.Sleep(time.Second)
}
}