Ejemplo n.º 1
0
        public static bool VerifyPacket(VNetPacket packet)
        {
            if (packet.length < VNetPacketHeader.SizeOf())
            {
                return(false);
            }

            // RJW Endianess... todo

            // Get out the packet header
            packet.header.ReadBytes(packet.data);

            // sizes don't match
            if (packet.length != packet.header.dataLength + VNetPacketHeader.SizeOf())
            {
                return(false);
            }

            if (!IsPacketHeaderValid(packet.header))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public void SendPacketToMulti()
        {
            for (int i = 0; i <= m_packetQueueIndex; i++)
            {
                if (i == VNetCommon.NET_PACKET_QUEUE_SIZE)
                {
                    break;
                }
                VNetPacket packet = m_packetOutQueue[i];
                if (packet.header.numMessages == 0)
                {
                    return;
                }

                packet.header.clientUID = VNet.Inst.GetUID();
                packet.header.lastReliablePacketRecvd = -1;
                packet.header.sessionUID      = VNetSession.Inst.GetSessionUID();
                packet.header.identHeader     = VNetManager.Inst.GUIDHeader;
                packet.header.parentEndianess = VNetPlatform.GetEndianValue();

                // End a packet if it is the last one this frame
                if (i == m_packetQueueIndex)
                {
                    packet.EndPacket();
                }

                VNet.Inst.SendOnMultiLink(packet);
                m_clientBandwidth.AddToBandwidth(packet.header.dataLength + VNetPacketHeader.SizeOf(), packet.header.origLength + VNetPacketHeader.SizeOf());
                packet.Clear();
            }
            m_packetQueueIndex = 0;
            ResetTimeToSend();
        }
Ejemplo n.º 3
0
        private static bool IsPacketHeaderValid(VNetPacketHeader header)
        {
            // client packet
            if (header.identHeader != VNetManager.Inst.GUIDHeader)
            {
                return(false);
            }

            // packet from me
            if (header.clientUID == VNet.Inst.GetUID())
            {
                return(false);
            }

            if (header.sessionUID != VNetCommon.NET_SESSION_INVALID_UID)
            {
                // Get the current session sessionID
                UInt64 currentSession = VNetSession.Inst.GetSessionUID();
                if (currentSession == VNetCommon.NET_SESSION_INVALID_UID)
                {
                    return(true);
                }

                if (header.sessionUID == currentSession)
                {
                    return(true);
                }

                // not this session
                return(false);
            }

            // it was a valid multicast packet
            return(true);
        }
Ejemplo n.º 4
0
        public VNetPacket()
        {
            int sizeofVnetPacket = VNetPacketHeader.SizeOf();

            data     = new byte[VNetCommon.NET_PACKET_MAX_LENGTH];
            m_cursor = sizeofVnetPacket;
        }
Ejemplo n.º 5
0
        public void SendPacketToClient()
        {
            // Add reliable messages
            PackReliableMessages();

            // for all packets to go
            for (int i = 0; i <= m_packetQueueIndex; i++)
            {
                if (i == VNetCommon.NET_PACKET_QUEUE_SIZE)
                {
                    break;
                }

                VNetPacket packet = m_packetOutQueue[i];
                if (packet.header.numMessages == 0)
                {
                    return;
                }

                // end a packet if it's the last one this frame
                if (i == m_packetQueueIndex)
                {
                    packet.EndPacket();
                }

                SetOutgoingPacketHeader(packet);

                VNet.Inst.SendOnClientLink(packet);
                m_clientBandwidth.AddToBandwidth(packet.header.dataLength + VNetPacketHeader.SizeOf(), packet.header.origLength + VNetPacketHeader.SizeOf());
                packet.Clear();
            }

            m_packetQueueIndex = 0;
            ResetTimeToSend();
        }
Ejemplo n.º 6
0
        // Decompression
        public void StartPacketRead()
        {
            m_nextMessageLength = 0;
            m_nextMessageType   = new VNetStringChksum(0);
            m_cursor            = VNetPacketHeader.SizeOf();

            m_currentRun    = 0;
            m_currentValue  = 0;
            m_previousValue = 0x100;
            m_readState     = 0;
        }
Ejemplo n.º 7
0
        public bool Send(VNetPacket outPacket)
        {
            if (m_isInitialized == false)
            {
                return(false);
            }
            int        ret        = -1;
            int        dataLength = outPacket.header.dataLength + VNetPacketHeader.SizeOf();
            IPEndPoint endpoint   = new IPEndPoint(outPacket.IP_Port.Address, VNetCommon.CLIENT_LINK_LOCAL_PORT);

            ret = m_clientSocket.SendTo(outPacket.GetBytes(), outPacket.GetLength(), SocketFlags.None, endpoint);
            return(ret != 0);
        }
Ejemplo n.º 8
0
        public bool AddNetMessage(VNetMessage message)
        {
            byte[] src = null;
            using (System.IO.MemoryStream m = new System.IO.MemoryStream())
            {
                using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(m))
                {
                    message.ToBytes(writer);
                }
                src = m.ToArray();
            }

            message.__size     = src.Length;
            header.origLength += (short)message.__size;

            int len = src.Length;

            // Carefully add the size of this message
            byte[] lengthBytes = BitConverter.GetBytes(src.Length);
            for (int i = 0; i < 4; i++)
            {
                src[4 + i] = lengthBytes[i];
            }

            if (m_cursor == VNetPacketHeader.SizeOf() &&
                header.numMessages != 0)
            {
                throw new Exception();
            }

            // Compress it
            int newCursor = m_cursor;

            if (!CompressBuffer(src, data, ref newCursor))
            {
                return(false);
            }

            // Sanity check...
            CheckCompression(src, data, m_cursor, message.__size, newCursor - m_cursor);


            m_cursor          = newCursor;
            header.dataLength = (short)(m_cursor - VNetPacketHeader.SizeOf());

            return(true);
        }
Ejemplo n.º 9
0
        public Byte DecompressNextByte()
        {
            if (m_cursor > header.dataLength + VNetPacketHeader.SizeOf())
            {
                return(0);
            }
            switch (m_readState)
            {
            case 0:
                m_currentValue = data[m_cursor++];
                if (m_currentValue != m_previousValue)
                {
                    m_readState = 1;
                }
                else
                {
                    m_readState = 2;
                }
                return(DecompressNextByte());

            case 1:
                m_previousValue = m_currentValue;
                m_readState     = 0;
                return(m_currentValue);

            case 2:
                m_currentRun = data[m_cursor++];
                m_readState  = 3;
                return(m_currentValue);

            case 3:
                if (m_currentRun == 0)
                {
                    m_readState     = 0;
                    m_previousValue = 0x100;
                    return(DecompressNextByte());
                }
                m_currentRun--;
                return(m_currentValue);

            default:
                return(0);
            }
        }
Ejemplo n.º 10
0
        public void Clear()
        {
            m_cursor = VNetPacketHeader.SizeOf();

            length  = 0;
            IP_Port = null;

            // Compression data
            m_currentRun    = 0;
            m_previousValue = 0x100;
            m_farRunValue   = 0;

            // clear the header
            header.sessionUID = 0;
            header.clientUID  = 0;
            header.lastReliablePacketRecvd = 0;
            header.dataLength      = 0;
            header.origLength      = 0;
            header.parentEndianess = 1;
            header.numMessages     = 0;
            header.compression     = 0;
        }
Ejemplo n.º 11
0
 public void EndPacket()
 {
     length            = (short)m_cursor;
     header.dataLength = (short)(m_cursor - VNetPacketHeader.SizeOf());
 }