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?
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:
/// <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.