Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer Attempt to send to not connected connection

Discussion in 'Connected Games' started by Ezro, Jul 13, 2018.

  1. Ezro

    Ezro

    Joined:
    Jun 7, 2013
    Posts:
    25
    Hey everyone,

    My current code flow is the following:
    WorldButton.OnClick() -> int connId = NetworkSystem.MigrateServer() -> NetworkSystem.SendConnect()

    Code (CSharp):
    1. void Start()
    2. {
    3.         NetworkTransport.Init();
    4.         ConnectionConfig cc = new ConnectionConfig();
    5.  
    6.         ReliableChannel = cc.AddChannel(QosType.Reliable);
    7.         ReliableFragmentedChannel = cc.AddChannel(QosType.ReliableFragmented);
    8.         UnreliableChannel = cc.AddChannel(QosType.Unreliable);
    9. }
    Code (CSharp):
    1.     public int MigrateServer(int connectionId, string address, int port)
    2.     {
    3.         NetworkTransport.Disconnect(hostId, connectionId, out error);
    4.         if (error == 0)
    5.         {
    6.             Connection connection = OpenConnections.First(a => a.ConnectionId == connectionId);
    7.             OpenConnections.Remove(connection);
    8.         }
    9.         int connId = NetworkTransport.Connect(hostId, address, port, 0, out error);
    10.         if (error == 0)
    11.         {
    12.             Connection connection = new Connection("WorldServer", connId);
    13.             Debug.Log("Connected to " + connection.ConnectionType + " as " + connection.ConnectionId);
    14.             OpenConnections.Add(connection);
    15.             byte[] connect = new byte[1];
    16.             connect[0] = 1;
    17.             NetworkTransport.Send(hostId, connId, ReliableChannelId, b, b.Length, out error);
    18.             return connId;
    19.         }
    20.         return -1;
    21.     }
    22.  
    In my MigrateServer, I see that I'm connecting properly with Debug.Log() (connId returns as != -1), but when I try to Send the connect[], I receive the message "Attempt to send to not connected connection".

    Does anyone know why this may be the case? I thought that since the connId was != -1 that it had already established the handshake.
     
  2. Ezro

    Ezro

    Joined:
    Jun 7, 2013
    Posts:
    25
    Over the weekend I was able to resolve this issue. I didn't read (understand) the documentation well enough, but have come to understand that when you connect it creates a connectionId from a connectionId pool within the requesting client itself.

    As a result, you can receive connectionId >= 0 with error == 0, but you're not actually connected to the other endpoint at that point. As the documentation states, I was able to remedy this by leveraging the ConnectEvent and NetworkTransport Receive.