Ejemplo n.º 1
0
        internal static ResponseData Get(BinaryReader reader)
        {
            ushort byteCount         = BinaryHelper.ReadUInt16(reader);
            UnknownResponseData data = new UnknownResponseData();

            if (byteCount > 0)
            {
                data.Bytes = reader.ReadBytes(byteCount);
            }
            else
            {
                data.Bytes = new byte[0];
            }

            return(data);
        }
Ejemplo n.º 2
0
 public void WriteTo(Stream stream)
 {
     foreach (string label in this)
     {
         if (label.Length == 0)
         {
             break;
         }
         byte[] bytes = Encoding.UTF8.GetBytes(label);
         //labels.Add(new KeyValuePair<byte[], byte>(bytes, (byte)bytes.Length));
         //totalLength += (ushort)(bytes.Length + 1);
         stream.WriteByte((byte)bytes.Length);
         BinaryHelper.Write(stream, bytes);
     }
     stream.WriteByte(0);
 }
Ejemplo n.º 3
0
        public override void WriteTo(Stream stream)
        {
            //ID
            BinaryHelper.Write(stream, ID);
            //Qr, Opcode, Aa, Tc, Rd
            byte b = 0;

            b += (byte)QueryResponse;
            b  = (byte)(b << 4);
            b += (byte)OpCode;
            b  = (byte)(b << 1);
            b += (AuthoritativeAnswer) ? (byte)1 : (byte)0;
            b  = (byte)(b << 1);
            b += (Truncated) ? (byte)1 : (byte)0;
            b  = (byte)(b << 1);
            b += (RecursionDesired) ? (byte)1 : (byte)0;
            stream.WriteByte(b);

            //Ra, Z, Rcode
            b  = 0;
            b += (RecursionAvailable) ? (byte)1 : (byte)0;
            b  = (byte)(b << 7);
            b += (byte)ResponseCode;
            stream.WriteByte(b);
            BinaryHelper.Write(stream, QuestionEntries);
            BinaryHelper.Write(stream, AnswerEntries);
            BinaryHelper.Write(stream, AuthorityEntries);
            BinaryHelper.Write(stream, AdditionalEntries);
            foreach (Question q in Questions)
            {
                q.WriteTo(stream);
            }
            foreach (Answer a in Answers)
            {
                a.WriteTo(stream);
            }
            foreach (Answer a in Authorities)
            {
                a.WriteTo(stream);
            }
            foreach (Answer a in Additionals)
            {
                a.WriteTo(stream);
            }
        }
Ejemplo n.º 4
0
        private object[] _receiveGameState(UdpClient _client)
        {
            IPEndPoint ipep   = null;
            var        data   = _client.Receive(ref ipep);    // Data of the game-state (tiles, entities, et cetera)
            int        length = BitConverter.ToInt32(data, 0);

            data = new byte[length];
            byte[] temp;
            int    recieved = 0;

            while (recieved < length)
            {
                temp = _client.Receive(ref ipep);
                temp.CopyTo(data, recieved);
                recieved += temp.Length;
            }
            return(BinaryHelper.LoadGameState(data));
        }
Ejemplo n.º 5
0
        private Dictionary <int, Bonus> _getBonuses(byte[] data)
        {
            int pos = 0;
            int num = BinaryHelper.ReadInt32(data, ref pos);
            Dictionary <int, Bonus> result = new Dictionary <int, Bonus>();
            Bonus b = null;

            for (int i = 0; i < num; i++)
            {
                int       bId  = BinaryHelper.ReadInt32(data, ref pos);
                int       bx   = BinaryHelper.ReadInt32(data, ref pos);
                int       by   = BinaryHelper.ReadInt32(data, ref pos);
                BonusType type = (BonusType)BinaryHelper.ReadByte(data, ref pos);
                b = _createBonus(bId, type, bx, by);
                result.Add(b.Id, b);
            }
            return(result);
        }
Ejemplo n.º 6
0
        private void _sendBonuses(IPEndPoint ipep)
        {
            var bonuses = Bonuses;
            int length  = bonuses.Count * (sizeof(int) + sizeof(int) + sizeof(int) + sizeof(byte)) + sizeof(int);

            byte[] data = new byte[length];
            int    pos  = 0;

            BinaryHelper.Write(bonuses.Count, ref pos, ref data);
            foreach (KeyValuePair <int, Bonus> B in bonuses)
            {
                BinaryHelper.Write(B.Value.Id, ref pos, ref data);
                BinaryHelper.Write(B.Value.iX, ref pos, ref data);
                BinaryHelper.Write(B.Value.iY, ref pos, ref data);
                BinaryHelper.Write((byte)B.Value.Type, ref pos, ref data);
            }
            _client.Send(data, data.Length, ipep);
        }
