4 using System.IO.Compression;
5 using System.Runtime.InteropServices;
7 using Microsoft.Xna.Framework;
11 public static class MsgConstants
16 public const int MTU = 1170;
18 public const int CompressionThreshold = 1000;
19 public const int InitialBufferSize = 256;
20 public const int BufferOverAllocateAmount = 4;
26 [StructLayout(LayoutKind.Explicit)]
42 internal static class MsgWriter
44 internal static void UpdateBitLength(ref
int bitLength,
int bitPos)
46 bitLength = Math.Max(bitLength, bitPos);
49 internal static void WriteBoolean(ref
byte[] buf, ref
int bitPos, ref
int bitLength,
bool val)
52 int resetPos = bitPos;
55 EnsureBufferSize(ref buf, bitPos + 1);
57 int bytePos = bitPos / 8;
58 int bitOffset = bitPos % 8;
59 byte bitFlag = (byte)(1 << bitOffset);
60 byte bitMask = (byte)((~bitFlag) & 0xff);
61 buf[bytePos] &= bitMask;
62 if (val) buf[bytePos] |= bitFlag;
64 UpdateBitLength(ref bitLength, bitPos);
66 bool testVal = MsgReader.ReadBoolean(buf, ref resetPos);
67 if (testVal != val || resetPos != bitPos)
69 DebugConsole.ThrowError($
"Boolean written incorrectly! {testVal}, {val}; {resetPos}, {bitPos}");
74 internal static void WritePadBits(ref
byte[] buf, ref
int bitPos, ref
int bitLength)
76 int bitOffset = bitPos % 8;
77 bitPos += ((8 - bitOffset) % 8);
78 UpdateBitLength(ref bitLength, bitPos);
79 EnsureBufferSize(ref buf, bitPos);
82 internal static void WriteByte(ref
byte[] buf, ref
int bitPos, ref
int bitLength,
byte val)
84 EnsureBufferSize(ref buf, bitPos + 8);
85 NetBitWriter.WriteByte(val, 8, buf, bitPos);
87 UpdateBitLength(ref bitLength, bitPos);
90 internal static void WriteUInt16(ref
byte[] buf, ref
int bitPos, ref
int bitLength, UInt16 val)
92 EnsureBufferSize(ref buf, bitPos + 16);
93 NetBitWriter.WriteUInt16(val, 16, buf, bitPos);
95 UpdateBitLength(ref bitLength, bitPos);
98 internal static void WriteInt16(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Int16 val)
100 EnsureBufferSize(ref buf, bitPos + 16);
101 NetBitWriter.WriteUInt16((UInt16)val, 16, buf, bitPos);
103 UpdateBitLength(ref bitLength, bitPos);
106 internal static void WriteUInt32(ref
byte[] buf, ref
int bitPos, ref
int bitLength, UInt32 val)
108 EnsureBufferSize(ref buf, bitPos + 32);
109 NetBitWriter.WriteUInt32(val, 32, buf, bitPos);
111 UpdateBitLength(ref bitLength, bitPos);
114 internal static void WriteInt32(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Int32 val)
116 EnsureBufferSize(ref buf, bitPos + 32);
117 NetBitWriter.WriteUInt32((UInt32)val, 32, buf, bitPos);
119 UpdateBitLength(ref bitLength, bitPos);
122 internal static void WriteUInt64(ref
byte[] buf, ref
int bitPos, ref
int bitLength, UInt64 val)
124 EnsureBufferSize(ref buf, bitPos + 64);
125 NetBitWriter.WriteUInt64(val, 64, buf, bitPos);
127 UpdateBitLength(ref bitLength, bitPos);
130 internal static void WriteInt64(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Int64 val)
132 EnsureBufferSize(ref buf, bitPos + 64);
133 NetBitWriter.WriteUInt64((UInt64)val, 64, buf, bitPos);
135 UpdateBitLength(ref bitLength, bitPos);
138 internal static void WriteSingle(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Single val)
143 su.SingleValue = val;
145 EnsureBufferSize(ref buf, bitPos + 32);
147 NetBitWriter.WriteUInt32(su.UIntValue, 32, buf, bitPos);
149 UpdateBitLength(ref bitLength, bitPos);
152 internal static void WriteDouble(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Double val)
154 EnsureBufferSize(ref buf, bitPos + 64);
156 byte[] bytes = BitConverter.GetBytes(val);
157 WriteBytes(ref buf, ref bitPos, ref bitLength, bytes, 0, 8);
160 internal static void WriteColorR8G8B8(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Color val)
162 EnsureBufferSize(ref buf, bitPos + 24);
164 WriteByte(ref buf, ref bitPos, ref bitLength, val.R);
165 WriteByte(ref buf, ref bitPos, ref bitLength, val.G);
166 WriteByte(ref buf, ref bitPos, ref bitLength, val.B);
169 internal static void WriteColorR8G8B8A8(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Color val)
171 EnsureBufferSize(ref buf, bitPos + 32);
173 WriteByte(ref buf, ref bitPos, ref bitLength, val.R);
174 WriteByte(ref buf, ref bitPos, ref bitLength, val.G);
175 WriteByte(ref buf, ref bitPos, ref bitLength, val.B);
176 WriteByte(ref buf, ref bitPos, ref bitLength, val.A);
179 internal static void WriteString(ref
byte[] buf, ref
int bitPos, ref
int bitLength,
string val)
181 if (
string.IsNullOrEmpty(val))
183 WriteVariableUInt32(ref buf, ref bitPos, ref bitLength, 0u);
187 byte[] bytes = Encoding.UTF8.GetBytes(val);
188 WriteVariableUInt32(ref buf, ref bitPos, ref bitLength, (uint)bytes.Length);
189 WriteBytes(ref buf, ref bitPos, ref bitLength, bytes, 0, bytes.Length);
192 internal static void WriteVariableUInt32(ref
byte[] buf, ref
int bitPos, ref
int bitLength, uint value)
194 uint remainingValue = value;
195 while (remainingValue >= 0x80)
197 WriteByte(ref buf, ref bitPos, ref bitLength, (
byte)(remainingValue | 0x80));
198 remainingValue >>= 7;
201 WriteByte(ref buf, ref bitPos, ref bitLength, (
byte)remainingValue);
204 internal static void WriteRangedInteger(ref
byte[] buf, ref
int bitPos, ref
int bitLength,
int val,
int min,
int max)
206 uint range = (uint)(max - min);
207 int numberOfBits = NetUtility.BitsToHoldUInt(range);
209 EnsureBufferSize(ref buf, bitPos + numberOfBits);
211 uint rvalue = (uint)(val - min);
212 NetBitWriter.WriteUInt32(rvalue, numberOfBits, buf, bitPos);
213 bitPos += numberOfBits;
214 UpdateBitLength(ref bitLength, bitPos);
217 internal static void WriteRangedSingle(ref
byte[] buf, ref
int bitPos, ref
int bitLength, Single val, Single min, Single max,
int numberOfBits)
219 float range = max - min;
220 float unit = ((val - min) / range);
221 int maxVal = (1 << numberOfBits) - 1;
223 EnsureBufferSize(ref buf, bitPos + numberOfBits);
225 NetBitWriter.WriteUInt32((UInt32)(maxVal * unit), numberOfBits, buf, bitPos);
226 bitPos += numberOfBits;
227 UpdateBitLength(ref bitLength, bitPos);
230 internal static void WriteBytes(ref
byte[] buf, ref
int bitPos, ref
int bitLength,
byte[] val,
int pos,
int length)
232 EnsureBufferSize(ref buf, bitPos + length * 8);
233 NetBitWriter.WriteBytes(val, pos, length, buf, bitPos);
234 bitPos += length * 8;
235 UpdateBitLength(ref bitLength, bitPos);
238 internal static void EnsureBufferSize(ref
byte[] buf,
int numberOfBits)
240 int byteLen = (numberOfBits + 7) / 8;
243 buf =
new byte[byteLen + MsgConstants.BufferOverAllocateAmount];
247 if (buf.Length < byteLen)
249 Array.Resize(ref buf, byteLen + MsgConstants.BufferOverAllocateAmount);
254 internal static class MsgReader
256 internal static bool ReadBoolean(
byte[] buf, ref
int bitPos)
258 byte retval = NetBitWriter.ReadByte(buf, 1, bitPos);
263 internal static void ReadPadBits(ref
int bitPos)
265 int bitOffset = bitPos % 8;
266 bitPos += (8 - bitOffset) % 8;
269 internal static byte ReadByte(
byte[] buf, ref
int bitPos)
271 byte retval = NetBitWriter.ReadByte(buf, 8, bitPos);
276 internal static byte PeekByte(
byte[] buf, ref
int bitPos)
278 byte retval = NetBitWriter.ReadByte(buf, 8, bitPos);
282 internal static UInt16 ReadUInt16(
byte[] buf, ref
int bitPos)
284 uint retval = NetBitWriter.ReadUInt16(buf, 16, bitPos);
286 return (ushort)retval;
289 internal static Int16 ReadInt16(
byte[] buf, ref
int bitPos)
291 return (Int16)ReadUInt16(buf, ref bitPos);
294 internal static UInt32 ReadUInt32(
byte[] buf, ref
int bitPos)
296 uint retval = NetBitWriter.ReadUInt32(buf, 32, bitPos);
301 internal static Int32 ReadInt32(
byte[] buf, ref
int bitPos)
303 return (Int32)ReadUInt32(buf, ref bitPos);
306 internal static UInt64 ReadUInt64(
byte[] buf, ref
int bitPos)
308 ulong low = NetBitWriter.ReadUInt32(buf, 32, bitPos);
310 ulong high = NetBitWriter.ReadUInt32(buf, 32, bitPos);
311 ulong retval = low + (high << 32);
316 internal static Int64 ReadInt64(
byte[] buf, ref
int bitPos)
318 return (Int64)ReadUInt64(buf, ref bitPos);
321 internal static Single ReadSingle(
byte[] buf, ref
int bitPos)
323 if ((bitPos & 7) == 0)
325 float retval = BitConverter.ToSingle(buf, bitPos >> 3);
330 byte[] bytes = ReadBytes(buf, ref bitPos, 4);
331 return BitConverter.ToSingle(bytes, 0);
334 internal static Double ReadDouble(
byte[] buf, ref
int bitPos)
336 if ((bitPos & 7) == 0)
339 double retval = BitConverter.ToDouble(buf, bitPos >> 3);
344 byte[] bytes = ReadBytes(buf, ref bitPos, 8);
345 return BitConverter.ToDouble(bytes, 0);
348 internal static Color ReadColorR8G8B8(
byte[] buf, ref
int bitPos)
350 byte r = ReadByte(buf, ref bitPos);
351 byte g = ReadByte(buf, ref bitPos);
352 byte b = ReadByte(buf, ref bitPos);
353 return new Color(r, g, b, (
byte)255);
356 internal static Color ReadColorR8G8B8A8(
byte[] buf, ref
int bitPos)
358 byte r = ReadByte(buf, ref bitPos);
359 byte g = ReadByte(buf, ref bitPos);
360 byte b = ReadByte(buf, ref bitPos);
361 byte a = ReadByte(buf, ref bitPos);
362 return new Color(r, g, b, a);
365 internal static UInt32 ReadVariableUInt32(
byte[] buf, ref
int bitPos)
367 int bitLength = buf.Length * 8;
371 while (bitLength - bitPos >= 8)
373 byte chunk = ReadByte(buf, ref bitPos);
374 result |= (chunk & 0x7f) << shift;
376 if ((chunk & 0x80) == 0) {
return (uint)result; }
383 internal static String ReadString(
byte[] buf, ref
int bitPos)
385 int bitLength = buf.Length * 8;
386 int byteLen = (int)ReadVariableUInt32(buf, ref bitPos);
388 if (byteLen <= 0) {
return String.Empty; }
390 if ((ulong)(bitLength - bitPos) < ((ulong)byteLen * 8))
396 if ((bitPos & 7) == 0)
399 string retval = Encoding.UTF8.GetString(buf, bitPos >> 3, byteLen);
400 bitPos += (8 * byteLen);
404 byte[] bytes = ReadBytes(buf, ref bitPos, byteLen);
405 return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
408 internal static int ReadRangedInteger(
byte[] buf, ref
int bitPos,
int min,
int max)
410 uint range = (uint)(max - min);
411 int numBits = NetUtility.BitsToHoldUInt(range);
413 uint rvalue = NetBitWriter.ReadUInt32(buf, numBits, bitPos);
416 return (
int)(min + rvalue);
419 internal static Single ReadRangedSingle(
byte[] buf, ref
int bitPos, Single min, Single max,
int bitCount)
421 int maxInt = (1 << bitCount) - 1;
422 int intVal = ReadRangedInteger(buf, ref bitPos, 0, maxInt);
423 Single range = max - min;
424 return min + range * intVal / maxInt;
427 internal static byte[] ReadBytes(
byte[] buf, ref
int bitPos,
int numberOfBytes)
429 byte[] retval =
new byte[numberOfBytes];
430 NetBitWriter.ReadBytes(buf, numberOfBytes, bitPos, retval, 0);
431 bitPos += 8 * numberOfBytes;
436 internal sealed
class WriteOnlyMessage : IWriteMessage
438 private byte[] buf =
new byte[MsgConstants.InitialBufferSize];
440 private int lengthBits;
442 public int BitPosition
445 set => seekPos = value;
448 public int BytePosition => seekPos / 8;
450 public byte[] Buffer => buf;
452 public int LengthBits
456 lengthBits = seekPos > lengthBits ? seekPos : lengthBits;
462 seekPos = seekPos > lengthBits ? lengthBits : seekPos;
463 MsgWriter.EnsureBufferSize(ref buf, lengthBits);
467 public int LengthBytes => (LengthBits + 7) / 8;
469 public void WriteBoolean(
bool val)
471 MsgWriter.WriteBoolean(ref buf, ref seekPos, ref lengthBits, val);
474 public void WritePadBits()
476 MsgWriter.WritePadBits(ref buf, ref seekPos, ref lengthBits);
479 public void WriteByte(
byte val)
481 MsgWriter.WriteByte(ref buf, ref seekPos, ref lengthBits, val);
484 public void WriteUInt16(UInt16 val)
486 MsgWriter.WriteUInt16(ref buf, ref seekPos, ref lengthBits, val);
489 public void WriteInt16(Int16 val)
491 MsgWriter.WriteInt16(ref buf, ref seekPos, ref lengthBits, val);
494 public void WriteUInt32(UInt32 val)
496 MsgWriter.WriteUInt32(ref buf, ref seekPos, ref lengthBits, val);
499 public void WriteInt32(Int32 val)
501 MsgWriter.WriteInt32(ref buf, ref seekPos, ref lengthBits, val);
504 public void WriteUInt64(UInt64 val)
506 MsgWriter.WriteUInt64(ref buf, ref seekPos, ref lengthBits, val);
509 public void WriteInt64(Int64 val)
511 MsgWriter.WriteInt64(ref buf, ref seekPos, ref lengthBits, val);
514 public void WriteSingle(Single val)
516 MsgWriter.WriteSingle(ref buf, ref seekPos, ref lengthBits, val);
519 public void WriteDouble(Double val)
521 MsgWriter.WriteDouble(ref buf, ref seekPos, ref lengthBits, val);
526 MsgWriter.WriteColorR8G8B8(ref buf, ref seekPos, ref lengthBits, val);
531 MsgWriter.WriteColorR8G8B8A8(ref buf, ref seekPos, ref lengthBits, val);
534 public void WriteVariableUInt32(UInt32 val)
536 MsgWriter.WriteVariableUInt32(ref buf, ref seekPos, ref lengthBits, val);
541 MsgWriter.WriteString(ref buf, ref seekPos, ref lengthBits, val);
544 public void WriteIdentifier(Identifier val)
549 public void WriteRangedInteger(
int val,
int min,
int max)
551 MsgWriter.WriteRangedInteger(ref buf, ref seekPos, ref lengthBits, val, min, max);
554 public void WriteRangedSingle(Single val, Single min, Single max,
int bitCount)
556 MsgWriter.WriteRangedSingle(ref buf, ref seekPos, ref lengthBits, val, min, max, bitCount);
559 public void WriteBytes(
byte[] val,
int startPos,
int length)
561 MsgWriter.WriteBytes(ref buf, ref seekPos, ref lengthBits, val, startPos, length);
564 public byte[] PrepareForSending(
bool compressPastThreshold, out
bool isCompressed, out
int length)
567 if (LengthBytes <= MsgConstants.CompressionThreshold || !compressPastThreshold)
569 isCompressed =
false;
570 outBuf =
new byte[LengthBytes];
571 Array.Copy(buf, outBuf, LengthBytes);
572 length = LengthBytes;
576 using MemoryStream output =
new MemoryStream();
578 using (DeflateStream dstream =
new DeflateStream(output, CompressionLevel.Fastest))
580 dstream.Write(buf, 0, LengthBytes);
583 byte[] compressedBuf = output.ToArray();
586 if (compressedBuf.Length >= LengthBytes)
588 isCompressed =
false;
589 outBuf =
new byte[LengthBytes];
590 Array.Copy(buf, outBuf, LengthBytes);
591 length = LengthBytes;
596 outBuf = compressedBuf;
597 length = outBuf.Length;
598 DebugConsole.Log($
"Compressed message: {LengthBytes} to {length}");
606 internal sealed
class ReadOnlyMessage : IReadMessage
609 private int lengthBits;
611 public int BitPosition
614 set => seekPos = value;
617 public int BytePosition => seekPos / 8;
619 public byte[] Buffer {
get; }
621 public int LengthBits
625 lengthBits = seekPos > lengthBits ? seekPos : lengthBits;
631 seekPos = seekPos > lengthBits ? lengthBits : seekPos;
635 public int LengthBytes => (LengthBits + 7) / 8;
637 public NetworkConnection Sender {
get; }
639 public ReadOnlyMessage(
byte[] inBuf,
bool isCompressed,
int startPos,
int byteLength, NetworkConnection sender)
644 byte[] decompressedData;
645 using (MemoryStream input =
new MemoryStream(inBuf, startPos, byteLength))
647 using (MemoryStream output =
new MemoryStream())
649 using (DeflateStream dstream =
new DeflateStream(input, CompressionMode.Decompress))
651 dstream.CopyTo(output);
654 decompressedData = output.ToArray();
658 Buffer =
new byte[decompressedData.Length];
661 Array.Copy(decompressedData, 0, Buffer, 0, decompressedData.Length);
663 catch (ArgumentException e)
665 throw new ArgumentException(
666 $
"Failed to copy the incoming compressed buffer. Source buffer length: {decompressedData.Length}, start position: {0}, length: {decompressedData.Length}, destination buffer length: {Buffer.Length}.", e);
669 lengthBits = decompressedData.Length * 8;
670 DebugConsole.Log(
"Decompressing message: " + byteLength +
" to " + LengthBytes);
674 Buffer =
new byte[inBuf.Length];
677 Array.Copy(inBuf, startPos, Buffer, 0, byteLength);
679 catch (ArgumentException e)
681 throw new ArgumentException($
"Failed to copy the incoming uncompressed buffer. Source buffer length: {inBuf.Length}, start position: {startPos}, length: {byteLength}, destination buffer length: {Buffer.Length}.", e);
684 lengthBits = byteLength * 8;
690 public bool ReadBoolean()
692 return MsgReader.ReadBoolean(Buffer, ref seekPos);
695 public void ReadPadBits() { MsgReader.ReadPadBits(ref seekPos); }
697 public byte ReadByte()
699 return MsgReader.ReadByte(Buffer, ref seekPos);
702 public byte PeekByte()
704 return MsgReader.PeekByte(Buffer, ref seekPos);
707 public UInt16 ReadUInt16()
709 return MsgReader.ReadUInt16(Buffer, ref seekPos);
712 public Int16 ReadInt16()
714 return MsgReader.ReadInt16(Buffer, ref seekPos);
717 public UInt32 ReadUInt32()
719 return MsgReader.ReadUInt32(Buffer, ref seekPos);
722 public Int32 ReadInt32()
724 return MsgReader.ReadInt32(Buffer, ref seekPos);
727 public UInt64 ReadUInt64()
729 return MsgReader.ReadUInt64(Buffer, ref seekPos);
732 public Int64 ReadInt64()
734 return MsgReader.ReadInt64(Buffer, ref seekPos);
737 public Single ReadSingle()
739 return MsgReader.ReadSingle(Buffer, ref seekPos);
742 public Double ReadDouble()
744 return MsgReader.ReadDouble(Buffer, ref seekPos);
747 public UInt32 ReadVariableUInt32()
749 return MsgReader.ReadVariableUInt32(Buffer, ref seekPos);
752 public String ReadString()
754 return MsgReader.ReadString(Buffer, ref seekPos);
757 public Identifier ReadIdentifier()
759 return ReadString().ToIdentifier();
762 public Color ReadColorR8G8B8()
764 return MsgReader.ReadColorR8G8B8(Buffer, ref seekPos);
767 public Color ReadColorR8G8B8A8()
769 return MsgReader.ReadColorR8G8B8A8(Buffer, ref seekPos);
772 public int ReadRangedInteger(
int min,
int max)
774 return MsgReader.ReadRangedInteger(Buffer, ref seekPos, min, max);
777 public Single ReadRangedSingle(Single min, Single max,
int bitCount)
779 return MsgReader.ReadRangedSingle(Buffer, ref seekPos, min, max, bitCount);
782 public byte[] ReadBytes(
int numberOfBytes)
784 return MsgReader.ReadBytes(Buffer, ref seekPos, numberOfBytes);
788 internal sealed
class ReadWriteMessage : IWriteMessage, IReadMessage
792 private int lengthBits;
794 public ReadWriteMessage()
796 buf =
new byte[MsgConstants.InitialBufferSize];
801 public ReadWriteMessage(
byte[] b,
int bitPos,
int lBits,
bool copyBuf)
803 buf = copyBuf ? (
byte[])b.Clone() : b;
808 public int BitPosition
811 set => seekPos = value;
814 public int BytePosition => seekPos / 8;
816 public byte[] Buffer => buf;
818 public int LengthBits
822 lengthBits = seekPos > lengthBits ? seekPos : lengthBits;
828 seekPos = seekPos > lengthBits ? lengthBits : seekPos;
832 public int LengthBytes => (LengthBits + 7) / 8;
834 public NetworkConnection Sender =>
null;
836 public void WriteBoolean(
bool val)
838 MsgWriter.WriteBoolean(ref buf, ref seekPos, ref lengthBits, val);
841 public void WritePadBits()
843 MsgWriter.WritePadBits(ref buf, ref seekPos, ref lengthBits);
846 public void WriteByte(
byte val)
848 MsgWriter.WriteByte(ref buf, ref seekPos, ref lengthBits, val);
851 public void WriteUInt16(UInt16 val)
853 MsgWriter.WriteUInt16(ref buf, ref seekPos, ref lengthBits, val);
856 public void WriteInt16(Int16 val)
858 MsgWriter.WriteInt16(ref buf, ref seekPos, ref lengthBits, val);
861 public void WriteUInt32(UInt32 val)
863 MsgWriter.WriteUInt32(ref buf, ref seekPos, ref lengthBits, val);
866 public void WriteInt32(Int32 val)
868 MsgWriter.WriteInt32(ref buf, ref seekPos, ref lengthBits, val);
871 public void WriteUInt64(UInt64 val)
873 MsgWriter.WriteUInt64(ref buf, ref seekPos, ref lengthBits, val);
876 public void WriteInt64(Int64 val)
878 MsgWriter.WriteInt64(ref buf, ref seekPos, ref lengthBits, val);
881 public void WriteSingle(Single val)
883 MsgWriter.WriteSingle(ref buf, ref seekPos, ref lengthBits, val);
886 public void WriteDouble(Double val)
888 MsgWriter.WriteDouble(ref buf, ref seekPos, ref lengthBits, val);
893 MsgWriter.WriteColorR8G8B8(ref buf, ref seekPos, ref lengthBits, val);
898 MsgWriter.WriteColorR8G8B8A8(ref buf, ref seekPos, ref lengthBits, val);
901 public void WriteVariableUInt32(UInt32 val)
903 MsgWriter.WriteVariableUInt32(ref buf, ref seekPos, ref lengthBits, val);
908 MsgWriter.WriteString(ref buf, ref seekPos, ref lengthBits, val);
911 public void WriteIdentifier(Identifier val)
916 public void WriteRangedInteger(
int val,
int min,
int max)
918 MsgWriter.WriteRangedInteger(ref buf, ref seekPos, ref lengthBits, val, min, max);
921 public void WriteRangedSingle(Single val, Single min, Single max,
int bitCount)
923 MsgWriter.WriteRangedSingle(ref buf, ref seekPos, ref lengthBits, val, min, max, bitCount);
926 public void WriteBytes(
byte[] val,
int startPos,
int length)
928 MsgWriter.WriteBytes(ref buf, ref seekPos, ref lengthBits, val, startPos, length);
931 public bool ReadBoolean()
933 return MsgReader.ReadBoolean(buf, ref seekPos);
936 public void ReadPadBits() { MsgReader.ReadPadBits(ref seekPos); }
938 public byte ReadByte()
940 return MsgReader.ReadByte(buf, ref seekPos);
943 public byte PeekByte()
945 return MsgReader.PeekByte(buf, ref seekPos);
948 public UInt16 ReadUInt16()
950 return MsgReader.ReadUInt16(buf, ref seekPos);
953 public Int16 ReadInt16()
955 return MsgReader.ReadInt16(buf, ref seekPos);
958 public UInt32 ReadUInt32()
960 return MsgReader.ReadUInt32(buf, ref seekPos);
963 public Int32 ReadInt32()
965 return MsgReader.ReadInt32(buf, ref seekPos);
968 public UInt64 ReadUInt64()
970 return MsgReader.ReadUInt64(buf, ref seekPos);
973 public Int64 ReadInt64()
975 return MsgReader.ReadInt64(buf, ref seekPos);
978 public Single ReadSingle()
980 return MsgReader.ReadSingle(buf, ref seekPos);
983 public Double ReadDouble()
985 return MsgReader.ReadDouble(buf, ref seekPos);
988 public UInt32 ReadVariableUInt32()
990 return MsgReader.ReadVariableUInt32(buf, ref seekPos);
993 public String ReadString()
995 return MsgReader.ReadString(buf, ref seekPos);
998 public Identifier ReadIdentifier()
1000 return ReadString().ToIdentifier();
1003 public Color ReadColorR8G8B8()
1005 return MsgReader.ReadColorR8G8B8(buf, ref seekPos);
1008 public Color ReadColorR8G8B8A8()
1010 return MsgReader.ReadColorR8G8B8A8(buf, ref seekPos);
1013 public int ReadRangedInteger(
int min,
int max)
1015 return MsgReader.ReadRangedInteger(buf, ref seekPos, min, max);
1018 public Single ReadRangedSingle(Single min, Single max,
int bitCount)
1020 return MsgReader.ReadRangedSingle(buf, ref seekPos, min, max, bitCount);
1023 public byte[] ReadBytes(
int numberOfBytes)
1025 return MsgReader.ReadBytes(buf, ref seekPos, numberOfBytes);
1028 public byte[] PrepareForSending(
bool compressPastThreshold, out
bool isCompressed, out
int outLength)
1030 throw new InvalidOperationException(
"ReadWriteMessages are not to be sent");
void WriteString(string val)
void WriteColorR8G8B8A8(Microsoft.Xna.Framework.Color val)
void WriteColorR8G8B8(Microsoft.Xna.Framework.Color val)
Utility struct for writing Singles
uint UIntValue
Value as an unsigned 32 bit integer
float SingleValue
Value as a 32 bit float