Author Topic: How to get a channel list  (Read 12407 times)

Burletech

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
How to get a channel list
« on: January 30, 2013, 05:11:57 PM »
Hi,

Would it be possible to get an example of how to get a list of the channels on the server. I know the Packet.RequestChannelList is there but I wasn't sure how to use it. Thanks for your time!

Edit : Also an example of a lobby system would be very useful.
« Last Edit: January 30, 2013, 05:25:09 PM by Burletech »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a channel list
« Reply #1 on: January 30, 2013, 06:54:49 PM »
@Burletech, first check the definition for the ResponsePacketList:
  1. /// <summary>
  2. /// List open channels on the server.
  3. /// int32: number of channels to follow
  4. /// For each channel:
  5. /// int32: ID
  6. /// int32: Number of players
  7. /// bool: Has a password
  8. /// bool: Is persistent
  9. /// string: Level
  10. /// </summary>
Immediately you know what the data will be for this packet, which lets you create a function for it:
  1. void OnChannelList (Packet response, BinaryReader reader, IPEndPoint source)
  2. {
  3.         int count = reader.ReadInt32();
  4.  
  5.         for (int i = 0; i < count; ++i)
  6.         {
  7.                 int channelID           = reader.ReadInt32();
  8.                 int playerCount         = reader.ReadInt32();
  9.                 bool password           = reader.ReadBoolean();
  10.                 bool isPersistent       = reader.ReadBoolean();
  11.                 string level            = reader.ReadString();
  12.  
  13.                 // Do something with this information -- add it to a list perhaps? Whatever you need.
  14.         }
  15. }
You can register this packet handler in a Start() function like so:
  1. TNManager.client.packetHandlers[(byte)Packet.ResponseChannelList] = OnChannelList;
You can request a server channel list at any time like so:
  1. TNManager.client.BeginSend(Packet.RequestChannelList);
  2. TNManager.client.EndSend();

Burletech

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: How to get a channel list
« Reply #2 on: January 30, 2013, 11:16:02 PM »
Thanks for the speedy reply Aren :D I'll check it out ASAP.

