-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello2.go
65 lines (51 loc) · 1.43 KB
/
hello2.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
59
60
61
62
63
64
65
// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ingore
package main
import (
"fmt"
"log"
"net/rpc"
"time"
stdrpc "github.com/chai2010/protorpc/examples/stdrpc.pb"
)
type Echo int
func (t *Echo) Echo(args *stdrpc.EchoRequest, reply *stdrpc.EchoResponse) error {
reply.Msg = args.Msg
return nil
}
func (t *Echo) EchoTwice(args *stdrpc.EchoRequest, reply *stdrpc.EchoResponse) error {
reply.Msg = args.Msg + args.Msg
return nil
}
func init() {
go stdrpc.ListenAndServeEchoService("tcp", `127.0.0.1:9527`, new(Echo))
}
func main() {
time.Sleep(time.Second)
echoClient, err := stdrpc.DialEchoService("tcp", `127.0.0.1:9527`)
if err != nil {
log.Fatalf("stdrpc.DialEchoService: %v", err)
}
defer echoClient.Close()
args := &stdrpc.EchoRequest{Msg: "你好, 世界!"}
reply, err := echoClient.EchoTwice(args)
if err != nil {
log.Fatalf("echoClient.EchoTwice: %v", err)
}
fmt.Println(reply.Msg)
// or use normal client
client, err := rpc.Dial("tcp", `127.0.0.1:9527`)
if err != nil {
log.Fatalf("rpc.Dial: %v", err)
}
defer client.Close()
echoClient1 := &stdrpc.EchoServiceClient{client}
echoClient2 := &stdrpc.EchoServiceClient{client}
reply, err = echoClient1.EchoTwice(args)
reply, err = echoClient2.EchoTwice(args)
_, _ = reply, err
// Output:
// 你好, 世界!你好, 世界!
}