Esempio n. 1
0
        /// <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));
        }
Esempio n. 2
0
 // 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());
 }
Esempio n. 3
0
 public static char ReadChar(this NetworkReader reader) => (char)reader.ReadUInt16();
Esempio n. 4
0
 public static ushort ReadUInt16Extension(this NetworkReader reader) => reader.ReadUInt16();
Esempio n. 5
0
 public static short ReadInt16(this NetworkReader reader) => (short)reader.ReadUInt16();