You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
535 lines
18 KiB
535 lines
18 KiB
1 month ago
|
|
||
|
|
||
|
using System;
|
||
|
using System.IO;
|
||
|
using System.Text;
|
||
|
|
||
|
namespace ProtoCSStruct
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Encodes and writes protocol message fields.
|
||
|
/// </summary>
|
||
|
/// <remarks>
|
||
|
/// <para>
|
||
|
/// This class is generally used by generated code to write appropriate
|
||
|
/// primitives to the stream. It effectively encapsulates the lowest
|
||
|
/// levels of protocol buffer format. Unlike some other implementations,
|
||
|
/// this does not include combined "write tag and value" methods. Generated
|
||
|
/// code knows the exact byte representations of the tags they're going to write,
|
||
|
/// so there's no need to re-encode them each time. Manually-written code calling
|
||
|
/// this class should just call one of the <c>WriteTag</c> overloads before each value.
|
||
|
/// </para>
|
||
|
/// </remarks>
|
||
|
public sealed partial class CodedOutputStream : IDisposable
|
||
|
{
|
||
|
// "Local" copy of Encoding.UTF8, for efficiency. (Yes, it makes a difference.)
|
||
|
public static readonly Encoding Utf8Encoding = Encoding.UTF8;
|
||
|
|
||
|
/// <summary>
|
||
|
/// The buffer size used by CreateInstance(Stream).
|
||
|
/// </summary>
|
||
|
public static readonly int DefaultBufferSize = 4096;
|
||
|
|
||
|
private readonly bool leaveOpen;
|
||
|
private readonly byte[] buffer;
|
||
|
private readonly int limit;
|
||
|
private int position;
|
||
|
private readonly Stream output;
|
||
|
|
||
|
#region Construction
|
||
|
/// <summary>
|
||
|
/// Creates a new CodedOutputStream that writes directly to the given
|
||
|
/// byte array. If more bytes are written than fit in the array,
|
||
|
/// OutOfSpaceException will be thrown.
|
||
|
/// </summary>
|
||
|
public CodedOutputStream(byte[] flatArray) : this(flatArray, 0, flatArray.Length)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Creates a new CodedOutputStream that writes directly to the given
|
||
|
/// byte array slice. If more bytes are written than fit in the array,
|
||
|
/// OutOfSpaceException will be thrown.
|
||
|
/// </summary>
|
||
|
private CodedOutputStream(byte[] buffer, int offset, int length)
|
||
|
{
|
||
|
this.output = null;
|
||
|
this.buffer = buffer;
|
||
|
this.position = offset;
|
||
|
this.limit = offset + length;
|
||
|
leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference
|
||
|
}
|
||
|
|
||
|
private CodedOutputStream(Stream output, byte[] buffer, bool leaveOpen)
|
||
|
{
|
||
|
this.output = ProtoPreconditions.CheckNotNull(output, nameof(output));
|
||
|
this.buffer = buffer;
|
||
|
this.position = 0;
|
||
|
this.limit = buffer.Length;
|
||
|
this.leaveOpen = leaveOpen;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Creates a new <see cref="CodedOutputStream" /> which write to the given stream, and disposes of that
|
||
|
/// stream when the returned <c>CodedOutputStream</c> is disposed.
|
||
|
/// </summary>
|
||
|
/// <param name="output">The stream to write to. It will be disposed when the returned <c>CodedOutputStream is disposed.</c></param>
|
||
|
public CodedOutputStream(Stream output) : this(output, DefaultBufferSize, false)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Creates a new CodedOutputStream which write to the given stream and uses
|
||
|
/// the specified buffer size.
|
||
|
/// </summary>
|
||
|
/// <param name="output">The stream to write to. It will be disposed when the returned <c>CodedOutputStream is disposed.</c></param>
|
||
|
/// <param name="bufferSize">The size of buffer to use internally.</param>
|
||
|
public CodedOutputStream(Stream output, int bufferSize) : this(output, new byte[bufferSize], false)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Creates a new CodedOutputStream which write to the given stream.
|
||
|
/// </summary>
|
||
|
/// <param name="output">The stream to write to.</param>
|
||
|
/// <param name="leaveOpen">If <c>true</c>, <paramref name="output"/> is left open when the returned <c>CodedOutputStream</c> is disposed;
|
||
|
/// if <c>false</c>, the provided stream is disposed as well.</param>
|
||
|
public CodedOutputStream(Stream output, bool leaveOpen) : this(output, DefaultBufferSize, leaveOpen)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Creates a new CodedOutputStream which write to the given stream and uses
|
||
|
/// the specified buffer size.
|
||
|
/// </summary>
|
||
|
/// <param name="output">The stream to write to.</param>
|
||
|
/// <param name="bufferSize">The size of buffer to use internally.</param>
|
||
|
/// <param name="leaveOpen">If <c>true</c>, <paramref name="output"/> is left open when the returned <c>CodedOutputStream</c> is disposed;
|
||
|
/// if <c>false</c>, the provided stream is disposed as well.</param>
|
||
|
public CodedOutputStream(Stream output, int bufferSize, bool leaveOpen) : this(output, new byte[bufferSize], leaveOpen)
|
||
|
{
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
/// <summary>
|
||
|
/// Returns the current position in the stream, or the position in the output buffer
|
||
|
/// </summary>
|
||
|
public long Position
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (output != null)
|
||
|
{
|
||
|
return output.Position + position;
|
||
|
}
|
||
|
return position;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#region Writing of values (not including tags)
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes a uint64 field value, without a tag, to the stream.
|
||
|
/// </summary>
|
||
|
/// <param name="value">The value to write</param>
|
||
|
public void WriteUInt64(ulong value)
|
||
|
{
|
||
|
WriteRawVarint64(value);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes an int64 field value, without a tag, to the stream.
|
||
|
/// </summary>
|
||
|
/// <param name="value">The value to write</param>
|
||
|
public void WriteInt64(long value)
|
||
|
{
|
||
|
WriteRawVarint64((ulong) value);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes an int32 field value, without a tag, to the stream.
|
||
|
/// </summary>
|
||
|
/// <param name="value">The value to write</param>
|
||
|
public void WriteInt32(int value)
|
||
|
{
|
||
|
if (value >= 0)
|
||
|
{
|
||
|
WriteRawVarint32((uint) value);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Must sign-extend.
|
||
|
WriteRawVarint64((ulong) value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes a bool field value, without a tag, to the stream.
|
||
|
/// </summary>
|
||
|
/// <param name="value">The value to write</param>
|
||
|
public void WriteBool(bool value)
|
||
|
{
|
||
|
WriteRawByte(value ? (byte) 1 : (byte) 0);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes a string field value, without a tag, to the stream.
|
||
|
/// The data is length-prefixed.
|
||
|
/// </summary>
|
||
|
/// <param name="value"></param>
|
||
|
unsafe public void WriteFixedStructString(char* pchar, int count)
|
||
|
{
|
||
|
// Optimise the case where we have enough space to write
|
||
|
// the string directly to the buffer, which should be common.
|
||
|
int length = Utf8Encoding.GetByteCount(pchar, count);
|
||
|
WriteLength(length);
|
||
|
if (limit - position >= length)
|
||
|
{
|
||
|
if (length == count) // Must be all ASCII...
|
||
|
{
|
||
|
for (int i = 0; i < length; i++)
|
||
|
{
|
||
|
buffer[position + i] = (byte)pchar[i];
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
fixed(byte* byteindex = &buffer[position])
|
||
|
{
|
||
|
Utf8Encoding.GetBytes(pchar, count, byteindex, length);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
position += length;
|
||
|
}
|
||
|
else//���������Ͳ�����Ч����,ʵ��ʹ�ú��ٷ���
|
||
|
{
|
||
|
byte[] bytes = new byte[length];
|
||
|
fixed (byte* byteindex = bytes)
|
||
|
{
|
||
|
Utf8Encoding.GetBytes(pchar, count, byteindex, length);
|
||
|
}
|
||
|
WriteRawBytes(bytes);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes a uint32 value, without a tag, to the stream.
|
||
|
/// </summary>
|
||
|
/// <param name="value">The value to write</param>
|
||
|
public void WriteUInt32(uint value)
|
||
|
{
|
||
|
WriteRawVarint32(value);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes an enum value, without a tag, to the stream.
|
||
|
/// </summary>
|
||
|
/// <param name="value">The value to write</param>
|
||
|
public void WriteEnum(int value)
|
||
|
{
|
||
|
WriteInt32(value);
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes a length (in bytes) for length-delimited data.
|
||
|
/// </summary>
|
||
|
/// <remarks>
|
||
|
/// This method simply writes a rawint, but exists for clarity in calling code.
|
||
|
/// </remarks>
|
||
|
/// <param name="length">Length value, in bytes.</param>
|
||
|
public void WriteLength(int length)
|
||
|
{
|
||
|
WriteRawVarint32((uint) length);
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Raw tag writing
|
||
|
/// <summary>
|
||
|
/// Encodes and writes a tag.
|
||
|
/// </summary>
|
||
|
/// <param name="fieldNumber">The number of the field to write the tag for</param>
|
||
|
/// <param name="type">The wire format type of the tag to write</param>
|
||
|
public void WriteTag(int fieldNumber, WireFormat.WireType type)
|
||
|
{
|
||
|
WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes an already-encoded tag.
|
||
|
/// </summary>
|
||
|
/// <param name="tag">The encoded tag</param>
|
||
|
public void WriteTag(uint tag)
|
||
|
{
|
||
|
WriteRawVarint32(tag);
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Underlying writing primitives
|
||
|
/// <summary>
|
||
|
/// Writes a 32 bit value as a varint. The fast route is taken when
|
||
|
/// there's enough buffer space left to whizz through without checking
|
||
|
/// for each byte; otherwise, we resort to calling WriteRawByte each time.
|
||
|
/// </summary>
|
||
|
internal void WriteRawVarint32(uint value)
|
||
|
{
|
||
|
// Optimize for the common case of a single byte value
|
||
|
if (value < 128 && position < limit)
|
||
|
{
|
||
|
buffer[position++] = (byte)value;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
while (value > 127 && position < limit)
|
||
|
{
|
||
|
buffer[position++] = (byte) ((value & 0x7F) | 0x80);
|
||
|
value >>= 7;
|
||
|
}
|
||
|
while (value > 127)
|
||
|
{
|
||
|
WriteRawByte((byte) ((value & 0x7F) | 0x80));
|
||
|
value >>= 7;
|
||
|
}
|
||
|
if (position < limit)
|
||
|
{
|
||
|
buffer[position++] = (byte) value;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
WriteRawByte((byte) value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal void WriteRawVarint64(ulong value)
|
||
|
{
|
||
|
while (value > 127 && position < limit)
|
||
|
{
|
||
|
buffer[position++] = (byte) ((value & 0x7F) | 0x80);
|
||
|
value >>= 7;
|
||
|
}
|
||
|
while (value > 127)
|
||
|
{
|
||
|
WriteRawByte((byte) ((value & 0x7F) | 0x80));
|
||
|
value >>= 7;
|
||
|
}
|
||
|
if (position < limit)
|
||
|
{
|
||
|
buffer[position++] = (byte) value;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
WriteRawByte((byte) value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal void WriteRawLittleEndian32(uint value)
|
||
|
{
|
||
|
if (position + 4 > limit)
|
||
|
{
|
||
|
WriteRawByte((byte) value);
|
||
|
WriteRawByte((byte) (value >> 8));
|
||
|
WriteRawByte((byte) (value >> 16));
|
||
|
WriteRawByte((byte) (value >> 24));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
buffer[position++] = ((byte) value);
|
||
|
buffer[position++] = ((byte) (value >> 8));
|
||
|
buffer[position++] = ((byte) (value >> 16));
|
||
|
buffer[position++] = ((byte) (value >> 24));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal void WriteRawLittleEndian64(ulong value)
|
||
|
{
|
||
|
if (position + 8 > limit)
|
||
|
{
|
||
|
WriteRawByte((byte) value);
|
||
|
WriteRawByte((byte) (value >> 8));
|
||
|
WriteRawByte((byte) (value >> 16));
|
||
|
WriteRawByte((byte) (value >> 24));
|
||
|
WriteRawByte((byte) (value >> 32));
|
||
|
WriteRawByte((byte) (value >> 40));
|
||
|
WriteRawByte((byte) (value >> 48));
|
||
|
WriteRawByte((byte) (value >> 56));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
buffer[position++] = ((byte) value);
|
||
|
buffer[position++] = ((byte) (value >> 8));
|
||
|
buffer[position++] = ((byte) (value >> 16));
|
||
|
buffer[position++] = ((byte) (value >> 24));
|
||
|
buffer[position++] = ((byte) (value >> 32));
|
||
|
buffer[position++] = ((byte) (value >> 40));
|
||
|
buffer[position++] = ((byte) (value >> 48));
|
||
|
buffer[position++] = ((byte) (value >> 56));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void WriteRawByte(byte value)
|
||
|
{
|
||
|
if (position == limit)
|
||
|
{
|
||
|
RefreshBuffer();
|
||
|
}
|
||
|
|
||
|
buffer[position++] = value;
|
||
|
}
|
||
|
|
||
|
internal void WriteRawByte(uint value)
|
||
|
{
|
||
|
WriteRawByte((byte) value);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes out an array of bytes.
|
||
|
/// </summary>
|
||
|
public void WriteRawBytes(byte[] value)
|
||
|
{
|
||
|
WriteRawBytes(value, 0, value.Length);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Writes out part of an array of bytes.
|
||
|
/// </summary>
|
||
|
public void WriteRawBytes(byte[] value, int offset, int length)
|
||
|
{
|
||
|
if (limit - position >= length)
|
||
|
{
|
||
|
ByteArray.Copy(value, offset, buffer, position, length);
|
||
|
// We have room in the current buffer.
|
||
|
position += length;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Write extends past current buffer. Fill the rest of this buffer and
|
||
|
// flush.
|
||
|
int bytesWritten = limit - position;
|
||
|
ByteArray.Copy(value, offset, buffer, position, bytesWritten);
|
||
|
offset += bytesWritten;
|
||
|
length -= bytesWritten;
|
||
|
position = limit;
|
||
|
RefreshBuffer();
|
||
|
|
||
|
// Now deal with the rest.
|
||
|
// Since we have an output stream, this is our buffer
|
||
|
// and buffer offset == 0
|
||
|
if (length <= limit)
|
||
|
{
|
||
|
// Fits in new buffer.
|
||
|
ByteArray.Copy(value, offset, buffer, 0, length);
|
||
|
position = length;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Write is very big. Let's do it all at once.
|
||
|
output.Write(value, offset, length);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
private void RefreshBuffer()
|
||
|
{
|
||
|
if (output == null)
|
||
|
{
|
||
|
// We're writing to a single buffer.
|
||
|
throw new OutOfSpaceException();
|
||
|
}
|
||
|
|
||
|
// Since we have an output stream, this is our buffer
|
||
|
// and buffer offset == 0
|
||
|
output.Write(buffer, 0, position);
|
||
|
position = 0;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Indicates that a CodedOutputStream wrapping a flat byte array
|
||
|
/// ran out of space.
|
||
|
/// </summary>
|
||
|
public sealed class OutOfSpaceException : IOException
|
||
|
{
|
||
|
internal OutOfSpaceException()
|
||
|
: base("CodedOutputStream was writing to a flat byte array and ran out of space.")
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Flushes any buffered data and optionally closes the underlying stream, if any.
|
||
|
/// </summary>
|
||
|
/// <remarks>
|
||
|
/// <para>
|
||
|
/// By default, any underlying stream is closed by this method. To configure this behaviour,
|
||
|
/// use a constructor overload with a <c>leaveOpen</c> parameter. If this instance does not
|
||
|
/// have an underlying stream, this method does nothing.
|
||
|
/// </para>
|
||
|
/// <para>
|
||
|
/// For the sake of efficiency, calling this method does not prevent future write calls - but
|
||
|
/// if a later write ends up writing to a stream which has been disposed, that is likely to
|
||
|
/// fail. It is recommend that you not call any other methods after this.
|
||
|
/// </para>
|
||
|
/// </remarks>
|
||
|
public void Dispose()
|
||
|
{
|
||
|
Flush();
|
||
|
if (!leaveOpen)
|
||
|
{
|
||
|
output.Dispose();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Flushes any buffered data to the underlying stream (if there is one).
|
||
|
/// </summary>
|
||
|
public void Flush()
|
||
|
{
|
||
|
if (output != null)
|
||
|
{
|
||
|
RefreshBuffer();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Verifies that SpaceLeft returns zero. It's common to create a byte array
|
||
|
/// that is exactly big enough to hold a message, then write to it with
|
||
|
/// a CodedOutputStream. Calling CheckNoSpaceLeft after writing verifies that
|
||
|
/// the message was actually as big as expected, which can help bugs.
|
||
|
/// </summary>
|
||
|
public void CheckNoSpaceLeft()
|
||
|
{
|
||
|
if (SpaceLeft != 0)
|
||
|
{
|
||
|
throw new InvalidOperationException("Did not write as much data as expected.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// If writing to a flat array, returns the space left in the array. Otherwise,
|
||
|
/// throws an InvalidOperationException.
|
||
|
/// </summary>
|
||
|
public int SpaceLeft
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (output == null)
|
||
|
{
|
||
|
return limit - position;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
throw new InvalidOperationException(
|
||
|
"SpaceLeft can only be called on CodedOutputStreams that are " +
|
||
|
"writing to a flat array.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|