示例#1
0
        private void processReceivePacket()
        {
            Byte[] packetBytes  = new Byte[client.ReceiveBufferSize];
            Byte[] remainPacket = new Byte[client.ReceiveBufferSize];
            while (IsConnected)
            {
                int offset  = 0;
                int readLen = 0;
                try {
                    readLen = stream.Read(packetBytes, offset, packetBytes.Length);
                    if (readLen == 0)
                    {
                        this.closeConnection();
                        break;
                    }
                }
                catch (Exception err) {
                    Debug.Log(err.Message);
                    break;
                }

                PacketObfuscator.decodeHeader(ref packetBytes);
                Int32 packetLen = PacketAnalyzer.GetHeader(packetBytes, ref offset);

                while (readLen < packetLen)
                {
                    Int32 remainLen = 0;
                    remainLen = stream.Read(remainPacket, 0, remainPacket.Length);
                    Buffer.BlockCopy(remainPacket, 0, packetBytes, readLen, remainLen);
                    readLen += remainLen;
                }

                Int32  packetDataSize = packetLen - offset;
                Byte[] packetData     = new byte[packetLen - offset];
                PacketObfuscator.decodeData(ref packetBytes, packetDataSize);
                Buffer.BlockCopy(packetBytes, offset, packetData, 0, packetDataSize);

                //Byte[] packetData = new byte[packetDataSize];
                //PacketObfuscator.decodeData(ref buffer, packetDataSize);
                //Buffer.BlockCopy(buffer, offset, packetData, 0, packetDataSize);

                Packet packet = PacketAnalyzer.AnalyzerPacket(packetData);
                if (packet == null)
                {
                    Debug.Log("unidentified packet received...");
                    continue;
                }

                lock (lockObject) {
                    receivePacketQueue.Enqueue(packet);
                }

                //Debug.Log("recv "+packet.type());
            }
        }
示例#2
0
        private IEnumerator ProcessSendPacketQueue()
        {
            MemoryStream packetStream = new MemoryStream();

            while (IsConnected)
            {
                yield return(null);

                if (sendPacketQueue.Count == 0)
                {
                    continue;
                }

                Packet packet       = sendPacketQueue.Dequeue();
                Int32  packetLen    = sizeof(Int32) + (Int32)(packet.getStream().Length);
                Byte[] packetHeader = BitConverter.GetBytes(packetLen);
                Byte[] packetData   = packet.getStream().ToArray();
                packetStream.Write(packetHeader, 0, packetHeader.Length);
                packetStream.Write(packetData, 0, packetData.Length);

                Byte[] buffer = packetStream.ToArray();
                PacketObfuscator.encodeHeader(ref buffer);
                PacketObfuscator.encodeData(ref buffer, packetLen - sizeof(Int32));

                Debug.Log(packetLen);

                try {
                    stream.Write(buffer, 0, buffer.Length);
                    stream.Flush();
                    packetStream.SetLength(0);
                } catch (Exception e) {
                    Debug.Log(e.Message);
                }

                //Debug.Log("send "+packet.type());
            }
        }