Author Topic: Use a password?  (Read 9047 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Use a password?
« on: June 29, 2016, 04:09:32 PM »
How do I test if a channel has a password ahead of trying to join it? I want a test so if there's no password, I connect, and if there is to bring up a password entry dialogue.

Tried the packet request code from the FAQ (http://www.tasharen.com/forum/index.php?topic=6538.msg30866#msg30866) but half the contents are unknown - BinaryReader, IPEndPoint, ReadInt32 etc.

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Use a password?
« Reply #1 on: June 29, 2016, 07:19:36 PM »
Add the following to the top of your source file:
  1. using System.Net;
  2. using System.IO;
  3.  

 ;)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #2 on: June 30, 2016, 06:03:22 AM »
Thanks. Is there a callback for if the password is incorrect? Is that an OnError?

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #3 on: June 30, 2016, 09:16:05 AM »
I'm not getting any ChannelList callback, not with:
  1.         TNManager.GetChannelList(OnChannelListRefresh);
  2.  
  3.         void OnChannelListRefresh(List<Channel.Info> list)      {
  4.                 print ("Channel list refresh");
  5.         }
  6.  
...nor...
  1.         TNManager.client.packetHandlers[(byte)Packet.ResponseChannelList] = OnChannelList;
  2.  
  3.         TNManager.client.BeginSend(Packet.RequestChannelList);
  4.         TNManager.client.EndSend();
  5.  
  6.         void OnChannelList (Packet response, BinaryReader reader, IPEndPoint source)    {
  7.                 print ("OnChannelList ");
  8.         }
  9.  
Neither method gets called.

NeilM

  • Global Moderator
  • Newbie
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 7
  • Posts: 16
    • View Profile
Re: Use a password?
« Reply #4 on: June 30, 2016, 09:20:18 AM »
Where are you calling TNManager.GetChannelList from?


Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #5 on: June 30, 2016, 09:26:01 AM »
OnNetworkConnect callback

  1.         void OnNetworkConnect(bool success, string message){
  2.                 bool home = Random.value < 0.5f;
  3.                 GameDetails.instance.SetDifficulty (1);
  4.                 GameDetails.instance.SetGameDetails (MATCHTYPE.NETWORKED, true, home, home, !home,"CLUB_001","CLUB_001");
  5.                 if(TNServerInstance.isActive){
  6.                         print ("Create channel");
  7.                         TNManager.CreateChannel ("MatchLauncher", false, 2, netPassword, true);
  8.                 }else{
  9.                         print ("Channels stuff ");
  10.                         TNManager.GetChannelList(OnChannelList);
  11. //                      TNManager.client.BeginSend(Packet.RequestChannelList);
  12. //                      TNManager.client.EndSend();
  13.                 }
  14.         }
  15.  

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #6 on: June 30, 2016, 09:33:02 AM »
Okay, I'm apparently not connected at that point. I've got a "The requested address is not valid in its context". I guess I'm not connecting.

I'm calling this on a button press...
  1. }else   if(serverList.Count == 1 && serverList[0].playerCount == 1){
  2.                         ServerList.Entry ent = serverList[0];
  3.                         print ("Button Press Join "+serverList.Count+"   "+ent.internalAddress+"    "+ent.externalAddress);
  4.                         TNManager.Connect(ent.internalAddress, ent.internalAddress);
  5.  
I get output...
Button Press Join 1   192.168.1.105:0    92.21.229.249:0

..then "The Resquested address is not valid in its context."

I've tried internalAddress and externalAddress in the connect method with the same results.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #7 on: June 30, 2016, 09:38:41 AM »
Rebuilt and restarted on Android and it connects now.

NeilM

  • Global Moderator
  • Newbie
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 7
  • Posts: 16
    • View Profile
Re: Use a password?
« Reply #8 on: June 30, 2016, 09:40:31 AM »
Are you now able to see the hasPassword bool in the Channel.Info list?

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #9 on: June 30, 2016, 09:48:52 AM »
I think so. I've found the problem is when I destroy and create a new server. That's when I get the "The Resquested address is not valid in its context." error on the client.

For clarity -
Create server on Android tablet. This creates a network game
Join game on PC. Can connect and play.
Quit game.
Cancel server.
Create new server on Android tablet. Works as normal.
Try to connect and get address error

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #10 on: June 30, 2016, 10:04:14 AM »
When I connect to the first server on the Android device, its address is 192.168.56.1:5127 in the server list entry.
When I use TNServerInstance.Stop (); to stop it, and then create another server on the Android device using
  1. TNManager.Connect("127.0.0.1:5127");
its address changes to 192.168.56.1:0 in the server list entry.

Do I need to release port 5127 or something?

On the host device:
  1. TNManager.Connect("127.0.0.1:5127");
  2.  
Launches a game just fine.

On the client device:
  1. ServerList.Entry ent = serverList[0];
  2. print ("Button Press Join "+serverList.Count+"   "+ent.internalAddress+"    "+ent.externalAddress);
  3. TNManager.Connect(ent.internalAddress, ent.internalAddress);
  4.  
has the wrong port number on the second server.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Use a password?
« Reply #11 on: July 02, 2016, 12:09:24 AM »
When you do TNServerInstance.Stop(), there is no more server running. Trying TNManager.Connect at that point should flat out fail.

Stopping the server closes the socket and releases the bound port. The exact line where it happens is "mListener.Stop();". The only place where port can be set to 0 is if you stop listening by calling MakePrivate() on the server.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #12 on: July 11, 2016, 08:55:27 AM »
I've rewritten the frontend to provide a straight forward server list. When creating a game, pressing the Create creates a channel with the password set from a text field. Running the same app on different Android devices, for some of them the server password is set and for others it's apparently not.

Trying to debug, I've added an NGUI UILabel and change its text at various places like OnNetworkConnect and OnNetworkJoin. Without leaving the scene, I've had a "MissingReferenceException: The object of type UILabel has been destroyed..." error even though the object is untouched, but it's inconsistent.

The offending line is DEV_Debug.text = "Channel list refresh "+list.Count+"   "+list.buffer[0].hasPassword;
  1.         void OnChannelListRefresh(List<Channel.Info> list)      {
  2.                 // use list[index].data.GetChild<string>("ChannelName");
  3.                 print ("Channel list refresh "+list.Count+"   "+list.buffer[0].hasPassword);
  4.                 DEV_Debug.text = "Channel list refresh "+list.Count+"   "+list.buffer[0].hasPassword;
  5.                 if(!list.buffer[0].hasPassword){
  6.                         TNManager.JoinChannel (1,"MatchLauncher");
  7.                 }else{
  8.                         // show password panel
  9.                         print ("netPasswordPanel.SetActive");
  10.                         DEV_Debug.text = "netPasswordPanel.SetActive ";
  11.                         twn_netPassword.SetActive (true);
  12.                 }
  13.         }
  14.  
The same can happen with the twn_netPassword GameObject, getting the error it's been destroyed when the line is called in OnChannelListRefresh(). At which point the password panel doesn't show and I can't enter the password, although I can enable it manually in the editor.

I also have the password not be accepted the first time I enter it, and have to reenter

  1.         public void EnterPassword(){
  2.                 // Enter into password to gain access
  3.                 netPassword = password.text.Trim ();
  4.                 print ("Enter password "+netPassword);
  5.                 // attempt join
  6.                 TNManager.JoinChannel (1,"MatchLauncher",false,2,netPassword);
  7.         }
The attached shows the correct password being rejected twice before being accepted.

Edit: Also added console report on missing UILabel and GameObject. I just press the same button 3 times, a few seconds between each time, and the third time it works. The UILabel in question is just a stationary, no nonsense label. That's maybe an NGUI problem.
« Last Edit: July 11, 2016, 02:26:32 PM by Shifty Geezer »

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #13 on: July 11, 2016, 03:42:28 PM »
Found that the channel password is sticking at the first password entered for the first server created on a device, even though I terminate the server and create a new one, and pass the new channel password every time I create the channel.

  1.         void Start () {
  2.                 Data_Manager.CreateInstance();
  3.                 if(TNServerInstance.isActive){
  4.                         TNServerInstance.Stop ();
  5.                 }
  6.                 ...
  7.         }
  8.  
  9.         public void ButtonPressedServerAccept(){
  10.                 loading.SetActive (true);
  11.                 netGameName = field_servername.text;
  12.                 netGamePassword = field_serverpassword.text;
  13.                 if(netGamePassword == "none"){
  14.                         netGamePassword = "";
  15.                 }
  16.                 field_servername.GetComponent<UIInput>().RemoveFocus ();
  17.                 field_serverpassword.GetComponent<UIInput>().RemoveFocus ();
  18.                 StartCoroutine ("StartServer");        
  19.         }
  20.  
  21.         IEnumerator StartServer(){
  22.                 Tools.ResolveIPs(null);
  23.                 yield return new WaitForSeconds(0.25f);   // allow loading image to be displayed
  24.                 if(!TNServerInstance.isActive){
  25.                         serverUdpPort = Random.Range(10000, 50000);
  26.                         if(TNServerInstance.Start(5127, serverUdpPort, lobby.remotePort, "server.dat", TNServerInstance.Type.Udp)){
  27.                                 lbl_NetMessage.enabled = false;
  28.                         }
  29.                         while(!TNServerInstance.isListening) yield return null; // wait for TNServerInstance to finalise
  30.                         print ("Set server name "+netGameName);
  31.                         TNServerInstance.serverName = netGameName;
  32.                         TNManager.Connect("127.0.0.1:5127");    // connect to local server
  33.                 }else{
  34.                         print ("Server already running");
  35.                 }
  36.         }
  37.  
  38.         void OnNetworkConnect(bool success, string message){
  39.                 bool home = Random.value < 0.5f;
  40.                 GameDetails.instance.SetDifficulty (1);
  41.                 string myClubKey = "CLUB_001";
  42.                 if(PlayerPrefs.GetInt (GLOBALS.PREFS_CLUB_CREATED, 0) != 0){
  43.                         myClubKey = "CLUB_900";
  44.                 }
  45.                 GameDetails.instance.SetGameDetails (MATCHTYPE.NETWORKED, true, home, home, !home, home ? myClubKey : "CLUB_001", !home ? myClubKey : "CLUB_001");
  46.                 if(TNServerInstance.isActive){
  47.                         print ("OnNetworkConnect "+TNServerInstance.listeningPort+"   "+netGameName+"   password "+netGamePassword);
  48.                         TNManager.JoinChannel (1,"MatchLauncher", false, 2, netGamePassword);
  49.                 }else{
  50.                         print ("Channels stuff "+success+"   "+message);
  51.                         TNManager.GetChannelList(OnChannelListRefresh);
  52.                 }
  53.         }
  54.  
The printed password is the new password, but the JoinChannel appears to be using the old password.
« Last Edit: July 11, 2016, 03:59:18 PM by Shifty Geezer »

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Use a password?
« Reply #14 on: July 11, 2016, 03:53:15 PM »
Eclipse (Android) reports an error:

07-11 21:52:07.894: E/Unity(5663): Access to the path "/server.dat" is denied.

Are the settings not being saved?
« Last Edit: July 11, 2016, 03:59:47 PM by Shifty Geezer »