Author Topic: External/Internal fallback  (Read 2432 times)

Duster

  • Guest
External/Internal fallback
« on: May 23, 2013, 12:53:34 PM »
Hi

This is with TNet 1.6.9 and Unity 3.5.7

I was having problems connecting to a game server (via TNManager.Connect) and eventually figured out that it was the path where falling back from the external address to the internal one that was going wrong (if I just called Connect with (internal, internal) it would work). The only difference I found between the fallback and the original connection is that the same socket object is being re-used. I changed the fallback code in TcpProtocol.OnConnectResult() to get a new object (just a one line change):

  1.                         // Begin a new connection attempt to the fallback address
  2.                         if (tcpEndPoint != null)
  3.                         {
  4.                                 mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // ADDED THIS LINE
  5.                                 result = mSocket.BeginConnect(tcpEndPoint, OnConnectResult, mSocket);
  6.                                 mCancelConnect = new Thread(CancelConnect);
  7.                                 mCancelConnect.Start(result);
  8.                                 return;
  9.                         }
  10.  

I'm not familiar with .Net's sockets, so couldn't say if there's a better way (perhaps the old socket should be cleaned up, or it can be safely reused with a simple tweak), but thought I'd post in case it helps anyone or made sense to make a change in the release code.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: External/Internal fallback
« Reply #1 on: May 23, 2013, 01:02:05 PM »
This should be fine, but don't forget to call
  1. if (mSocket != null) mSocket.Close();
before that.

Duster

  • Guest
Re: External/Internal fallback
« Reply #2 on: June 02, 2013, 07:57:44 PM »
So I upgraded to 1.7.0 and the TCP fallback stopped working for me. I wonder if there might be something unusual about my network because I don't see other people talking about this here - a little doubtful on this though, it's just an SMC cable router (SMCD3G) with a fairly simple configuration.

Anyway, the result of connecting to the external address is an immediate Connection Refused (verified by telnet). The WaitOne() in CancelConnect() never returns because the thread is killed in OnConnectResult(), and thus ConnectToFallback() never is called.

So my workaround for the moment is changing the end of OnConnectResult() like this:

      else if (!ConnectToFallback())
      {
         Error(errMsg);
         Close(false);
      }
   }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: External/Internal fallback
« Reply #3 on: June 02, 2013, 08:13:09 PM »
The wonders of different devices, I suppose. SMC doesn't allow loopback addressing from what I recall (using external IP to connect to your own machine will fail on it) -- which is likely what you are seeing. I'll add your fix to the main repo and see if it causes anything bad, but it should be safe.