public void ConnectOtherPacket() { Player[] allPlayers = (Player[])data; int playerCount = (int)BitFunctions.CountBits(Server.MaxPlayers); int playerLength = 0; uint currentIndex = (uint)secData; string tempMessage = ""; bool check = (bool)thirdData; for (int i = 0; i < allPlayers.Length; i++) { if (currentIndex != allPlayers[i].GetPlayerId() && allPlayers[i].isConnected() && check == false) { playerLength++; tempMessage = tempMessage + Convert.ToString(allPlayers[i].GetPlayerId(), toBase: 2).PadLeft(playerCount, '0'); Console.WriteLine("other id: " + allPlayers[i].GetPlayerId()); } else if (currentIndex == allPlayers[i].GetPlayerId() && allPlayers[i].isConnected() && check == true) { playerLength++; tempMessage = tempMessage + Convert.ToString(allPlayers[i].GetPlayerId(), toBase: 2).PadLeft(playerCount, '0'); } } //(uint)playerLength Console.WriteLine(playerLength); message = message + Convert.ToString(playerLength, toBase: 2).PadLeft(playerCount, '0'); message = message + tempMessage; }
public void ConnectedPacket() // function name by enum { int playerCount = (int)BitFunctions.CountBits(Server.MaxPlayers); uint spawnId = (uint)secData; int secdataCount = (int)BitFunctions.CountBits((int)spawnId); message = message + Convert.ToString((uint)Server.MaxPlayers, toBase: 2).PadLeft(7, '0'); message = message + Convert.ToString((uint)data, toBase: 2).PadLeft(playerCount, '0'); message = message + Convert.ToString((uint)secdataCount, toBase: 2).PadLeft(7, '0'); message = message + Convert.ToString(spawnId, toBase: 2).PadLeft(secdataCount, '0'); }
private void PacketLength() { int typeLen = (int)BitFunctions.CountBits((int)PacketType.NumberOfValues); float messageLen = this.message.Length + 7 + typeLen; // + 7 for packet length, + for consolle check messageLen = messageLen / 8; if (messageLen - Math.Truncate(messageLen) > 0) { messageLen++; } message = Convert.ToString(1, toBase: 2).PadLeft(typeLen, '0') + Convert.ToString(Math.Max(1, (int)Math.Truncate(messageLen)), toBase: 2).PadLeft(7, '0') + message; }
public byte[] CommandPacket(ServerCommand commandIndex) { message = ""; message = message + Convert.ToString((int)commandIndex, toBase: 2).PadLeft(commandLen, '0'); Type thisType = this.GetType(); MethodInfo theMethod = thisType.GetMethod(commandIndex.ToString() + "Packet"); theMethod.Invoke(this, null); PacketLength(); BitArray bits = new BitArray(this.message.Select(c => c == '1').ToArray()); return(BitFunctions.BitArrayToByteArray(bits)); }
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 ServerCommands(Server server) { this.server = server; this.commandLen = (int)BitFunctions.CountBits((int)ServerCommand.ConnectedOther); // count bit size of last index in command enum }
public void DisconnectedFromOtherPacket() { int playerCount = (int)BitFunctions.CountBits(Server.MaxPlayers); message = message + Convert.ToString((uint)data, toBase: 2).PadLeft(playerCount, '0'); }