Advertisement

Kryonet Server won't recieve one custom packet, but recieves the rest?

Started by July 25, 2015 08:01 PM
0 comments, last by Clobslee 9 years, 3 months ago

I'm messing around with a multiplayer game concept. Does anyone know why this server would not be recieving a certain packet? It doesn't recieve the MobHitPack, but it recieves all others.

The client and server packets are registered in the same order. I have verified the client sends the packet, but the server doesn't recieve it at all (the server's recived() function isn't ever called.) It's weird because I'm using this packet the exact same say I'm using others that work, and I registered it the same way?? I have a hunch it has to do with how I'm compiling the packs twice, once for the server and once for the client, but why would the other packets work...?

Server:


public boolean runServer()
	{
		server = new Server();

		kryo = server.getKryo();
		kryo.register(PosPack.class);
		kryo.register(PCP.class);
		kryo.register(PlayerUpdateRequest.class);
		kryo.register(PlayerUpdatePack.class);
		kryo.register(NewPlayerPack.class);
		kryo.register(DCPack.class);
		kryo.register(ChatPack.class);
		kryo.register(SpawnMobPack.class);
		kryo.register(MobHitPack.class);

   		server.start();
		players = new LinkedList<ServerPlayer>();

		try
		{
    		 	server.bind(5500, 6500);
			System.out.println("Server Running...");
		}
		catch (Exception e)
		{
			System.out.println("Couldn't bind to port tard ass");
			System.exit(0);
		}

		

		server.addListener(new Listener()
		{
				public void received(Connection connection, Object object)
				{
					System.out.println("Recieved packet...");
					if (object instanceof ChatPack)
					{
						System.out.println("Recieved chat pack from: " + connection.getID());
						ChatPack pack = (ChatPack) object;
						pack.id = connection.getID();
						chat.add(pack.line);
						//connection.sendTCP(pack);
						
						//Let all players know that isn't self
						for (ServerPlayer player: players)
							//if(player.getConnection().getID() != connection.getID())
									player.getConnection().sendTCP(pack);
						
					}

					if(object instanceof MobHitPack)
					{
						MobHitPack pack = (MobHitPack) object;
						System.out.println("Caught MobHitPack");
						
						final Iterator<ServerMob> mob = mobList.iterator();
						while(mob.hasNext())
						{
							ServerMob currentMob = mob.next();
							if (currentMob.mobID == pack.mobID)
								currentMob.health = currentMob.health - pack.damage; 
						}

						for(ServerPlayer player: players)
						{
							player.getConnection().sendTCP(pack);
						}
					}

					if (object instanceof SpawnMobPack)
					{
						SpawnMobPack pack = (SpawnMobPack) object;
						System.out.println("Caught SpawnMobPack passed from: " + pack.id);
						ServerMob mob = new ServerMob(pack);
						mob.mobID = mobCount;
						mobList.add(new ServerMob(pack));

						System.out.println("Sending SpawnPack to all connected players");
						for(ServerPlayer player : players)
							player.getConnection().sendTCP(pack);		
						mobCount++;
					}

					if (object instanceof PosPack)
					{
						PosPack pos = (PosPack) object;
						System.out.println("Player with ID: " + connection.getID() + " is moving.");
						playerWithId(connection.getID()).updatePos(pos);
						pos.id = connection.getID();
						
		
						for (ServerPlayer player: players)
								if (player.getID() != connection.getID())
								{
									System.out.println("Sending movement update...");
									player.getConnection().sendUDP(pos);
								}
					}

					if (object instanceof PlayerUpdateRequest)
					{
						System.out.println("Responding to update request from: " + connection.getID());
						//updatePack = new PlayerUpdatePack();
						//updatePack.connectedPlayerCount = players.size();
						//connection.sendTCP(updatePack);
						for (ServerPlayer player: players)
						{
							if (player.id != connection.getID())
							{
								NewPlayerPack pack = new NewPlayerPack();
								pack.id = player.id;
								pack.x = player.x;
								pack.y = player.y;
								System.out.println("Sending new player pack to: " + player.getConnection().getID()); 
								player.getConnection().sendTCP(new NewPlayerPack());
							}
						}
					}

				}
				});

		server.addListener(new Listener() {
				public void connected(Connection connection)
			    	{
				//connection.sendTCP(new float[] {5.0f, 6.0f, 7.0f, 8.0f});
					System.out.println("Player connecting to server...");
					ServerPlayer newPlayer = new ServerPlayer(connection);
					newPlayer.id = connection.getID();
					System.out.println("New player's ID is: " + newPlayer.id);
					players.add(newPlayer);
					//connections.add(connection);

					//notify players of new connection
					for (ServerPlayer player: players)
					{
						
						if (player.getID() != newPlayer.id)
							{
								System.out.println("Sending NPP to " + player.getID());
								npp = new NewPlayerPack();
								npp.id = newPlayer.id;
								npp.x = newPlayer.x;
								npp.y = newPlayer.y;		
								player.getConnection().sendTCP(npp);
							}
					}


					for (ServerPlayer player: players)
					{
						if (player.getID() != newPlayer.id)
						{
							System.out.println("Packing player: " + player.getID());
							npp = new NewPlayerPack();
							npp.id = player.id;
							npp.x = player.x;
							npp.y = player.y;
							connection.sendTCP(npp);
							System.out.println("Sent NewPlayerPack(ID,X,Y): " + npp.id + "," + npp.x + "," + npp.y); 	
						}
					}
					System.out.println("Player connected to server with ID: " + newPlayer.getID()); 
            			}
    		});

			
		server.addListener(new Listener() {
				public void disconnected(Connection connection)
			    	{
					System.out.println("A player has left the server");
					int id = -20;
					for (ServerPlayer player: players)
						if (player.id == connection.getID())
						{
							id = player.getID();
							players.remove(player);
						}

					for (ServerPlayer player: players)
					{
						DCPack dc = new DCPack();
						dc.id = id;
						player.getConnection().sendTCP(dc);
					}
							
					
				}
		});

	

		return true;
	}
}

