forked from hazelcast/hazelcast-client-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec-template.cs.j2
348 lines (333 loc) · 18.7 KB
/
codec-template.cs.j2
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
{% macro encode_var_sized(param) -%}
{% if is_var_sized_list(param.type) or is_var_sized_list_contains_nullable(param.type) -%}
ListMultiFrameCodec.Encode{% if is_var_sized_list_contains_nullable(param.type)%}ContainsNullable{% endif %}{% if param.nullable %}Nullable{% endif %}(clientMessage, {{ escape_keyword(param.name) }}, {{ item_type(lang_name, param.type) }}Codec.Encode)
{%- elif is_var_sized_entry_list(param.type) -%}
EntryListCodec.Encode(clientMessage, {{ escape_keyword(param.name) }}, {{ key_type(lang_name, param.type) }}Codec.Encode, {{ value_type(lang_name, param.type) }}Codec.Encode)
{%- elif is_var_sized_map(param.type) -%}
MapCodec.Encode{% if param.nullable %}Nullable{% endif %}(clientMessage, {{ escape_keyword(param.name) }}, {{ key_type(lang_name, param.type) }}Codec.Encode, {{ value_type(lang_name, param.type) }}Codec.Encode)
{%- else -%}
{%- if param.nullable -%}
CodecUtil.EncodeNullable(clientMessage, {{ escape_keyword(param.name) }}, {{ lang_name(param.type) }}Codec.Encode)
{%- else -%}
{{ lang_name(param.type) }}Codec.Encode(clientMessage, {{ escape_keyword(param.name) }})
{%- endif %}
{% endif %}
{%- endmacro %}
{% macro decode_var_sized(param) -%}
{%- if is_var_sized_list(param.type) or is_var_sized_list_contains_nullable(param.type) -%}
ListMultiFrameCodec.Decode{% if is_var_sized_list_contains_nullable(param.type) %}ContainsNullable{% endif %}{% if param.nullable %}Nullable{% endif %}(iterator, {{ item_type(lang_name, param.type) }}Codec.Decode)
{%- elif is_var_sized_entry_list(param.type) -%}
EntryListCodec.Decode(iterator, {{ key_type(lang_name, param.type) }}Codec.Decode, {{ value_type(lang_name, param.type) }}Codec.Decode)
{%- elif is_var_sized_map(param.type) -%}
MapCodec.Decode{% if param.nullable %}Nullable{% endif %}(iterator, {{ key_type(lang_name, param.type) }}Codec.Decode, {{ value_type(lang_name, param.type) }}Codec.Decode)
{%- else -%}
{%- if param.nullable -%}
CodecUtil.DecodeNullable(iterator, {{ lang_name(param.type) }}Codec.Decode)
{%- else -%}
{{ lang_name(param.type) }}Codec.Decode(iterator)
{%- endif -%}
{%- endif -%}
{%- endmacro %}
// Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// <auto-generated>
// This code was generated by a tool.
// Hazelcast Client Protocol Code Generator
// https://github.com/hazelcast/hazelcast-client-protocol
// Change to this file will be lost if the code is regenerated.
// </auto-generated>
#pragma warning disable IDE0051 // Remove unused private members
// ReSharper disable UnusedMember.Local
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Hazelcast.Protocol.BuiltInCodecs;
using Hazelcast.Protocol.CustomCodecs;
using Hazelcast.Core;
using Hazelcast.Messaging;
using Hazelcast.Clustering;
using Hazelcast.Serialization;
using Microsoft.Extensions.Logging;
{% if namespace %}
namespace {{ namespace }}
{% else %}
namespace Hazelcast.Protocol.Codecs
{% endif %}
{
/// <summary>
{% for line in method.doc.splitlines() %}
/// {{ line }}
{% endfor %}
///</summary>
#if SERVER_CODEC
internal static class {{ service_name|capital }}{{ method.name|capital }}ServerCodec
#else
internal static class {{ service_name|capital }}{{ method.name|capital }}Codec
#endif
{
public const int RequestMessageType = {{ method.request.id }}; // {{ '0x%06X'|format(method.request.id) }}
public const int ResponseMessageType = {{ method.response.id }}; // {{ '0x%06X'|format(method.response.id) }}
{#FIXED SIZED PARAMETER OFFSET CONSTANTS#}
{% for param in fixed_params(method.request.params) %}
private const int Request{{param.name|capital}}FieldOffset = {% if loop.first %}Messaging.FrameFields.Offset.PartitionId + BytesExtensions.SizeOfInt{% else %}Request{{ loop.previtem.name|capital}}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(loop.previtem.type)|capital}}{% endif %};
{% if loop.last %}
private const int RequestInitialFrameSize = Request{{param.name|capital}}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(param.type)|capital}};
{% endif %}
{% else %}
private const int RequestInitialFrameSize = Messaging.FrameFields.Offset.PartitionId + BytesExtensions.SizeOfInt;
{% endfor %}
{% for param in fixed_params(method.response.params) %}
private const int Response{{param.name|capital}}FieldOffset = {% if loop.first %}Messaging.FrameFields.Offset.ResponseBackupAcks + BytesExtensions.SizeOfByte{% else %}Response{{ loop.previtem.name|capital}}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(loop.previtem.type)|capital}}{% endif %};
{% if loop.last %}
private const int ResponseInitialFrameSize = Response{{param.name|capital}}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(param.type)|capital}};
{% endif %}
{% else %}
private const int ResponseInitialFrameSize = Messaging.FrameFields.Offset.ResponseBackupAcks + BytesExtensions.SizeOfByte;
{% endfor %}
{% for event in method.events%}
{% for param in fixed_params(event.params) %}
private const int Event{{event.name|capital}}{{param.name|capital}}FieldOffset = {% if loop.first %}Messaging.FrameFields.Offset.PartitionId + BytesExtensions.SizeOfInt{% else %}Event{{ event.name|capital}}{{ loop.previtem.name|capital}}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(loop.previtem.type)|capital}}{% endif %};
{% if loop.last %}
private const int Event{{event.name|capital}}InitialFrameSize = Event{{event.name|capital}}{{param.name|capital}}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(param.type)|capital}};
{% endif %}
{% else %}
private const int Event{{event.name|capital}}InitialFrameSize = Messaging.FrameFields.Offset.PartitionId + BytesExtensions.SizeOfInt;
{% endfor %}
private const int Event{{event.name|capital}}MessageType = {{ event.id }}; // {{ '0x%06X'|format(event.id) }}
{% endfor %}
#if SERVER_CODEC
{#REQUEST PARAMETERS#}
public sealed class RequestParameters
{
{% for param in method.request.params %}
/// <summary>
{% for line in param.doc.splitlines() %}
/// {{ line }}
{% endfor %}
///</summary>
public {{ lang_types_decode(param.type) }} {{ param.name|capital }} { get; set; }
{% endfor %}
{% for param in new_params(method.since, method.request.params) %}
/// <summary>
/// <c>true</c> if the {{ param.name }} is received from the client, <c>false</c> otherwise.
/// If this is false, {{ param.name }} has the default value for its type.
/// </summary>
public bool Is{{ param.name|capital }}Exists { get; set; }
{% endfor %}
}
#endif
{#REQUEST ENCODE#}
public static ClientMessage EncodeRequest({% for param in method.request.params %}{{ lang_types_encode(param.type) }} {{escape_keyword(param.name)}}{% if not loop.last %}, {% endif %}{% endfor %})
{
var clientMessage = new ClientMessage
{
IsRetryable = {{ method.request.retryable|lower }},
OperationName = "{{ service_name|capital }}.{{ method.name|capital }}"
};
var initialFrame = new Frame(new byte[RequestInitialFrameSize], (FrameFlags) ClientMessageFlags.Unfragmented);
initialFrame.Bytes.WriteIntL(Messaging.FrameFields.Offset.MessageType, RequestMessageType);
initialFrame.Bytes.WriteIntL(Messaging.FrameFields.Offset.PartitionId, -1);
{% for param in fixed_params(method.request.params) %}
initialFrame.Bytes.Write{{ lang_types_decode(param.type)|capital }}L(Request{{param.name|capital}}FieldOffset, {{escape_keyword(param.name)}});
{% endfor %}
clientMessage.Append(initialFrame);
{% for param in var_size_params(method.request.params) %}
{{ encode_var_sized(param) }};
{% endfor %}
return clientMessage;
}
#if SERVER_CODEC
{#REQUEST DECODE#}
public static RequestParameters DecodeRequest(ClientMessage clientMessage)
{
using var iterator = clientMessage.GetEnumerator();
var request = new RequestParameters();
{% if fixed_params(method.request.params)|length != 0 %}
var initialFrame = iterator.Take();
{% else %}
iterator.Take(); // empty initial frame
{% endif %}
{% for param in fixed_params(method.request.params) %}
{% if param in new_params(method.since, method.request.params) %}
if (initialFrame.Bytes.Length >= Request{{ param.name|capital }}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(param.type)|capital}})
{
request.{{ param.name|capital }} = initialFrame.Bytes.Read{{ lang_types_decode(param.type)|capital }}L(Request{{param.name|capital}}FieldOffset);
request.Is{{ param.name|capital }}Exists = true;
}
else request.Is{{ param.name|capital }}Exists = false;
{% else %}
request.{{ param.name|capital }} = initialFrame.Bytes.Read{{ lang_types_decode(param.type)|capital }}L(Request{{param.name|capital}}FieldOffset);
{% endif %}
{% endfor %}
{% for param in var_size_params(method.request.params) %}
{% if param in new_params(method.since, method.request.params) %}
if (iterator.Current?.Next != null)
{
request.{{ param.name|capital }} = {{ decode_var_sized(param) }};
request.Is{{ param.name|capital }}Exists = true;
}
else request.Is{{ param.name|capital }}Exists = false;
{% else %}
request.{{ param.name|capital }} = {{ decode_var_sized(param) }};
{% endif %}
{% endfor %}
return request;
}
#endif
{#RESPONSE PARAMETERS#}
public sealed class ResponseParameters
{
{% for param in method.response.params %}
/// <summary>
{% for line in param.doc.splitlines() %}
/// {{ line }}
{% endfor %}
///</summary>
public {{ lang_types_decode(param.type) }} {{ param.name|capital }} { get; set; }
{% endfor %}
{% for param in new_params(method.since, method.response.params) %}
/// <summary>
/// <c>true</c> if the {{ param.name }} is received from the member, <c>false</c> otherwise.
/// If this is false, {{ param.name }} has the default value for its type.
/// </summary>
public bool Is{{ param.name|capital }}Exists { get; set; }
{% endfor %}
}
#if SERVER_CODEC
{#RESPONSE ENCODE#}
public static ClientMessage EncodeResponse({% for param in method.response.params %}{{ lang_types_encode(param.type) }} {{escape_keyword(param.name)}}{% if not loop.last %}, {% endif %}{% endfor %})
{
var clientMessage = new ClientMessage();
var initialFrame = new Frame(new byte[ResponseInitialFrameSize], (FrameFlags) ClientMessageFlags.Unfragmented);
initialFrame.Bytes.WriteIntL(Messaging.FrameFields.Offset.MessageType, ResponseMessageType);
{% for param in fixed_params(method.response.params) %}
initialFrame.Bytes.Write{{ lang_types_decode(param.type)|capital }}L(Response{{param.name|capital}}FieldOffset, {{escape_keyword(param.name)}});
{% endfor %}
clientMessage.Append(initialFrame);
{% for param in var_size_params(method.response.params) %}
{{ encode_var_sized(param) }};
{% endfor %}
return clientMessage;
}
#endif
{#RESPONSE DECODE#}
public static ResponseParameters DecodeResponse(ClientMessage clientMessage)
{
using var iterator = clientMessage.GetEnumerator();
var response = new ResponseParameters();
{% if fixed_params(method.response.params)|length != 0 %}
var initialFrame = iterator.Take();
{% else %}
iterator.Take(); // empty initial frame
{% endif %}
{% for param in fixed_params(method.response.params) %}
{% if param in new_params(method.since, method.response.params) %}
if (initialFrame.Bytes.Length >= Response{{ param.name|capital }}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(param.type)|capital}})
{
response.{{ param.name|capital }} = initialFrame.Bytes.Read{{ lang_types_decode(param.type)|capital }}L(Response{{param.name|capital}}FieldOffset);
response.Is{{ param.name|capital }}Exists = true;
}
else response.Is{{ param.name|capital }}Exists = false;
{% else %}
response.{{ param.name|capital }} = initialFrame.Bytes.Read{{ lang_types_decode(param.type)|capital }}L(Response{{param.name|capital}}FieldOffset);
{% endif %}
{% endfor %}
{% for param in var_size_params(method.response.params) %}
{% if param in new_params(method.since, method.response.params) %}
if (iterator.Current?.Next != null)
{
response.{{ param.name|capital }} = {{ decode_var_sized(param) }};
response.Is{{ param.name|capital }}Exists = true;
}
else response.Is{{ param.name|capital }}Exists = false;
{% else %}
response.{{ param.name|capital }} = {{ decode_var_sized(param) }};
{% endif %}
{% endfor %}
return response;
}
{# EVENTS#}
{% if method.events|length != 0 %}
#if SERVER_CODEC
{% for event in method.events%}
public static ClientMessage Encode{{(event.name|capital)}}Event({% for param in event.params %}{{ lang_types_encode(param.type) }} {{escape_keyword(param.name)}}{% if not loop.last %}, {% endif %}{% endfor %})
{
var clientMessage = new ClientMessage();
var initialFrame = new Frame(new byte[Event{{(event.name|capital)}}InitialFrameSize], (FrameFlags) ClientMessageFlags.Unfragmented);
initialFrame.Bytes.WriteIntL(Messaging.FrameFields.Offset.MessageType, Event{{(event.name|capital)}}MessageType);
initialFrame.Bytes.WriteIntL(Messaging.FrameFields.Offset.PartitionId, -1);
{% for param in fixed_params(event.params) %}
initialFrame.Bytes.Write{{ lang_types_encode(param.type)|capital }}L(Event{{(event.name|capital)}}{{param.name|capital}}FieldOffset, {{escape_keyword(param.name)}});
{% endfor %}
clientMessage.Append(initialFrame);
clientMessage.Flags |= ClientMessageFlags.Event;
{% for param in var_size_params(event.params) %}
{{ encode_var_sized(param) }};
{% endfor %}
return clientMessage;
}
{% endfor %}
#endif
{% endif %}
{% if method.events|length != 0 %}
public static ValueTask HandleEventAsync(ClientMessage clientMessage{% for event in method.events%}, Func<{% for param in event.params %}{% if param in new_params(event.since, event.params) %}bool is{{ param.name|capital }}Exists, {% endif %}{{ lang_types_decode(param.type) }}, {% endfor %}object, ValueTask> handle{{ event.name|capital }}EventAsync{% endfor %}, object state, ILoggerFactory loggerFactory)
{
using var iterator = clientMessage.GetEnumerator();
var messageType = clientMessage.MessageType;
{% for event in method.events%}
if (messageType == Event{{(event.name|capital)}}MessageType)
{
{% if fixed_params(event.params)|length != 0 %}
var initialFrame = iterator.Take();
{% else %}
iterator.Take(); // empty initial frame
{% endif %}
{% for param in fixed_params(event.params) %}
{% if param in new_params(event.since, event.params) %}
var is{{ param.name|capital }}Exists = false;
{{ lang_types_encode(param.type) }} {{escape_keyword(param.name)}} = default;
if (initialFrame.Bytes.Length >= Event{{event.name|capital}}{{param.name|capital}}FieldOffset + BytesExtensions.SizeOf{{lang_types_decode(param.type)|capital}})
{
{{escape_keyword(param.name)}} = initialFrame.Bytes.Read{{ lang_types_decode(param.type)|capital }}L(Event{{(event.name|capital)}}{{param.name|capital}}FieldOffset);
is{{ param.name|capital }}Exists = true;
}
{% else %}
var {{escape_keyword(param.name)}} = initialFrame.Bytes.Read{{ lang_types_decode(param.type)|capital }}L(Event{{(event.name|capital)}}{{param.name|capital}}FieldOffset);
{% endif %}
{% endfor %}
{% for param in var_size_params(event.params) %}
{% if param in new_params(event.since, event.params) %}
var is{{ param.name|capital }}Exists = false;
{{ lang_types_encode(param.type) }} {{escape_keyword(param.name)}} = default;
if (iterator.Current?.Next != null)
{
{{escape_keyword(param.name)}} = {{ decode_var_sized(param) }};
is{{ param.name|capital }}Exists = true;
}
{% else %}
var {{escape_keyword(param.name)}} = {{ decode_var_sized(param) }};
{% endif %}
{% endfor %}
return handle{{ event.name|capital }}EventAsync({% for param in event.params %}{% if param in new_params(event.since, event.params) %}is{{ param.name|capital }}Exists, {% endif %}{{escape_keyword(param.name)}}, {% endfor %}state);
}
{% endfor %}
loggerFactory.CreateLogger(typeof(EventHandler)).LogDebug("Unknown message type received on event handler :" + messageType);
return default;
}
{% endif %}
}
}