private void OnDataReceived(IAsyncResult iAr)
        {
            try
            {
                int bytesReceived = socket.EndReceive(iAr);

                if (bytesReceived > 0)
                {
                    byte[] packetBuffer = new byte[bytesReceived];

                    // Decrypt the bytes with the xOrKey.
                    for (int i = 0; i < bytesReceived; i++)
                    {
                        packetBuffer[i] = (byte)(this.buffer[i] ^ Core.Networking.Constants.xOrKeyInternalSend);
                    }

                    int oldLength = cacheBuffer.Length;
                    Array.Resize(ref cacheBuffer, oldLength + bytesReceived);
                    Array.Copy(packetBuffer, 0, cacheBuffer, oldLength, packetBuffer.Length);

                    int startIndex = 0;                                  // Determs whre the bytes should split
                    for (int i = 0; i < cacheBuffer.Length; i++)
                    {                                                    // loop trough our cached buffer.
                        if (cacheBuffer[i] == 0x0A)
                        {                                                // Found a complete packet
                            byte[] newPacket = new byte[i - startIndex]; // determ the new packet size.
                            for (int j = 0; j < (i - startIndex); j++)
                            {
                                newPacket[j] = cacheBuffer[startIndex + j]; // copy the buffer to the buffer of the new packet.
                            }
                            packetCount++;

                            // Handle the packet instantly.
                            Core.Networking.InPacket inPacket = new Core.Networking.InPacket(newPacket, this);
                            if (inPacket.Id > 0)
                            {
                                Networking.PacketHandler pHandler = Managers.PacketManager.Instance.FindInternal(inPacket);
                                if (pHandler != null)
                                {
                                    // try {
                                    pHandler.Handle(inPacket);
                                    //} catch { /*Disconnect(); }
                                }
                            }

                            startIndex = i + 1;
                        }
                    }

                    if (startIndex > 0)
                    {
                        byte[] fullCopy = cacheBuffer;
                        Array.Resize(ref cacheBuffer, (cacheBuffer.Length - startIndex));
                        for (int i = 0; i < (cacheBuffer.Length - startIndex); i++)
                        {
                            cacheBuffer[i] = fullCopy[startIndex + i];
                        }
                        fullCopy = null;
                    }

                    socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
                }
                else
                {
                    Disconnect();
                }
            }
            catch
            {
                Disconnect();
            }
        }
Esempio n. 2
0
        private void OnDataReceived(IAsyncResult iAr)
        {
            try
            {
                int bytesReceived = _socket.EndReceive(iAr);

                if (bytesReceived > 0)
                {
                    byte[] packetBuffer = new byte[bytesReceived];

                    // Decrypt the bytes with the xOrKey.
                    for (int i = 0; i < bytesReceived; i++)
                    {
                        packetBuffer[i] = (byte)(_buffer[i] ^ Core.Networking.Constants.xOrKeyClientRecieve);
                    }

                    int oldLength = _cacheBuffer.Length;
                    Array.Resize(ref _cacheBuffer, oldLength + bytesReceived);
                    Array.Copy(packetBuffer, 0, _cacheBuffer, oldLength, packetBuffer.Length);

                    int startIndex = 0;                                  // Determs where the bytes should split
                    for (int i = 0; i < _cacheBuffer.Length; i++)
                    {                                                    // loop trough our cached buffer.
                        if (_cacheBuffer[i] == 0x0A)
                        {                                                // Found a complete packet
                            byte[] newPacket = new byte[i - startIndex]; // determ the new packet size.
                            for (int j = 0; j < (i - startIndex); j++)
                            {
                                newPacket[j] = _cacheBuffer[startIndex + j]; // copy the buffer to the buffer of the new packet.
                            }
                            _packetCount++;
                            // Instant handeling
                            Core.Networking.InPacket inPacket = new Core.Networking.InPacket(newPacket, this);
                            if (inPacket != null)
                            {
                                if (inPacket.Id > 0)
                                {
                                    Networking.PacketHandler pHandler = Managers.PacketManager.Instance.FindExternal(inPacket);
                                    if (pHandler != null)
                                    {
                                        try
                                        {
                                            pHandler.Handle(inPacket);
                                        }
                                        catch (Exception e) { Log.Error(e.ToString()); }
                                    }
                                }
                            }
                            // Increase start index.
                            startIndex = i + 1;
                        }
                    }

                    if (startIndex > 0)
                    {
                        byte[] fullCopy = _cacheBuffer;
                        Array.Resize(ref _cacheBuffer, (_cacheBuffer.Length - startIndex));
                        for (int i = 0; i < (_cacheBuffer.Length - startIndex); i++)
                        {
                            _cacheBuffer[i] = fullCopy[startIndex + i];
                        }
                        fullCopy = null;
                    }
                    _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
                }
                else
                {
                    Disconnect();
                }
            }
            catch
            {
                Disconnect();
            }
        }