예제 #1
0
        public static ArraySegment <byte> ReadBytesAndSizeSegment(this NetworkReader reader)
        {
            // dont need to ValidateSize here because we dont allocate for segment

            return(ReadCountPlusOne(reader, out var count)
                ? reader.ReadBytesSegment(count)
                : default);
예제 #2
0
        public static ArraySegment <byte> ReadBytesAndSizeSegment(this NetworkReader reader)
        {
            // count = 0 means the array was null
            // otherwise count - 1 is the length of the array
            uint count = reader.ReadPackedUInt32();

            return(count == 0 ? default : reader.ReadBytesSegment(checked ((int)(count - 1u))));
        }
예제 #3
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));
        }