krissebesta

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: How to get a channel list
« Reply #3 on: February 15, 2013, 08:12:11 AM »
@ArenMook: OK, help me out here please.  What am I doing wrong?  To begin with I start the server and then create a channel on the server (computer #1 ).  I can compile the code below without any errors.  However, after connecting from computer #2, when I put a break point on the first line below and run it I get this error message in the Mono debugger when I hover over "Packet" in the first line of code below.  Global::TNet.Packet Unknown member Packet.

  1. TNManager.client.BeginSend(Packet.RequestChannelList);
  2. TNManager.client.EndSend();
  3.  

And in my Start() method I get this error when I hover over the "OnChannelList" code.  Unknown Identifier: OnChannelList

  1. TNManager.client.packetHandlers[(byte)Packet.ResponseChannelList] = OnChannelList;
  2.  

I did a search through all the .cs files that come with TNet and the text "OnChannelList" is not found in any of them.  I have the version from the Unity Asset store which is Version 1.3.1.

I'm sure I'm missing something simple but I'm not understanding why it compiles yet I get these errors when I hover over the code. Needless to say the callback method OnChannelList never gets triggered on the client.

Thanks, Kris
« Last Edit: February 15, 2013, 08:36:51 AM by krissebesta »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a channel list
« Reply #4 on: February 15, 2013, 02:04:37 PM »
OnChannelList is just your own custom function that will do something with the list that was sent.

krissebesta

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: How to get a channel list
« Reply #5 on: February 15, 2013, 05:55:29 PM »
@ArenMook:  But I do have OnChannelList defined in my main class which has all the other code in it (which I copied from the mainMenu example).  My OnChannelList is copied exactly as it is from this forum page (see below).  I've literally copied all three pieces from this forum page and I'm still having the two issues (both Packet and OnChannelList are shown in MonoDevelop as "Unknown" and OnChannelList never gets called).

  1. void OnChannelList (Packet response, BinaryReader reader, IPEndPoint source)
  2. {
  3.         int count = reader.ReadInt32();
  4.        
  5.         for (int i = 0; i < count; ++i)
  6.         {
  7.                 int channelID           = reader.ReadInt32();
  8.                 int playerCount         = reader.ReadInt32();
  9.                 bool password           = reader.ReadBoolean();
  10.                 bool isPersistent       = reader.ReadBoolean();
  11.                 string scene            = reader.ReadString();
  12.                
  13.                 // Do something with this information -- add it to a list perhaps? Whatever you need.
  14.         }
  15. }
  16.  

Also, can you also answer my first question?

I get this error message in the Mono debugger when I hover over "Packet" in the first line of code below.  Global::TNet.Packet Unknown member Packet.

  1. TNManager.client.BeginSend(Packet.RequestChannelList);
  2. TNManager.client.EndSend();
  3.  

Thanks, Kris
« Last Edit: February 15, 2013, 06:35:07 PM by krissebesta »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a channel list
« Reply #6 on: February 16, 2013, 01:54:08 AM »
Are you sure you imported everything correctly? If it can't determine what "packet" is, it suggests that you're missing some parts of TNet. TNPacket.cs file defines class Packet that's within the TNet namespace.

blueskined

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: How to get a channel list
« Reply #7 on: February 20, 2013, 10:22:45 AM »
can you make a more detailed tutorial on this? I think it's important to also know how to do work on the server side! that's the point for the server's source code isn't it!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a channel list
« Reply #8 on: February 20, 2013, 07:29:58 PM »
Yup. I'll come up with something.

krissebesta

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: How to get a channel list
« Reply #9 on: February 21, 2013, 07:16:54 AM »
@ArenMook:  I did get the channels working finally.  Solution: When I imported TNet into a new project and ran it, started the server, attempt to connect to a channel (which would attempt to load Example1 scene) it would disconnect.  Well it turns out you MUST add all the Example scenes to the Build Settings > Scenes in Build list in order for the scene to load and NOT get disconnected.  I guess it couldn't load the scene and would disconnect.  Anyways, it's working now and I'm happy.  Thanks!!!  Also, an example of the lobby would be great!
« Last Edit: February 21, 2013, 07:19:13 AM by krissebesta »

speedyw03

  • Guest
Re: How to get a channel list
« Reply #10 on: March 08, 2013, 11:08:44 AM »
I'm starting to switch over to C# because of Tnet and NGUI :) When I try this out, I get some errors:
The type or namespace name `Packet' could not be found. Are you missing a using directive or an assembly reference?
same goes for binaryreader and IPEndPoint
here's my full script http://pastebin.com/3YmhpBCf
UPDATE!
Figured out I need to add this lol:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using TNet;
« Last Edit: March 08, 2013, 03:28:03 PM by speedyw03 »

TheCodeTraveller

  • Guest
Re: How to get a channel list
« Reply #11 on: September 26, 2013, 01:44:14 AM »
Bit of a necro post but still relevant. The signature of that response has since changed in terms of additional parameters, data as well as playerLimit (quoting from memory might have variable names incorrect).

What was strange is when I receive the packet back and read, "Sometimes" I get a end of stream exception (reading too far), yet, if I debug the same method and pause just after the first read (id if I recall), it goes through without troubles.

Are there times that less data can be sent, according to the response packet server side, no.

Strange behavior and I'll look into it more later, I'm sure it's specific to my code / something I've done. If someone else has seen similar please post back.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a channel list
« Reply #12 on: September 26, 2013, 04:30:42 PM »
The amount of data sent may change, but only in a consistent fashion. You get the number indicating how many entries will follow, and if that number is zero then you shouldn't do anything else.

TheCodeTraveller

  • Guest
Re: How to get a channel list
« Reply #13 on: September 27, 2013, 04:21:36 AM »
Will look into that. However, how do you indicate that there are x amount of entries to follow?

int count = reader.ReadInt32();

Simply results in the count of channels available. It will never read if the count is 0. Is there another property you are filling in to say for example, the second reader.ReadBoolean(); isn't valid for the returned packet. I don't see why this would make sense in any case because the response includes all of this data in it's write back to the client. The worst that could happen is reading a NULL, which should perhaps be checked server side anyway. Let me know if there is something I'm not understanding about the response coming back. So far I've managed to not run into the same issue by not reading past the playerCount.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a channel list
« Reply #14 on: September 27, 2013, 07:03:53 PM »
From the description of Packet.ResponseChannelList:
  1.         /// <summary>
  2.         /// List open channels on the server.
  3.         /// int32: number of channels to follow
  4.         /// For each channel:
  5.         /// int32: ID
  6.         /// ushort: Number of players
  7.         /// ushort: Player limit
  8.         /// bool: Has a password
  9.         /// bool: Is persistent
  10.         /// string: Level
  11.         /// string: Custom data
  12.         /// </summary>
The first 4 bytes is an integer indicating the number of entries to follow. Each entry is of fixed size. There are only two variable length fields -- but both are strings, so get handled seamlessly.