Server.Data.ToByte C# (CSharp) Méthode

ToByte() public méthode

Converts the Data structure into an array of bytes
public ToByte ( List playerList, List bulletList, List removeids, int playercount, List planetList ) : byte[]
playerList List A list that contains the players instance
bulletList List A list that contains the Bullets instance
removeids List A list that contains the id of the object removed
playercount int The number of player currently in the game
planetList List A list that contains the Planets instance
Résultat byte[]
        public byte[] ToByte(List<Player> playerList, List<Bullet> bulletList, List<int> removeids, int playercount, List<Planets> planetList)
        {
            List<byte> result = new List<byte>();

            //First four are for the Command
            result.AddRange(BitConverter.GetBytes((int)cmdCommand));

            //Add how many players there are (for client)
            result.AddRange(BitConverter.GetBytes(playercount));

            result.AddRange(BitConverter.GetBytes((int)id));

            //Next add locations of every player
            foreach (Player p in playerList)
            {   //May need names/angles later.
                //Add the length of the name
                result.AddRange(BitConverter.GetBytes((int)p.x));
                result.AddRange(BitConverter.GetBytes((int)p.y));
                result.AddRange(BitConverter.GetBytes((int)p.angle));

            }

            result.AddRange(BitConverter.GetBytes((int)planetList.Count));
            foreach (Planets planet in planetList)
            {
                result.AddRange(BitConverter.GetBytes((int)planet.x));
                result.AddRange(BitConverter.GetBytes((int)planet.y));
            }

            //Add the length of the name
            if (strName != null)
                result.AddRange(BitConverter.GetBytes(strName.Length));
            else
                result.AddRange(BitConverter.GetBytes(0));

            //Length of the message
            if (strMessage != null)
                result.AddRange(BitConverter.GetBytes(strMessage.Length));
            else
                result.AddRange(BitConverter.GetBytes(0));

            //Add the name
            if (strName != null)
                result.AddRange(Encoding.UTF8.GetBytes(strName));
            else
                result.AddRange(BitConverter.GetBytes(0));

            //And, lastly we add the message text to our array of bytes
            if (strMessage != null)
                result.AddRange(Encoding.UTF8.GetBytes(strMessage));
            else
                result.AddRange(BitConverter.GetBytes(0));

            result.AddRange(BitConverter.GetBytes((int)bulletList.Count));
            for (int j = 0; j < (int)bulletList.Count; j++)//Bullet b in bulletList
            {
                //TODO:angle of bullet if image is pointed
                result.AddRange(BitConverter.GetBytes((int)bulletList[j].x));
                result.AddRange(BitConverter.GetBytes((int)bulletList[j].y));

            }

            return result.ToArray();
        }

Same methods

Data::ToByte ( List playerList, int playercount ) : byte[]

Usage Example

Exemple #1
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint   epSender  = (EndPoint)ipeSender;

                serverSocket.EndReceiveFrom(ar, ref epSender);

                //Transformar o array de bytes recebido do utilizador num objecto de dados
                Data msgReceived = new Data(byteData);
                //Enviar o objecto em resposta aos pedidos dos clientes
                Data msgToSend = new Data();

                byte[] message;
                message = msgToSend.ToByte();
                if (msgReceived.cmdCommand == Command.Login)
                {
                    ClientInfo clientI = new ClientInfo();
                    clientI.strName  = msgReceived.strName;
                    clientI.endpoint = epSender;
                    clientList.Add(clientI);
                }
                if (msgReceived.cmdCommand == Command.Atacar)
                {
                    foreach (ClientInfo clientI in clientList)
                    {
                        if (clientI.strName != msgReceived.strName)
                        {
                            msgToSend.strMessage = msgReceived.strMessage;
                            msgToSend.cmdCommand = msgReceived.cmdCommand;
                            msgToSend.strName    = clientI.strName;
                            message = msgToSend.ToByte();
                            //Enviar a posição ao client 2 depois clienmt 2 ve se acerta se ss message = Acertou!
                            serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, clientI.endpoint,
                                                     new AsyncCallback(OnSend), clientI.endpoint);
                        }
                    }
                }



                //Se o utilizador saiu, não é necessário continuar a aguardar dados
                if (msgReceived.cmdCommand != Command.Logout)
                {
                    //Aguardar dados do cliente
                    serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
                                                  new AsyncCallback(OnReceive), epSender);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Servidor", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
All Usage Examples Of Server.Data::ToByte