public int ExecuteSend(NetBase netBase, NetBuffer buffer, NetConnection connection, IPEndPoint remoteEP)
		{
			int len = buffer.LengthBytes;
#if DEBUG
			if (connection != null)
			{
				NetConnectionConfiguration config = connection.Configuration;

				if (config.LossChance > 0.0f && NetRandom.Default.Chance(config.LossChance))
				{
					//m_log.Debug("(simulating loss of sent packet)");
					return len;
				}
				
				if (config.LagDelayChance > 0.0f && NetRandom.Default.Chance(config.LagDelayChance))
				{
					float delayAmount = config.LagDelayMinimum + (NetRandom.Default.NextFloat() * config.LagDelayVariance);

					DelayedPacket pk = new DelayedPacket();
					pk.Data = new byte[len];
					Array.Copy(buffer.Data, pk.Data, buffer.LengthBytes);
					pk.DelayAmount = delayAmount;
					pk.DelayedUntil = NetTime.Now + delayAmount;
					pk.RemoteEP = remoteEP;
					m_delayed.Add(pk);

					//m_log.Debug("(queueing packet for " + (int)(pk.DelayAmount * 1000.0f) + " ms)");

					return len;
				}
			}
#endif

			try
			{
				int bytesSent = netBase.m_socket.SendTo(buffer.Data, 0, len, SocketFlags.None, remoteEP);
				m_log.Verbose(string.Format(CultureInfo.InvariantCulture, "Sent {0} bytes to {1}", bytesSent, remoteEP));

#if DEBUG
				if (connection != null)
				{
					NetConnectionConfiguration config = connection.Configuration;
					if (NetRandom.Default.Chance(config.DuplicatedPacketChance))
					{
						m_log.Debug("(simulating send packet duplication)");
						netBase.m_socket.SendTo(buffer.Data, 0, buffer.LengthBytes, SocketFlags.None, remoteEP);
					}
				}
#endif

				return bytesSent;
			}
			catch (SocketException sex)
			{
				if (sex.SocketErrorCode == SocketError.ConnectionReset ||
					sex.SocketErrorCode == SocketError.ConnectionRefused ||
					sex.SocketErrorCode == SocketError.ConnectionAborted)
				{
					m_log.Warning("Remote socket forcefully closed: " + sex.SocketErrorCode);
					if (connection != null)
						connection.Disconnect("Socket forcefully closed: " + sex.SocketErrorCode);
					return 0;
				}

				m_log.Warning("Execute SocketException: " + sex.SocketErrorCode);
				return 0;
			}
		}
示例#2
0
      protected void HdlLogin(BBMessage msg, NetConnection sender)
      {         
         // Test if login is possible
         if (PList.GetPlayer(msg.PlayerCarac.Nick, null) != null)
         {
            // No : player already exists   
            BBMessage msg_back = new BBMessage();
            msg_back.MsgNoLogin("Player \"" + msg.PlayerCarac.Nick + "\" already exists");
            Server.SendMessage(msg_back.GetNetMessage(), sender, NetChannel.ReliableUnordered);

            // And close the connection
            sender.Disconnect("Player \"" + msg.PlayerCarac.Nick + "\" already exists, Login refused");
         }
         else
         {
            if ((msg.Param1 == null) || (msg.Param1 != BBMessage.Version))
            {
               BBMessage msg_back1 = new BBMessage();
               string str = msg.Param1;
               if (str == null)
                  str = "No version";

               msg_back1.MsgNoLogin("Player \"" + msg.PlayerCarac.Nick + "\" , Login refused. Version Serveur = " + BBMessage.Version + " Version Client = " + str);
               Server.SendMessage(msg_back1.GetNetMessage(), sender, NetChannel.ReliableUnordered);

               //sender.Disconnect("Player \"" + msg.PlayerCarac.Nick + "\" , Login refused. Version Serveur = " + BBMessage.Version + " Version Client = " + msg.Param1.ToString());
            }
            else
            {
               // Yes : Add the player to the list and send OK
               // Now add the player to our list
               int id = PList.CreatePlayer(msg.PlayerCarac, sender);
               Log.Info("Player added : " + msg.PlayerCarac.Nick + ", " + id);

               // Send OK
               msg.MsgOkLogin(id);
               Server.SendMessage(msg.GetNetMessage(), sender, NetChannel.ReliableUnordered);

               // Send connection of other clients to our new client
               IDictionaryEnumerator en = PList.GetPlayerEnum();
               BBMessage msg_notice;
               while (en.MoveNext())
               {
                  if (((Player)en.Value).Carac.ActorID != id)
                  {
                     msg_notice = new BBMessage();
                     msg_notice.MsgAddPlayer((Player)en.Value, false);
                     Server.SendMessage(msg_notice.GetNetMessage(), sender, NetChannel.ReliableUnordered);
                  }

               }

               // Notify other clients
               BBMessage msg_back = new BBMessage();
               msg_back.MsgAddPlayer(PList.GetPlayer(id), true);
               BroadcastMsgExcept(msg_back.GetNetMessage(), NetChannel.ReliableUnordered, id);

               // Notify
               NotifyClientConnected(id);
            }
         }
      }
示例#3
0
        internal int ExecuteSend(NetBuffer buffer, NetConnection connection, IPEndPoint remoteEP)
        {
            if (buffer.LengthBytes < 0)
            {
                Log.Warning("ExecuteSend passed 0 bytes to send");
                return 0;
            }

            #if DEBUG
            if (connection != null)
            {
                if (connection.Configuration.SimulateLagLoss && m_lagLoss != null)
                    return m_lagLoss.ExecuteSend(this, buffer, connection, remoteEP);
            }
            #endif
            // encrypt
            if (connection != null && connection.m_encryption.SymmetricEncryptionKeyBytes != null)
            {
                // Log.Debug("SEND: Encrypting packet using key: " + Convert.ToBase64String(connection.SymmetricEncryptionKey));
                connection.m_encryption.EncryptSymmetric(buffer);
            }

            try
            {
                int bytesSent = m_socket.SendTo(buffer.Data, 0, buffer.LengthBytes, SocketFlags.None, remoteEP);

                Debug.Assert(bytesSent == buffer.LengthBytes, "Ouch, sent partial UDP message?!");

                //Log.Verbose(string.Format(CultureInfo.InvariantCulture, "Sent {0} bytes to {1}", bytesSent, remoteEP));
                return bytesSent;
            }
            catch (SocketException sex)
            {
                if (sex.SocketErrorCode == SocketError.WouldBlock)
                {
                    // send buffer overflow?
            #if DEBUG
                    Log.Error("SocketException.WouldBlock thrown during sending; send buffer overflow? Increase buffer using NetAppConfiguration.SendBufferSize");
                    throw new NetException("SocketException.WouldBlock thrown during sending; send buffer overflow? Increase buffer using NetAppConfiguration.SendBufferSize", sex);
            #else
                    // let reliability handle it, but log warning
                    Log.Warning("Network send buffer overflow");
            #endif
                }

                if (sex.SocketErrorCode == SocketError.ConnectionReset ||
                    sex.SocketErrorCode == SocketError.ConnectionRefused ||
                    sex.SocketErrorCode == SocketError.ConnectionAborted)
                {
                    Log.Warning("Remote socket forcefully closed: " + sex.SocketErrorCode);
                    if (connection != null)
                        connection.Disconnect("Socket forcefully closed: " + sex.SocketErrorCode);
                    return 0;
                }

                Log.Warning("Execute SocketException: " + sex.SocketErrorCode);
                return 0;
            }
        }