Client:


	kryo = client.getKryo();
	    	kryo.register(PosPack.class);
		kryo.register(PCP.class);
		kryo.register(PlayerUpdateRequest.class);
		kryo.register(PlayerUpdatePack.class);
		kryo.register(NewPlayerPack.class);
		kryo.register(DCPack.class);
		kryo.register(ChatPack.class);
		kryo.register(SpawnMobPack.class);
		kryo.register(MobHitPack.class);
		
    		

		
	    	

		client.addListener(new Listener() {
		       public void received (Connection connection, Object object) 
		{

			//System.out.println("Recieved Packet from server...");

			if (object instanceof ChatPack)
			{
				ChatPack pack = (ChatPack) object;
				hud.logLines.addFirst("Player " + pack.id + ": " + pack.line);
				
			}

			if (object instanceof MobHitPack)
			{
				System.out.println("MobHitPack recieved");

				MobHitPack pack = (MobHitPack) object;
				final Iterator<Monster> mob = mobs.iterator();
				while(mob.hasNext())
				{
					Monster currentMob = mob.next();
					if(currentMob.mobID == pack.mobID)
					{
						currentMob.setHealth(currentMob.getHealth() - pack.damage);
						System.out.println("Updated Mob Health");
						gameLog.addLine("" + pack.playerID + " hit mob for: " + pack.damage); 
					}
				}
			}

			if (object instanceof PosPack)
			{
				//Identify who sent packet, update online players information
				System.out.println("Processing POS Pack");
				PosPack pack = (PosPack) object;
				for (Player players: playersOnline)
				{
					System.out.println("PlayersID: " + players.getID() + " PacksID: " + pack.id);
					if (players.getID() == pack.id)
					{
						
						players.setMap_x((float) pack.x);
						players.setMap_y(-(float) pack.y);
						System.out.println("Updated player " + players.getID() + ": " + players.getMap_x() + ", " + players.getMap_y());
						//players.setCurrentFloor(pack.z);

						players.setPreviousDirection(pack.direction);
					}
				}
			}
			
			if (object instanceof PlayerUpdatePack)
			{
				PlayerUpdatePack pup = (PlayerUpdatePack) object;
				System.out.println("Processing Player Update Pack");
				/*for (int i = 0; i < pup.connectedPlayerCount; i++)
				{
					newPlayer.setID(i);
					playersOnline.add(newPlayer);
					renderer.addSprite(newPlayer);
				}*/
			}

			if(object instanceof SpawnMobPack)
			{
				if(System.currentTimeMillis() > nextSpawnTime)
				{
					System.out.println("Recieved SpawnMobPack. Doing so...");
					SpawnMobPack pack = (SpawnMobPack) object;
				
					
					System.out.println("Found room for new mob");
					Monster mob = mobMemory[mobCount];
					mob.setMap_x(pack.x);
					mob.setMap_y(pack.y);
					//mobs.add(mob);
					mob.calcScreenPos((int)camera.position.x, (int)camera.position.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
					mob.collisionSquare.setPosition(mob.screen_x, mob.screen_y);
					mob.mobID = mobCount;
					hud.stage.addActor(mob.collisionSquare);
					renderer.mobList.add(mob);
					mobs.add(mob);

					mobCount++;
				}
				nextSpawnTime = System.currentTimeMillis() + 1000;
						
					
				
				
			}


			if (object instanceof NewPlayerPack)
			{
				gameLog.addLine("A new player has connected...");
				System.out.println("A new player has connected...");
				NewPlayerPack npp = (NewPlayerPack) object;
				Mino added = newPlayer[playersConnected];
				added.setID(npp.id);
				added.setMap_x(npp.x);
				added.setMap_y(npp.y);
				playersOnline.add(added);
				renderer.addSprite(playersOnline.get(playersOnline.indexOf(newPlayer[playersConnected])));
				final int pID = added.getID();
				added.collisionSquare.addListener( new ClickListener() 
		{              
		    @Override
		    public void clicked(InputEvent event, float x, float y) {
			playerClicked(pID);
			
			
		    };
		});

				hud.stage.addActor(added.collisionSquare);
				playersConnected++;
			}

	
			if (object instanceof DCPack)
			{
				System.out.println("A player has left the server...");
				DCPack dc = (DCPack) object;
				for (Player player: playersOnline)
				{
					if (dc.id == player.getID())
					{
						playersOnline.remove(player);
						renderer.remove(player);
						player.collisionSquare.remove();
					}
				}
			}

			
          	}
       
    });

		client.addListener(new Listener() {
		       public void connected(Connection connection)
            {

		System.out.println("Asking server for an initial update!");
		player.setID(connection.getID());
		player.loggedIn = true;
                
                //connection.sendTCP(new float[] {5.0f, 6.0f, 7.0f, 8.0f});

		//HERE I AM. SEND ME GAMEINFO!
		
            }
    });

		//client.sendTCP(new PlayerUpdateRequest());	 
	}

	

I swear this happens all the time, but I found my problem right after I posted this. I was simply missing a space in my compile command so it wasn't even compiling the Server and I was running an old one by mistake.

This topic is closed to new replies.

Advertisement