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; } } }
private void MessageHandler() { string received_data; Byte[] receive_byte_array; try { while (serverWorks) { //Console.WriteLine("Waiting for broadcast"); IPEndPoint user = new IPEndPoint(IPAddress.Any, Port); receive_byte_array = listener.Receive(ref user); //Console.WriteLine("Received a broadcast from {0}", user.ToString()); receivedMessage = new BitArray(receive_byte_array); //listener.Send(receive_byte_array, receive_byte_array.Length, user); received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length); //Console.WriteLine(receive_byte_array); int packetTypeCount = (int)BitFunctions.CountBits((int)ServerCommands.PacketType.NumberOfValues); BitArray packetTypeBits = BitFunctions.BitsReverse(BitFunctions.Range(0, packetTypeCount, receivedMessage)); int packetType = (int)BitFunctions.BitArrayToUInt(packetTypeBits); if (packetType == (int)ServerCommands.PacketType.ServerCommand) { uint commandLen = BitFunctions.CountBits((int)ServerCommands.ServerCommand.ConnectedOther); int index = 0; BitArray packetLenBits = BitFunctions.BitsReverse(BitFunctions.Range(packetTypeCount, (int)commandLen, receivedMessage)); int commandIndex = (int)BitFunctions.BitArrayToUInt(packetLenBits); if (Enum.IsDefined(typeof(ServerCommands.ServerCommand), commandIndex)) { ServerCommands.ServerCommand command = (ServerCommands.ServerCommand)commandIndex; if (command == ServerCommands.ServerCommand.Connect) { commands.AddUserForConnection(user); commands.Command(command); } else if (command == ServerCommands.ServerCommand.ConnectedOther) { commands.AddData(user.ToString()); commands.Command(command); } if (connectedIP.Contains(user.ToString())) { if (command == ServerCommands.ServerCommand.Disconnect) { int playerCount = (int)BitFunctions.CountBits(Server.MaxPlayers); packetLenBits = BitFunctions.BitsReverse(BitFunctions.Range((int)(packetTypeCount + commandLen), playerCount, receivedMessage)); int res = (int)BitFunctions.BitArrayToUInt(packetLenBits); commands.AddData(res); commands.AddUserForConnection(user); byte[] message = commands.CommandPacket(ServerCommands.ServerCommand.Disconnected); listener.Send(message, message.Length, user); Console.WriteLine("Disconnected from server: " + user.ToString() + " " + commands.GetMessage()); } else if (command == ServerCommands.ServerCommand.DisconnectedFromOther) { commands.AddData(user.ToString()); commands.Command(command); Console.WriteLine("trying to disconnect"); } // next console commands commands.Command((ServerCommands.ServerCommand)commandIndex); } if (!connectedIP.Contains(user.ToString()) && disconnectedIP.Contains(user.ToString())) { if (command == ServerCommands.ServerCommand.Disconnect) { int playerCount = (int)BitFunctions.CountBits(Server.MaxPlayers); packetLenBits = BitFunctions.BitsReverse(BitFunctions.Range((int)(packetTypeCount + commandLen), playerCount, receivedMessage)); int res = (int)BitFunctions.BitArrayToUInt(packetLenBits); commands.AddData(res); commands.AddUserForConnection(user); commands.Command((ServerCommands.ServerCommand)commandIndex); byte[] message = commands.CommandPacket(ServerCommands.ServerCommand.Disconnected); listener.Send(message, message.Length, user); Console.WriteLine("Disconnected from server: " + user.ToString() + " " + commands.GetMessage()); } } } //Console.WriteLine("message To Server"); } else { Packet packet = new Packet(receive_byte_array); if (packet.GetReceivedSenderId() > 0 && packet.GetReceivedSenderId() < playerArray.Length) { if (packetType == (int)ServerCommands.PacketType.UserServer) { attackHandler.AttackMessage(packet); } else { if (packet.GetReceivedSenderId() != 0) { if (connectedIP.Contains(user.ToString()) && !playerArray[packet.GetReceivedSenderId() - 1].PlayerIsDead()) { BroadcastMessageAll(receive_byte_array); //Console.WriteLine(receive_byte_array); } } } } } if (timer > disconnectedListResetTime) { disconnectedIP = new List <string>(); time = DateTime.Now; } timer = (DateTime.Now - time).TotalSeconds; //Console.WriteLine("data follows \n{0}\n\n", received_data); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
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(); } } }