Ejemplo n.º 1
0
        /// <summary>
        /// Sends data to the client.
        /// </summary>
        /// <param name="buffer">The buffer to send.</param>
        public void Send(DataPacket buffer)
        {
            //lock (lockme)
            //{
            //bool PacketSend = false;

            //try
            //{
            if (buffer.BufferLength > 65535)
            {
                Disconnect("Too big packet...");
                return;
            }

            byte[] Buffer = buffer.Copy();

            lock (send_lock)
            {
                if (Crypto != null)
                {
                    //lock (Crypto)
                    Crypto.Encrypt(Buffer);
                }


                try
                {
                    // (PacketSend = Monitor.TryEnter(this, 50))
                    //{
                    if (clientSocket.Connected)
                    {
                        clientSocket.Send(Buffer);
                        //clientSocket.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(Send_Callback), Buffer);
                    }
                    else
                    {
                        Disconnect("Not Connected.");
                    }
                }
                catch (SocketException se)
                {
                    Disconnect(string.Format("Failed to send packet... Error: {0}", se.SocketErrorCode.ToString()));
                }
                //
                //}
                //finally
                //{
                //	if (!PacketSend)
                //		Disconnect("Lag."); // lag
                //	else
                //		Monitor.Exit(this);
                //}
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sends a packet to the client.
        /// </summary>
        /// <param name="Packet">The packet to send.</param>
        public void Send(DataPacket Packet)
        {
            if (IsAIBot)
                return;

            // TODO: rewrite the sockets to actually handle this proper ...
            if (Packet.ReadString(Packet.BufferLength - 8, 8) == "TQClient") // this is actually never used, not removing it though just to be sure
            {
                using (var sendPacket = new DataPacket(Packet))
                {
                    sendPacket.WriteBytes(Packet.Copy(), 0);
                    sendPacket.WriteString("TQServer", sendPacket.BufferLength - 8);
                    NetworkClient.Send(sendPacket);
                }
            }
            else
            {
                using (var sendPacket = new DataPacket(new byte[Packet.BufferLength + 8]))
                {
                    sendPacket.WriteBytes(Packet.Copy(), 0);
                    sendPacket.WriteString("TQServer", sendPacket.BufferLength - 8);
                    NetworkClient.Send(sendPacket);
                }
            }
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sends data to the client.
        /// </summary>
        /// <param name="buffer">The buffer to send.</param>
        public void Send(DataPacket buffer)
        {
            //lock (lockme)
            //{
            //bool PacketSend = false;

            //try
            //{
            if (buffer.BufferLength > 65535)
            {
                Disconnect("Too big packet...");
                return;
            }

            byte[] Buffer = buffer.Copy();

            lock (send_lock)
            {
                if (Crypto != null)
                {
                    //lock (Crypto)
                    Crypto.Encrypt(Buffer);
                }

                try
                {
                    // (PacketSend = Monitor.TryEnter(this, 50))
                    //{
                    if (clientSocket.Connected)
                    {
                        clientSocket.Send(Buffer);
                        //clientSocket.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(Send_Callback), Buffer);
                    }
                    else
                        Disconnect("Not Connected.");
                }
                catch (SocketException se)
                {
                    Disconnect(string.Format("Failed to send packet... Error: {0}", se.SocketErrorCode.ToString()));
                }
                //
                //}
                //finally
                //{
                //	if (!PacketSend)
                //		Disconnect("Lag."); // lag
                //	else
                //		Monitor.Exit(this);
                //}
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Handles the auth request packet.
        /// </summary>
        /// <param name="client">The auth client.</param>
        /// <param name="Packet">The packet.</param>
        public static void Handle(Client.AuthClient client, DataPacket Packet)
        {
            using (var auth = new AuthRequestPacket(Packet))
            {
                Enums.AccountStatus status = Database.ServerDatabase.Authenticate(client, auth.Account, auth.Password);
                client.EntityUID = (uint)ProjectX_V3_Lib.ThreadSafe.RandomGenerator.Generator.Next(1000000, 699999999);

                if (status == Enums.AccountStatus.Ready)
                {
                    try
                    {
                        Database.ServerDatabase.UpdateAuthentication(client);
                        Socket quicksock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                        quicksock.Connect(new IPEndPoint(IPAddress.Parse(Program.Config.ReadString("GameIP")), Program.Config.ReadInt32("GameAuthPort")));

                        using (DataPacket packet = new DataPacket(44, 9001))
                        {
                            packet.WriteString(Program.Config.ReadString("ServerPassword"), 4);
                            packet.WriteString(client.Account, 20);
                            packet.WriteInt32(client.DatabaseUID, 36);
                            packet.WriteUInt32(client.EntityUID, 40);
                            quicksock.BeginSend(packet.Copy(), 0, 44, SocketFlags.None,
                                                new AsyncCallback((ia) =>
                                                                  {
                                                                  	int send = quicksock.EndSend(ia);
                                                                  	if (send != 44)
                                                                  	{
                                                                  		status = Enums.AccountStatus.Datebase_Error;
                                                                  		client.EntityUID = 0;
                                                                  	}
                                                                  	Console.WriteLine("Database Notified: [Account: {0}] [DUID: {1}] [EUID: {2}]", client.Account, client.DatabaseUID, client.EntityUID);
                                                                  }), null);

                        }
                        System.Threading.Thread.Sleep(2000);
                    }
                    catch
                    {
                        status = Enums.AccountStatus.Datebase_Error;
                    }
                }

                using (var resp = new AuthResponsePacket())
                {
                    if (status == Enums.AccountStatus.Ready)
                    {
                        Console.WriteLine("Incoming login. [Account: {0}] [Password: {1}]", client.Account, client.Password);

                        resp.EntityUID = client.EntityUID;
                        resp.Port = Program.Config.ReadUInt32("GamePort");
                        resp.IPAddress = Program.Config.ReadString("GameIP");
                    }
                    else
                    {
                        resp.EntityUID = 0;
                    }
                    resp.AccountStatus = status;
                    client.Send(resp);
                }
                System.Threading.Thread.Sleep(5000);
                client.NetworkClient.Disconnect("TIME_OUT");
            }
        }