Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow encoding and decoding protobuf messages that have no fields #352

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/DotNetty.Codecs.Protobuf/ProtobufDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ protected override void Decode(IChannelHandlerContext context, IByteBuffer messa
Contract.Requires(output != null);

int length = message.ReadableBytes;
if (length <= 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I don't think this is very good idea because there is no way to tell whether you received something that is empty or nothing is received at all. I ran into similar situation before like this and ended up having 'Identity' message (e.g. all messages with ID, 0 means empty) to ensure the senders intention is unambiguous.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In short, received something meaning 'empty' is different from nothing is received at all. On the decoder side, this 'default' behavour might not be desirable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I certainly agree if someone was trying to just use the raw protobuf data, but I would imagine most use cases have a header or at least length in front of the data. That should indicate whether or not protobuf data is expected.
That said, if we don't want to change the default behavior, perhaps it could be something configurable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I prefer to make them virtual so that you can override them if this is the case. But from comms point of view, I prefer not to have some lower level surprises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see that much use in overriding the Decode method as it is the functionality.
We could do something like

int length = message.ReadableBytes;
if (RejectContent(length))
{
    return;
}
...
protected virtual bool RejectContent(int contentLength)
{
    return contentLength <= 0;
}

Given that empty messages are just silently dropped with the current behavior, I feel as though it should be documented at a minimum and ideally able to be function.

Copy link
Contributor

@StormHub StormHub Feb 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for such a simple use case, just override Decode from Decoder which

protected override void Decode(IChannelHandlerContext context, IByteBuffer message, List output)
{
int length = message.ReadableBytes;
if (length <= 0)
{
output.Add("Something empty");
return;
}
base.Decode(context, message, output);
}

Why would we change the base decoder for such an application specific behaviour?

{
return;
}

Stream inputStream = null;
try
Expand Down
5 changes: 0 additions & 5 deletions src/DotNetty.Codecs.Protobuf/ProtobufEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ protected override void Encode(IChannelHandlerContext context, IMessage message,

try
{
int size = message.CalculateSize();
if (size == 0)
{
return;
}
//todo: Implement ByteBufferStream to avoid allocations.
buffer = Unpooled.WrappedBuffer(message.ToByteArray());
output.Add(buffer);
Expand Down
4 changes: 0 additions & 4 deletions src/DotNetty.Codecs.ProtocolBuffers/ProtobufDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ protected override void Decode(IChannelHandlerContext context, IByteBuffer messa
Contract.Requires(output != null);

int length = message.ReadableBytes;
if (length <= 0)
{
return;
}

Stream inputStream = null;
try
Expand Down
4 changes: 0 additions & 4 deletions src/DotNetty.Codecs.ProtocolBuffers/ProtobufEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ protected override void Encode(IChannelHandlerContext context, IMessageLite mess
try
{
int size = message.SerializedSize;
if (size <= 0)
{
return;
}

buffer = context.Allocator.Buffer(size);
ArraySegment<byte> data = buffer.GetIoBuffer(buffer.WriterIndex, size);
Expand Down
Loading