Esempio n. 1
0
        internal void SendPong(int pingNumber)
        {
            m_peer.VerifyNetworkThread();

            NetOutgoingMessage om = m_peer.CreateMessage(5);

            om.Write((byte)pingNumber);
            om.Write((float)NetTime.Now);             // we should update this value to reflect the exact point in time the packet is SENT
            om.m_messageType = NetMessageType.Pong;

            int len = om.Encode(m_peer.m_sendBuffer, 0, 0);

            m_peer.SendPacket(len, m_remoteEndPoint, 1, out var connectionReset);

            m_statistics.PacketSent(len, 1);
            m_peer.Recycle(om);
        }
Esempio n. 2
0
        private bool SendOutMessage(NetOutgoingMessage msg, IPEndPoint endPoint)
        {
            byte[] data = new byte[msg.GetEncodedSize()];
            int    len  = msg.Encode(data, 0, 0);

            try
            {
                peer.Send(data, data.Length, endPoint);
            }
            catch (Exception e)
            {
                Debug.LogError("Cannot send message to MS. \n" + e.ToString());
                // if udp client has a connection socket, than forget it
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        internal void SendPing()
        {
            m_peer.VerifyNetworkThread();

            m_sentPingNumber++;

            m_sentPingTime = NetTime.Now;
            NetOutgoingMessage om = m_peer.CreateMessage(1);

            om.Write((byte)m_sentPingNumber);             // truncating to 0-255
            om.m_messageType = NetMessageType.Ping;

            int len = om.Encode(m_peer.m_sendBuffer, 0, 0);

            m_peer.SendPacket(len, m_remoteEndPoint, 1, out var connectionReset);

            m_statistics.PacketSent(len, 1);
            m_peer.Recycle(om);
        }
Esempio n. 4
0
        private void SendExpandMTU(double now, int size)
        {
            NetOutgoingMessage om = m_peer.CreateMessage(size);

            byte[] tmp = new byte[size];
            om.Write(tmp);
            om.m_messageType = NetMessageType.ExpandMTURequest;
            int len = om.Encode(m_peer.m_sendBuffer, 0, 0);

            bool ok = m_peer.SendMTUPacket(len, m_remoteEndPoint);

            if (ok == false)
            {
                //m_peer.LogDebug("Send MTU failed for size " + size);

                // failure
                if (m_smallestFailedMTU == -1 || size < m_smallestFailedMTU)
                {
                    m_smallestFailedMTU = size;
                    m_mtuAttemptFails++;
                    if (m_mtuAttemptFails >= m_peerConfiguration.ExpandMTUFailAttempts)
                    {
                        FinalizeMTU(m_largestSuccessfulMTU);
                        return;
                    }
                }
                ExpandMTU(now);
                return;
            }

            m_lastSentMTUAttemptSize = size;
            m_lastSentMTUAttemptTime = now;

            m_statistics.PacketSent(len, 1);
            m_peer.Recycle(om);
        }
            // Queue an item for immediate sending on the wire
            // This method is called from the ISenderChannels
            internal void QueueSendMessage(NetOutgoingMessage om, int seqNr)
            {
                m_peer.VerifyNetworkThread();

                int sz = om.GetEncodedSize();
                if (sz > m_currentMTU)
                    m_peer.LogWarning("Message larger than MTU! Fragmentation must have failed!");

                if (m_sendBufferWritePtr + sz > m_currentMTU)
                {
                    bool connReset; // TODO: handle connection reset
                    NetException.Assert(m_sendBufferWritePtr > 0 && m_sendBufferNumMessages > 0); // or else the message should have been fragmented earlier
                    m_peer.SendPacket(m_sendBufferWritePtr, m_remoteEndPoint, m_sendBufferNumMessages, out connReset);
                    m_statistics.PacketSent(m_sendBufferWritePtr, m_sendBufferNumMessages);
                    m_sendBufferWritePtr = 0;
                    m_sendBufferNumMessages = 0;
                }

                m_sendBufferWritePtr = om.Encode(m_peer.m_sendBuffer, m_sendBufferWritePtr, seqNr);
                m_sendBufferNumMessages++;

                NetException.Assert(m_sendBufferWritePtr > 0, "Encoded zero size message?");
                NetException.Assert(m_sendBufferNumMessages > 0);
            }
            // send message immediately
            internal void SendLibrary(NetOutgoingMessage msg, IPEndPoint recipient)
            {
                VerifyNetworkThread();
                NetException.Assert(msg.m_isSent == false);

                bool connReset;
                int len = msg.Encode(m_sendBuffer, 0, 0);
                SendPacket(len, recipient, 1, out connReset);
            }