示例#1
0
 internal NetConnection(NetBase parent, IPEndPoint remote)
 {
     m_parent                = parent;
     m_remoteEndpoint        = remote;
     m_nextSequenceNumber    = new ushort[NetConstants.NumSequenceChannels];
     m_sequenceHandler       = new NetSequenceHandler();
     m_unsentMessages        = new Queue <NetMessage>(10);
     m_receivedMessages      = new Queue <NetMessage>(10);
     m_withheldMessages      = new List <NetMessage>();
     m_unsentAcknowledges    = new Queue <NetMessage>(10);
     m_lastHeardFromRemote   = (float)NetTime.Now;
     m_reusedAckMessage      = new NetMessage(NetMessageType.Acknowledge, null);
     m_savedReliableMessages = new List <NetMessage> [NetConstants.NumSequenceChannels];
     Configuration           = new NetConnectionConfiguration(this);
     m_ping        = new NetPing(this);
     Statistics    = new NetStatistics(this);
     m_stringTable = new NetStringTable();
     m_encryption  = new NetEncryption();
     if (parent.Configuration.UsesEncryption)
     {
         m_encryption.SetRSAKey(parent.Configuration.ServerPublicKeyBytes, parent.Configuration.ServerPrivateKeyBytes);
     }
 }
示例#2
0
		internal NetConnection(NetBase parent, IPEndPoint remote)
		{
			m_parent = parent;
			m_remoteEndpoint = remote;
			m_nextSequenceNumber = new ushort[NetConstants.NumSequenceChannels];
			m_sequenceHandler = new NetSequenceHandler();
			m_unsentMessages = new Queue<NetMessage>(10);
			m_receivedMessages = new Queue<NetMessage>(10);
			m_withheldMessages = new List<NetMessage>();
			m_unsentAcknowledges = new Queue<NetMessage>(10);
			m_lastHeardFromRemote = (float)NetTime.Now;
			m_reusedAckMessage = new NetMessage(NetMessageType.Acknowledge, null);
			m_savedReliableMessages = new List<NetMessage>[NetConstants.NumSequenceChannels];
			Configuration = new NetConnectionConfiguration(this);
			m_ping = new NetPing(this);
			Statistics = new NetStatistics(this);
			m_stringTable = new NetStringTable();
			m_encryption = new NetEncryption();
			if (parent.Configuration.UsesEncryption)
				m_encryption.SetRSAKey(parent.Configuration.ServerPublicKeyBytes, parent.Configuration.ServerPrivateKeyBytes);
		}
示例#3
0
        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);
            }
        }