Author Topic: Ping listed gameserver on Serverbrowser  (Read 2339 times)

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Ping listed gameserver on Serverbrowser
« on: May 31, 2014, 05:49:08 AM »
Hey,

how is it possible to ping a remote gameserver, thats listed on my serverbrowser?
Im not connected on any server.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Ping listed gameserver on Serverbrowser
« Reply #1 on: May 31, 2014, 01:20:15 PM »
You can add the ability to ping game servers via UDP by modifying TNGameServer's ThreadFunction(). Line 293 section is currently:
  1.                                                         if (request == Packet.RequestActivateUDP)
  2.                                                         {
  3.                                                                 int pid = reader.ReadInt32();
  4.                                                                 player = GetPlayer(pid);
  5.  
  6.                                                                 // This message must arrive after RequestSetUDP which sets the UDP end point.
  7.                                                                 // We do an additional step here because in some cases UDP port can be changed
  8.                                                                 // by the router so that it appears that packets come from a different place.
  9.                                                                 if (player != null && player.udpEndPoint != null && player.udpEndPoint.Address == ip.Address)
  10.                                                                 {
  11.                                                                         player.udpEndPoint = ip;
  12.                                                                         player.udpIsUsable = true;
  13.                                                                         mUdp.SendEmptyPacket(player.udpEndPoint);
  14.                                                                 }
  15.                                                         }
You will want to append the Packet.RequestPing handling to it at the end like so:
  1.                                                         if (request == Packet.RequestActivateUDP)
  2.                                                         {
  3.                                                                 int pid = reader.ReadInt32();
  4.                                                                 player = GetPlayer(pid);
  5.  
  6.                                                                 // This message must arrive after RequestSetUDP which sets the UDP end point.
  7.                                                                 // We do an additional step here because in some cases UDP port can be changed
  8.                                                                 // by the router so that it appears that packets come from a different place.
  9.                                                                 if (player != null && player.udpEndPoint != null && player.udpEndPoint.Address == ip.Address)
  10.                                                                 {
  11.                                                                         player.udpEndPoint = ip;
  12.                                                                         player.udpIsUsable = true;
  13.                                                                         mUdp.SendEmptyPacket(player.udpEndPoint);
  14.                                                                 }
  15.                                                         }
  16.                                                         else if (request == Packet.RequestPing)
  17.                                                         {
  18.                                                                 BeginSend(Packet.ResponsePing);
  19.                                                                 EndSend(ip);
  20.                                                         }

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Ping listed gameserver on Serverbrowser
« Reply #2 on: June 01, 2014, 10:02:42 AM »
Thank you, very much!

Yesterday, i found this solution:

  1. System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
  2. PingReply pingReply = ping.Send("8.8.8.8");
  3.  

Is it okay, or should i use, your solution?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Ping listed gameserver on Serverbrowser
« Reply #3 on: June 01, 2014, 06:50:38 PM »
That's google's public DNS. What you're doing gives you ping to google's servers, not your game server.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Ping listed gameserver on Serverbrowser
« Reply #4 on: June 02, 2014, 03:30:25 AM »
I know :D

It was only an example IP, what i want to know is, can i use the NetworkInformation from .Net?

Why i ask so stupid is, i dont know how to use your Solution.
If you could explain me maybe, how to request an Ping from a gameserver, it would be very nice, to use your solution.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Ping listed gameserver on Serverbrowser
« Reply #5 on: June 03, 2014, 12:00:38 AM »
You can't use NetworkInformation's ping with TNet because when you ping a remote computer you don't go through TNet, and in most cases remote computers are not ping-able.

The code I posted lets you ping TNet's game servers, assuming they are listening to UDP traffic. To request a ping, use
  1. TNManager.BeginPacket(Packet.RequestPing);
  2. TNManager.EndPacket(remoteEndPoint);
...where "remoteEndPoint" is the end point of your server that you should already know.

Edit: Looking at the code though... there are further changes necessary in order for this to work as the "Ping" packet is handled internally inside GameClient.ProcessPacket, and there is no event callback for it which you'd want. I'll just add it to the next update.