From 229279a8d9068c0714c0dc087d888ae7b3342404 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Thu, 28 Jun 2018 08:10:32 +0800 Subject: [PATCH] =?UTF-8?q?ch4-01:=20=E5=AE=8C=E5=96=84=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch4-rpc/ch4-01-rpc-intro.md | 6 ++-- .../hello-service-v2/api/hello.go | 31 +++++++++++++++++ .../hello-service-v2/client/main.go | 31 +++++++++++++++++ .../hello-service-v2/server/main.go | 34 +++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 examples/ch4-01-rpc-inro/hello-service-v2/api/hello.go create mode 100644 examples/ch4-01-rpc-inro/hello-service-v2/client/main.go create mode 100644 examples/ch4-01-rpc-inro/hello-service-v2/server/main.go diff --git a/ch4-rpc/ch4-01-rpc-intro.md b/ch4-rpc/ch4-01-rpc-intro.md index a03704a..5a877a0 100644 --- a/ch4-rpc/ch4-01-rpc-intro.md +++ b/ch4-rpc/ch4-01-rpc-intro.md @@ -78,7 +78,7 @@ type HelloServiceInterface = interface { } func RegisterHelloService(svc HelloServiceInterface) error { - rpc.RegisterName(HelloServiceName, svc) + return rpc.RegisterName(HelloServiceName, svc) } ``` @@ -117,11 +117,11 @@ func DialHelloService(network, address string) (*HelloServiceClient, error) { if err != nil { return nil, err } - return &HelloServiceClient{Client: c} + return &HelloServiceClient{Client: c}, nil } func (p *HelloServiceClient) Hello(request string, reply *string) error { - return client.Call(HelloServiceName+".Hello", request, reply) + return p.Client.Call(HelloServiceName+".Hello", request, reply) } ``` diff --git a/examples/ch4-01-rpc-inro/hello-service-v2/api/hello.go b/examples/ch4-01-rpc-inro/hello-service-v2/api/hello.go new file mode 100644 index 0000000..5795f8f --- /dev/null +++ b/examples/ch4-01-rpc-inro/hello-service-v2/api/hello.go @@ -0,0 +1,31 @@ +package api + +import "net/rpc" + +const HelloServiceName = "path/to/pkg.HelloService" + +type HelloServiceInterface = interface { + Hello(request string, reply *string) error +} + +func RegisterHelloService(svc HelloServiceInterface) error { + return rpc.RegisterName(HelloServiceName, svc) +} + +type HelloServiceClient struct { + *rpc.Client +} + +var _ HelloServiceInterface = (*HelloServiceClient)(nil) + +func DialHelloService(network, address string) (*HelloServiceClient, error) { + c, err := rpc.Dial(network, address) + if err != nil { + return nil, err + } + return &HelloServiceClient{Client: c}, nil +} + +func (p *HelloServiceClient) Hello(request string, reply *string) error { + return p.Client.Call(HelloServiceName+".Hello", request, reply) +} diff --git a/examples/ch4-01-rpc-inro/hello-service-v2/client/main.go b/examples/ch4-01-rpc-inro/hello-service-v2/client/main.go new file mode 100644 index 0000000..6362fd2 --- /dev/null +++ b/examples/ch4-01-rpc-inro/hello-service-v2/client/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "log" + + "github.com/chai2010/advanced-go-programming-book/examples/ch4-01-rpc-inro/hello-service-v2/api" +) + +type HelloService struct{} + +func (p *HelloService) Hello(request string, reply *string) error { + *reply = "hello:" + request + return nil +} + +func main() { + + client, err := api.DialHelloService("tcp", "localhost:1234") + if err != nil { + log.Fatal("dialing:", err) + } + + var reply string + err = client.Hello("hello", &reply) + if err != nil { + log.Fatal(err) + } + + fmt.Println(reply) +} diff --git a/examples/ch4-01-rpc-inro/hello-service-v2/server/main.go b/examples/ch4-01-rpc-inro/hello-service-v2/server/main.go new file mode 100644 index 0000000..f6df3dc --- /dev/null +++ b/examples/ch4-01-rpc-inro/hello-service-v2/server/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "log" + "net" + "net/rpc" + + "github.com/chai2010/advanced-go-programming-book/examples/ch4-01-rpc-inro/hello-service-v2/api" +) + +type HelloService struct{} + +func (p *HelloService) Hello(request string, reply *string) error { + *reply = "hello:" + request + return nil +} + +func main() { + api.RegisterHelloService(new(HelloService)) + + listener, err := net.Listen("tcp", ":1234") + if err != nil { + log.Fatal("ListenTCP error:", err) + } + + for { + conn, err := listener.Accept() + if err != nil { + log.Fatal("Accept error:", err) + } + + go rpc.ServeConn(conn) + } +}