Search Unity

[Solved/Workaround] Custom data for Network Discovery component

Discussion in 'Multiplayer' started by xVergilx, Sep 12, 2017.

  1. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I'm trying to write a custom network discovery component to supply it with custom data (based on HLAPI source).
    So this question a bit LLAPI.
    Is there a way to change buffer array data after calling NetworkTransport.StartBroadcastDiscovery?

    Seems like there's only get methods on it.
     
    Last edited: Sep 12, 2017
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Anyone? This could be very usefull, e.g. broadcasting current player count etc.

    Maybe @aabramychev can give a little hint?

    I really don't want to implement a custom server for the functionality that may already exist.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Okay, since nobody offered anything, I've came up with a workaround. Restarting BroadcastDiscovery on my own in a coroutine at certain intervals and refreshing the buffer. Not the prettiest solution, but it works.

    Here's pseudo-code for those who may encounter same problem in the future:

    Code (CSharp):
    1.  
    2.         // StartAsServer replacement
    3.         /// <summary>
    4.         /// Perform broadcasts over network
    5.         /// </summary>
    6.         public bool StartAsServer() {
    7.           ...
    8.  
    9.             _hostId = NetworkTransport.AddHost(_defaultTopology, 0);
    10.            ...
    11.  
    12.             byte err;
    13.  
    14.            // Fill buffer here!
    15.             _msgOutBuffer = CompileBuffer(_matchMetadata);
    16.  
    17.             if (!NetworkTransport.StartBroadcastDiscovery(_hostId,
    18.                                                           BroadcastPort,
    19.                                                           BroadcastKey,
    20.                                                           BroadcastVersion,
    21.                                                           BroadcastSubVersion,
    22.                                                           _msgOutBuffer,
    23.                                                           _msgOutBuffer.Length,
    24.                                                           MBroadcastInterval,
    25.                                                           out err)) {
    26.                 return false;
    27.             }
    28.  
    29.             Running = true;
    30.             IsServer = true;
    31.  
    32.             _refreshBufferCoroutine = StartCoroutine(UpdateBroadcastData());
    33.  
    34.            ....
    35.  
    36.             return true;
    37.         }
    Code (CSharp):
    1.  
    2.         /// <summary>
    3.         /// Workaround coroutine to update the buffer of broadcast server
    4.         /// </summary>
    5.         /// <returns></returns>
    6.         private IEnumerator UpdateBroadcastData() {
    7.             yield return _refreshDelay;
    8.        
    9.             while (Running) {
    10.                 NetworkTransport.StopBroadcastDiscovery();
    11.                 NetworkTransport.RemoveHost(_hostId);
    12.  
    13.                 // We need to wait until remove operation finishes, otherwise NetworkTransport will throw that
    14.                 // NetworkDiscovery is still running
    15.                 while (NetworkTransport.IsBroadcastDiscoveryRunning()) {
    16.                     yield return _recheckDelay;
    17.                 }
    18.  
    19.                 if (!Running) {
    20.                     yield break;
    21.                 }
    22.  
    23.                 _hostId = NetworkTransport.AddHost(_defaultTopology, 0);
    24.                 byte err;
    25.              
    26.                 // Update buffer here
    27.                 _msgOutBuffer = CompileBuffer(_matchMetadata);
    28.  
    29.                 if (!NetworkTransport.StartBroadcastDiscovery(_hostId,
    30.                                                               BroadcastPort,
    31.                                                               BroadcastKey,
    32.                                                               BroadcastVersion,
    33.                                                               BroadcastSubVersion,
    34.                                                               _msgOutBuffer,
    35.                                                               _msgOutBuffer.Length,
    36.                                                               MBroadcastInterval,
    37.                                                               out err)) {
    38.                     yield break;
    39.                 }
    40.  
    41.                 yield return _refreshDelay;
    42.             }
    43.         }
    _refreshDelay is WaitForSeconds(5) and _recheckDelay is WaitForSeconds(0.1)

    Everything else is from NetworkDiscovery source of HLAPI, though it's refactored for our code style. Full source of that you can always find on Unity's BitBucket.
     
  4. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Hm, not sure, bit if i remember if you stop discovery, change buffer and start it again it will change
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    That's exactly what I'm doing right now.
     
    aabramychev likes this.