Ejemplo n.º 1
0
        /// <summary>
        /// Adds a command packet to the reliable queue.
        /// </summary>
        /// <param name="cmd">Command packet to add to the queue</param>
        /// <returns>UDP_OK or error code</returns>
        public int AddReliableCommand(ReliableEntry cmd)
        {
            if (this.Count >= UdpConsts.MAX_RELIABLE_QUEUED)
            {
                return(UdpConsts.UDP_RELIABLEQUEUEFULL);
            }

            lock (this.SyncRoot)
            {
                if (this.Count > 0)
                {
                    ReliableEntry tmp = this.Peek() as ReliableEntry;
                    if (tmp != null)
                    {
                        if (tmp.SequenceNum == cmd.SequenceNum)
                        {
                            return(UdpConsts.UDP_ALREADYINQUEUE);
                        }
                    }
                }

                this.Enqueue(cmd);
            }
            return(UdpConsts.UDP_OK);
        }
Ejemplo n.º 2
0
        public int AddReliableCommand(ReliableEntry cmd)
        {
            if (this.Count >= UdpConsts.MAX_RELIABLE_QUEUED)
            {
                return(UdpConsts.UDP_RELIABLEQUEUEFULL);
            }
            object syncRoot;

            Monitor.Enter(syncRoot = this.SyncRoot);
            try
            {
                if (this.Count > 0)
                {
                    ReliableEntry reliableEntry = this.Peek() as ReliableEntry;
                    if (reliableEntry != null && reliableEntry.SequenceNum == cmd.SequenceNum)
                    {
                        return(UdpConsts.UDP_ALREADYINQUEUE);
                    }
                }
                this.Enqueue(cmd);
            }
            finally
            {
                Monitor.Exit(syncRoot);
            }
            return(UdpConsts.UDP_OK);
        }
Ejemplo n.º 3
0
        public int CacheReliablePacket(string packet)
        {
            ReliableEntry reliableEntry = new ReliableEntry();

            reliableEntry.SequenceNum   = this.m_LastSentPacketR;
            reliableEntry.CommandPacket = packet;
            return(this.m_RQueue.AddReliableCommand(reliableEntry));
        }
