/MonoGame.Framework/Net/PacketWriter.cs

https://bitbucket.org/refuzion/monogame · C# · 176 lines · 109 code · 22 blank · 45 comment · 2 complexity · dccc535a97f392d1dafb052ff9b94478 MD5 · raw file

  1. #region License
  2. // /*
  3. // Microsoft Public License (Ms-PL)
  4. // MonoGame - Copyright © 2009 The MonoGame Team
  5. //
  6. // All rights reserved.
  7. //
  8. // This license governs use of the accompanying software. If you use the software, you accept this license. If you do not
  9. // accept the license, do not use the software.
  10. //
  11. // 1. Definitions
  12. // The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under
  13. // U.S. copyright law.
  14. //
  15. // A "contribution" is the original software, or any additions or changes to the software.
  16. // A "contributor" is any person that distributes its contribution under this license.
  17. // "Licensed patents" are a contributor's patent claims that read directly on its contribution.
  18. //
  19. // 2. Grant of Rights
  20. // (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
  21. // each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
  22. // (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
  23. // each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
  24. //
  25. // 3. Conditions and Limitations
  26. // (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
  27. // (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
  28. // your patent license from such contributor to the software ends automatically.
  29. // (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution
  30. // notices that are present in the software.
  31. // (D) If you distribute any portion of the software in source code form, you may do so only under this license by including
  32. // a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
  33. // code form, you may only do so under a license that complies with this license.
  34. // (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
  35. // or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
  36. // permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
  37. // purpose and non-infringement.
  38. // */
  39. #endregion License
  40. #region Using clause
  41. using System;
  42. using System.IO;
  43. using Microsoft.Xna.Framework.Graphics;
  44. using Microsoft.Xna.Framework.Graphics.PackedVector;
  45. #endregion Using clause
  46. namespace Microsoft.Xna.Framework.Net
  47. {
  48. public class PacketWriter : BinaryWriter
  49. {
  50. // I thought about using an array but that means more code in my opinion and it does not make sense
  51. // since the memory stream is perfect for this.
  52. // Using a memory stream also fits nicely with the constructors see:
  53. // http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.net.packetwriter.packetwriter.aspx
  54. // We will see when testing begins.
  55. #region Constructors
  56. public PacketWriter () : this (0)
  57. {
  58. }
  59. public PacketWriter (int capacity) : base ( new MemoryStream(capacity))
  60. {
  61. }
  62. #endregion
  63. #region Methods
  64. public void Write (Color Value)
  65. {
  66. base.Write (Value.PackedValue);
  67. }
  68. public override void Write (double Value)
  69. {
  70. base.Write (Value);
  71. }
  72. public void Write (Matrix Value)
  73. {
  74. // After looking at a captured packet it looks like all the values of
  75. // the matrix are written. This is different than the Lidgren XNAExtensions
  76. base.Write (Value.M11);
  77. base.Write (Value.M12);
  78. base.Write (Value.M13);
  79. base.Write (Value.M14);
  80. base.Write (Value.M21);
  81. base.Write (Value.M22);
  82. base.Write (Value.M23);
  83. base.Write (Value.M24);
  84. base.Write (Value.M31);
  85. base.Write (Value.M32);
  86. base.Write (Value.M33);
  87. base.Write (Value.M34);
  88. base.Write (Value.M41);
  89. base.Write (Value.M42);
  90. base.Write (Value.M43);
  91. base.Write (Value.M44);
  92. }
  93. public void Write (Quaternion Value)
  94. {
  95. // This may need to be corrected as have no test for it
  96. base.Write(Value.X);
  97. base.Write(Value.Y);
  98. base.Write(Value.Z);
  99. base.Write(Value.W);
  100. }
  101. public override void Write (float Value)
  102. {
  103. base.Write(Value);
  104. }
  105. public void Write (Vector2 Value)
  106. {
  107. base.Write(Value.X);
  108. base.Write(Value.Y);
  109. }
  110. public void Write (Vector3 Value)
  111. {
  112. base.Write(Value.X);
  113. base.Write(Value.Y);
  114. base.Write(Value.Z);
  115. }
  116. public void Write (Vector4 Value)
  117. {
  118. base.Write(Value.X);
  119. base.Write(Value.Y);
  120. base.Write(Value.Z);
  121. base.Write(Value.W);
  122. }
  123. #endregion
  124. internal byte[] Data
  125. {
  126. get {
  127. MemoryStream stream = (MemoryStream)this.BaseStream;
  128. return stream.GetBuffer();
  129. }
  130. }
  131. #region Properties
  132. public int Length {
  133. get {
  134. return (int)BaseStream.Length;
  135. }
  136. }
  137. public int Position {
  138. get {
  139. return (int)BaseStream.Position;
  140. }
  141. set {
  142. if (BaseStream.Position != value)
  143. BaseStream.Position = value;
  144. }
  145. }
  146. internal void Reset()
  147. {
  148. MemoryStream stream = (MemoryStream)this.BaseStream;
  149. stream.SetLength(0);
  150. stream.Position = 0;
  151. }
  152. #endregion
  153. }
  154. }