5
5
using ProtoBuf . Grpc . Configuration ;
6
6
using System . Threading . Tasks ;
7
7
using System . Linq ;
8
+ using System . Threading ;
8
9
9
10
namespace ProtoBuf . Grpc . Internal
10
11
{
@@ -50,6 +51,7 @@ internal enum TypeCategory
50
51
CallOptions ,
51
52
ServerCallContext ,
52
53
CallContext ,
54
+ CancellationToken ,
53
55
AsyncUnaryCall ,
54
56
AsyncClientStreamingCall ,
55
57
AsyncDuplexStreamingCall ,
@@ -90,6 +92,13 @@ internal enum TypeCategory
90
92
{ ( TypeCategory . CallContext , TypeCategory . None , TypeCategory . None , TypeCategory . TypedTask ) , ( ContextKind . CallContext , MethodType . Unary , ResultKind . Task , VoidKind . Request , VOID , RET ) } ,
91
93
{ ( TypeCategory . CallContext , TypeCategory . None , TypeCategory . None , TypeCategory . TypedValueTask ) , ( ContextKind . CallContext , MethodType . Unary , ResultKind . ValueTask , VoidKind . Request , VOID , RET ) } ,
92
94
95
+ { ( TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . None , TypeCategory . Void ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Sync , VoidKind . Both , VOID , VOID ) } ,
96
+ { ( TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . None , TypeCategory . Data ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Sync , VoidKind . Request , VOID , RET ) } ,
97
+ { ( TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . None , TypeCategory . UntypedTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Task , VoidKind . Both , VOID , VOID ) } ,
98
+ { ( TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . None , TypeCategory . UntypedValueTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . ValueTask , VoidKind . Both , VOID , VOID ) } ,
99
+ { ( TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . None , TypeCategory . TypedTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Task , VoidKind . Request , VOID , RET ) } ,
100
+ { ( TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . None , TypeCategory . TypedValueTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . ValueTask , VoidKind . Request , VOID , RET ) } ,
101
+
93
102
// unary with parameter, with or without a return value
94
103
{ ( TypeCategory . Data , TypeCategory . None , TypeCategory . None , TypeCategory . Void ) , ( ContextKind . NoContext , MethodType . Unary , ResultKind . Sync , VoidKind . Response , 0 , VOID ) } ,
95
104
{ ( TypeCategory . Data , TypeCategory . None , TypeCategory . None , TypeCategory . Data ) , ( ContextKind . NoContext , MethodType . Unary , ResultKind . Sync , VoidKind . None , 0 , RET ) } ,
@@ -105,6 +114,13 @@ internal enum TypeCategory
105
114
{ ( TypeCategory . Data , TypeCategory . CallContext , TypeCategory . None , TypeCategory . TypedTask ) , ( ContextKind . CallContext , MethodType . Unary , ResultKind . Task , VoidKind . None , 0 , RET ) } ,
106
115
{ ( TypeCategory . Data , TypeCategory . CallContext , TypeCategory . None , TypeCategory . TypedValueTask ) , ( ContextKind . CallContext , MethodType . Unary , ResultKind . ValueTask , VoidKind . None , 0 , RET ) } ,
107
116
117
+ { ( TypeCategory . Data , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . Void ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Sync , VoidKind . Response , 0 , VOID ) } ,
118
+ { ( TypeCategory . Data , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . Data ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Sync , VoidKind . None , 0 , RET ) } ,
119
+ { ( TypeCategory . Data , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . UntypedTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Task , VoidKind . Response , 0 , VOID ) } ,
120
+ { ( TypeCategory . Data , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . UntypedValueTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . ValueTask , VoidKind . Response , 0 , VOID ) } ,
121
+ { ( TypeCategory . Data , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . TypedTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . Task , VoidKind . None , 0 , RET ) } ,
122
+ { ( TypeCategory . Data , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . TypedValueTask ) , ( ContextKind . CancellationToken , MethodType . Unary , ResultKind . ValueTask , VoidKind . None , 0 , RET ) } ,
123
+
108
124
// client streaming
109
125
{ ( TypeCategory . IAsyncEnumerable , TypeCategory . None , TypeCategory . None , TypeCategory . TypedValueTask ) , ( ContextKind . NoContext , MethodType . ClientStreaming , ResultKind . ValueTask , VoidKind . None , 0 , RET ) } ,
110
126
{ ( TypeCategory . IAsyncEnumerable , TypeCategory . None , TypeCategory . None , TypeCategory . UntypedValueTask ) , ( ContextKind . NoContext , MethodType . ClientStreaming , ResultKind . ValueTask , VoidKind . Response , 0 , VOID ) } ,
@@ -116,19 +132,27 @@ internal enum TypeCategory
116
132
{ ( TypeCategory . IAsyncEnumerable , TypeCategory . CallContext , TypeCategory . None , TypeCategory . TypedTask ) , ( ContextKind . CallContext , MethodType . ClientStreaming , ResultKind . Task , VoidKind . None , 0 , RET ) } ,
117
133
{ ( TypeCategory . IAsyncEnumerable , TypeCategory . CallContext , TypeCategory . None , TypeCategory . UntypedTask ) , ( ContextKind . CallContext , MethodType . ClientStreaming , ResultKind . Task , VoidKind . Response , 0 , VOID ) } ,
118
134
135
+ { ( TypeCategory . IAsyncEnumerable , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . TypedValueTask ) , ( ContextKind . CancellationToken , MethodType . ClientStreaming , ResultKind . ValueTask , VoidKind . None , 0 , RET ) } ,
136
+ { ( TypeCategory . IAsyncEnumerable , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . UntypedValueTask ) , ( ContextKind . CancellationToken , MethodType . ClientStreaming , ResultKind . ValueTask , VoidKind . Response , 0 , VOID ) } ,
137
+ { ( TypeCategory . IAsyncEnumerable , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . TypedTask ) , ( ContextKind . CancellationToken , MethodType . ClientStreaming , ResultKind . Task , VoidKind . None , 0 , RET ) } ,
138
+ { ( TypeCategory . IAsyncEnumerable , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . UntypedTask ) , ( ContextKind . CancellationToken , MethodType . ClientStreaming , ResultKind . Task , VoidKind . Response , 0 , VOID ) } ,
139
+
119
140
// server streaming
120
141
{ ( TypeCategory . None , TypeCategory . None , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . NoContext , MethodType . ServerStreaming , ResultKind . AsyncEnumerable , VoidKind . Request , VOID , RET ) } ,
121
142
{ ( TypeCategory . CallContext , TypeCategory . None , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . CallContext , MethodType . ServerStreaming , ResultKind . AsyncEnumerable , VoidKind . Request , VOID , RET ) } ,
143
+ { ( TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . CancellationToken , MethodType . ServerStreaming , ResultKind . AsyncEnumerable , VoidKind . Request , VOID , RET ) } ,
122
144
{ ( TypeCategory . Data , TypeCategory . None , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . NoContext , MethodType . ServerStreaming , ResultKind . AsyncEnumerable , VoidKind . None , 0 , RET ) } ,
123
145
{ ( TypeCategory . Data , TypeCategory . CallContext , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . CallContext , MethodType . ServerStreaming , ResultKind . AsyncEnumerable , VoidKind . None , 0 , RET ) } ,
146
+ { ( TypeCategory . Data , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . CancellationToken , MethodType . ServerStreaming , ResultKind . AsyncEnumerable , VoidKind . None , 0 , RET ) } ,
124
147
125
148
// duplex
126
149
{ ( TypeCategory . IAsyncEnumerable , TypeCategory . None , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . NoContext , MethodType . DuplexStreaming , ResultKind . AsyncEnumerable , VoidKind . None , 0 , RET ) } ,
127
150
{ ( TypeCategory . IAsyncEnumerable , TypeCategory . CallContext , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . CallContext , MethodType . DuplexStreaming , ResultKind . AsyncEnumerable , VoidKind . None , 0 , RET ) } ,
151
+ { ( TypeCategory . IAsyncEnumerable , TypeCategory . CancellationToken , TypeCategory . None , TypeCategory . IAsyncEnumerable ) , ( ContextKind . CancellationToken , MethodType . DuplexStreaming , ResultKind . AsyncEnumerable , VoidKind . None , 0 , RET ) } ,
128
152
} ;
129
153
internal static int SignatureCount => s_signaturePatterns . Count ;
130
154
131
- internal static int GeneralPurposeSignatureCount ( ) => s_signaturePatterns . Values . Count ( x => x . Context == ContextKind . CallContext || x . Context == ContextKind . NoContext ) ;
155
+ internal static int GeneralPurposeSignatureCount ( ) => s_signaturePatterns . Values . Count ( x => x . Context == ContextKind . CallContext || x . Context == ContextKind . NoContext || x . Context == ContextKind . CancellationToken ) ;
132
156
133
157
static TypeCategory GetCategory ( MarshallerCache marshallerCache , Type type , IBindContext ? bindContext )
134
158
{
@@ -139,6 +163,7 @@ static TypeCategory GetCategory(MarshallerCache marshallerCache, Type type, IBin
139
163
if ( type == typeof ( ServerCallContext ) ) return TypeCategory . ServerCallContext ;
140
164
if ( type == typeof ( CallOptions ) ) return TypeCategory . CallOptions ;
141
165
if ( type == typeof ( CallContext ) ) return TypeCategory . CallContext ;
166
+ if ( type == typeof ( CancellationToken ) ) return TypeCategory . CancellationToken ;
142
167
143
168
if ( type . IsGenericType )
144
169
{
@@ -297,6 +322,7 @@ where method.IsGenericMethodDefinition
297
322
{
298
323
case ContextKind . CallContext :
299
324
case ContextKind . NoContext :
325
+ case ContextKind . CancellationToken :
300
326
return _clientResponseMap . TryGetValue ( ( MethodType , Result , Void & VoidKind . Response ) , out var helper ) ? helper : null ;
301
327
default :
302
328
return null ;
@@ -335,6 +361,7 @@ internal enum ContextKind
335
361
CallContext , // pb-net shared context kind
336
362
CallOptions , // GRPC core client context kind
337
363
ServerCallContext , // GRPC core server context kind
364
+ CancellationToken , // cancellation (without extra context)
338
365
}
339
366
340
367
internal enum ResultKind
0 commit comments