Ejemplo n.º 1
0
        public GSPacketIn Clone()
        {
            GSPacketIn pkg = new GSPacketIn(this.m_buffer, this.m_length);

            pkg.ReadHeader();
            pkg.Offset = this.m_length;
            pkg.ClearChecksum();
            return(pkg);
        }
Ejemplo n.º 2
0
 public void ReceiveBytes(int numBytes)
 {
     Monitor.Enter(this);
     try
     {
         int bufferSize = this.m_client.PacketBufSize + numBytes;
         if (bufferSize < (int)GSPacketIn.HDR_SIZE)
         {
             this.m_client.PacketBufSize = bufferSize;
         }
         else
         {
             byte[] buffer = this.m_client.PacketBuf;
             if (bufferSize > buffer.Length)
             {
                 StreamProcessor.log.ErrorFormat("Error Buffersize: num {0}   buffersize:{1}  old buffer size:{2} buffer length:{3}", new object[]
                 {
                     numBytes,
                     bufferSize,
                     this.m_client.PacketBufSize,
                     buffer.Length
                 });
                 bufferSize = buffer.Length;
             }
             this.m_client.PacketBufSize = 0;
             int curOffset = 0;
             int dataleft  = bufferSize;
             while (dataleft >= (int)GSPacketIn.HDR_SIZE && curOffset >= 0)
             {
                 int packetLength = 0;
                 if (this.m_client.Encryted)
                 {
                     int key = this.receive_fsm.getState();
                     int i   = this.receive_fsm.count;
                     while (curOffset + 4 <= bufferSize)
                     {
                         int header = ((int)((byte)((int)buffer[curOffset] ^ (key++ & 16711680) >> 16)) << 8) + (int)((byte)((int)buffer[curOffset + 1] ^ (key++ & 16711680) >> 16));
                         if (header == (int)GSPacketIn.HEADER)
                         {
                             packetLength = ((int)((byte)((int)buffer[curOffset + 2] ^ (key++ & 16711680) >> 16)) << 8) + (int)((byte)((int)buffer[curOffset + 3] ^ (key++ & 16711680) >> 16));
                             break;
                         }
                         curOffset++;
                     }
                 }
                 else
                 {
                     while (curOffset + 4 <= bufferSize)
                     {
                         int header = ((int)buffer[curOffset] << 8) + (int)buffer[curOffset + 1];
                         if (header == (int)GSPacketIn.HEADER)
                         {
                             packetLength = ((int)buffer[curOffset + 2] << 8) + (int)buffer[curOffset + 3];
                             break;
                         }
                         curOffset++;
                     }
                 }
                 dataleft = bufferSize - curOffset;
                 if (packetLength < (int)GSPacketIn.HDR_SIZE || packetLength > buffer.Length)
                 {
                     StreamProcessor.log.Error(string.Concat(new object[]
                     {
                         "packetLength:",
                         packetLength,
                         ",GSPacketIn.HDR_SIZE:",
                         GSPacketIn.HDR_SIZE,
                         ",offset:",
                         curOffset,
                         ",bufferSize:",
                         bufferSize,
                         ",numBytes:",
                         numBytes
                     }));
                     StreamProcessor.log.ErrorFormat("Err pkg from {0}:", this.m_client.TcpEndpoint);
                     StreamProcessor.log.Error(Marshal.ToHexDump("===> error buffer", buffer, curOffset, (dataleft > 48) ? 48 : dataleft));
                     if (this.m_client.Strict && packetLength != 0)
                     {
                         StreamProcessor.log.Error("Disconnect the client in [Strict] mode.");
                         this.m_client.PacketBufSize = 0;
                         this.m_client.Disconnect();
                         return;
                     }
                     curOffset += 2;
                     dataleft   = bufferSize - curOffset;
                 }
                 else
                 {
                     if (dataleft < packetLength)
                     {
                         break;
                     }
                     int        size = (packetLength <= 1024) ? 1024 : packetLength;
                     GSPacketIn pkg  = new GSPacketIn(new byte[size], size);
                     if (this.m_client.Encryted)
                     {
                         pkg.CopyFrom(buffer, curOffset, 0, packetLength, this.receive_fsm.getState());
                         this.receive_fsm.UpdateState();
                     }
                     else
                     {
                         pkg.CopyFrom(buffer, curOffset, 0, packetLength);
                     }
                     pkg.ReadHeader();
                     if (pkg.CheckSum == pkg.CalculateChecksum())
                     {
                         pkg.ClearChecksum();
                         //if (StreamProcessor.log.IsDebugEnabled)
                         //{
                         //StreamProcessor.log.Debug(Marshal.ToHexDump("Recieve Packet:", pkg.Buffer, 0, packetLength));
                         //}
                         try
                         {
                             //StreamProcessor.log.Debug("Receive Packet!");
                             this.m_client.OnRecvPacket(pkg);
                         }
                         catch (Exception e)
                         {
                             StreamProcessor.log.Error("HandlePacket(pak)", e);
                         }
                         curOffset += packetLength;
                         dataleft   = bufferSize - curOffset;
                     }
                     else
                     {
                         curOffset += 2;
                         dataleft   = bufferSize - curOffset;
                     }
                 }
             }
             if (dataleft > 0)
             {
                 Array.Copy(buffer, curOffset, buffer, 0, dataleft);
                 this.m_client.PacketBufSize = dataleft;
             }
         }
     }
     finally
     {
         Monitor.Exit(this);
     }
 }