Exemplo n.º 1
0
 public void Update()
 {
     byte[] buffer = new byte [1024];
     while (socket.Available > 0)
     {
         try
         {
             EndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
             int      length   = socket.ReceiveFrom(buffer, ref endpoint);
             if (length > buffer.Length)
             {
                 continue;
             }
             if (length < HeaderSize)
             {
                 continue;
             }
             int        magic = BitConv.FromInt32(buffer, 0);
             PacketType type  = (PacketType)buffer[4];
             if (magic != Magic)
             {
                 continue;
             }
             byte[] data = new byte [length - HeaderSize];
             Array.Copy(buffer, HeaderSize, data, 0, data.Length);
             ReceivePacket(endpoint, type, data);
         }
         catch (SocketException)
         {
             // TODO :: Log exception
         }
     }
 }
Exemplo n.º 2
0
        public WadArchive(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (stream.Length < 16)
            {
                throw new ApplicationException();
            }
            byte[] buffer = new byte [16];
            stream.Read(buffer, 0, 12);
            string magic     = Encoding.ASCII.GetString(buffer, 0, 4);
            int    lumpcount = BitConv.FromInt32(buffer, 4);
            int    diroffset = BitConv.FromInt32(buffer, 8);

            if (magic != "IWAD" && magic != "PWAD")
            {
                throw new ApplicationException();
            }
            if (lumpcount < 0)
            {
                throw new ApplicationException();
            }
            if (diroffset < 0)
            {
                throw new ApplicationException();
            }
            if (stream.Length < diroffset + lumpcount * 16)
            {
                throw new ApplicationException();
            }
            lumps           = new List <ILump>(lumpcount);
            stream.Position = diroffset;
            for (int i = 0; i < lumpcount; i++)
            {
                stream.Read(buffer, 0, 16);
                int    offset = BitConv.FromInt32(buffer, 0);
                int    length = BitConv.FromInt32(buffer, 4);
                string name   = Encoding.ASCII.GetString(buffer, 8, 8);
                if (name.Contains("\0"))
                {
                    name = name.Remove(name.IndexOf('\0'));
                }
                name = name.ToUpper();
                if (offset < 0)
                {
                    throw new ApplicationException();
                }
                if (length < 0)
                {
                    throw new ApplicationException();
                }
                if (stream.Length < offset + length)
                {
                    throw new ApplicationException();
                }
                lumps.Add(new StreamLump(stream, name, length, offset));
            }
        }
Exemplo n.º 3
0
        private void HandleGamePacket(object sender, PacketEventArgs e)
        {
            INetNode node = (INetNode)sender;

            if (e.Type == PacketType.Connect)
            {
                if (e.Data.Length != 0)
                {
                    return;
                }
                byte[] data = new byte [8];
                BitConv.ToInt32(data, 0, nodes.IndexOf(node));
                BitConv.ToInt32(data, 4, nodes.Count);
                node.SendPacket(PacketType.Accepted, data);
            }
            else if (e.Type == PacketType.DoomData)
            {
                if (e.Data.Length < 4)
                {
                    return;
                }
                int nodeid = BitConv.FromInt32(e.Data, 0);
                if (nodeid < 0 || nodeid >= nodes.Count)
                {
                    return;
                }
                byte[] data = new byte [e.Data.Length];
                BitConv.ToInt32(data, 0, nodes.IndexOf(node));
                Array.Copy(e.Data, 4, data, 4, e.Data.Length - 4);
                nodes[nodeid].SendPacket(PacketType.DoomData, data);
            }
        }
Exemplo n.º 4
0
        public MusicTrack(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (data.Length < 16)
            {
                throw new ApplicationException();
            }
            int magic           = BitConv.FromInt32(data, 0);
            int scorelength     = (ushort)BitConv.FromInt16(data, 4);
            int scorestart      = (ushort)BitConv.FromInt16(data, 6);
            int channels        = (ushort)BitConv.FromInt16(data, 8);
            int secchannels     = (ushort)BitConv.FromInt16(data, 10);
            int instrumentcount = (ushort)BitConv.FromInt16(data, 12);
            int unused          = (ushort)BitConv.FromInt16(data, 14);

            if (magic != Magic)
            {
                throw new ApplicationException();
            }
            if (scorestart < 16)
            {
                throw new ApplicationException();
            }
            if (scorelength <= 0)
            {
                throw new ApplicationException();
            }
            if (scorestart + scorelength != data.Length)
            {
                throw new ApplicationException();
            }
            if (scorestart < 16 + instrumentcount * 2)
            {
                throw new ApplicationException();
            }
            instruments = new short [instrumentcount];
            for (int i = 0; i < instrumentcount; i++)
            {
                instruments[i] = BitConv.FromInt16(data, 16 + i * 2);
            }
            score = new List <MusicEvent>();
            int offset = scorestart;

            while (offset != data.Length)
            {
                score.Add(MusicEvent.Parse(data, ref offset));
            }
        }