Пример #1
0
        /// <summary>
        /// Read length off the stream
        /// </summary>
        /// <param name="second"></param>
        /// <returns></returns>
        public uint ReadLength(byte second)
        {
            uint len = 0;

            if (second == 126)
            {
                bool error;
                len = TCPReaderWriter.ReadUShort(Stream, false, out error);
            }
            else if (second == 127)
            {
                bool error;
                len = (uint)TCPReaderWriter.ReadULong(Stream, false, out error);

                if (len < 0 || len > TCPServer.MaxPackageLength)
                {
                    return(0);
                }
            }
            else if (second > 0 && second < 126)
            {
                len = second;
            }

            return(len);
        }
Пример #2
0
        /// <summary>
        /// Write message to stream
        /// </summary>
        /// <param name="code"></param>
        /// <param name="data"></param>
        public void Write(TCPMessageCode code, byte[] data)
        {
            using (MemoryStream ms = new MemoryStream()) {
                ms.WriteByte((byte)code);

                byte second;

                if (data.Length <= 125)
                {
                    second = (byte)data.Length;
                    ms.WriteByte(second);
                }
                else if (data.Length <= 65535)
                {
                    second = 126;
                    ms.WriteByte(second);
                    TCPReaderWriter.WriteNumber(ms, (ushort)data.Length, false);
                }
                else
                {
                    second = 127;
                    ms.WriteByte(second);
                    TCPReaderWriter.WriteNumber(ms, (ulong)data.Length, false);
                }

                // encode data xor

                byte[] key     = new byte[4];
                byte[] encoded = new byte[data.Length];
                RandomGen.Random.NextBytes(key);

                for (int e = 0; e < encoded.Length; e++)
                {
                    encoded[e] = (byte)(data[e] ^ key[e % 4]);
                }

                ms.Write(key, 0, key.Length);
                ms.Write(encoded, 0, encoded.Length);

                byte[] buffer = ms.ToArray();
                Stream.Write(buffer, 0, buffer.Length);
            }
        }
Пример #3
0
        /// <summary>
        /// Read tcp message off the stream
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public TCPMessage Read(TCPServerClient client)
        {
            byte first;

            try {
                first = (byte)Stream.ReadByte();
            } catch (Exception e) {
                return(null);
            }

            if (!IsClientConnected(client))
            {
                return(null);
            }

            byte second = (byte)Stream.ReadByte();
            uint len    = ReadLength(second);

            if (len != 0)
            {
                // solve encoded bytes xor

                byte[] key     = TCPReaderWriter.Read(Stream, 4);
                byte[] encoded = TCPReaderWriter.Read(Stream, len);
                byte[] decoded = new byte[len];

                for (int e = 0; e < encoded.Length; e++)
                {
                    decoded[e] = (byte)(encoded[e] ^ key[e % 4]);
                }

                return(new TCPMessage()
                {
                    Client = client,
                    Code = (TCPMessageCode)first,
                    Content = decoded,
                    Sent = DateTime.Now
                });
            }

            return(null);
        }