diff --git a/.gitignore b/.gitignore index 75ec3f0..78b4678 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode/* \ No newline at end of file +.vscode/* +vendor/* \ No newline at end of file diff --git a/client/client.go b/client/client.go index 01ba5d0..cd10b52 100644 --- a/client/client.go +++ b/client/client.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-15 22:08:18 * @LastEditors: viletyy - * @LastEditTime: 2021-06-15 22:42:16 + * @LastEditTime: 2021-06-15 23:04:16 * @FilePath: /grpc-demo/client/client.go */ package main @@ -28,8 +28,9 @@ func main() { defer conn.Close() client := pb.NewGreeterClient(conn) - _ = SayHello(client) // 单次RPC - _ = SayList(client, &pb.HelloRequest{}) // 服务端流式RPC + _ = SayHello(client) // 单次RPC + _ = SayList(client, &pb.HelloRequest{Name: "viletyy"}) // 服务端流式RPC + _ = SayRecord(client, &pb.HelloRequest{Name: "viletyy"}) // 客户端流式RPC } func SayHello(client pb.GreeterClient) error { @@ -54,3 +55,14 @@ func SayList(client pb.GreeterClient, r *pb.HelloRequest) error { } return nil } + +func SayRecord(client pb.GreeterClient, r *pb.HelloRequest) error { + stream, _ := client.SayRecord(context.Background()) + for n := 0; n < 6; n++ { + _ = stream.Send(r) + } + resp, _ := stream.CloseAndRecv() + log.Printf("resp err: %v", resp) + + return nil +} diff --git a/proto/helloworld.pb.go b/proto/helloworld.pb.go index 3a3a793..5614490 100644 --- a/proto/helloworld.pb.go +++ b/proto/helloworld.pb.go @@ -110,17 +110,19 @@ func init() { func init() { proto.RegisterFile("proto/helloworld.proto", fileDescriptor_4d53fe9c48eadaad) } var fileDescriptor_4d53fe9c48eadaad = []byte{ - // 160 bytes of a gzipped FileDescriptorProto + // 178 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0xd7, 0xcf, 0x48, 0xcd, 0xc9, 0x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d9, + 0x73, 0xb1, 0x07, 0x27, 0x56, 0xfa, 0x64, 0x16, 0x97, 0x90, 0xa3, 0xdd, 0x80, 0x51, 0xc8, 0x91, + 0x8b, 0x33, 0x38, 0xb1, 0x32, 0x28, 0x35, 0x39, 0xbf, 0x28, 0x85, 0x1c, 0x23, 0x34, 0x18, 0x93, + 0xd8, 0xc0, 0xc1, 0x65, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0xe5, 0x07, 0x84, 0xc5, 0x48, 0x01, + 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -137,6 +139,7 @@ const _ = grpc.SupportPackageIsVersion6 type GreeterClient interface { SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) SayList(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (Greeter_SayListClient, error) + SayRecord(ctx context.Context, opts ...grpc.CallOption) (Greeter_SayRecordClient, error) } type greeterClient struct { @@ -188,10 +191,45 @@ func (x *greeterSayListClient) Recv() (*HelloReply, error) { return m, nil } +func (c *greeterClient) SayRecord(ctx context.Context, opts ...grpc.CallOption) (Greeter_SayRecordClient, error) { + stream, err := c.cc.NewStream(ctx, &_Greeter_serviceDesc.Streams[1], "/helloworld.Greeter/SayRecord", opts...) + if err != nil { + return nil, err + } + x := &greeterSayRecordClient{stream} + return x, nil +} + +type Greeter_SayRecordClient interface { + Send(*HelloRequest) error + CloseAndRecv() (*HelloReply, error) + grpc.ClientStream +} + +type greeterSayRecordClient struct { + grpc.ClientStream +} + +func (x *greeterSayRecordClient) Send(m *HelloRequest) error { + return x.ClientStream.SendMsg(m) +} + +func (x *greeterSayRecordClient) CloseAndRecv() (*HelloReply, error) { + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + m := new(HelloReply) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + // GreeterServer is the server API for Greeter service. type GreeterServer interface { SayHello(context.Context, *HelloRequest) (*HelloReply, error) SayList(*HelloRequest, Greeter_SayListServer) error + SayRecord(Greeter_SayRecordServer) error } // UnimplementedGreeterServer can be embedded to have forward compatible implementations. @@ -204,6 +242,9 @@ func (*UnimplementedGreeterServer) SayHello(ctx context.Context, req *HelloReque func (*UnimplementedGreeterServer) SayList(req *HelloRequest, srv Greeter_SayListServer) error { return status.Errorf(codes.Unimplemented, "method SayList not implemented") } +func (*UnimplementedGreeterServer) SayRecord(srv Greeter_SayRecordServer) error { + return status.Errorf(codes.Unimplemented, "method SayRecord not implemented") +} func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) { s.RegisterService(&_Greeter_serviceDesc, srv) @@ -248,6 +289,32 @@ func (x *greeterSayListServer) Send(m *HelloReply) error { return x.ServerStream.SendMsg(m) } +func _Greeter_SayRecord_Handler(srv interface{}, stream grpc.ServerStream) error { + return srv.(GreeterServer).SayRecord(&greeterSayRecordServer{stream}) +} + +type Greeter_SayRecordServer interface { + SendAndClose(*HelloReply) error + Recv() (*HelloRequest, error) + grpc.ServerStream +} + +type greeterSayRecordServer struct { + grpc.ServerStream +} + +func (x *greeterSayRecordServer) SendAndClose(m *HelloReply) error { + return x.ServerStream.SendMsg(m) +} + +func (x *greeterSayRecordServer) Recv() (*HelloRequest, error) { + m := new(HelloRequest) + if err := x.ServerStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + var _Greeter_serviceDesc = grpc.ServiceDesc{ ServiceName: "helloworld.Greeter", HandlerType: (*GreeterServer)(nil), @@ -263,6 +330,11 @@ var _Greeter_serviceDesc = grpc.ServiceDesc{ Handler: _Greeter_SayList_Handler, ServerStreams: true, }, + { + StreamName: "SayRecord", + Handler: _Greeter_SayRecord_Handler, + ClientStreams: true, + }, }, Metadata: "proto/helloworld.proto", } diff --git a/proto/helloworld.proto b/proto/helloworld.proto index 2be387c..f6a5e77 100644 --- a/proto/helloworld.proto +++ b/proto/helloworld.proto @@ -5,6 +5,7 @@ package helloworld; service Greeter { rpc SayHello(HelloRequest) returns (HelloReply) {} rpc SayList(HelloRequest) returns (stream HelloReply) {} + rpc SayRecord(stream HelloRequest) returns (HelloReply) {} } message HelloRequest { diff --git a/server/server.go b/server/server.go index 3aca823..7119dd8 100644 --- a/server/server.go +++ b/server/server.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-15 22:01:41 * @LastEditors: viletyy - * @LastEditTime: 2021-06-15 22:33:53 + * @LastEditTime: 2021-06-15 22:58:25 * @FilePath: /grpc-demo/server/server.go */ package main @@ -9,6 +9,8 @@ package main import ( "context" "flag" + "io" + "log" "net" pb "github.com/go-inpro/grpc-demo/proto" @@ -34,6 +36,21 @@ func (s *GreeterServer) SayList(r *pb.HelloRequest, stream pb.Greeter_SayListSer } return nil } +func (s *GreeterServer) SayRecord(stream pb.Greeter_SayRecordServer) error { + for { + resp, err := stream.Recv() + if err == io.EOF { + message := &pb.HelloReply{Message: "say.record"} + return stream.SendAndClose(message) + } + if err != nil { + return err + } + + log.Printf("resp: %v", resp) + } + +} func main() { server := grpc.NewServer()