1 /* 2 * 3 * Copyright 2024 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package grpc 20 21 // ServerStreamingClient represents the client side of a server-streaming (one 22 // request, many responses) RPC. It is generic over the type of the response 23 // message. It is used in generated code. 24 type ServerStreamingClient[Res any] interface { 25 Recv() (*Res, error) 26 ClientStream 27 } 28 29 // ServerStreamingServer represents the server side of a server-streaming (one 30 // request, many responses) RPC. It is generic over the type of the response 31 // message. It is used in generated code. 32 type ServerStreamingServer[Res any] interface { 33 Send(*Res) error 34 ServerStream 35 } 36 37 // ClientStreamingClient represents the client side of a client-streaming (many 38 // requests, one response) RPC. It is generic over both the type of the request 39 // message stream and the type of the unary response message. It is used in 40 // generated code. 41 type ClientStreamingClient[Req any, Res any] interface { 42 Send(*Req) error 43 CloseAndRecv() (*Res, error) 44 ClientStream 45 } 46 47 // ClientStreamingServer represents the server side of a client-streaming (many 48 // requests, one response) RPC. It is generic over both the type of the request 49 // message stream and the type of the unary response message. It is used in 50 // generated code. 51 type ClientStreamingServer[Req any, Res any] interface { 52 Recv() (*Req, error) 53 SendAndClose(*Res) error 54 ServerStream 55 } 56 57 // BidiStreamingClient represents the client side of a bidirectional-streaming 58 // (many requests, many responses) RPC. It is generic over both the type of the 59 // request message stream and the type of the response message stream. It is 60 // used in generated code. 61 type BidiStreamingClient[Req any, Res any] interface { 62 Send(*Req) error 63 Recv() (*Res, error) 64 ClientStream 65 } 66 67 // BidiStreamingServer represents the server side of a bidirectional-streaming 68 // (many requests, many responses) RPC. It is generic over both the type of the 69 // request message stream and the type of the response message stream. It is 70 // used in generated code. 71 type BidiStreamingServer[Req any, Res any] interface { 72 Recv() (*Req, error) 73 Send(*Res) error 74 ServerStream 75 } 76 77 // GenericClientStream implements the ServerStreamingClient, ClientStreamingClient, 78 // and BidiStreamingClient interfaces. It is used in generated code. 79 type GenericClientStream[Req any, Res any] struct { 80 ClientStream 81 } 82 83 var _ ServerStreamingClient[string] = (*GenericClientStream[int, string])(nil) 84 var _ ClientStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) 85 var _ BidiStreamingClient[int, string] = (*GenericClientStream[int, string])(nil) 86 87 // Send pushes one message into the stream of requests to be consumed by the 88 // server. The type of message which can be sent is determined by the Req type 89 // parameter of the GenericClientStream receiver. 90 func (x *GenericClientStream[Req, Res]) Send(m *Req) error { 91 return x.ClientStream.SendMsg(m) 92 } 93 94 // Recv reads one message from the stream of responses generated by the server. 95 // The type of the message returned is determined by the Res type parameter 96 // of the GenericClientStream receiver. 97 func (x *GenericClientStream[Req, Res]) Recv() (*Res, error) { 98 m := new(Res) 99 if err := x.ClientStream.RecvMsg(m); err != nil { 100 return nil, err 101 } 102 return m, nil 103 } 104 105 // CloseAndRecv closes the sending side of the stream, then receives the unary 106 // response from the server. The type of message which it returns is determined 107 // by the Res type parameter of the GenericClientStream receiver. 108 func (x *GenericClientStream[Req, Res]) CloseAndRecv() (*Res, error) { 109 if err := x.ClientStream.CloseSend(); err != nil { 110 return nil, err 111 } 112 m := new(Res) 113 if err := x.ClientStream.RecvMsg(m); err != nil { 114 return nil, err 115 } 116 return m, nil 117 } 118 119 // GenericServerStream implements the ServerStreamingServer, ClientStreamingServer, 120 // and BidiStreamingServer interfaces. It is used in generated code. 121 type GenericServerStream[Req any, Res any] struct { 122 ServerStream 123 } 124 125 var _ ServerStreamingServer[string] = (*GenericServerStream[int, string])(nil) 126 var _ ClientStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) 127 var _ BidiStreamingServer[int, string] = (*GenericServerStream[int, string])(nil) 128 129 // Send pushes one message into the stream of responses to be consumed by the 130 // client. The type of message which can be sent is determined by the Res 131 // type parameter of the serverStreamServer receiver. 132 func (x *GenericServerStream[Req, Res]) Send(m *Res) error { 133 return x.ServerStream.SendMsg(m) 134 } 135 136 // SendAndClose pushes the unary response to the client. The type of message 137 // which can be sent is determined by the Res type parameter of the 138 // clientStreamServer receiver. 139 func (x *GenericServerStream[Req, Res]) SendAndClose(m *Res) error { 140 return x.ServerStream.SendMsg(m) 141 } 142 143 // Recv reads one message from the stream of requests generated by the client. 144 // The type of the message returned is determined by the Req type parameter 145 // of the clientStreamServer receiver. 146 func (x *GenericServerStream[Req, Res]) Recv() (*Req, error) { 147 m := new(Req) 148 if err := x.ServerStream.RecvMsg(m); err != nil { 149 return nil, err 150 } 151 return m, nil 152 } 153