Exemplo n.º 1
0
        private void SendAckQueue()
        {
            PlayerNetworkSession session = this;
            var queue  = session.PlayerAckQueue;
            int lenght = queue.Count;

            if (lenght == 0)
            {
                return;
            }

            Acks acks = Acks.CreateObject();

            //Acks acks = new Acks();
            for (int i = 0; i < lenght; i++)
            {
                int ack;
                if (!session.PlayerAckQueue.TryDequeue(out ack))
                {
                    break;
                }

                Interlocked.Increment(ref Server.ServerInfo.NumberOfAckSent);
                acks.acks.Add(ack);
            }

            if (acks.acks.Count > 0)
            {
                byte[] data = acks.Encode();
                Server.SendData(data, session.EndPoint);
            }

            acks.PutPool();
        }
Exemplo n.º 2
0
        private void SendAckQueue(object state)
        {
            Parallel.ForEach(_playerSessions.Values.ToArray(), delegate(PlayerNetworkSession session)
            {
                var queue  = session.PlayerAckQueue;
                int lenght = queue.Count;

                if (lenght == 0)
                {
                    return;
                }

                Acks acks = Acks.CreateObject();
                for (int i = 0; i < lenght; i++)
                {
                    int ack;
                    if (!session.PlayerAckQueue.TryDequeue(out ack))
                    {
                        break;
                    }

                    acks.acks.Add(ack);
                }

                if (acks.acks.Count > 0)
                {
                    byte[] data = acks.Encode();
                    SendData(data, session.EndPoint, session.SyncRoot);
                }

                acks.PutPool();
            });
        }
Exemplo n.º 3
0
        private async Task SendAckQueueAsync()
        {
            RaknetSession session    = this;
            var           queue      = session.OutgoingAckQueue;
            int           queueCount = queue.Count;

            if (queueCount == 0)
            {
                return;
            }

            var acks = new Acks();

            for (int i = 0; i < queueCount; i++)
            {
                if (!queue.TryDequeue(out int ack))
                {
                    break;
                }

                Interlocked.Increment(ref ConnectionInfo.NumberOfAckSent);
                acks.acks.Add(ack);
            }

            if (acks.acks.Count > 0)
            {
                byte[] data = acks.Encode();
                await _packetSender.SendDataAsync(data, session.EndPoint);
            }
            //acks.PutPool();
        }
Exemplo n.º 4
0
		private void SendAckQueue(object state)
		{
			var sessions = _playerSessions.Values.ToArray();

			foreach (var s in sessions)
			{
				ThreadPool.QueueUserWorkItem(delegate(object o)
				{
					PlayerNetworkSession session = (PlayerNetworkSession) o;
					var queue = session.PlayerAckQueue;
					int lenght = queue.Count;

					if (lenght == 0) return;

					Acks acks = Acks.CreateObject();
					for (int i = 0; i < lenght; i++)
					{
						int ack;
						if (!session.PlayerAckQueue.TryDequeue(out ack)) break;

						acks.acks.Add(ack);
					}

					if (acks.acks.Count > 0)
					{
						byte[] data = acks.Encode();
						SendData(data, session.EndPoint);
					}

					acks.PutPool();
				}, s);
			}
		}