Ejemplo n.º 7
0
        public static Question FromBytes(byte[] bytes, ref int index)
        {
            Question q = new Question();

            q.DomainName = DomainName.FromBytes(bytes, ref index);
            ushort s;

            BinaryHelper.FromBytes(bytes, index, out s);
            index += 2;
            q.Type = (QType)s;
            BinaryHelper.FromBytes(bytes, index, out s);
            q.Class = (QClass)s;
            index  += 2;

/* openHAB, the open Home Automation Bus.
 * Copyright (C) 2010-${year}, openHAB.org <*****@*****.**>
 *
 * See the contributors.txt file in the distribution for a
 * full listing of individual contributors.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Additional permission under GNU GPL version 3 section 7
 *
 * If you modify this Program, or any covered work, by linking or
 * combining it with Eclipse (or a modified version of that library),
 * containing parts covered by the terms of the Eclipse Public License
 * (EPL), the licensors of this Program grant you additional permission
 * to convey the resulting work.
 */

            return(q);
        }
        public void Update(Player p)
        {
            int pos    = 0;
            int length = BASIC_DATA_LENGTH;

            if (lastAddedBonus != null)
            {
                length += BONUS_DATA_LENGTH;
            }
            byte[] data = new byte[length];

            BinaryHelper.Write(p.Id, ref pos, ref data);
            BinaryHelper.Write(p.X, ref pos, ref data);
            BinaryHelper.Write(p.Y, ref pos, ref data);
            BinaryHelper.Write(p.Direction, ref pos, ref data);
            BinaryHelper.Write(p.Shooting, ref pos, ref data);
            BinaryHelper.Write((int)p.Team, ref pos, ref data);
            BinaryHelper.Write((byte)p.CurrentInvItemAKey, ref pos, ref data);

            if (p.LastUsedInvItem != null)
            {
                BinaryHelper.Write((int)p.LastUsedInvItem.Type, ref pos, ref data);
            }
            else
            {
                BinaryHelper.Write(0, ref pos, ref data);
            }
            BinaryHelper.Write(lastRemovedBonus, ref pos, ref data);
            lastRemovedBonus = -1;
            BinaryHelper.Write(p.Deaths, ref pos, ref data);
            BinaryHelper.Write(p.Kills, ref pos, ref data);
            if (lastAddedBonus != null)
            {
                BinaryHelper.Write(lastAddedBonus.Id, ref pos, ref data);
                BinaryHelper.Write(lastAddedBonus.X, ref pos, ref data);
                BinaryHelper.Write(lastAddedBonus.Y, ref pos, ref data);
                BinaryHelper.Write((byte)lastAddedBonus.Type, ref pos, ref data);
                lastAddedBonus = null;
            }

            _broadcast(data);
        }
Ejemplo n.º 9
0
        public override void WriteTo(Stream writer)
        {
            ushort length = 0;
            List <KeyValuePair <byte[], byte> > bytes = new List <KeyValuePair <byte[], byte> >();

            foreach (KeyValuePair <string, string> kvp in Properties)
            {
                byte[] kvpBytes = Encoding.UTF8.GetBytes(kvp.Key + "=" + kvp.Value);
                bytes.Add(new KeyValuePair <byte[], byte>(kvpBytes, (byte)kvpBytes.Length));
                //writer.Write((byte)kvpBytes.Length);
                //writer.Write(kvpBytes);
                length += (ushort)kvpBytes.Length;
                length++;
            }
            BinaryHelper.Write(writer, length);
            foreach (var properties in bytes)
            {
                writer.WriteByte(properties.Value);
                BinaryHelper.Write(writer, properties.Key);
            }
        }
Ejemplo n.º 10
0
 public static uint ReadUInt32(Stream stream)
 {
     return(ReadUInt32(BinaryHelper.Read(stream, sizeof(uint))));
 }
Ejemplo n.º 11
0
 public static ushort ReadUInt16(Stream stream)
 {
     return(ReadUInt16(BinaryHelper.Read(stream, sizeof(ushort))));
 }
Ejemplo n.º 12
0
 public byte[] GetBytes()
 {
     return(BinaryHelper.GetBytes(this));
 }
