public NetReliableOrderedReceiver(NetConnection connection, int windowSize)
     : base(connection)
 {
     m_windowSize = windowSize;
     m_withheldMessages = new NetIncomingMessage[windowSize];
     m_earlyReceived = new NetBitVector(windowSize);
 }
 public InfoJoueurMultijoueur(Personnage avatar, string gamertag, Texture2D imageJoueur, GestionPartie gestionnairePartie, bool estActif, NetConnection ip)
 {
     Gamertag = gamertag;
     if(imageJoueur != null)
         ImageJoueur = imageJoueur.Name;
     InfoGestionnairePartie = new InfoGestionPartie();
     EstActif = estActif;
     IP = ip.ToString();
 }
 internal NetReliableSenderChannel(NetConnection connection, int windowSize)
 {
     m_connection = connection;
     m_windowSize = windowSize;
     m_windowStart = 0;
     m_sendStart = 0;
     m_anyStoredResends = false;
     m_receivedAcks = new NetBitVector(NetConstants.NumSequenceNumbers);
     m_storedMessages = new NetStoredReliableMessage[m_windowSize];
     m_queuedSends = new NetQueue<NetOutgoingMessage>(8);
     m_resendDelay = m_connection.GetResendDelay();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Send a message to all connections except one
        /// </summary>
        /// <param name="msg">The message to send</param>
        /// <param name="method">How to deliver the message</param>
        /// <param name="except">Don't send to this particular connection</param>
        /// <param name="sequenceChannel">Which sequence channel to use for the message</param>
        public void SendToAll(NetOutgoingMessage msg, NetConnection except, NetDeliveryMethod method, int sequenceChannel)
        {
            var all = this.Connections;
            if (all.Count <= 0) {
                if (msg.m_isSent == false)
                    Recycle(msg);
                return;
            }

            if (except == null)
            {
                SendMessage(msg, all, method, sequenceChannel);
                return;
            }

            List<NetConnection> recipients = new List<NetConnection>(all.Count - 1);
            foreach (var conn in all)
                if (conn != except)
                    recipients.Add(conn);

            if (recipients.Count > 0)
                SendMessage(msg, recipients, method, sequenceChannel);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Send a message to a specific connection
        /// </summary>
        /// <param name="msg">The message to send</param>
        /// <param name="recipient">The recipient connection</param>
        /// <param name="method">How to deliver the message</param>
        /// <param name="sequenceChannel">Sequence channel within the delivery method</param>
        public NetSendResult SendMessage(NetOutgoingMessage msg, NetConnection recipient, NetDeliveryMethod method, int sequenceChannel)
        {
            if (msg == null)
                throw new ArgumentNullException("msg");
            if (recipient == null)
                throw new ArgumentNullException("recipient");
            if (sequenceChannel >= NetConstants.NetChannelsPerDeliveryMethod)
                throw new ArgumentOutOfRangeException("sequenceChannel");

            NetException.Assert(
                ((method != NetDeliveryMethod.Unreliable && method != NetDeliveryMethod.ReliableUnordered) ||
                ((method == NetDeliveryMethod.Unreliable || method == NetDeliveryMethod.ReliableUnordered) && sequenceChannel == 0)),
                "Delivery method " + method + " cannot use sequence channels other than 0!"
            );

            NetException.Assert(method != NetDeliveryMethod.Unknown, "Bad delivery method!");

            if (msg.m_isSent)
                throw new NetException("This message has already been sent! Use NetPeer.SendMessage() to send to multiple recipients efficiently");
            msg.m_isSent = true;

            bool suppressFragmentation = (method == NetDeliveryMethod.Unreliable || method == NetDeliveryMethod.UnreliableSequenced) && m_configuration.UnreliableSizeBehaviour != NetUnreliableSizeBehaviour.NormalFragmentation;

            int len = NetConstants.UnfragmentedMessageHeaderSize + msg.LengthBytes; // headers + length, faster than calling msg.GetEncodedSize
            if (len <= recipient.m_currentMTU || suppressFragmentation)
            {
                Interlocked.Increment(ref msg.m_recyclingCount);
                return recipient.EnqueueMessage(msg, method, sequenceChannel);
            }
            else
            {
                // message must be fragmented!
                if (recipient.m_status != NetConnectionStatus.Connected)
                    return NetSendResult.FailedNotConnected;
                return SendFragmentedMessage(msg, new NetConnection[] { recipient }, method, sequenceChannel);
            }
        }
        private void ReceivedUnconnectedLibraryMessage(double now, NetEndPoint senderEndPoint, NetMessageType tp, int ptr, int payloadByteLength)
        {
            NetConnection shake;
            if (m_handshakes.TryGetValue(senderEndPoint, out shake))
            {
                shake.ReceivedHandshake(now, tp, ptr, payloadByteLength);
                return;
            }

            //
            // Library message from a completely unknown sender; lets just accept Connect
            //
            switch (tp)
            {
                case NetMessageType.Discovery:
                    HandleIncomingDiscoveryRequest(now, senderEndPoint, ptr, payloadByteLength);
                    return;
                case NetMessageType.DiscoveryResponse:
                    HandleIncomingDiscoveryResponse(now, senderEndPoint, ptr, payloadByteLength);
                    return;
                case NetMessageType.NatIntroduction:
                    if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess))
                        HandleNatIntroduction(ptr);
                    return;
                case NetMessageType.NatPunchMessage:
                    if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.NatIntroductionSuccess))
                        HandleNatPunch(ptr, senderEndPoint);
                    return;
                case NetMessageType.ConnectResponse:

                    lock (m_handshakes)
                    {
                        foreach (var hs in m_handshakes)
                        {
                            if (hs.Key.Address.Equals(senderEndPoint.Address))
                            {
                                if (hs.Value.m_connectionInitiator)
                                {
                                    //
                                    // We are currently trying to connection to XX.XX.XX.XX:Y
                                    // ... but we just received a ConnectResponse from XX.XX.XX.XX:Z
                                    // Lets just assume the router decided to use this port instead
                                    //
                                    var hsconn = hs.Value;
                                    m_connectionLookup.Remove(hs.Key);
                                    m_handshakes.Remove(hs.Key);

                                    LogDebug("Detected host port change; rerouting connection to " + senderEndPoint);
                                    hsconn.MutateEndPoint(senderEndPoint);

                                    m_connectionLookup.Add(senderEndPoint, hsconn);
                                    m_handshakes.Add(senderEndPoint, hsconn);

                                    hsconn.ReceivedHandshake(now, tp, ptr, payloadByteLength);
                                    return;
                                }
                            }
                        }
                    }

                    LogWarning("Received unhandled library message " + tp + " from " + senderEndPoint);
                    return;
                case NetMessageType.Connect:
                    if (m_configuration.AcceptIncomingConnections == false)
                    {
                        LogWarning("Received Connect, but we're not accepting incoming connections!");
                        return;
                    }
                    // handle connect
                    // It's someone wanting to shake hands with us!

                    int reservedSlots = m_handshakes.Count + m_connections.Count;
                    if (reservedSlots >= m_configuration.m_maximumConnections)
                    {
                        // server full
                        NetOutgoingMessage full = CreateMessage("Server full");
                        full.m_messageType = NetMessageType.Disconnect;
                        SendLibrary(full, senderEndPoint);
                        return;
                    }

                    // Ok, start handshake!
                    NetConnection conn = new NetConnection(this, senderEndPoint);
                    conn.m_status = NetConnectionStatus.ReceivedInitiation;
                    m_handshakes.Add(senderEndPoint, conn);
                    conn.ReceivedHandshake(now, tp, ptr, payloadByteLength);
                    return;

                case NetMessageType.Disconnect:
                    // this is probably ok
                    LogVerbose("Received Disconnect from unconnected source: " + senderEndPoint);
                    return;
                default:
                    LogWarning("Received unhandled library message " + tp + " from " + senderEndPoint);
                    return;
            }
        }
 public NetUnreliableSequencedReceiver(NetConnection connection)
     : base(connection)
 {
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Reads a value, in local time comparable to NetTime.Now, written using WriteTime() for the connection supplied
        /// </summary>
        public double ReadTime(NetConnection connection, bool highPrecision)
        {
            double remoteTime = (highPrecision ? ReadDouble() : (double)ReadSingle());

            if (connection == null)
                throw new NetException("Cannot call ReadTime() on message without a connected sender (ie. unconnected messages)");

            // lets bypass NetConnection.GetLocalTime for speed
            return remoteTime - connection.m_remoteTimeOffset;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Send a message to a specific connection
 /// </summary>
 /// <param name="msg">The message to send</param>
 /// <param name="recipient">The recipient connection</param>
 /// <param name="method">How to deliver the message</param>
 public NetSendResult SendMessage(NetOutgoingMessage msg, NetConnection recipient, NetDeliveryMethod method)
 {
     return SendMessage(msg, recipient, method, 0);
 }
 public NetReliableSequencedReceiver(NetConnection connection, int windowSize)
     : base(connection)
 {
     m_windowSize = windowSize;
 }
 public JoueurMultijoueur(Game game, NetConnection ip)
     : base(game)
 {
     IP = ip;
 }
 public JoueurMultijoueur(Game game, NetConnection ip, NetworkClient client)
     : base(game)
 {
     Client = client;
     IP = ip;
 }
 public NetUnreliableUnorderedReceiver(NetConnection connection)
     : base(connection)
 {
     m_doFlowControl = connection.Peer.Configuration.SuppressUnreliableUnorderedAcks == false;
 }
Ejemplo n.º 14
0
        internal void AcceptConnection(NetConnection conn)
        {
            // LogDebug("Accepted connection " + conn);
            conn.InitExpandMTU(NetTime.Now);

            if (m_handshakes.Remove(conn.m_remoteEndPoint) == false)
                LogWarning("AcceptConnection called but m_handshakes did not contain it!");

            lock (m_connections)
            {
                if (m_connections.Contains(conn))
                {
                    LogWarning("AcceptConnection called but m_connection already contains it!");
                }
                else
                {
                    m_connections.Add(conn);
                    m_connectionLookup.Add(conn.m_remoteEndPoint, conn);
                }
            }
        }
 public NetReceiverChannelBase(NetConnection connection)
 {
     m_connection = connection;
     m_peer = connection.m_peer;
 }
 internal void Reset()
 {
     m_incomingMessageType = NetIncomingMessageType.Error;
     m_readPosition = 0;
     m_receivedMessageType = NetMessageType.LibraryError;
     m_senderConnection = null;
     m_bitLength = 0;
     m_isFragment = false;
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Create a connection to a remote endpoint
        /// </summary>
        public virtual NetConnection Connect(NetEndPoint remoteEndPoint, NetOutgoingMessage hailMessage)
        {
            if (remoteEndPoint == null)
                throw new ArgumentNullException("remoteEndPoint");

            lock (m_connections)
            {
                if (m_status == NetPeerStatus.NotRunning)
                    throw new NetException("Must call Start() first");

                if (m_connectionLookup.ContainsKey(remoteEndPoint))
                    throw new NetException("Already connected to that endpoint!");

                NetConnection hs;
                if (m_handshakes.TryGetValue(remoteEndPoint, out hs))
                {
                    // already trying to connect to that endpoint; make another try
                    switch (hs.m_status)
                    {
                        case NetConnectionStatus.InitiatedConnect:
                            // send another connect
                            hs.m_connectRequested = true;
                            break;
                        case NetConnectionStatus.RespondedConnect:
                            // send another response
                            hs.SendConnectResponse(NetTime.Now, false);
                            break;
                        default:
                            // weird
                            LogWarning("Weird situation; Connect() already in progress to remote endpoint; but hs status is " + hs.m_status);
                            break;
                    }
                    return hs;
                }

                NetConnection conn = new NetConnection(this, remoteEndPoint);
                conn.m_status = NetConnectionStatus.InitiatedConnect;
                conn.m_localHailMessage = hailMessage;

                // handle on network thread
                conn.m_connectRequested = true;
                conn.m_connectionInitiator = true;

                m_handshakes.Add(remoteEndPoint, conn);

                return conn;
            }
        }
		internal NetConnectionStatistics(NetConnection conn)
		{
			m_connection = conn;
			Reset();
		}
 public NetReliableUnorderedReceiver(NetConnection connection, int windowSize)
     : base(connection)
 {
     m_windowSize = windowSize;
     m_earlyReceived = new NetBitVector(windowSize);
 }