Exemplo n.º 1
0
        /// <summary>
        /// This function is called internally when a command has been parsed and is ready to be processed
        /// by the UDP engine and possibly the host application.
        /// </summary>
        /// <param name="cmd">Object containing data about the command to be processed.</param>
        public void ProcessCompletedCommand(Command cmd)
        {
            //Client sent login details
            if (!Authed)
            {
                if (cmd.OPCode == UdpConsts.OPCODE_LOGINDETAILS)
                {
                    m_Parent.ConnectionAuthing(this, cmd);
                    return;
                }
            }

            //Remote host sent us a ping
            if (cmd.OPCode == UdpConsts.OPCODE_PING)
            {
                if (Authed)
                {
                    m_Parent.DebugDump("Received ping from " + this.RemoteEP.ToString());

                    UpdateTimeout();

                    if (!Server) //If this is not a connection to a server
                    {
                        SendUnreliableCommand(0, UdpConsts.OPCODE_PING, null);
                    }
                }
                return;
            }

            //Server sent acknowledgement of our connection
            if (cmd.OPCode == UdpConsts.OPCODE_LOGINACK)
            {
                if (cmd.Fields[0] == "OK")
                {
                    Authed = true;
                    m_Parent.DebugDump("Authenticated with " + this.RemoteEP.ToString() + " OK. Connected!");

                    //Send the authenticated event to the third party application
                    m_Parent.AuthenticatedWithConnection(this, true, "");
                }
                else
                {
                    m_Parent.DebugDump("Authentication to " + this.RemoteEP.ToString() + " Failed!");

                    //Send the not authed event
                    m_Parent.AuthenticatedWithConnection(this, false, cmd.Fields[1]);

                    //Disconnect the connection (but don't send a disconnection packet back)
                    m_Parent.RemoveConnection(this, false, cmd.Fields[1]);
                }
                return;
            }

            //Remote host disconnected from us
            if (cmd.OPCode == UdpConsts.OPCODE_DISCONNECT)
            {
                m_Parent.RemoveConnection(this, false, cmd.Fields[0]);
                return;
            }

            //Reliable packet acknowledgement
            if (cmd.OPCode == UdpConsts.OPCODE_RELIABLEACK)
            {
                m_Parent.DebugDump("Received reliable ACK for packet " + cmd.Fields[0] + ".");

                //Get the current reliable command
                ReliableEntry rcmd = null;
                RQueue.GetCurrentReliableCommand(out rcmd);

                //If the sequence number from the one stored in the first field
                //of the ACK is the same as the one in the queue, remove it.
                if (rcmd != null)
                {
                    try
                    {
                        if (rcmd.SequenceNum == Convert.ToUInt32(cmd.Fields[0]))
                        {
                            RQueue.NextReliableCommand();

                            ReliableEntry next_rel = null;
                            RQueue.GetCurrentReliableCommand(out next_rel);

                            if (next_rel != null)
                            {
                                m_Parent.DebugDump("Moving reliable packet queue - next packet is " + next_rel.SequenceNum.ToString());

                                //Send the next reliable packet
                                m_Parent.SendData(RemoteEP.Address.ToString(), RemoteEP.Port, next_rel.CommandPacket);
                            }
                            else
                            {
                                m_Parent.DebugDump("Moving reliable packet queue - no more packets on reliable queue.");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        m_Parent.DebugDump("Exception: " + e.Message);
                    }
                }
            }

            //Give unrecognised commands to the application
            if (Authed)
            {
                m_Parent.CommandReceived(this, cmd);
            }
        }