Search Unity

LLAPI - How to refuse unwanted connections before connecting?

Discussion in 'Multiplayer' started by HiddenMonk, Jun 7, 2017.

  1. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    All of this is in regards to the LLAPI.

    From what I understand, when you open up a connection such as like this
    Code (CSharp):
    1.  
    2. void Setup()
    3. {
    4.     ConnectionConfig config = new ConnectionConfig();
    5.     channelID = config.AddChannel(QosType.Unreliable);
    6.     HostTopology topology = new HostTopology(config, maxConnections);
    7.     socketId = NetworkTransport.AddHost(topology, socketPort);
    8. }
    Whenever someone calls Connect, like this
    Code (CSharp):
    1. targetConnectionID = NetworkTransport.Connect(socketId, address, port, 0, out error);
    The opened connection will just blindly accept anyone that asks.

    Is there a way to decide if you actually want to connect?

    For example, steamworks peer to peer has something called P2PSessionRequest_t, which I think is sent by steams servers to you when someone requests to connect to you. You can then grab the steamID from the request and check if the player is actually in the same lobby as you, and if so you can safely accept the connection by using AcceptP2PSessionWithUser.
    I was thinking I could do all of that, then when I accept the connection and get their ip address by using steams
    Code (CSharp):
    1.  
    2. GetP2PSessionState( CSteamID steamIDRemote, P2PSessionState_t *pConnectionState )
    3.  
    which the P2PSessionState_t struct has the IP Address and Port in the m_nRemoteIP variable.
    Then ill send a Connect request through unitys networking so that I dont have to use steamworks networking.
    Then hopefully I can end the connection on the steamworks side without it messing up anything such as if steam decided to use a relay server for the connection, hopefully it keeps the relay.
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Still not sure how to block connections before they actually connect.

    I tried something like this...

    Code (CSharp):
    1.  
    2.             NetworkEventType receiveNetworkEvent = NetworkTransport.Receive(out recSocketId, out recConnectionId, out recChannelId, recBuffer, bufferSize, out dataSize, out error);
    3.  
    4.             switch(receiveNetworkEvent)
    5.             {
    6.                 //......
    7.                 case NetworkEventType.ConnectEvent:
    8.                     NetworkTransport.RemoveHost(recSocketId);
    9.                     break;
    10.                 //....
    11.             }
    This was tested only over localhost (editor and build on the same computer).
    RemoveHost Doesnt seem to work to refuse the connection, the other end still receives a ConnectEvent
    It seems when you RemoveHost, you dont get a DisconnectEvent on your side for the connection.
    It also seems RemoveHost doesnt send a Disconnect event to the connections on the socket, so instead they are disconnected by the DisconnectTimeout.
     
  3. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    I think you're basically talking about code that would be implemented on top of the transport. It would be up to you to sort through incoming connections and refuse ones you don't want based on whatever you can see from their connection (not much) or based on data they send you, such as an authentication token, lobby membership, whatever.
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    It could be something simple, like a event OnConnectionRequest(ConnectionRequestInfo connectionRequestInfo) we can subscribe to in the NetworkTransport.
    We can have connections accepted by default, but if you subscribe to the OnConnectionRequest youll have the ability to call NetworkTransport.RefuseConnection(connectionRequestInfo.connectionID) so that the connection is never made and no packets are sent back to the other end so that they have no idea if there is anything to really connect to.