Example #1
0
        public void ConectToServer(IPEndPoint endPoint, UdpClient listener, ServerCommands commands)
        {
            health = 100;
            if (this.connected != 1)
            {
                this.endPoint = new IPEndPoint(endPoint.Address, endPoint.Port);
                Server.UserConnects();

                spawnPosition = server.GenerateRandomPos();

                Console.WriteLine("connected to server: " + this.endPoint.ToString());

                commands.AddData((uint)this.id);
                commands.AddSecData((uint)spawnPosition);
                byte[] send_buffer = commands.CommandPacket(ServerCommands.ServerCommand.Connected);

                this.connected = 1;
                Server.AddIPToList(this.endPoint.ToString());

                listener.Send(send_buffer, send_buffer.Length, endPoint);

                server.ResetReceivedConnectionOtherList();
                server.AddOtherReceivedAddress(endPoint.ToString());

                Player[] players = Server.GetConnected();
                commands.AddData(players);
                commands.AddSecData((uint)this.id);
                commands.AddThirdData(false);
                send_buffer = commands.CommandPacket(ServerCommands.ServerCommand.ConnectOther);
                listener.Send(send_buffer, send_buffer.Length, endPoint);



                players = new Player[] { this };
                int index = 0;
                commands.AddData(players);
                commands.AddSecData((uint)this.id);
                commands.AddThirdData(true);
                send_buffer = commands.CommandPacket(ServerCommands.ServerCommand.ConnectOther);
                Thread threadOther = new Thread(() => server.MessagesForOtherReacivingConnection(send_buffer));
                threadOther.Start();

                send_buffer = commands.CommandPacket(ServerCommands.ServerCommand.Start);

                Thread serverStatus = new Thread(() => Server.BroadcastMultipleMessageToReceiver(send_buffer, 200, id - 1));
                serverStatus.Start();

                Thread serverStatusForOthers = new Thread(() => Server.BroadcastMutipleMessageAll(send_buffer, 20));
                serverStatusForOthers.Start();
            }
            else
            {
                if (endPoint.ToString() == this.endPoint.ToString())
                {
                    commands.AddData((uint)this.id);
                    commands.AddSecData((uint)spawnPosition);
                    byte[] send_buffer = commands.CommandPacket(ServerCommands.ServerCommand.Connected);
                    listener.Send(send_buffer, send_buffer.Length, endPoint);

                    Player[] players = Server.GetConnected();
                    commands.AddData(players);
                    commands.AddSecData((uint)this.id);
                    commands.AddThirdData(false);
                    send_buffer = commands.CommandPacket(ServerCommands.ServerCommand.ConnectOther);
                    listener.Send(send_buffer, send_buffer.Length, endPoint);

                    send_buffer = commands.CommandPacket(ServerCommands.ServerCommand.Start);
                    Thread serverStatus = new Thread(() => Server.BroadcastMultipleMessageToReceiver(send_buffer, 200, id - 1));
                    serverStatus.Start();
                }
            }
        }