Ejemplo n.º 4
0
        public void ResendReliablePacket()
        {
            ReliableEntry reliableEntry = null;

            this.RQueue.GetCurrentReliableCommand(out reliableEntry);
            if (reliableEntry != null)
            {
                this.m_Parent.SendData(this.RemoteEP.Address.ToString(), this.RemoteEP.Port, reliableEntry.CommandPacket);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Resends the current reliable packet, used if a reliable packet acknowledgement
        /// does not come through fast enough to guarantee delivery of the packet.
        /// </summary>
        public void ResendReliablePacket()
        {
            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)
            {
                m_Parent.DebugDump("Found a reliable packet on the queue, resending.");
                m_Parent.SendData(RemoteEP.Address.ToString(), RemoteEP.Port, rcmd.CommandPacket);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Ges the current reliable command from the queue, but does not remove it from the queue.
        /// </summary>
        /// <param name="cmd_out">Command at start of queue</param>
        /// <returns>UDP_OK or error code</returns>
        public int GetCurrentReliableCommand(out ReliableEntry cmd_out)
        {
            cmd_out = null;

            lock (this.SyncRoot)
            {
                //No reliable packets on the queue
                if (this.Count == 0)
                {
                    return(UdpConsts.UDP_NOTFOUND);
                }

                cmd_out = this.Peek() as ReliableEntry;
            }

            return(UdpConsts.UDP_OK);
        }
Ejemplo n.º 7
0
        public int GetCurrentReliableCommand(out ReliableEntry cmd_out)
        {
            cmd_out = null;
            object syncRoot;

            Monitor.Enter(syncRoot = this.SyncRoot);
            try
            {
                if (this.Count == 0)
                {
                    return(UdpConsts.UDP_NOTFOUND);
                }
                cmd_out = (this.Peek() as ReliableEntry);
            }
            finally
            {
                Monitor.Exit(syncRoot);
            }
            return(UdpConsts.UDP_OK);
        }
Ejemplo n.º 8
0
 public void ProcessCompletedCommand(Command cmd)
 {
     if (!this.Authed && cmd.OPCode == UdpConsts.OPCODE_LOGINDETAILS)
     {
         this.m_Parent.ConnectionAuthing(this, cmd);
         return;
     }
     if (cmd.OPCode == UdpConsts.OPCODE_PING)
     {
         if (this.Authed)
         {
             this.UpdateTimeout();
             if (!this.Server)
             {
                 this.SendUnreliableCommand(0, UdpConsts.OPCODE_PING, null);
             }
         }
         return;
     }
     if (cmd.OPCode == UdpConsts.OPCODE_LOGINACK)
     {
         if (cmd.Fields[0] == "OK")
         {
             this.Authed = true;
             this.m_Parent.AuthenticatedWithConnection(this, true, "");
             return;
         }
         this.m_Parent.AuthenticatedWithConnection(this, false, cmd.Fields[1]);
         this.m_Parent.RemoveConnection(this, false, cmd.Fields[1]);
         return;
     }
     else
     {
         if (cmd.OPCode == UdpConsts.OPCODE_DISCONNECT)
         {
             this.m_Parent.RemoveConnection(this, false, cmd.Fields[0]);
             return;
         }
         if (cmd.OPCode == UdpConsts.OPCODE_RELIABLEACK)
         {
             ReliableEntry reliableEntry = null;
             this.RQueue.GetCurrentReliableCommand(out reliableEntry);
             if (reliableEntry != null)
             {
                 try
                 {
                     if (reliableEntry.SequenceNum == Convert.ToUInt32(cmd.Fields[0]))
                     {
                         this.RQueue.NextReliableCommand();
                         ReliableEntry reliableEntry2 = null;
                         this.RQueue.GetCurrentReliableCommand(out reliableEntry2);
                         if (reliableEntry2 != null)
                         {
                             this.m_Parent.SendData(this.RemoteEP.Address.ToString(), this.RemoteEP.Port, reliableEntry2.CommandPacket);
                         }
                     }
                 }
                 catch (Exception)
                 {
                 }
             }
         }
         if (this.Authed)
         {
             this.m_Parent.CommandReceived(this, cmd);
         }
         return;
     }
 }
Ejemplo n.º 9
0
		/// <summary>
		/// Adds a reliable packet to the reliable packet queue on the specified connection
		/// </summary>
		public int CacheReliablePacket(string packet)
		{
			ReliableEntry re = new ReliableEntry();
			re.SequenceNum = m_LastSentPacketR;
			re.CommandPacket = packet;
			return m_RQueue.AddReliableCommand(re);
		}
Ejemplo n.º 10
0
        /// <summary>
        /// Ges the current reliable command from the queue, but does not remove it from the queue.
        /// </summary>
        /// <param name="cmd_out">Command at start of queue</param>
        /// <returns>UDP_OK or error code</returns>
        public int GetCurrentReliableCommand(out ReliableEntry cmd_out)
        {
            cmd_out = null;
            
            lock (this.SyncRoot)
            {
                //No reliable packets on the queue
                if (this.Count == 0)
                    return UdpConsts.UDP_NOTFOUND;

                cmd_out = this.Peek() as ReliableEntry;
            }

            return UdpConsts.UDP_OK;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Adds a command packet to the reliable queue.
        /// </summary>
        /// <param name="cmd">Command packet to add to the queue</param>
        /// <returns>UDP_OK or error code</returns>
        public int AddReliableCommand(ReliableEntry cmd)
        {
			if(this.Count >= UdpConsts.MAX_RELIABLE_QUEUED)
				return UdpConsts.UDP_RELIABLEQUEUEFULL;

            lock (this.SyncRoot)
            {
                if (this.Count > 0)
                {
                    ReliableEntry tmp = this.Peek() as ReliableEntry;
                    if (tmp != null)
                    {
                        if (tmp.SequenceNum == cmd.SequenceNum)
                            return UdpConsts.UDP_ALREADYINQUEUE;
                    }
                }

                this.Enqueue(cmd);
            }
            return UdpConsts.UDP_OK;
        }
Ejemplo n.º 12
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);
            }
        }