/// <returns>string or null</returns> /// <exception cref="ArgumentException">Throws if invalid utf8 string is received</exception> public static string ReadString(this NetworkReader reader) { // read number of bytes ushort size = reader.ReadUInt16(); if (size == 0) { return(null); } int realSize = size - 1; // make sure it's within limits to avoid allocation attacks etc. if (realSize >= MaxStringLength) { throw new EndOfStreamException($"ReadString too long: {realSize}. Limit is: {MaxStringLength}"); } ArraySegment <byte> data = reader.ReadBytesSegment(realSize); // convert directly from buffer to string via encoding return(encoding.GetString(data.Array, data.Offset, data.Count)); }
// unpack message after receiving // -> pass NetworkReader so it's less strange if we create it in here // and pass it upwards. // -> NetworkReader will point at content afterwards! public static int UnpackId(NetworkReader messageReader) { return(messageReader.ReadUInt16()); }
public static char ReadChar(this NetworkReader reader) => (char)reader.ReadUInt16();
public static ushort ReadUInt16Extension(this NetworkReader reader) => reader.ReadUInt16();
public static short ReadInt16(this NetworkReader reader) => (short)reader.ReadUInt16();