Tasharen Entertainment Forum

Support => TNet 3 Support => Topic started by: Burletech on January 30, 2013, 05:11:57 PM

Title: How to get a channel list
Post by: Burletech 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.
Title: Re: How to get a channel list
Post by: ArenMook 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();
Title: Re: How to get a channel list
Post by: Burletech on January 30, 2013, 11:16:02 PM
Thanks for the speedy reply Aren :D I'll check it out ASAP.
Title: Re: How to get a channel list
Post by: krissebesta 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
Title: Re: How to get a channel list
Post by: ArenMook 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.
Title: Re: How to get a channel list
Post by: krissebesta 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
Title: Re: How to get a channel list
Post by: ArenMook 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.
Title: Re: How to get a channel list
Post by: blueskined 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!
Title: Re: How to get a channel list
Post by: ArenMook on February 20, 2013, 07:29:58 PM
Yup. I'll come up with something.
Title: Re: How to get a channel list
Post by: krissebesta 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!
Title: Re: How to get a channel list
Post by: speedyw03 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 (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;
Title: Re: How to get a channel list
Post by: TheCodeTraveller 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.
Title: Re: How to get a channel list
Post by: ArenMook 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.
Title: Re: How to get a channel list
Post by: TheCodeTraveller 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.
Title: Re: How to get a channel list
Post by: ArenMook 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.
Title: Re: How to get a channel list
Post by: inewland53 on May 24, 2014, 05:18:36 PM
I was running into a read error when trying to get the Player Limit as well. In order to get a ushort from the BinaryReader you need to use this code:

  1. int playerLimit         = reader.ReadUInt16();   // Don't use ReadInt32();
  2.  

Hope that helps someone.
Title: Re: How to get a channel list
Post by: Doomlazy on October 17, 2015, 07:08:23 PM
  1. void OnChannelList(Packet response, BinaryReader reader, IPEndPoint source)
  2.     {
  3.         Console.AddMessage("Fetching Room List...");
  4.  
  5.         int count = reader.ReadInt32();
  6.  
  7.         for (int i = 0; i < count; ++i)
  8.         {
  9.             int channelID = reader.ReadInt32();
  10.             int playerCount = reader.ReadInt32();
  11.             bool password = reader.ReadBoolean();
  12.             bool isPersistent = reader.ReadBoolean();
  13.             string level = reader.ReadString();
  14.             string channelName = reader.ReadString();
  15.  
  16.             print(playerCount);
  17.         }
  18.  
  19.         Console.AddMessage("Fetched Room List");
  20.     }

From this code you can see that I should get a message in my Unity console containing the number of players in a channel.
For some reason I get rediculous 6 digit numbers instead of the amount of players in a channel.

Example: 1 player connected to a room is supposed to print "1". Instead I get this: "589825"

Please help :P
Title: Re: How to get a channel list
Post by: devomage on October 18, 2015, 01:56:17 AM
TNPacket.cs
line #306-319

you are reading an "int" when it should be "ushort"...

this problem was addressed many times on page 1 of this thread.

  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>
  13.  
  14. ResponseChannelList,
  15.  
Title: Re: How to get a channel list
Post by: Doomlazy on October 18, 2015, 06:19:13 AM
Thanks! This seems to have solved my problem :)

However now I just want to know. What is the difference between, int, int16, int32, int64, uint16, uin32 and uint64..   ???
Title: Re: How to get a channel list
Post by: ArenMook on October 18, 2015, 06:53:15 PM
The difference is size in bytes. Int16 is 16 bits (2 bytes). Int32 is 32 bits (4 bytes) and Int64 is 64 bits (8 bytes).

You were trying to read too much data from the stream.
Title: Re: How to get a channel list
Post by: devomage on October 18, 2015, 08:05:55 PM
Thanks! This seems to have solved my problem :)

However now I just want to know. What is the difference between, int, int16, int32, int64, uint16, uin32 and uint64..   ???

id rather not post this here, but its a nice reference:

  1. C# Type                 .NET Framework Type             mySQL                                           Range
  2. ---------------------------------------------------------------------------------------------------------------------
  3.  
  4. bool                    System.Boolean                  tinyint(1)
  5.  
  6. byte                    System.Byte                     tinyint(3) unsigned                     0 to 255
  7. sbyte                   System.SByte                    tinyint(3)                              -128 to 127
  8. short                   System.Int16                    smallint(5)                             -32,768 to 32,767
  9. ushort                  System.UInt16                   smallint(5)     unsigned                0 to 65,535
  10. int                     System.Int32                    int(11)                                 -2,147,483,648 to 2,147,483,647
  11. uint                    System.UInt32                   int(11) unsigned                        0 to 4,294,967,295
  12. long                    System.Int64                    bigint(20)                              –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  13. ulong                   System.UInt64                   bigint(20) unsigned                     0 to 18,446,744,073,709,551,615
  14.