Search Unity

Attempt to send to not connected connection

Discussion in 'Multiplayer' started by Ezro, Jul 13, 2018.

  1. Ezro

    Ezro

    Joined:
    Jun 7, 2013
    Posts:
    32
    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:
    32
    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.
     
  3. wechat_os_Qy0x8kV7OeiqTUgihI8CzJaUo

    wechat_os_Qy0x8kV7OeiqTUgihI8CzJaUo

    Joined:
    Dec 22, 2018
    Posts:
    2
    Sorry but I don't understand so well what "leveraging the ConnectEvent and NetworkTransport Receive" means?