ProtoWriter.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using ProtoBuf.Meta;
  5. #if MF
  6. using OverflowException = System.ApplicationException;
  7. #endif
  8. #if FEAT_IKVM
  9. using Type = IKVM.Reflection.Type;
  10. #endif
  11. namespace ProtoBuf
  12. {
  13. /// <summary>
  14. /// Represents an output stream for writing protobuf data.
  15. ///
  16. /// Why is the API backwards (static methods with writer arguments)?
  17. /// See: http://marcgravell.blogspot.com/2010/03/last-will-be-first-and-first-will-be.html
  18. /// </summary>
  19. public sealed class ProtoWriter : IDisposable
  20. {
  21. private Stream dest;
  22. TypeModel model;
  23. /// <summary>
  24. /// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type).
  25. /// </summary>
  26. /// <param name="value">The object to write.</param>
  27. /// <param name="key">The key that uniquely identifies the type within the model.</param>
  28. /// <param name="writer">The destination.</param>
  29. public static void WriteObject(object value, int key, ProtoWriter writer)
  30. {
  31. #if FEAT_IKVM
  32. throw new NotSupportedException();
  33. #else
  34. if (writer == null) throw new ArgumentNullException("writer");
  35. if (writer.model == null)
  36. {
  37. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  38. }
  39. SubItemToken token = StartSubItem(value, writer);
  40. if (key >= 0)
  41. {
  42. writer.model.Serialize(key, value, writer);
  43. }
  44. else if (writer.model != null && writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, Serializer.ListItemTag, value, false))
  45. {
  46. // all ok
  47. }
  48. else
  49. {
  50. TypeModel.ThrowUnexpectedType(value.GetType());
  51. }
  52. EndSubItem(token, writer);
  53. #endif
  54. }
  55. /// <summary>
  56. /// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type) - but the
  57. /// caller is asserting that this relationship is non-recursive; no recursion check will be
  58. /// performed.
  59. /// </summary>
  60. /// <param name="value">The object to write.</param>
  61. /// <param name="key">The key that uniquely identifies the type within the model.</param>
  62. /// <param name="writer">The destination.</param>
  63. public static void WriteRecursionSafeObject(object value, int key, ProtoWriter writer)
  64. {
  65. if (writer == null) throw new ArgumentNullException("writer");
  66. if (writer.model == null)
  67. {
  68. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  69. }
  70. SubItemToken token = StartSubItem(null, writer);
  71. writer.model.Serialize(key, value, writer);
  72. EndSubItem(token, writer);
  73. }
  74. internal static void WriteObject(object value, int key, ProtoWriter writer, PrefixStyle style, int fieldNumber)
  75. {
  76. #if FEAT_IKVM
  77. throw new NotSupportedException();
  78. #else
  79. if (writer.model == null)
  80. {
  81. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  82. }
  83. if (writer.wireType != WireType.None) throw ProtoWriter.CreateException(writer);
  84. switch (style)
  85. {
  86. case PrefixStyle.Base128:
  87. writer.wireType = WireType.String;
  88. writer.fieldNumber = fieldNumber;
  89. if (fieldNumber > 0) WriteHeaderCore(fieldNumber, WireType.String, writer);
  90. break;
  91. case PrefixStyle.Fixed32:
  92. case PrefixStyle.Fixed32BigEndian:
  93. writer.fieldNumber = 0;
  94. writer.wireType = WireType.Fixed32;
  95. break;
  96. default:
  97. throw new ArgumentOutOfRangeException("style");
  98. }
  99. SubItemToken token = StartSubItem(value, writer, true);
  100. if (key < 0)
  101. {
  102. if (!writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, Serializer.ListItemTag, value, false))
  103. {
  104. TypeModel.ThrowUnexpectedType(value.GetType());
  105. }
  106. }
  107. else
  108. {
  109. writer.model.Serialize(key, value, writer);
  110. }
  111. EndSubItem(token, writer, style);
  112. #endif
  113. }
  114. internal int GetTypeKey(ref Type type)
  115. {
  116. return model.GetKey(ref type);
  117. }
  118. private readonly NetObjectCache netCache = new NetObjectCache();
  119. internal NetObjectCache NetCache
  120. {
  121. get { return netCache;}
  122. }
  123. private int fieldNumber, flushLock;
  124. WireType wireType;
  125. internal WireType WireType { get { return wireType; } }
  126. /// <summary>
  127. /// Writes a field-header, indicating the format of the next data we plan to write.
  128. /// </summary>
  129. public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer) {
  130. if (writer == null) throw new ArgumentNullException("writer");
  131. if (writer.wireType != WireType.None) throw new InvalidOperationException("Cannot write a " + wireType.ToString()
  132. + " header until the " + writer.wireType.ToString() + " data has been written");
  133. if(fieldNumber < 0) throw new ArgumentOutOfRangeException("fieldNumber");
  134. #if DEBUG
  135. switch (wireType)
  136. { // validate requested header-type
  137. case WireType.Fixed32:
  138. case WireType.Fixed64:
  139. case WireType.String:
  140. case WireType.StartGroup:
  141. case WireType.SignedVariant:
  142. case WireType.Variant:
  143. break; // fine
  144. case WireType.None:
  145. case WireType.EndGroup:
  146. default:
  147. throw new ArgumentException("Invalid wire-type: " + wireType.ToString(), "wireType");
  148. }
  149. #endif
  150. if (writer.packedFieldNumber == 0) {
  151. writer.fieldNumber = fieldNumber;
  152. writer.wireType = wireType;
  153. WriteHeaderCore(fieldNumber, wireType, writer);
  154. }
  155. else if (writer.packedFieldNumber == fieldNumber)
  156. { // we'll set things up, but note we *don't* actually write the header here
  157. switch (wireType)
  158. {
  159. case WireType.Fixed32:
  160. case WireType.Fixed64:
  161. case WireType.Variant:
  162. case WireType.SignedVariant:
  163. break; // fine
  164. default:
  165. throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString());
  166. }
  167. writer.fieldNumber = fieldNumber;
  168. writer.wireType = wireType;
  169. }
  170. else
  171. {
  172. throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString());
  173. }
  174. }
  175. internal static void WriteHeaderCore(int fieldNumber, WireType wireType, ProtoWriter writer)
  176. {
  177. uint header = (((uint)fieldNumber) << 3)
  178. | (((uint)wireType) & 7);
  179. WriteUInt32Variant(header, writer);
  180. }
  181. /// <summary>
  182. /// Writes a byte-array to the stream; supported wire-types: String
  183. /// </summary>
  184. public static void WriteBytes(byte[] data, ProtoWriter writer)
  185. {
  186. if (data == null) throw new ArgumentNullException("data");
  187. ProtoWriter.WriteBytes(data, 0, data.Length, writer);
  188. }
  189. /// <summary>
  190. /// Writes a byte-array to the stream; supported wire-types: String
  191. /// </summary>
  192. public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
  193. {
  194. if (data == null) throw new ArgumentNullException("data");
  195. if (writer == null) throw new ArgumentNullException("writer");
  196. switch (writer.wireType)
  197. {
  198. case WireType.Fixed32:
  199. if (length != 4) throw new ArgumentException("length");
  200. goto CopyFixedLength; // ugly but effective
  201. case WireType.Fixed64:
  202. if (length != 8) throw new ArgumentException("length");
  203. goto CopyFixedLength; // ugly but effective
  204. case WireType.String:
  205. WriteUInt32Variant((uint)length, writer);
  206. writer.wireType = WireType.None;
  207. if (length == 0) return;
  208. if (writer.flushLock != 0 || length <= writer.ioBuffer.Length) // write to the buffer
  209. {
  210. goto CopyFixedLength; // ugly but effective
  211. }
  212. // writing data that is bigger than the buffer (and the buffer
  213. // isn't currently locked due to a sub-object needing the size backfilled)
  214. Flush(writer); // commit any existing data from the buffer
  215. // now just write directly to the underlying stream
  216. writer.dest.Write(data, offset, length);
  217. writer.position += length; // since we've flushed offset etc is 0, and remains
  218. // zero since we're writing directly to the stream
  219. return;
  220. }
  221. throw CreateException(writer);
  222. CopyFixedLength: // no point duplicating this lots of times, and don't really want another stackframe
  223. DemandSpace(length, writer);
  224. Helpers.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
  225. IncrementedAndReset(length, writer);
  226. }
  227. private static void CopyRawFromStream(Stream source, ProtoWriter writer)
  228. {
  229. byte[] buffer = writer.ioBuffer;
  230. int space = buffer.Length - writer.ioIndex, bytesRead = 1; // 1 here to spoof case where already full
  231. // try filling the buffer first
  232. while (space > 0 && (bytesRead = source.Read(buffer, writer.ioIndex, space)) > 0)
  233. {
  234. writer.ioIndex += bytesRead;
  235. writer.position += bytesRead;
  236. space -= bytesRead;
  237. }
  238. if (bytesRead <= 0) return; // all done using just the buffer; stream exhausted
  239. // at this point the stream still has data, but buffer is full;
  240. if (writer.flushLock == 0)
  241. {
  242. // flush the buffer and write to the underlying stream instead
  243. Flush(writer);
  244. while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
  245. {
  246. writer.dest.Write(buffer, 0, bytesRead);
  247. writer.position += bytesRead;
  248. }
  249. }
  250. else
  251. {
  252. do
  253. {
  254. // need more space; resize (double) as necessary,
  255. // requesting a reasonable minimum chunk each time
  256. // (128 is the minimum; there may actually be much
  257. // more space than this in the buffer)
  258. DemandSpace(128, writer);
  259. if((bytesRead = source.Read(writer.ioBuffer, writer.ioIndex,
  260. writer.ioBuffer.Length - writer.ioIndex)) <= 0) break;
  261. writer.position += bytesRead;
  262. writer.ioIndex += bytesRead;
  263. } while (true);
  264. }
  265. }
  266. private static void IncrementedAndReset(int length, ProtoWriter writer)
  267. {
  268. Helpers.DebugAssert(length >= 0);
  269. writer.ioIndex += length;
  270. writer.position += length;
  271. writer.wireType = WireType.None;
  272. }
  273. int depth = 0;
  274. const int RecursionCheckDepth = 25;
  275. /// <summary>
  276. /// Indicates the start of a nested record.
  277. /// </summary>
  278. /// <param name="instance">The instance to write.</param>
  279. /// <param name="writer">The destination.</param>
  280. /// <returns>A token representing the state of the stream; this token is given to EndSubItem.</returns>
  281. public static SubItemToken StartSubItem(object instance, ProtoWriter writer)
  282. {
  283. return StartSubItem(instance, writer, false);
  284. }
  285. MutableList recursionStack;
  286. private void CheckRecursionStackAndPush(object instance)
  287. {
  288. int hitLevel;
  289. if (recursionStack == null) { recursionStack = new MutableList(); }
  290. else if (instance != null && (hitLevel = recursionStack.IndexOfReference(instance)) >= 0)
  291. {
  292. #if DEBUG
  293. Helpers.DebugWriteLine("Stack:");
  294. foreach(object obj in recursionStack)
  295. {
  296. Helpers.DebugWriteLine(obj == null ? "<null>" : obj.ToString());
  297. }
  298. Helpers.DebugWriteLine(instance == null ? "<null>" : instance.ToString());
  299. #endif
  300. throw new ProtoException("Possible recursion detected (offset: " + (recursionStack.Count - hitLevel).ToString() + " level(s)): " + instance.ToString());
  301. }
  302. recursionStack.Add(instance);
  303. }
  304. private void PopRecursionStack() { recursionStack.RemoveLast(); }
  305. private static SubItemToken StartSubItem(object instance, ProtoWriter writer, bool allowFixed)
  306. {
  307. if (writer == null) throw new ArgumentNullException("writer");
  308. if (++writer.depth > RecursionCheckDepth)
  309. {
  310. writer.CheckRecursionStackAndPush(instance);
  311. }
  312. if(writer.packedFieldNumber != 0) throw new InvalidOperationException("Cannot begin a sub-item while performing packed encoding");
  313. switch (writer.wireType)
  314. {
  315. case WireType.StartGroup:
  316. writer.wireType = WireType.None;
  317. return new SubItemToken(-writer.fieldNumber);
  318. case WireType.String:
  319. #if DEBUG
  320. if(writer.model != null && writer.model.ForwardsOnly)
  321. {
  322. throw new ProtoException("Should not be buffering data");
  323. }
  324. #endif
  325. writer.wireType = WireType.None;
  326. DemandSpace(32, writer); // make some space in anticipation...
  327. writer.flushLock++;
  328. writer.position++;
  329. return new SubItemToken(writer.ioIndex++); // leave 1 space (optimistic) for length
  330. case WireType.Fixed32:
  331. {
  332. if (!allowFixed) throw CreateException(writer);
  333. DemandSpace(32, writer); // make some space in anticipation...
  334. writer.flushLock++;
  335. SubItemToken token = new SubItemToken(writer.ioIndex);
  336. ProtoWriter.IncrementedAndReset(4, writer); // leave 4 space (rigid) for length
  337. return token;
  338. }
  339. default:
  340. throw CreateException(writer);
  341. }
  342. }
  343. /// <summary>
  344. /// Indicates the end of a nested record.
  345. /// </summary>
  346. /// <param name="token">The token obtained from StartubItem.</param>
  347. /// <param name="writer">The destination.</param>
  348. public static void EndSubItem(SubItemToken token, ProtoWriter writer)
  349. {
  350. EndSubItem(token, writer, PrefixStyle.Base128);
  351. }
  352. private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
  353. {
  354. if (writer == null) throw new ArgumentNullException("writer");
  355. if (writer.wireType != WireType.None) { throw CreateException(writer); }
  356. int value = token.value;
  357. if (writer.depth <= 0) throw CreateException(writer);
  358. if (writer.depth-- > RecursionCheckDepth)
  359. {
  360. writer.PopRecursionStack();
  361. }
  362. writer.packedFieldNumber = 0; // ending the sub-item always wipes packed encoding
  363. if (value < 0)
  364. { // group - very simple append
  365. WriteHeaderCore(-value, WireType.EndGroup, writer);
  366. writer.wireType = WireType.None;
  367. return;
  368. }
  369. // so we're backfilling the length into an existing sequence
  370. int len;
  371. switch(style)
  372. {
  373. case PrefixStyle.Fixed32:
  374. len = (int)((writer.ioIndex - value) - 4);
  375. ProtoWriter.WriteInt32ToBuffer(len, writer.ioBuffer, value);
  376. break;
  377. case PrefixStyle.Fixed32BigEndian:
  378. len = (int)((writer.ioIndex - value) - 4);
  379. byte[] buffer = writer.ioBuffer;
  380. ProtoWriter.WriteInt32ToBuffer(len, buffer, value);
  381. // and swap the byte order
  382. byte b = buffer[value];
  383. buffer[value] = buffer[value + 3];
  384. buffer[value + 3] = b;
  385. b = buffer[value + 1];
  386. buffer[value + 1] = buffer[value + 2];
  387. buffer[value + 2] = b;
  388. break;
  389. case PrefixStyle.Base128:
  390. // string - complicated because we only reserved one byte;
  391. // if the prefix turns out to need more than this then
  392. // we need to shuffle the existing data
  393. len = (int)((writer.ioIndex - value) - 1);
  394. int offset = 0;
  395. uint tmp = (uint)len;
  396. while ((tmp >>= 7) != 0) offset++;
  397. if (offset == 0)
  398. {
  399. writer.ioBuffer[value] = (byte)(len & 0x7F);
  400. }
  401. else
  402. {
  403. DemandSpace(offset, writer);
  404. byte[] blob = writer.ioBuffer;
  405. Helpers.BlockCopy(blob, value + 1, blob, value + 1 + offset, len);
  406. tmp = (uint)len;
  407. do
  408. {
  409. blob[value++] = (byte)((tmp & 0x7F) | 0x80);
  410. } while ((tmp >>= 7) != 0);
  411. blob[value - 1] = (byte)(blob[value - 1] & ~0x80);
  412. writer.position += offset;
  413. writer.ioIndex += offset;
  414. }
  415. break;
  416. default:
  417. throw new ArgumentOutOfRangeException("style");
  418. }
  419. // and this object is no longer a blockage - also flush if sensible
  420. const int ADVISORY_FLUSH_SIZE = 1024;
  421. if (--writer.flushLock == 0 && writer.ioIndex >= ADVISORY_FLUSH_SIZE)
  422. {
  423. ProtoWriter.Flush(writer);
  424. }
  425. }
  426. /// <summary>
  427. /// Creates a new writer against a stream
  428. /// </summary>
  429. /// <param name="dest">The destination stream</param>
  430. /// <param name="model">The model to use for serialization; this can be null, but this will impair the ability to serialize sub-objects</param>
  431. /// <param name="context">Additional context about this serialization operation</param>
  432. public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
  433. {
  434. if (dest == null) throw new ArgumentNullException("dest");
  435. if (!dest.CanWrite) throw new ArgumentException("Cannot write to stream", "dest");
  436. //if (model == null) throw new ArgumentNullException("model");
  437. this.dest = dest;
  438. this.ioBuffer = BufferPool.GetBuffer();
  439. this.model = model;
  440. this.wireType = WireType.None;
  441. if (context == null) { context = SerializationContext.Default; }
  442. else { context.Freeze(); }
  443. this.context = context;
  444. }
  445. private readonly SerializationContext context;
  446. /// <summary>
  447. /// Addition information about this serialization operation.
  448. /// </summary>
  449. public SerializationContext Context { get { return context; } }
  450. void IDisposable.Dispose()
  451. {
  452. Dispose();
  453. }
  454. private void Dispose()
  455. { // importantly, this does **not** own the stream, and does not dispose it
  456. if (dest != null)
  457. {
  458. Flush(this);
  459. dest = null;
  460. }
  461. model = null;
  462. BufferPool.ReleaseBufferToPool(ref ioBuffer);
  463. }
  464. private byte[] ioBuffer;
  465. private int ioIndex;
  466. // note that this is used by some of the unit tests and should not be removed
  467. internal static int GetPosition(ProtoWriter writer) { return writer.position; }
  468. private int position;
  469. private static void DemandSpace(int required, ProtoWriter writer)
  470. {
  471. // check for enough space
  472. if ((writer.ioBuffer.Length - writer.ioIndex) < required)
  473. {
  474. if (writer.flushLock == 0)
  475. {
  476. Flush(writer); // try emptying the buffer
  477. if ((writer.ioBuffer.Length - writer.ioIndex) >= required) return;
  478. }
  479. // either can't empty the buffer, or that didn't help; need more space
  480. BufferPool.ResizeAndFlushLeft(ref writer.ioBuffer, required + writer.ioIndex, 0, writer.ioIndex);
  481. }
  482. }
  483. /// <summary>
  484. /// Flushes data to the underlying stream, and releases any resources. The underlying stream is *not* disposed
  485. /// by this operation.
  486. /// </summary>
  487. public void Close()
  488. {
  489. if (depth != 0 || flushLock != 0) throw new InvalidOperationException("Unable to close stream in an incomplete state");
  490. Dispose();
  491. }
  492. internal void CheckDepthFlushlock()
  493. {
  494. if (depth != 0 || flushLock != 0) throw new InvalidOperationException("The writer is in an incomplete state");
  495. }
  496. /// <summary>
  497. /// Get the TypeModel associated with this writer
  498. /// </summary>
  499. public TypeModel Model { get { return model; } }
  500. /// <summary>
  501. /// Writes any buffered data (if possible) to the underlying stream.
  502. /// </summary>
  503. /// <param name="writer">The writer to flush</param>
  504. /// <remarks>It is not always possible to fully flush, since some sequences
  505. /// may require values to be back-filled into the byte-stream.</remarks>
  506. internal static void Flush(ProtoWriter writer)
  507. {
  508. if (writer.flushLock == 0 && writer.ioIndex != 0)
  509. {
  510. writer.dest.Write(writer.ioBuffer, 0, writer.ioIndex);
  511. writer.ioIndex = 0;
  512. }
  513. }
  514. /// <summary>
  515. /// Writes an unsigned 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  516. /// </summary>
  517. private static void WriteUInt32Variant(uint value, ProtoWriter writer)
  518. {
  519. DemandSpace(5, writer);
  520. int count = 0;
  521. do {
  522. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  523. count++;
  524. } while ((value >>= 7) != 0);
  525. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  526. writer.position += count;
  527. }
  528. static readonly UTF8Encoding encoding = new UTF8Encoding();
  529. internal static uint Zig(int value)
  530. {
  531. return (uint)((value << 1) ^ (value >> 31));
  532. }
  533. internal static ulong Zig(long value)
  534. {
  535. return (ulong)((value << 1) ^ (value >> 63));
  536. }
  537. private static void WriteUInt64Variant(ulong value, ProtoWriter writer)
  538. {
  539. DemandSpace(10, writer);
  540. int count = 0;
  541. do
  542. {
  543. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  544. count++;
  545. } while ((value >>= 7) != 0);
  546. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  547. writer.position += count;
  548. }
  549. /// <summary>
  550. /// Writes a string to the stream; supported wire-types: String
  551. /// </summary>
  552. public static void WriteString(string value, ProtoWriter writer)
  553. {
  554. if (writer == null) throw new ArgumentNullException("writer");
  555. if (writer.wireType != WireType.String) throw CreateException(writer);
  556. if (value == null) throw new ArgumentNullException("value"); // written header; now what?
  557. int len = value.Length;
  558. if (len == 0)
  559. {
  560. WriteUInt32Variant(0, writer);
  561. writer.wireType = WireType.None;
  562. return; // just a header
  563. }
  564. #if MF
  565. byte[] bytes = encoding.GetBytes(value);
  566. int actual = bytes.Length;
  567. writer.WriteUInt32Variant((uint)actual);
  568. writer.Ensure(actual);
  569. Helpers.BlockCopy(bytes, 0, writer.ioBuffer, writer.ioIndex, actual);
  570. #else
  571. int predicted = encoding.GetByteCount(value);
  572. WriteUInt32Variant((uint)predicted, writer);
  573. DemandSpace(predicted, writer);
  574. int actual = encoding.GetBytes(value, 0, value.Length, writer.ioBuffer, writer.ioIndex);
  575. Helpers.DebugAssert(predicted == actual);
  576. #endif
  577. IncrementedAndReset(actual, writer);
  578. }
  579. /// <summary>
  580. /// Writes an unsigned 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  581. /// </summary>
  582. public static void WriteUInt64(ulong value, ProtoWriter writer)
  583. {
  584. if (writer == null) throw new ArgumentNullException("writer");
  585. switch (writer.wireType)
  586. {
  587. case WireType.Fixed64:
  588. ProtoWriter.WriteInt64((long)value, writer);
  589. return;
  590. case WireType.Variant:
  591. WriteUInt64Variant(value, writer);
  592. writer.wireType = WireType.None;
  593. return;
  594. case WireType.Fixed32:
  595. checked { ProtoWriter.WriteUInt32((uint)value, writer); }
  596. return;
  597. default:
  598. throw CreateException(writer);
  599. }
  600. }
  601. /// <summary>
  602. /// Writes a signed 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  603. /// </summary>
  604. public static void WriteInt64(long value, ProtoWriter writer)
  605. {
  606. byte[] buffer;
  607. int index;
  608. if (writer == null) throw new ArgumentNullException("writer");
  609. switch (writer.wireType)
  610. {
  611. case WireType.Fixed64:
  612. DemandSpace(8, writer);
  613. buffer = writer.ioBuffer;
  614. index = writer.ioIndex;
  615. buffer[index] = (byte)value;
  616. buffer[index + 1] = (byte)(value >> 8);
  617. buffer[index + 2] = (byte)(value >> 16);
  618. buffer[index + 3] = (byte)(value >> 24);
  619. buffer[index + 4] = (byte)(value >> 32);
  620. buffer[index + 5] = (byte)(value >> 40);
  621. buffer[index + 6] = (byte)(value >> 48);
  622. buffer[index + 7] = (byte)(value >> 56);
  623. IncrementedAndReset(8, writer);
  624. return;
  625. case WireType.SignedVariant:
  626. WriteUInt64Variant(Zig(value), writer);
  627. writer.wireType = WireType.None;
  628. return;
  629. case WireType.Variant:
  630. if (value >= 0)
  631. {
  632. WriteUInt64Variant((ulong)value, writer);
  633. writer.wireType = WireType.None;
  634. }
  635. else
  636. {
  637. DemandSpace(10, writer);
  638. buffer = writer.ioBuffer;
  639. index = writer.ioIndex;
  640. buffer[index] = (byte)(value | 0x80);
  641. buffer[index + 1] = (byte)((int)(value >> 7) | 0x80);
  642. buffer[index + 2] = (byte)((int)(value >> 14) | 0x80);
  643. buffer[index + 3] = (byte)((int)(value >> 21) | 0x80);
  644. buffer[index + 4] = (byte)((int)(value >> 28) | 0x80);
  645. buffer[index + 5] = (byte)((int)(value >> 35) | 0x80);
  646. buffer[index + 6] = (byte)((int)(value >> 42) | 0x80);
  647. buffer[index + 7] = (byte)((int)(value >> 49) | 0x80);
  648. buffer[index + 8] = (byte)((int)(value >> 56) | 0x80);
  649. buffer[index + 9] = 0x01; // sign bit
  650. IncrementedAndReset(10, writer);
  651. }
  652. return;
  653. case WireType.Fixed32:
  654. checked { WriteInt32((int)value, writer); }
  655. return;
  656. default:
  657. throw CreateException(writer);
  658. }
  659. }
  660. /// <summary>
  661. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  662. /// </summary>
  663. public static void WriteUInt32(uint value, ProtoWriter writer)
  664. {
  665. if (writer == null) throw new ArgumentNullException("writer");
  666. switch (writer.wireType)
  667. {
  668. case WireType.Fixed32:
  669. ProtoWriter.WriteInt32((int)value, writer);
  670. return;
  671. case WireType.Fixed64:
  672. ProtoWriter.WriteInt64((int)value, writer);
  673. return;
  674. case WireType.Variant:
  675. WriteUInt32Variant(value, writer);
  676. writer.wireType = WireType.None;
  677. return;
  678. default:
  679. throw CreateException(writer);
  680. }
  681. }
  682. /// <summary>
  683. /// Writes a signed 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  684. /// </summary>
  685. public static void WriteInt16(short value, ProtoWriter writer)
  686. {
  687. ProtoWriter.WriteInt32(value, writer);
  688. }
  689. /// <summary>
  690. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  691. /// </summary>
  692. public static void WriteUInt16(ushort value, ProtoWriter writer)
  693. {
  694. ProtoWriter.WriteUInt32(value, writer);
  695. }
  696. /// <summary>
  697. /// Writes an unsigned 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  698. /// </summary>
  699. public static void WriteByte(byte value, ProtoWriter writer)
  700. {
  701. ProtoWriter.WriteUInt32(value, writer);
  702. }
  703. /// <summary>
  704. /// Writes a signed 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  705. /// </summary>
  706. public static void WriteSByte(sbyte value, ProtoWriter writer)
  707. {
  708. ProtoWriter.WriteInt32(value, writer);
  709. }
  710. private static void WriteInt32ToBuffer(int value, byte[] buffer, int index)
  711. {
  712. buffer[index] = (byte)value;
  713. buffer[index + 1] = (byte)(value >> 8);
  714. buffer[index + 2] = (byte)(value >> 16);
  715. buffer[index + 3] = (byte)(value >> 24);
  716. }
  717. /// <summary>
  718. /// Writes a signed 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  719. /// </summary>
  720. public static void WriteInt32(int value, ProtoWriter writer)
  721. {
  722. byte[] buffer;
  723. int index;
  724. if (writer == null) throw new ArgumentNullException("writer");
  725. switch (writer.wireType)
  726. {
  727. case WireType.Fixed32:
  728. DemandSpace(4, writer);
  729. WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
  730. IncrementedAndReset(4, writer);
  731. return;
  732. case WireType.Fixed64:
  733. DemandSpace(8, writer);
  734. buffer = writer.ioBuffer;
  735. index = writer.ioIndex;
  736. buffer[index] = (byte)value;
  737. buffer[index + 1] = (byte)(value >> 8);
  738. buffer[index + 2] = (byte)(value >> 16);
  739. buffer[index + 3] = (byte)(value >> 24);
  740. buffer[index + 4] = buffer[index + 5] =
  741. buffer[index + 6] = buffer[index + 7] = 0;
  742. IncrementedAndReset(8, writer);
  743. return;
  744. case WireType.SignedVariant:
  745. WriteUInt32Variant(Zig(value), writer);
  746. writer.wireType = WireType.None;
  747. return;
  748. case WireType.Variant:
  749. if (value >= 0)
  750. {
  751. WriteUInt32Variant((uint)value, writer);
  752. writer.wireType = WireType.None;
  753. }
  754. else
  755. {
  756. DemandSpace(10, writer);
  757. buffer = writer.ioBuffer;
  758. index = writer.ioIndex;
  759. buffer[index] = (byte)(value | 0x80);
  760. buffer[index + 1] = (byte)((value >> 7) | 0x80);
  761. buffer[index + 2] = (byte)((value >> 14) | 0x80);
  762. buffer[index + 3] = (byte)((value >> 21) | 0x80);
  763. buffer[index + 4] = (byte)((value >> 28) | 0x80);
  764. buffer[index + 5] = buffer[index + 6] =
  765. buffer[index + 7] = buffer[index + 8] = (byte)0xFF;
  766. buffer[index + 9] = (byte)0x01;
  767. IncrementedAndReset(10, writer);
  768. }
  769. return;
  770. default:
  771. throw CreateException(writer);
  772. }
  773. }
  774. /// <summary>
  775. /// Writes a double-precision number to the stream; supported wire-types: Fixed32, Fixed64
  776. /// </summary>
  777. public static void WriteDouble(double value, ProtoWriter writer)
  778. {
  779. if (writer == null) throw new ArgumentNullException("writer");
  780. switch (writer.wireType)
  781. {
  782. case WireType.Fixed32:
  783. float f = (float)value;
  784. if (Helpers.IsInfinity(f)
  785. && !Helpers.IsInfinity(value))
  786. {
  787. throw new OverflowException();
  788. }
  789. ProtoWriter.WriteSingle(f, writer);
  790. return;
  791. case WireType.Fixed64:
  792. ProtoWriter.WriteInt64(BitConverter.ToInt64(BitConverter.GetBytes(value), 0), writer);
  793. return;
  794. default:
  795. throw CreateException(writer);
  796. }
  797. }
  798. /// <summary>
  799. /// Writes a single-precision number to the stream; supported wire-types: Fixed32, Fixed64
  800. /// </summary>
  801. public
  802. static void WriteSingle(float value, ProtoWriter writer)
  803. {
  804. if (writer == null) throw new ArgumentNullException("writer");
  805. switch (writer.wireType)
  806. {
  807. case WireType.Fixed32:
  808. ProtoWriter.WriteInt32(BitConverter.ToInt32(BitConverter.GetBytes(value), 0), writer);
  809. return;
  810. case WireType.Fixed64:
  811. ProtoWriter.WriteDouble((double)value, writer);
  812. return;
  813. default:
  814. throw CreateException(writer);
  815. }
  816. }
  817. /// <summary>
  818. /// Throws an exception indicating that the given enum cannot be mapped to a serialized value.
  819. /// </summary>
  820. public static void ThrowEnumException(ProtoWriter writer, object enumValue)
  821. {
  822. if (writer == null) throw new ArgumentNullException("writer");
  823. string rhs = enumValue == null ? "<null>" : (enumValue.GetType().FullName + "." + enumValue.ToString());
  824. throw new ProtoException("No wire-value is mapped to the enum " + rhs + " at position " + writer.position.ToString());
  825. }
  826. // general purpose serialization exception message
  827. internal static Exception CreateException(ProtoWriter writer)
  828. {
  829. if (writer == null) throw new ArgumentNullException("writer");
  830. return new ProtoException("Invalid serialization operation with wire-type " + writer.wireType.ToString() + " at position " + writer.position.ToString());
  831. }
  832. /// <summary>
  833. /// Writes a boolean to the stream; supported wire-types: Variant, Fixed32, Fixed64
  834. /// </summary>
  835. public static void WriteBoolean(bool value, ProtoWriter writer)
  836. {
  837. ProtoWriter.WriteUInt32(value ? (uint)1 : (uint)0, writer);
  838. }
  839. /// <summary>
  840. /// Copies any extension data stored for the instance to the underlying stream
  841. /// </summary>
  842. public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
  843. {
  844. if (instance == null) throw new ArgumentNullException("instance");
  845. if (writer == null) throw new ArgumentNullException("writer");
  846. // we expect the writer to be raw here; the extension data will have the
  847. // header detail, so we'll copy it implicitly
  848. if(writer.wireType != WireType.None) throw CreateException(writer);
  849. IExtension extn = instance.GetExtensionObject(false);
  850. if (extn != null)
  851. {
  852. // unusually we *don't* want "using" here; the "finally" does that, with
  853. // the extension object being responsible for disposal etc
  854. Stream source = extn.BeginQuery();
  855. try
  856. {
  857. CopyRawFromStream(source, writer);
  858. }
  859. finally { extn.EndQuery(source); }
  860. }
  861. }
  862. private int packedFieldNumber;
  863. /// <summary>
  864. /// Used for packed encoding; indicates that the next field should be skipped rather than
  865. /// a field header written. Note that the field number must match, else an exception is thrown
  866. /// when the attempt is made to write the (incorrect) field. The wire-type is taken from the
  867. /// subsequent call to WriteFieldHeader. Only primitive types can be packed.
  868. /// </summary>
  869. public static void SetPackedField(int fieldNumber, ProtoWriter writer)
  870. {
  871. if (fieldNumber <= 0) throw new ArgumentOutOfRangeException("fieldNumber");
  872. if (writer == null) throw new ArgumentNullException("writer");
  873. writer.packedFieldNumber = fieldNumber;
  874. }
  875. internal string SerializeType(System.Type type)
  876. {
  877. return TypeModel.SerializeType(model, type);
  878. }
  879. /// <summary>
  880. /// Specifies a known root object to use during reference-tracked serialization
  881. /// </summary>
  882. public void SetRootObject(object value)
  883. {
  884. NetCache.SetKeyedObject(NetObjectCache.Root, value);
  885. }
  886. /// <summary>
  887. /// Writes a Type to the stream, using the model's DynamicTypeFormatting if appropriate; supported wire-types: String
  888. /// </summary>
  889. public static void WriteType(System.Type value, ProtoWriter writer)
  890. {
  891. if (writer == null) throw new ArgumentNullException("writer");
  892. WriteString(writer.SerializeType(value), writer);
  893. }
  894. }
  895. }