Ejemplo n.º 13
0
        private void _analyzeUDPCommand(byte[] data, IPEndPoint ipep)
        {
            for (int i = 0; i < _connections.Count; i++)
            {
                if (_connections[i].Address.Address == ipep.Address.Address && _connections[i].Port == ipep.Port)
                {
                    _requestTime[i] = DateTime.Now;
                }
            }

            int pos = 0;
            int id  = BinaryHelper.ReadInt32(data, ref pos);

            //check if id is id and not a server command
            if (id >= 0)
            {
                //if id is id
                //load current pos and direction of player
                float      x   = BinaryHelper.ReadFloat(data, ref pos);
                float      y   = BinaryHelper.ReadFloat(data, ref pos);
                Directions dir = (Directions)BinaryHelper.ReadInt32(data, ref pos);

                //if player shooted
                bool shooting = BinaryHelper.ReadBool(data, ref pos);
                int  team     = BinaryHelper.ReadInt32(data, ref pos);

                //selected inv item
                byte    curInvItem = BinaryHelper.ReadByte(data, ref pos);
                InvType invType    = (InvType)BinaryHelper.ReadInt32(data, ref pos);

                //check if bonus was removed
                int removedBonus = BinaryHelper.ReadInt32(data, ref pos);
                if (removedBonus != -1)
                {
                    if (OnBonusRemoved != null)
                    {
                        OnBonusRemoved(removedBonus);
                    }
                }

                //load statistics information
                int Deaths = BinaryHelper.ReadInt32(data, ref pos);
                int Kills  = BinaryHelper.ReadInt32(data, ref pos);

                //check if invItem was used
                lock (Players[id])
                {
                    Players[id].CurrentInvItemAKey = curInvItem;
                    Players[id].X         = x;
                    Players[id].Y         = y;
                    Players[id].Direction = dir;
                    Players[id].SetTeam((Teams)team);
                    Players[id].Kills  = Kills;
                    Players[id].Deaths = Deaths;

                    if (shooting)
                    {
                        Players[id].Shoot(true);
                    }
                    if (invType != InvType.Unknown && invType != InvType.Cannon)
                    {
                        GameLevel.CreateEntity(_createInvItem(invType), id);
                    }
                }

                //check if new bonus was created
                if (pos != data.Length)
                {
                    int       bId  = BinaryHelper.ReadInt32(data, ref pos);
                    int       bx   = (int)BinaryHelper.ReadFloat(data, ref pos);
                    int       by   = (int)BinaryHelper.ReadFloat(data, ref pos);
                    BonusType type = (BonusType)BinaryHelper.ReadByte(data, ref pos);
                    Bonus     b    = _createBonus(bId, type, bx, by);
                    GameLevel.AddBonus(b);
                }
            }
            else
            {
                //if id is a server command
                UDPCommands command = (UDPCommands)id;
                switch (command)
                {
                case UDPCommands.AddPlayer:
                {
                    Player t = new Player(Players.Count, 0, 0);
                    t.Id      = BinaryHelper.ReadInt32(data, ref pos);
                    t.Removed = true;
                    if (t.Id == Players.Count)
                    {
                        Players.Add(t);
                        _connections.Add(ipep);
                        _requestTime.Add(DateTime.Now);
                    }
                    else
                    {
                        Players[t.Id]      = t;
                        _connections[t.Id] = ipep;
                        _requestTime[t.Id] = DateTime.Now;
                    }
                    break;
                }

                case UDPCommands.AddPlayerComplete:
                {
                    id = BinaryHelper.ReadInt32(data, ref pos);
                    lock (Players[id])
                    {
                        Players[id].Removed = false;
                    }
                    break;
                }

                case UDPCommands.ClientDisconnected:
                {
                    id = BinaryHelper.ReadInt32(data, ref pos);
                    lock (Players[id])
                    {
                        Players[id]      = null;
                        _connections[id] = null;
                        _requestTime[id] = new DateTime();
                    }
                    //MessageBox.Show("Client " + id + " disconnected");
                    break;
                }

                case UDPCommands.ConnectToGame:
                {
                    _connectToGame(ipep);
                    break;
                }

                case UDPCommands.ServerChanged:
                {
                    long ip = BinaryHelper.ReadLong(data, ref pos);
                    if (ip == CIp.Address)
                    {
                        _isMain = true;
                    }
                    GameLevel.BonusId = BinaryHelper.ReadInt32(data, ref pos);
                    break;
                }
                }
            }
        }
