-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathProtobufEncoder.cs
More file actions
40 lines (36 loc) · 1.22 KB
/
Copy pathProtobufEncoder.cs
File metadata and controls
40 lines (36 loc) · 1.22 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DotNetty.Codecs.Protobuf
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using Google.Protobuf;
public sealed class ProtobufEncoder : MessageToMessageEncoder<IMessage>
{
protected override void Encode(IChannelHandlerContext context, IMessage message, List<object> output)
{
Contract.Requires(context != null);
Contract.Requires(message != null);
Contract.Requires(output != null);
IByteBuffer buffer = null;
try
{
//todo: Implement ByteBufferStream to avoid allocations.
buffer = Unpooled.WrappedBuffer(message.ToByteArray());
output.Add(buffer);
buffer = null;
}
catch (Exception exception)
{
throw new CodecException(exception);
}
finally
{
buffer?.Release();
}
}
}
}