private void StartTimer() { ServerCommands commandsForThread = new ServerCommands(this); long elapsedMilliseconds = 0; while (serverWorks) { if (CheckMinConnected() && !startTimerCheck) { startTimerCheck = true; elapsedMilliseconds = 0; } else if (startTimerCheck) { int time = (minutesAfterStart * 60) - (int)(elapsedMilliseconds / 1000); if (time > -1) { Console.WriteLine(time); commandsForThread.AddData(time); byte[] send_buffer = commandsForThread.CommandPacket(ServerCommands.ServerCommand.Timer); BroadcastMutipleMessageAll(send_buffer, 1); } elapsedMilliseconds += 50; Thread.Sleep(50); } if (!CheckMinConnected() && startTimerCheck) { elapsedMilliseconds = 0; startTimerCheck = false; } } }
public void Run(int maxPlayerCount, int minPlayerCount, int spawnPosCount, int minutesAfterStart, double resetTimeDisconecctedList, int port) { var externalip = new WebClient().DownloadString("https://ipv4.icanhazip.com/").TrimEnd(); Console.WriteLine("ip address: " + externalip + ":" + port.ToString()); this.minutesAfterStart = minutesAfterStart; this.spawnPosCount = spawnPosCount; serverWorks = true; MaxPlayers = maxPlayerCount; minPlayersToStart = minPlayerCount; Port = port; listener = new UdpClient(port); playerArray = new Player[maxPlayerCount]; connectedIP = new List <string>(); disconnectedIP = new List <string>(); commands = new ServerCommands(this); time = DateTime.Now; disconnectedListResetTime = resetTimeDisconecctedList; otherConnectionCount = new List <string>(); disconnectedFromOthersCount = new List <string>(); startTimer = new Stopwatch(); UserSetup(); attackHandler = new Attack(this, playerArray); sendLock = new object(); threadStartTimer = new Thread(() => StartTimer()); threadStartTimer.Start(); MessageHandler(); // this always should be last //listener.Close(); }
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(); } } }