Ejemplo n.º 14
0
        public AddPlayerResult AddPlayer(List <Player> Players)
        {
            this.Players = Players;

            _client.Send(BitConverter.GetBytes((int)UDPCommands.ConnectToGame), sizeof(int), ServerIpEp);
            _getConnections();

            for (int i = 0; i < _connections.Count; i++)
            {
                if (_connections[i] != null)
                {
                    currentTeamCaret += 1;
                    Player player = new Player(i, 0, 0);
                    if (gameMode == Modes.Deathmatch)
                    {
                        player.SetTeam((Teams)currentTeamCaret);
                    }
                    Players.Add(player);
                    _requestTime.Add(DateTime.Now);
                }
                else
                {
                    Players.Add(null);
                    _requestTime.Add(new DateTime());
                }
            }

            AddPlayerResult result = new AddPlayerResult();

            IPEndPoint ipep = null;

            int pos = 0;

            byte[] data = _client.Receive(ref ipep);
            result.Mode = BinaryHelper.ReadInt32(data, ref pos);
            gameMode    = (Modes)result.Mode;

            // Receiving
            var currentGameState = _receiveGameState(_client);

            result.LevelWidth  = (int)currentGameState[0];
            result.LevelHeight = (int)currentGameState[1];
            result.Tiles       = (List <GEntity>)currentGameState[2];
            result.Entities    = (List <GEntity>)currentGameState[3];
            result.Spawners    = (List <GEntity>)currentGameState[4];

            data           = _client.Receive(ref ipep);   // Bonuses data
            result.Bonuses = _getBonuses(data);

            pos  = 0;
            data = _client.Receive(ref ipep);
            Spawner spawner = (Spawner)result.Spawners[Program.Rand.Next(0, result.Spawners.Count - 1)];

            CurrentPlayer = new Player(BinaryHelper.ReadInt32(data, ref pos), spawner.X, spawner.Y);
            result.Player = CurrentPlayer;
            if (gameMode == Modes.Deathmatch)
            {
                result.Player.SetTeam((Teams)currentTeamCaret);
            }
            Players[CurrentPlayer.Id]      = CurrentPlayer;
            _requestTime[CurrentPlayer.Id] = DateTime.Now;

            _thread = new Thread(_waitForClientData);
            _thread.Start();
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 100;
            timer.Start();
            CurrentPlayer.Removed = true;
            IsConnected           = false;
            timer.Tick           += _timer_Tick;

            data = new byte[sizeof(int) * 2];
            pos  = 0;
            BinaryHelper.Write((int)UDPCommands.AddPlayer, ref pos, ref data);
            BinaryHelper.Write(CurrentPlayer.Id, ref pos, ref data);
            _broadcast(data);

            return(result);
        }
Ejemplo n.º 15
0
 public byte[] GetBytes()
 {
     return(BinaryHelper.GetBytes((IServerResponse)this));
 }
Ejemplo n.º 16
0
 public static ulong ReadUInt64(Stream stream)
 {
     return(ReadUInt64(BinaryHelper.Read(stream, sizeof(ulong))));
 }
Ejemplo n.º 17
0
 public void WriteTo(Stream stream)
 {
     DomainName.WriteTo(stream);
     BinaryHelper.Write(stream, (ushort)Type);
     BinaryHelper.Write(stream, (ushort)Class);
 }
Ejemplo n.º 18
0
 public override void WriteTo(Stream writer)
 {
     BinaryHelper.Write(writer, DomainName.GetByteCount());
     DomainName.WriteTo(writer);
 }
Ejemplo n.º 19
0
 public override void WriteTo(Stream writer)
 {
     byte[] address = Address.GetAddressBytes();
     BinaryHelper.Write(writer, (ushort)address.Length);
     BinaryHelper.Write(writer, address);
 }
Ejemplo n.º 20
0
 public override void WriteTo(Stream writer)
 {
     BinaryHelper.Write(writer, ((ushort)Encoding.UTF8.GetByteCount(CNAME)));
     BinaryHelper.Write(writer, Encoding.UTF8.GetBytes(CNAME));
 }
Ejemplo n.º 21
0
 public static int ReadInt32(Stream stream)
 {
     return(ReadInt32(BinaryHelper.Read(stream, 4)));
 }
Ejemplo n.º 22
0
 protected virtual T GetMessage(byte[] requestBytes)
 {
     return(BinaryHelper.FromBytes((IServerRequest <T>) this, requestBytes));
 }