-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.go
62 lines (49 loc) · 1.44 KB
/
hello.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
// 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"
"github.com/chai2010/protorpc"
service "github.com/chai2010/protorpc/examples/service.pb"
)
type Echo int
func (t *Echo) Echo(args *service.EchoRequest, reply *service.EchoResponse) error {
reply.Msg = args.Msg
return nil
}
func (t *Echo) EchoTwice(args *service.EchoRequest, reply *service.EchoResponse) error {
reply.Msg = args.Msg + args.Msg
return nil
}
func init() {
go service.ListenAndServeEchoService("tcp", `127.0.0.1:9527`, new(Echo))
}
func main() {
echoClient, err := service.DialEchoService("tcp", `127.0.0.1:9527`)
if err != nil {
log.Fatalf("service.DialEchoService: %v", err)
}
defer echoClient.Close()
args := &service.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 := protorpc.Dial("tcp", `127.0.0.1:9527`)
if err != nil {
log.Fatalf("protorpc.Dial: %v", err)
}
defer client.Close()
echoClient1 := &service.EchoServiceClient{client}
echoClient2 := &service.EchoServiceClient{client}
reply, err = echoClient1.EchoTwice(args)
reply, err = echoClient2.EchoTwice(args)
_, _ = reply, err
// Output:
// 你好, 世界!你好, 世界!
}