Exemplo n.º 1
0
        /// <summary>
        /// The callback from Receive().
        /// </summary>
        /// <param name="asyncResult">The async result.</param>
        private void Receive_Callback(IAsyncResult asyncResult)
        {
            try
            {
                if (!clientSocket.Connected)
                {
                    Disconnect("Not connected.");
                    return;
                }

                if (!continue_rec)
                {
                    Disconnect("Not allowed to receive.");
                    return;
                }

                SocketError err;
                int         rSize = clientSocket.EndReceive(asyncResult, out err);

                if (err != SocketError.Success)
                {
                    Disconnect("Failed receive. (99% regular DC) Socket Error: " + err.ToString());
                    return;
                }

                if (rSize < 4)
                {
                    Disconnect("Invalid Packet Header. (99% regular DC) Size: " + rSize);
                    return;
                }

                byte[] rBuffer = new byte[rSize];
                System.Buffer.BlockCopy(dataHolder, 0, rBuffer, 0, rSize);

                using (var receiveData = new DataPacket(rBuffer))
                {
                    if (Crypto != null)
                    {
                        lock (Crypto)
                        {
                            receiveData.Decrypt(Crypto);

                            if (Crypto is Cryptography.GameCrypto)
                            {
                                Cryptography.GameCrypto crypto = (Crypto as Cryptography.GameCrypto);
                                if (!crypto.Exchanged)
                                {
                                    if (Cryptography.GameCrypto.HandleExchange(crypto, this, receiveData))
                                    {
                                        Receive();
                                    }
                                    return;
                                }
                            }
                        }
                    }
                    if (receiveData.MatchingSize())
                    {
                        if (!alreadyDisconnected)
                        {
                            byte[] rcopy   = receiveData.Copy();
                            byte[] rpacket = new byte[receiveData.PacketSize];

                            System.Buffer.BlockCopy(rcopy, 0, rpacket, 0, rpacket.Length);
                            using (var packet = new DataPacket(rpacket))
                            {
                                continue_rec = socketEvents.OnReceive.Invoke(this, packet);
                            }
                            rcopy = null;
                        }
                    }
                    else
                    {
                        Split(receiveData.Copy());
                    }
                }
            }
            catch (Exception e)
            {
                Disconnect(e.ToString());
            }
            Receive();
        }
Exemplo n.º 2
0
        /// <summary>
        /// The callback from Receive().
        /// </summary>
        /// <param name="asyncResult">The async result.</param>
        private void Receive_Callback(IAsyncResult asyncResult)
        {
            try
            {
                if (!clientSocket.Connected)
                {
                    Disconnect("Not connected.");
                    return;
                }

                if (!continue_rec)
                {
                    Disconnect("Not allowed to receive.");
                    return;
                }

                SocketError err;
                int rSize = clientSocket.EndReceive(asyncResult, out err);

                if (err != SocketError.Success)
                {
                    Disconnect("Failed receive. (99% regular DC) Socket Error: " + err.ToString());
                    return;
                }

                if (rSize < 4)
                {
                    Disconnect("Invalid Packet Header. (99% regular DC) Size: " + rSize);
                    return;
                }

                byte[] rBuffer = new byte[rSize];
                System.Buffer.BlockCopy(dataHolder, 0, rBuffer, 0, rSize);

                using (var receiveData = new DataPacket(rBuffer))
                {
                    if (Crypto != null)
                    {
                        lock (Crypto)
                        {
                            receiveData.Decrypt(Crypto);

                            if (Crypto is Cryptography.GameCrypto)
                            {
                                Cryptography.GameCrypto crypto = (Crypto as Cryptography.GameCrypto);
                                if (!crypto.Exchanged)
                                {
                                    if (Cryptography.GameCrypto.HandleExchange(crypto, this, receiveData))
                                        Receive();
                                    return;
                                }
                            }
                        }
                    }
                    if (receiveData.MatchingSize())
                    {
                        if (!alreadyDisconnected)
                        {
                            byte[] rcopy = receiveData.Copy();
                            byte[] rpacket = new byte[receiveData.PacketSize];

                            System.Buffer.BlockCopy(rcopy, 0, rpacket, 0, rpacket.Length);
                            using (var packet = new DataPacket(rpacket))
                            {
                                continue_rec = socketEvents.OnReceive.Invoke(this, packet);
                            }
                            rcopy = null;
                        }
                    }
                    else
                        Split(receiveData.Copy());
                }
            }
            catch (Exception e)
            {
                Disconnect(e.ToString());
            }
            Receive();
        }