[Solved/Workaround] Custom data for Network Discovery component

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.

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.

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:

        // StartAsServer replacement
        /// <summary>
        /// Perform broadcasts over network
        /// </summary>
        public bool StartAsServer() {
          ...

            _hostId = NetworkTransport.AddHost(_defaultTopology, 0);
           ...

            byte err;

           // Fill buffer here!
            _msgOutBuffer = CompileBuffer(_matchMetadata);

            if (!NetworkTransport.StartBroadcastDiscovery(_hostId,
                                                          BroadcastPort,
                                                          BroadcastKey,
                                                          BroadcastVersion,
                                                          BroadcastSubVersion,
                                                          _msgOutBuffer,
                                                          _msgOutBuffer.Length,
                                                          MBroadcastInterval,
                                                          out err)) {
                return false;
            }

            Running = true;
            IsServer = true;

            _refreshBufferCoroutine = StartCoroutine(UpdateBroadcastData());

           ....

            return true;
        }
        /// <summary>
        /// Workaround coroutine to update the buffer of broadcast server
        /// </summary>
        /// <returns></returns>
        private IEnumerator UpdateBroadcastData() {
            yield return _refreshDelay;
        
            while (Running) {
                NetworkTransport.StopBroadcastDiscovery();
                NetworkTransport.RemoveHost(_hostId);

                // We need to wait until remove operation finishes, otherwise NetworkTransport will throw that
                // NetworkDiscovery is still running
                while (NetworkTransport.IsBroadcastDiscoveryRunning()) {
                    yield return _recheckDelay;
                }

                if (!Running) {
                    yield break;
                }

                _hostId = NetworkTransport.AddHost(_defaultTopology, 0);
                byte err;
             
                // Update buffer here
                _msgOutBuffer = CompileBuffer(_matchMetadata);

                if (!NetworkTransport.StartBroadcastDiscovery(_hostId,
                                                              BroadcastPort,
                                                              BroadcastKey,
                                                              BroadcastVersion,
                                                              BroadcastSubVersion,
                                                              _msgOutBuffer,
                                                              _msgOutBuffer.Length,
                                                              MBroadcastInterval,
                                                              out err)) {
                    yield break;
                }

                yield return _refreshDelay;
            }
        }

_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.

Hm, not sure, bit if i remember if you stop discovery, change buffer and start it again it will change

That’s exactly what I’m doing right now.

1 Like