コード例 #1
0
ファイル: NetworkReader.cs プロジェクト: MichalPetryka/Mirror
        // note: this will throw an ArgumentException if an invalid utf8 string is sent
        // null support, see NetworkWriter
        public static string ReadString(this NetworkReader reader)
        {
            // read number of bytes
            ushort size = reader.ReadUInt16();

            switch (size)
            {
            case 0:
                return(null);

            case 1:
                return("");
            }

            int realSize = size - 1;

            // make sure it's within limits to avoid allocation attacks etc.
            if (realSize >= NetworkWriter.MaxStringLength)
            {
                throw new EndOfStreamException("ReadString too long: " + realSize + ". Limit is: " + NetworkWriter.MaxStringLength);
            }

            ArraySegment <byte> data = reader.ReadBytesSegment(realSize);

            // convert directly from buffer to string via encoding
            return(encoding.GetString(data.Array, data.Offset, data.Count));
        }
コード例 #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
        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.ReadUInt();

            // Use checked() to force it to throw OverflowException if data is invalid
            return(count == 0 ? default : reader.ReadBytesSegment(checked ((int)(count - 1u))));
        }
コード例 #4
0
        public static string ReadString(this NetworkReader reader)
        {
            // read number of bytes
            ushort size = reader.ReadUShort();

            // null support, see NetworkWriter
            if (size == 0)
            {
                return(null);
            }

            int realSize = size - 1;

            // make sure it's within limits to avoid allocation attacks etc.
            if (realSize >= NetworkWriter.MaxStringLength)
            {
                throw new EndOfStreamException($"ReadString too long: {realSize}. Limit is: {NetworkWriter.MaxStringLength}");
            }

            ArraySegment <byte> data = reader.ReadBytesSegment(realSize);

            // convert directly from buffer to string via encoding
            return(encoding.GetString(data.Array, data.Offset, data.Count));
        }