本文整理汇总了C#中ProtoBuf.ProtoWriter类的典型用法代码示例。如果您正苦于以下问题:C# ProtoWriter类的具体用法?C# ProtoWriter怎么用?C# ProtoWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ProtoWriter类属于ProtoBuf命名空间,在下文中一共展示了ProtoWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteObject
internal static void WriteObject(object value, int key, ProtoWriter writer, PrefixStyle style, int fieldNumber)
{
if (writer.model == null)
{
throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
}
if (writer.wireType != WireType.None) throw ProtoWriter.CreateException(writer);
switch (style)
{
case PrefixStyle.Base128:
writer.wireType = WireType.String;
writer.fieldNumber = fieldNumber;
if (fieldNumber > 0) WriteHeaderCore(fieldNumber, WireType.String, writer);
break;
case PrefixStyle.Fixed32:
case PrefixStyle.Fixed32BigEndian:
writer.fieldNumber = 0;
writer.wireType = WireType.Fixed32;
break;
default:
throw new ArgumentOutOfRangeException("style");
}
SubItemToken token = StartSubItem(value, writer, true);
writer.model.Serialize(key, value, writer);
EndSubItem(token, writer, style);
}
示例2: WriteObject
/// <summary>
/// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type).
/// </summary>
/// <param name="value">The object to write.</param>
/// <param name="key">The key that uniquely identifies the type within the model.</param>
/// <param name="writer">The destination.</param>
public static void WriteObject(object value, int key, ProtoWriter writer)
{
#if FEAT_IKVM
throw new NotSupportedException();
#else
if (writer == null) throw new ArgumentNullException("writer");
if (writer.model == null)
{
throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
}
SubItemToken token = StartSubItem(value, writer);
if (key >= 0)
{
writer.model.Serialize(key, value, writer);
}
else if (writer.model != null && writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, Serializer.ListItemTag, value, false))
{
// all ok
}
else
{
TypeModel.ThrowUnexpectedType(value.GetType());
}
EndSubItem(token, writer);
#endif
}
示例3: WriteTimeSpan
/// <summary>
/// Writes a TimeSpan to a protobuf stream
/// </summary>
public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
{
TimeSpanScale scale;
long value = timeSpan.Ticks;
if (timeSpan == TimeSpan.MaxValue)
{
value = 1;
scale = TimeSpanScale.MinMax;
}
else if (timeSpan == TimeSpan.MinValue)
{
value = -1;
scale = TimeSpanScale.MinMax;
}
else if (value % TimeSpan.TicksPerDay == 0)
{
scale = TimeSpanScale.Days;
value /= TimeSpan.TicksPerDay;
}
else if (value % TimeSpan.TicksPerHour == 0)
{
scale = TimeSpanScale.Hours;
value /= TimeSpan.TicksPerHour;
}
else if (value % TimeSpan.TicksPerMinute == 0)
{
scale = TimeSpanScale.Minutes;
value /= TimeSpan.TicksPerMinute;
}
else if (value % TimeSpan.TicksPerSecond == 0)
{
scale = TimeSpanScale.Seconds;
value /= TimeSpan.TicksPerSecond;
}
else if (value % TimeSpan.TicksPerMillisecond == 0)
{
scale = TimeSpanScale.Milliseconds;
value /= TimeSpan.TicksPerMillisecond;
}
else
{
scale = TimeSpanScale.Ticks;
}
SubItemToken token = ProtoWriter.StartSubItem(null, dest);
if(value != 0) {
ProtoWriter.WriteFieldHeader(FieldTimeSpanValue, WireType.SignedVariant, dest);
ProtoWriter.WriteInt64(value, dest);
}
if(scale != TimeSpanScale.Days) {
ProtoWriter.WriteFieldHeader(FieldTimeSpanScale, WireType.Variant, dest);
ProtoWriter.WriteInt32((int)scale, dest);
}
ProtoWriter.EndSubItem(token, dest);
}
示例4: WriteRecursionSafeObject
/// <summary>
/// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type) - but the
/// caller is asserting that this relationship is non-recursive; no recursion check will be
/// performed.
/// </summary>
/// <param name="value">The object to write.</param>
/// <param name="key">The key that uniquely identifies the type within the model.</param>
/// <param name="writer">The destination.</param>
public static void WriteRecursionSafeObject(object value, int key, ProtoWriter writer)
{
if (writer.model == null)
{
throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
}
SubItemToken token = StartSubItem(null, writer);
writer.model.Serialize(key, value, writer);
EndSubItem(token, writer);
}
示例5: Serialize
public void Serialize(Stream destination, object clsObj)
{
SInstance sInstance = clsObj as SInstance;
if (sInstance == null)
{
throw new ArgumentNullException("无效CSLight脚本对象: " + clsObj);
}
using (ProtoWriter writer = new ProtoWriter(destination, null, null))
{
WriteSInstance(writer, sInstance);
writer.Close();
}
}
示例6: GetReader
static ProtoReader GetReader()
{
var model = TypeModel.Create();
model.Add(typeof(Foo), true);
model.CompileInPlace();
var ms = new MemoryStream();
var obj = new Foo { Bar = "abc", Blap = "abc" };
using (var writer = new ProtoWriter(ms, model, null))
{
writer.Model.Serialize(writer, obj);
}
ms.Position = 0;
return new ProtoReader(ms, model, null);
}
示例7: Serialize
/// <summary>
/// The serialize.
/// </summary>
/// <param name="num">
/// The num.
/// </param>
/// <param name="obj">
/// The obj.
/// </param>
/// <param name="protoWriter">
/// The proto writer.
/// </param>
protected override void Serialize(int num, object obj, ProtoWriter protoWriter)
{
switch (num)
{
case 0:
Write((CompiledAsset)obj, protoWriter);
return;
case 1:
Write((PlatformData)obj, protoWriter);
return;
case 2:
Write((TargetPlatform)obj, protoWriter);
return;
default:
return;
}
}
示例8: WriteFieldHeader
/// <summary>
/// Writes a field-header, indicating the format of the next data we plan to write.
/// </summary>
public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer) {
if (writer.wireType != WireType.None) throw new InvalidOperationException("Cannot write a " + wireType
+ " header until the " + writer.wireType + " data has been written");
if(fieldNumber < 0) throw new ArgumentOutOfRangeException("fieldNumber");
#if DEBUG
switch (wireType)
{ // validate requested header-type
case WireType.Fixed32:
case WireType.Fixed64:
case WireType.String:
case WireType.StartGroup:
case WireType.SignedVariant:
case WireType.Variant:
break; // fine
case WireType.None:
case WireType.EndGroup:
default:
throw new ArgumentException("Invalid wire-type: " + wireType, "wireType");
}
#endif
if (writer.packedFieldNumber == 0) {
writer.fieldNumber = fieldNumber;
writer.wireType = wireType;
WriteHeaderCore(fieldNumber, wireType, writer);
}
else if (writer.packedFieldNumber == fieldNumber)
{ // we'll set things up, but note we *don't* actually write the header here
switch (wireType)
{
case WireType.Fixed32:
case WireType.Fixed64:
case WireType.Variant:
case WireType.SignedVariant:
break; // fine
default:
throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType);
}
writer.fieldNumber = fieldNumber;
writer.wireType = wireType;
}
else
{
throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber + " but received " + fieldNumber);
}
}
示例9: AppendExtensionData
/// <summary>
/// Copies any extension data stored for the instance to the underlying stream
/// </summary>
public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
{
if (instance == null) throw new ArgumentNullException("instance");
// we expect the writer to be raw here; the extension data will have the
// header detail, so we'll copy it implicitly
if(writer.wireType != WireType.None) throw CreateException(writer);
IExtension extn = instance.GetExtensionObject(false);
if (extn != null)
{
// unusually we *don't* want "using" here; the "finally" does that, with
// the extension object being responsible for disposal etc
Stream source = extn.BeginQuery();
try
{
CopyRawFromStream(source, writer);
}
finally { extn.EndQuery(source); }
}
}
示例10: CreateException
// general purpose serialization exception message
internal static Exception CreateException(ProtoWriter writer)
{
return new ProtoException("Invalid serialization operation with wire-type " + writer.wireType + " at position " + writer.position);
}
示例11: WriteSingle
static void WriteSingle(float value, ProtoWriter writer)
{
switch (writer.wireType)
{
case WireType.Fixed32:
#if FEAT_SAFE
ProtoWriter.WriteInt32(BitConverter.ToInt32(BitConverter.GetBytes(value), 0), writer);
#else
ProtoWriter.WriteInt32(*(int*)&value, writer);
#endif
return;
case WireType.Fixed64:
ProtoWriter.WriteDouble((double)value, writer);
return;
default:
throw CreateException(writer);
}
}
示例12: WriteInt32
/// <summary>
/// Writes a signed 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
/// </summary>
public static void WriteInt32(int value, ProtoWriter writer)
{
byte[] buffer;
int index;
switch (writer.wireType)
{
case WireType.Fixed32:
DemandSpace(4, writer);
WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
IncrementedAndReset(4, writer);
return;
case WireType.Fixed64:
DemandSpace(8, writer);
buffer = writer.ioBuffer;
index = writer.ioIndex;
buffer[index] = (byte)value;
buffer[index + 1] = (byte)(value >> 8);
buffer[index + 2] = (byte)(value >> 16);
buffer[index + 3] = (byte)(value >> 24);
buffer[index + 4] = buffer[index + 5] =
buffer[index + 6] = buffer[index + 7] = 0;
IncrementedAndReset(8, writer);
return;
case WireType.SignedVariant:
WriteUInt32Variant(Zig(value), writer);
writer.wireType = WireType.None;
return;
case WireType.Variant:
if (value >= 0)
{
WriteUInt32Variant((uint)value, writer);
writer.wireType = WireType.None;
}
else
{
DemandSpace(10, writer);
buffer = writer.ioBuffer;
index = writer.ioIndex;
buffer[index] = (byte)(value | 0x80);
buffer[index + 1] = (byte)((value >> 7) | 0x80);
buffer[index + 2] = (byte)((value >> 14) | 0x80);
buffer[index + 3] = (byte)((value >> 21) | 0x80);
buffer[index + 4] = (byte)((value >> 28) | 0x80);
buffer[index + 5] = buffer[index + 6] =
buffer[index + 7] = buffer[index + 8] = (byte)0xFF;
buffer[index + 9] = (byte)0x01;
IncrementedAndReset(10, writer);
}
return;
default:
throw CreateException(writer);
}
}
示例13: WriteByte
/// <summary>
/// Writes an unsigned 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
/// </summary>
public static void WriteByte(byte value, ProtoWriter writer)
{
ProtoWriter.WriteUInt32(value, writer);
}
示例14: WriteUInt32
/// <summary>
/// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
/// </summary>
public static void WriteUInt32(uint value, ProtoWriter writer)
{
switch (writer.wireType)
{
case WireType.Fixed32:
ProtoWriter.WriteInt32((int)value, writer);
return;
case WireType.Fixed64:
ProtoWriter.WriteInt64((int)value, writer);
return;
case WireType.Variant:
WriteUInt32Variant(value, writer);
writer.wireType = WireType.None;
return;
default:
throw CreateException(writer);
}
}
示例15: StartSubItem
private static SubItemToken StartSubItem(object instance, ProtoWriter writer, bool allowFixed)
{
if (++writer.depth > RecursionCheckDepth)
{
writer.CheckRecursionStackAndPush(instance);
}
if(writer.packedFieldNumber != 0) throw new InvalidOperationException("Cannot begin a sub-item while performing packed encoding");
switch (writer.wireType)
{
case WireType.StartGroup:
writer.wireType = WireType.None;
return new SubItemToken(-writer.fieldNumber);
case WireType.String:
writer.wireType = WireType.None;
DemandSpace(32, writer); // make some space in anticipation...
writer.flushLock++;
writer.position++;
return new SubItemToken(writer.ioIndex++); // leave 1 space (optimistic) for length
case WireType.Fixed32:
{
if (!allowFixed) throw CreateException(writer);
DemandSpace(32, writer); // make some space in anticipation...
writer.flushLock++;
SubItemToken token = new SubItemToken(writer.ioIndex);
ProtoWriter.IncrementedAndReset(4, writer); // leave 4 space (rigid) for length
return token;
}
default:
throw CreateException(writer);
}
}