Exemplo n.º 5
0
        /// <summary>
        ///     Processes a message.
        /// </summary>
        /// <param name="receiveBytes">The received bytes.</param>
        /// <param name="senderEndpoint">The sender's endpoint.</param>
        /// <exception cref="System.Exception">Receive ERROR, NAK in wrong place</exception>
        private void ProcessMessage(byte[] receiveBytes, IPEndPoint senderEndpoint)
        {
            byte msgId = receiveBytes[0];

            if (msgId <= (byte) DefaultMessageIdTypes.ID_USER_PACKET_ENUM)
            {
                DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes) msgId;

                Package message = PackageFactory.CreatePackage(msgId, receiveBytes);

                if (message == null) return;

                TraceReceive(message);

                switch (msgIdType)
                {
                    case DefaultMessageIdTypes.ID_UNCONNECTED_PONG:
                    {
                        UnconnectedPong incoming = (UnconnectedPong) message;
                        HaveServer = true;
                        SendOpenConnectionRequest1();

                        break;
                    }
                    case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_1:
                    {
                        OpenConnectionReply1 incoming = (OpenConnectionReply1) message;
                        _mtuSize = incoming.mtuSize;
                        //if (incoming.mtuSize < _mtuSize) throw new Exception("Error:" + incoming.mtuSize);
                        SendOpenConnectionRequest2();
                        break;
                    }
                    case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_2:
                    {
                        OpenConnectionReply2 incoming = (OpenConnectionReply2) message;
                        //_mtuSize = incoming.mtuSize;
                        SendConnectionRequest();
                        break;
                    }
                }
            }
            else
            {
                DatagramHeader header = new DatagramHeader(receiveBytes[0]);
                if (!header.isACK && !header.isNAK && header.isValid)
                {
                    if (receiveBytes[0] == 0xa0)
                    {
                        throw new Exception("Receive ERROR, NAK in wrong place");
                    }

                    //ConnectedPackage package = ConnectedPackage.CreateObject();
                    ConnectedPackage package = new ConnectedPackage();
                    package.Decode(receiveBytes);
                    header = package._datagramHeader;
                    //Log.Debug($"> Datagram #{header.datagramSequenceNumber}, {package._hasSplit}, {package._splitPacketId}, {package._reliability}, {package._reliableMessageNumber}, {package._sequencingIndex}, {package._orderingChannel}, {package._orderingIndex}");

                    var messages = package.Messages;

                    //Reliability reliability = package._reliability;
                    //if (reliability == Reliability.Reliable
                    //	|| reliability == Reliability.ReliableSequenced
                    //	|| reliability == Reliability.ReliableOrdered
                    //	)
                    {
                        if (header.datagramSequenceNumber == 1000)
                        {
                            Log.Error("Datagram 1000 ignored");
                        }
                        else
                        {
                            // Send ACK
                            Acks ack = new Acks();
                            ack.acks.Add(package._datagramSequenceNumber.IntValue());
                            byte[] data = ack.Encode();
                            //Log.Info("Send ACK #" + package._datagramSequenceNumber.IntValue());
                            SendData(data, senderEndpoint);
                        }
                    }

                    //if (LoginSent) return; //HACK

                    foreach (var message in messages)
                    {
                        if (message is SplitPartPackage)
                        {
                            lock (Session.SyncRoot)
                            {
                                HandleSplitMessage(Session, package, (SplitPartPackage) message);
                            }

                            continue;
                        }

                        message.Timer.Restart();
                        HandlePackage(message);
                        message.PutPool();
                    }

                    //package.PutPool();
                }
                else if (header.isPacketPair)
                {
                    Log.Warn("header.isPacketPair");
                }
                else if (header.isACK && header.isValid)
                {
                    HandleAck(receiveBytes, senderEndpoint);
                }
                else if (header.isNAK && header.isValid)
                {
                    Nak nak = new Nak();
                    nak.Decode(receiveBytes);
                    HandleNak(receiveBytes, senderEndpoint);
                }
                else if (!header.isValid)
                {
                    Log.Warn("!!!! ERROR, Invalid header !!!!!");
                }
                else
                {
                    Log.Warn("!! WHAT THE F");
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Processes a message.
        /// </summary>
        /// <param name="receiveBytes">The received bytes.</param>
        /// <param name="senderEndpoint">The sender's endpoint.</param>
        /// <exception cref="System.Exception">Receive ERROR, NAK in wrong place</exception>
        private void ProcessMessage(byte[] receiveBytes, IPEndPoint senderEndpoint)
        {
            byte msgId = receiveBytes[0];

            if (msgId <= (byte)DefaultMessageIdTypes.ID_USER_PACKET_ENUM)
            {
                DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes)msgId;

                Package message = PackageFactory.CreatePackage(msgId, receiveBytes);

                if (message == null)
                {
                    return;
                }

                TraceReceive(message);

                switch (msgIdType)
                {
                case DefaultMessageIdTypes.ID_UNCONNECTED_PONG:
                {
                    UnconnectedPong incoming = (UnconnectedPong)message;
                    HaveServer = true;
                    SendOpenConnectionRequest1();

                    break;
                }

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_1:
                {
                    OpenConnectionReply1 incoming = (OpenConnectionReply1)message;
                    _mtuSize = incoming.mtuSize;
                    //if (incoming.mtuSize < _mtuSize) throw new Exception("Error:" + incoming.mtuSize);
                    SendOpenConnectionRequest2();
                    break;
                }

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_2:
                {
                    OpenConnectionReply2 incoming = (OpenConnectionReply2)message;
                    //_mtuSize = incoming.mtuSize;
                    SendConnectionRequest();
                    break;
                }
                }
            }
            else
            {
                DatagramHeader header = new DatagramHeader(receiveBytes[0]);
                if (!header.isACK && !header.isNAK && header.isValid)
                {
                    if (receiveBytes[0] == 0xa0)
                    {
                        throw new Exception("Receive ERROR, NAK in wrong place");
                    }

                    //ConnectedPackage package = ConnectedPackage.CreateObject();
                    ConnectedPackage package = new ConnectedPackage();
                    package.Decode(receiveBytes);
                    header = package._datagramHeader;
                    //Log.Debug(">\tReceive Datagram #" + package._datagramSequenceNumber.IntValue());
                    Log.Debug($"> Datagram #{header.datagramSequenceNumber}, {package._hasSplit}, {package._splitPacketId}, {package._reliability}, {package._reliableMessageNumber}, {package._sequencingIndex}, {package._orderingChannel}, {package._orderingIndex}");

                    var messages = package.Messages;

                    //Reliability reliability = package._reliability;
                    //if (reliability == Reliability.Reliable
                    //	|| reliability == Reliability.ReliableSequenced
                    //	|| reliability == Reliability.ReliableOrdered
                    //	)
                    {
                        if (header.datagramSequenceNumber == 1000)
                        {
                            Log.Error("Datagram 1000 ignored");
                        }
                        else
                        {
                            // Send ACK
                            Acks ack = new Acks();
                            ack.acks.Add(package._datagramSequenceNumber.IntValue());
                            byte[] data = ack.Encode();
                            //Log.Info("Send ACK #" + package._datagramSequenceNumber.IntValue());
                            SendData(data, senderEndpoint);
                        }
                    }

                    //if (LoginSent) return; //HACK

                    foreach (var message in messages)
                    {
                        if (message is SplitPartPackage)
                        {
                            lock (Session.SyncRoot)
                            {
                                HandleSplitMessage(Session, package, (SplitPartPackage)message);
                            }

                            continue;
                        }

                        message.Timer.Restart();
                        HandlePackage(message);
                        message.PutPool();
                    }

                    //package.PutPool();
                }
                else if (header.isPacketPair)
                {
                    Log.Warn("header.isPacketPair");
                }
                else if (header.isACK && header.isValid)
                {
                    HandleAck(receiveBytes, senderEndpoint);
                }
                else if (header.isNAK && header.isValid)
                {
                    Nak nak = new Nak();
                    nak.Decode(receiveBytes);
                    HandleNak(receiveBytes, senderEndpoint);
                }
                else if (!header.isValid)
                {
                    Log.Warn("!!!! ERROR, Invalid header !!!!!");
                }
                else
                {
                    Log.Warn("!! WHAT THE F");
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Processes a message.
        /// </summary>
        /// <param name="receiveBytes">The received bytes.</param>
        /// <param name="senderEndpoint">The sender's endpoint.</param>
        /// <exception cref="System.Exception">Receive ERROR, NAK in wrong place</exception>
        private void ProcessMessage(byte[] receiveBytes, IPEndPoint senderEndpoint)
        {
            //_serverEndpoint = senderEndpoint;
            byte msgId = receiveBytes[0];

            if (msgId <= (byte)DefaultMessageIdTypes.ID_USER_PACKET_ENUM)
            {
                DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes)msgId;

                Package message = PackageFactory.CreatePackage(msgId, receiveBytes);

                if (message == null)
                {
                    return;
                }

                TraceReceive(message);

                switch (msgIdType)
                {
                case DefaultMessageIdTypes.ID_UNCONNECTED_PONG:
                {
                    //Thread.Sleep(50);
                    UnconnectedPong incoming = (UnconnectedPong)message;
                    SendOpenConnectionRequest1();

                    break;
                }

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_1:
                {
                    //Thread.Sleep(50);
                    OpenConnectionReply1 incoming = (OpenConnectionReply1)message;
                    if (incoming.mtuSize < _mtuSize)
                    {
                        throw new Exception("Error:" + incoming.mtuSize);
                    }
                    SendOpenConnectionRequest2();
                    break;
                }

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_2:
                {
                    OpenConnectionReply2 incoming = (OpenConnectionReply2)message;
                    //Thread.Sleep(50);
                    //_mtuSize = incoming.mtuSize;
                    SendConnectionRequest();
                    break;
                }

                case DefaultMessageIdTypes.ID_CONNECTION_REQUEST_ACCEPTED:
                {
                    //Thread.Sleep(50);
                    SendNewIncomingConnection();
                    var t1 = new Timer(state => SendConnectedPing(), null, 0, 5000);
                    //Thread.Sleep(50);
                    SendLogin("Client12");
                    break;
                }
                }
            }
            else
            {
                DatagramHeader header = new DatagramHeader(receiveBytes[0]);
                if (!header.isACK && !header.isNAK && header.isValid)
                {
                    if (receiveBytes[0] == 0xa0)
                    {
                        throw new Exception("Receive ERROR, NAK in wrong place");
                    }

                    ConnectedPackage package = ConnectedPackage.CreateObject();
                    package.Decode(receiveBytes);
                    Log.Debug(">\tReceive Datagram #" + package._datagramSequenceNumber.IntValue());

                    var messages = package.Messages;

                    //Log.Debug("Received package: #" + package._datagramSequenceNumber.IntValue());

                    Reliability reliability = package._reliability;
                    //Log.InfoFormat("Reliability: {0}", reliability);

                    //if (reliability == Reliability.Reliable
                    //	|| reliability == Reliability.ReliableSequenced
                    //	|| reliability == Reliability.ReliableOrdered
                    //	)
                    {
                        // Send ACK
                        Acks ack = new Acks();
                        ack.acks.Add(package._datagramSequenceNumber.IntValue());
                        byte[] data = ack.Encode();
                        //Log.Debug("<\tSend ACK on #" + package._datagramSequenceNumber.IntValue());
                        SendData(data, senderEndpoint);
                    }

                    foreach (var message in messages)
                    {
                        if (message is SplitPartPackage)
                        {
                            SplitPartPackage splitMessage = message as SplitPartPackage;

                            int spId    = package._splitPacketId;
                            int spIdx   = package._splitPacketIndex;
                            int spCount = package._splitPacketCount;

                            Log.DebugFormat("Got split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);

                            if (!_splits.ContainsKey(spId))
                            {
                                _splits[spId] = new SplitPartPackage[spCount];
                            }
                            else
                            {
                                Log.WarnFormat("Resent split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);
                            }

                            SplitPartPackage[] spPackets = _splits[spId];
                            spPackets[spIdx] = splitMessage;

                            bool haveEmpty = false;
                            for (int i = 0; i < spPackets.Length; i++)
                            {
                                haveEmpty = haveEmpty || spPackets[i] == null;
                            }

                            if (!haveEmpty)
                            {
                                Log.WarnFormat("Got all {0} split packages for split ID: {1}", spCount, spId);

                                MemoryStream stream = new MemoryStream();
                                for (int i = 0; i < spPackets.Length; i++)
                                {
                                    byte[] buf = spPackets[i].Message;
                                    stream.Write(buf, 0, buf.Length);
                                }

                                try
                                {
                                    byte[] buffer      = stream.ToArray();
                                    var    fullMessage = PackageFactory.CreatePackage(buffer[0], buffer) ?? new UnknownPackage(buffer[0], buffer);
                                    Log.Debug("Processing split-message");
                                    HandlePackage(fullMessage, senderEndpoint);
                                    fullMessage.PutPool();
                                }
                                catch (Exception e)
                                {
                                    Log.Warn("When processing split-message", e);
                                }
                            }

                            message.PutPool();
                            return;
                        }

                        {
                            message.Timer.Restart();
                            HandlePackage(message, senderEndpoint);
                            //message.PutPool();
                        }
                    }

                    //package.PutPool();
                }
                else if (header.isPacketPair)
                {
                    Log.Warn("header.isPacketPair");
                }
                else if (header.isACK && header.isValid)
                {
                    HandleAck(receiveBytes, senderEndpoint);
                }
                else if (header.isNAK && header.isValid)
                {
                    Nak nak = new Nak();
                    nak.Decode(receiveBytes);

                    Log.Warn("!!!! NAK !!!!!" + nak.sequenceNumber.IntValue());
                    HandleNak(receiveBytes, senderEndpoint);
                }
                else if (!header.isValid)
                {
                    Log.Warn("!!!! ERROR, Invalid header !!!!!");
                }
                else
                {
                    Log.Warn("!! WHAT THE F");
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Processes a message.
        /// </summary>
        /// <param name="receiveBytes">The received bytes.</param>
        /// <param name="senderEndpoint">The sender's endpoint.</param>
        /// <exception cref="System.Exception">Receive ERROR, NAK in wrong place</exception>
        private void ProcessMessage(byte[] receiveBytes, IPEndPoint senderEndpoint)
        {
            //_serverEndpoint = senderEndpoint;
            byte msgId = receiveBytes[0];

            if (msgId <= (byte) DefaultMessageIdTypes.ID_USER_PACKET_ENUM)
            {
                DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes) msgId;

                Package message = PackageFactory.CreatePackage(msgId, receiveBytes);

                if (message == null) return;

                TraceReceive(message);

                switch (msgIdType)
                {
                    case DefaultMessageIdTypes.ID_UNCONNECTED_PONG:
                    {
                        //Thread.Sleep(50);
                        UnconnectedPong incoming = (UnconnectedPong) message;
                        SendOpenConnectionRequest1();

                        break;
                    }
                    case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_1:
                    {
                        //Thread.Sleep(50);
                        OpenConnectionReply1 incoming = (OpenConnectionReply1) message;
                        if (incoming.mtuSize < _mtuSize) throw new Exception("Error:" + incoming.mtuSize);
                        SendOpenConnectionRequest2();
                        break;
                    }
                    case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_2:
                    {
                        OpenConnectionReply2 incoming = (OpenConnectionReply2) message;
                        //Thread.Sleep(50);
                        //_mtuSize = incoming.mtuSize;
                        SendConnectionRequest();
                        break;
                    }
                    case DefaultMessageIdTypes.ID_CONNECTION_REQUEST_ACCEPTED:
                    {
                        //Thread.Sleep(50);
                        SendNewIncomingConnection();
                        var t1 = new Timer(state => SendConnectedPing(), null, 0, 5000);
                        //Thread.Sleep(50);
                        SendLogin("Client12");
                        break;
                    }
                }
            }
            else
            {
                DatagramHeader header = new DatagramHeader(receiveBytes[0]);
                if (!header.isACK && !header.isNAK && header.isValid)
                {
                    if (receiveBytes[0] == 0xa0)
                    {
                        throw new Exception("Receive ERROR, NAK in wrong place");
                    }

                    ConnectedPackage package = ConnectedPackage.CreateObject();
                    package.Decode(receiveBytes);
                    Log.Debug(">\tReceive Datagram #" + package._datagramSequenceNumber.IntValue());

                    var messages = package.Messages;

                    //Log.Debug("Received package: #" + package._datagramSequenceNumber.IntValue());

                    Reliability reliability = package._reliability;
                    //Log.InfoFormat("Reliability: {0}", reliability);

                    //if (reliability == Reliability.Reliable
                    //	|| reliability == Reliability.ReliableSequenced
                    //	|| reliability == Reliability.ReliableOrdered
                    //	)
                    {
                        // Send ACK
                        Acks ack = new Acks();
                        ack.acks.Add(package._datagramSequenceNumber.IntValue());
                        byte[] data = ack.Encode();
                        //Log.Debug("<\tSend ACK on #" + package._datagramSequenceNumber.IntValue());
                        SendData(data, senderEndpoint);
                    }

                    foreach (var message in messages)
                    {
                        if (message is SplitPartPackage)
                        {
                            SplitPartPackage splitMessage = message as SplitPartPackage;

                            int spId = package._splitPacketId;
                            int spIdx = package._splitPacketIndex;
                            int spCount = package._splitPacketCount;

                            Log.DebugFormat("Got split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);

                            if (!_splits.ContainsKey(spId))
                            {
                                _splits[spId] = new SplitPartPackage[spCount];
                            }
                            else
                            {
                                Log.WarnFormat("Resent split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);
                            }

                            SplitPartPackage[] spPackets = _splits[spId];
                            spPackets[spIdx] = splitMessage;

                            bool haveEmpty = false;
                            for (int i = 0; i < spPackets.Length; i++)
                            {
                                haveEmpty = haveEmpty || spPackets[i] == null;
                            }

                            if (!haveEmpty)
                            {
                                Log.WarnFormat("Got all {0} split packages for split ID: {1}", spCount, spId);

                                MemoryStream stream = new MemoryStream();
                                for (int i = 0; i < spPackets.Length; i++)
                                {
                                    byte[] buf = spPackets[i].Message;
                                    stream.Write(buf, 0, buf.Length);
                                }

                                try
                                {
                                    byte[] buffer = stream.ToArray();
                                    var fullMessage = PackageFactory.CreatePackage(buffer[0], buffer) ?? new UnknownPackage(buffer[0], buffer);
                                    Log.Debug("Processing split-message");
                                    HandlePackage(fullMessage, senderEndpoint);
                                    fullMessage.PutPool();
                                }
                                catch (Exception e)
                                {
                                    Log.Warn("When processing split-message", e);
                                }
                            }

                            message.PutPool();
                            return;
                        }

                        {
                            message.Timer.Restart();
                            HandlePackage(message, senderEndpoint);
                            //message.PutPool();
                        }
                    }

                    //package.PutPool();
                }
                else if (header.isPacketPair)
                {
                    Log.Warn("header.isPacketPair");
                }
                else if (header.isACK && header.isValid)
                {
                    HandleAck(receiveBytes, senderEndpoint);
                }
                else if (header.isNAK && header.isValid)
                {
                    Nak nak = new Nak();
                    nak.Decode(receiveBytes);

                    Log.Warn("!!!! NAK !!!!!" + nak.sequenceNumber.IntValue());
                    HandleNak(receiveBytes, senderEndpoint);
                }
                else if (!header.isValid)
                {
                    Log.Warn("!!!! ERROR, Invalid header !!!!!");
                }
                else
                {
                    Log.Warn("!! WHAT THE F");
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Processes a message.
        /// </summary>
        /// <param name="receiveBytes">The received bytes.</param>
        /// <param name="senderEndpoint">The sender's endpoint.</param>
        /// <exception cref="System.Exception">Receive ERROR, NAK in wrong place</exception>
        private void ProcessMessage(byte[] receiveBytes, IPEndPoint senderEndpoint)
        {
            byte msgId = receiveBytes[0];

            //Log.DebugFormat("Recieve {0} 0x{0:x2} len: {1}", msgId, receiveBytes.Length);

            if (msgId <= (byte)DefaultMessageIdTypes.ID_USER_PACKET_ENUM)
            {
                DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes)msgId;

                Package message = PackageFactory.CreatePackage(msgId, receiveBytes);

                if (message == null)
                {
                    return;
                }

                TraceReceive(message);

                switch (msgIdType)
                {
                case DefaultMessageIdTypes.ID_UNCONNECTED_PONG:
                {
                    UnconnectedPong incoming = (UnconnectedPong)message;
                    SendOpenConnectionRequest1();

                    break;
                }

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_1:
                {
                    OpenConnectionReply1 incoming = (OpenConnectionReply1)message;
                    _mtuSize = incoming.mtuSize;
                    //if (incoming.mtuSize < _mtuSize) throw new Exception("Error:" + incoming.mtuSize);
                    SendOpenConnectionRequest2();
                    break;
                }

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_2:
                {
                    OpenConnectionReply2 incoming = (OpenConnectionReply2)message;
                    //_mtuSize = incoming.mtuSize;
                    SendConnectionRequest();
                    break;
                }
                }
            }
            else
            {
                DatagramHeader header = new DatagramHeader(receiveBytes[0]);
                if (!header.isACK && !header.isNAK && header.isValid)
                {
                    if (receiveBytes[0] == 0xa0)
                    {
                        throw new Exception("Receive ERROR, NAK in wrong place");
                    }

                    //ConnectedPackage package = ConnectedPackage.CreateObject();
                    ConnectedPackage package = new ConnectedPackage();
                    package.Decode(receiveBytes);
                    //Log.Debug(">\tReceive Datagram #" + package._datagramSequenceNumber.IntValue());

                    var messages = package.Messages;

                    //Reliability reliability = package._reliability;
                    //if (reliability == Reliability.Reliable
                    //	|| reliability == Reliability.ReliableSequenced
                    //	|| reliability == Reliability.ReliableOrdered
                    //	)
                    {
                        // Send ACK
                        Acks ack = new Acks();
                        ack.acks.Add(package._datagramSequenceNumber.IntValue());
                        byte[] data = ack.Encode();
                        //Log.Info("Send ACK #" + package._datagramSequenceNumber.IntValue());
                        SendData(data, senderEndpoint);
                    }

                    //if (LoginSent) return; //HACK

                    foreach (var message in messages)
                    {
                        if (message is SplitPartPackage)
                        {
                            lock (Session.SyncRoot)
                            {
                                var splits = Session.Splits;
                                SplitPartPackage splitMessage = message as SplitPartPackage;

                                int spId    = package._splitPacketId;
                                int spIdx   = package._splitPacketIndex;
                                int spCount = package._splitPacketCount;

                                Log.DebugFormat("Got split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);

                                if (!splits.ContainsKey(spId))
                                {
                                    splits.Add(spId, new SplitPartPackage[spCount]);
                                }

                                SplitPartPackage[] spPackets = splits[spId];
                                if (spIdx < 0 || spIdx >= spPackets.Length)
                                {
                                    Log.DebugFormat("Unexpeted split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);
                                    continue;
                                }

                                if (splitMessage.Message == null)
                                {
                                    Log.DebugFormat("Empty split package");
                                    continue;
                                }

                                spPackets[spIdx] = splitMessage;

                                bool haveEmpty = false;
                                for (int i = 0; i < spPackets.Length; i++)
                                {
                                    haveEmpty = haveEmpty || spPackets[i] == null;
                                }

                                if (!haveEmpty)
                                {
                                    Log.DebugFormat("Got all {0} split packages for split ID: {1}", spCount, spId);

                                    MemoryStream stream = new MemoryStream();
                                    for (int i = 0; i < spPackets.Length; i++)
                                    {
                                        byte[] buf = spPackets[i].Message;
                                        stream.Write(buf, 0, buf.Length);
                                    }

                                    try
                                    {
                                        byte[] buffer = stream.ToArray();
                                        Log.DebugFormat("Processing split-message 0x{1:x2}, lenght={0}", buffer.Length, buffer[0]);
                                        Package fullMessage = PackageFactory.CreatePackage(buffer[0], buffer) ?? new UnknownPackage(buffer[0], buffer);
                                        HandlePackage(fullMessage, senderEndpoint);
                                        fullMessage.PutPool();
                                        continue;
                                    }
                                    catch (Exception e)
                                    {
                                        Log.Error("When processing split-message", e);
                                    }
                                }

                                message.PutPool();
                                continue;
                            }
                        }
                        else
                        {
                            message.Timer.Restart();
                            HandlePackage(message, senderEndpoint);
                            message.PutPool();
                        }
                    }

                    //package.PutPool();
                }
                else if (header.isPacketPair)
                {
                    Log.Warn("header.isPacketPair");
                }
                else if (header.isACK && header.isValid)
                {
                    HandleAck(receiveBytes, senderEndpoint);
                }
                else if (header.isNAK && header.isValid)
                {
                    Nak nak = new Nak();
                    nak.Decode(receiveBytes);

                    Log.Warn("!!!! NAK !!!!!" + nak.sequenceNumber.IntValue());
                    HandleNak(receiveBytes, senderEndpoint);
                }
                else if (!header.isValid)
                {
                    Log.Warn("!!!! ERROR, Invalid header !!!!!");
                }
                else
                {
                    Log.Warn("!! WHAT THE F");
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        ///     Processes a message.
        /// </summary>
        /// <param name="receiveBytes">The received bytes.</param>
        /// <param name="senderEndpoint">The sender's endpoint.</param>
        /// <exception cref="System.Exception">Receive ERROR, NAK in wrong place</exception>
        private void ProcessMessage(byte[] receiveBytes, IPEndPoint senderEndpoint)
        {
            byte msgId = receiveBytes[0];

            //Log.DebugFormat("Recieve {0} 0x{0:x2} len: {1}", msgId, receiveBytes.Length);

            if (msgId <= (byte) DefaultMessageIdTypes.ID_USER_PACKET_ENUM)
            {
                DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes) msgId;

                Package message = PackageFactory.CreatePackage(msgId, receiveBytes);

                if (message == null) return;

                TraceReceive(message);

                switch (msgIdType)
                {
                    case DefaultMessageIdTypes.ID_UNCONNECTED_PONG:
                    {
                        UnconnectedPong incoming = (UnconnectedPong) message;
                        SendOpenConnectionRequest1();

                        break;
                    }
                    case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_1:
                    {
                        OpenConnectionReply1 incoming = (OpenConnectionReply1) message;
                        _mtuSize = incoming.mtuSize;
                        //if (incoming.mtuSize < _mtuSize) throw new Exception("Error:" + incoming.mtuSize);
                        SendOpenConnectionRequest2();
                        break;
                    }
                    case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_2:
                    {
                        OpenConnectionReply2 incoming = (OpenConnectionReply2) message;
                        //_mtuSize = incoming.mtuSize;
                        SendConnectionRequest();
                        break;
                    }
                }
            }
            else
            {
                DatagramHeader header = new DatagramHeader(receiveBytes[0]);
                if (!header.isACK && !header.isNAK && header.isValid)
                {
                    if (receiveBytes[0] == 0xa0)
                    {
                        throw new Exception("Receive ERROR, NAK in wrong place");
                    }

                    //ConnectedPackage package = ConnectedPackage.CreateObject();
                    ConnectedPackage package = new ConnectedPackage();
                    package.Decode(receiveBytes);
                    //Log.Debug(">\tReceive Datagram #" + package._datagramSequenceNumber.IntValue());

                    var messages = package.Messages;

                    //Reliability reliability = package._reliability;
                    //if (reliability == Reliability.Reliable
                    //	|| reliability == Reliability.ReliableSequenced
                    //	|| reliability == Reliability.ReliableOrdered
                    //	)
                    {
                        // Send ACK
                        Acks ack = new Acks();
                        ack.acks.Add(package._datagramSequenceNumber.IntValue());
                        byte[] data = ack.Encode();
                        //Log.Info("Send ACK #" + package._datagramSequenceNumber.IntValue());
                        SendData(data, senderEndpoint);
                    }

                    //if (LoginSent) return; //HACK

                    foreach (var message in messages)
                    {
                        if (message is SplitPartPackage)
                        {
                            lock (Session.SyncRoot)
                            {
                                var splits = Session.Splits;
                                SplitPartPackage splitMessage = message as SplitPartPackage;

                                int spId = package._splitPacketId;
                                int spIdx = package._splitPacketIndex;
                                int spCount = package._splitPacketCount;

                                Log.DebugFormat("Got split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);

                                if (!splits.ContainsKey(spId))
                                {
                                    splits.Add(spId, new SplitPartPackage[spCount]);
                                }

                                SplitPartPackage[] spPackets = splits[spId];
                                if (spIdx < 0 || spIdx >= spPackets.Length)
                                {
                                    Log.DebugFormat("Unexpeted split package {2} (of {0}) for split ID: {1}", spCount, spId, spIdx);
                                    continue;
                                }

                                if (splitMessage.Message == null)
                                {
                                    Log.DebugFormat("Empty split package");
                                    continue;
                                }

                                spPackets[spIdx] = splitMessage;

                                bool haveEmpty = false;
                                for (int i = 0; i < spPackets.Length; i++)
                                {
                                    haveEmpty = haveEmpty || spPackets[i] == null;
                                }

                                if (!haveEmpty)
                                {
                                    Log.DebugFormat("Got all {0} split packages for split ID: {1}", spCount, spId);

                                    MemoryStream stream = new MemoryStream();
                                    for (int i = 0; i < spPackets.Length; i++)
                                    {
                                        byte[] buf = spPackets[i].Message;
                                        stream.Write(buf, 0, buf.Length);
                                    }

                                    try
                                    {
                                        byte[] buffer = stream.ToArray();
                                        Log.DebugFormat("Processing split-message 0x{1:x2}, lenght={0}", buffer.Length, buffer[0]);
                                        Package fullMessage = PackageFactory.CreatePackage(buffer[0], buffer) ?? new UnknownPackage(buffer[0], buffer);
                                        HandlePackage(fullMessage, senderEndpoint);
                                        fullMessage.PutPool();
                                        continue;
                                    }
                                    catch (Exception e)
                                    {
                                        Log.Error("When processing split-message", e);
                                    }
                                }

                                message.PutPool();
                                continue;
                            }
                        }
                        else
                        {
                            message.Timer.Restart();
                            HandlePackage(message, senderEndpoint);
                            message.PutPool();
                        }
                    }

                    //package.PutPool();
                }
                else if (header.isPacketPair)
                {
                    Log.Warn("header.isPacketPair");
                }
                else if (header.isACK && header.isValid)
                {
                    HandleAck(receiveBytes, senderEndpoint);
                }
                else if (header.isNAK && header.isValid)
                {
                    Nak nak = new Nak();
                    nak.Decode(receiveBytes);
                    HandleNak(receiveBytes, senderEndpoint);
                }
                else if (!header.isValid)
                {
                    Log.Warn("!!!! ERROR, Invalid header !!!!!");
                }
                else
                {
                    Log.Warn("!! WHAT THE F");
                }
            }
        }