Author Topic: Sending more server info to connecting players  (Read 3200 times)

Lumos

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 4
  • Posts: 21
    • View Profile
Sending more server info to connecting players
« on: December 08, 2014, 09:02:27 AM »
Greetings! I've been dabbling with TNet, and I'm so far thoroughly impressed (as a trained networking engineer who'd never actually done any network programming on end devices, lol). My relatively simple game took a trivial amount of time to setup, once I wrapped my head around the RFC idea (which, fortunately, didn't take long), and even my rigidbody-based guided missiles seemed to work basically right out of the box. Spectacular!
So, thing is, I'm now getting to the point where I need some testing that's not done by myself on my PC, connecting to my loopback, so I'll need a custom window for connecting (the server browser can wait until I get to when I really need it) to an arbitrary address.
Thing is, I'm not getting how to send custom data from the server - I'm using procedurally-generated "terrain", so I can't load some random level; all "maps" are to be loaded through one scene. Is there an explicit way to send extra data to the clients? Just asking, 'cause in one thread I found Aren recommends adding the data to the server's name. Is that the "right" way to do it? :P

Also, I'm not quite getting what "channels" are. Would I be right in thinking that a channel on a lobby server would be just a regular joinable server (the same thing that Photon calls "rooms", IIRC)?

Leslie

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 17
    • View Profile
Re: Sending more server info to connecting players
« Reply #1 on: December 08, 2014, 10:14:24 AM »
Once the client is connected you can use RFC to send the map data. Encode it into a string.

This is how I do it. In my game only one payer can connect to a created match (1vs1) so I just respond to first player join (which is not the server).

  1. protected void OnNetworkPlayerJoin(TNet.Player plr)
  2. {
  3.         if (isServer)
  4.         {
  5.                 BattlemassUI.Instance.ShowMessage("", "Opponent connected. Setting up match ...", BattlemassUI.Icon_Wait, true);
  6.  
  7.                 otherPlayer = plr;
  8.                 state = State.WaitingToStart;
  9.  
  10.                 // client connected, remove self from server list since this server is "full"
  11.                 StopBroadcastingServer();
  12.  
  13.                 // send map data to client and wait for reply
  14.                 tno.Send((byte)RFCs.OnMatchData, otherPlayer, Map.Instance.EnodeMapData());
  15.         }
  16. }
  17.  
  18. [TNet.RFC(2)]
  19. public void OnMatchData(string mapData)
  20. {
  21.         // send by server: gives client info on map to create and tell it to start match
  22.         GameGlobal.Instance.sessionSettings.mapData = mapData;
  23.         BattlemassUI.Instance.ShowLoading();
  24.         Invoke("LoadMap", 0.1f); // wait a few frames before calling LoadLevel so that "loading" screen can appear
  25. }
  26.  
  27. private void LoadMap()
  28. {
  29.    Application.LoadLevel("map");
  30. }
  31.  
« Last Edit: December 08, 2014, 10:23:07 AM by Leslie »

Leslie

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 17
    • View Profile
Re: Sending more server info to connecting players
« Reply #2 on: December 08, 2014, 10:20:21 AM »
The channels are like rooms.

The data in the name idea is more for stuff you need before a client connects. For example, I use it to indicate that the server has a password and what build/ version it is; so I can show this info in the list of servers.

  1. public void StartServer()
  2. {
  3.         ...
  4.  
  5.         // encode the server data into the TNServerInstance.serverName
  6.         TNServerInstance.serverName = GameGlobal.Instance.sessionSettings.matchName + // server name
  7.                 (char)30 + GameGlobal.BUILD + // server build
  8.                 (char)30 + (string.IsNullOrEmpty(GameGlobal.Instance.sessionSettings.password) ? "0" : "1");// server has password?
  9.  
  10.         ...
  11. }
  12.  
  13. // client side ...
  14. private void OnServerListChanged()
  15. {
  16.         ....
  17.  
  18.         if (TNLobbyClient.knownServers.list.size > 0)
  19.         {
  20.                 TNet.List<TNet.ServerList.Entry> list = TNLobbyClient.knownServers.list;
  21.                 for (int i = 0; i < list.size; i++)
  22.                 {
  23.                         TNet.ServerList.Entry ent = list[i];
  24.                         string[] data = ent.name.Split((char)30);
  25.  
  26.                         serverList.Add(new ServerInfo()
  27.                         {
  28.                                 pw = (data[2] == "1"), // true if there is password
  29.                                 name = data[0], // name of match
  30.                                 ip = null, // only set for LAN games
  31.                                 build = data[1], // what version the server is
  32.                                 server = ent // server info used when tryin gto connect to it
  33.                         });
  34.                 }
  35.         }
  36.         ...
  37. }
  38.  

Lumos

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 4
  • Posts: 21
    • View Profile
Re: Sending more server info to connecting players
« Reply #3 on: December 08, 2014, 10:59:43 AM »
This is simply brilliant. But of course!
I was actually going to send the "map name" or something along these lines along with the server name, so that's what I was going to use. And (char)30?! Good God, using the built-in delimiter character as intended... I was thinking about which keyboard character I should use as a delimiter (and should therefore ban from server names)...
Maybe, just maybe, the past two nights of not sleeping are getting to me. Maybe...

By the way, how are you doing your static RFC call? Does the TNObject auto-compute some sort of RFC enum, or did you add one yourself?

Leslie

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 17
    • View Profile
Re: Sending more server info to connecting players
« Reply #4 on: December 08, 2014, 01:29:31 PM »
I did not want to call via string name so I set up an enum so I do not have to remember which number belongs where ...

  1. public enum RFCs : byte
  2. {
  3.    OnServerBroadcast = 1,
  4.    OnMatchData = 2,
  5.    OnClientReadyToPlay
  6.  

Lumos

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 4
  • Posts: 21
    • View Profile
Re: Sending more server info to connecting players
« Reply #5 on: December 12, 2014, 02:59:12 AM »
You, sir*, are a bloody genious. Here's all the thanks I can give.

* or madam. Sorry, "Leslie" is an ambiguous name.