refactored ConvaiUDPSpeechSender for faster audio clip detection and simplified UDPPeerDiscovery to maintain connection without timeouts or heartbeats
This commit is contained in:
@ -292,12 +292,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
{
|
||||
checkCount++;
|
||||
|
||||
// Log periodically to show we're still monitoring
|
||||
if (enableDebugLogging && checkCount % 10 == 0)
|
||||
{
|
||||
ConvaiLogger.DebugLog($"🔊 Monitoring... check #{checkCount}, current clip: {(audioSource?.clip != null ? audioSource.clip.name : "null")}, isTalking: {sourceNPC.IsCharacterTalking}", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
|
||||
// SIMPLIFIED: Check EVERY frame if there's a new clip, don't wait for events
|
||||
if (audioSource?.clip != null && audioSource.clip != lastClip)
|
||||
{
|
||||
// New clip detected!
|
||||
@ -312,6 +307,14 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
// Get the transcript from the most recent available transcript
|
||||
string transcript = GetRecentTranscript();
|
||||
|
||||
// Start transmission if not already started
|
||||
if (!_isSendingSpeech)
|
||||
{
|
||||
_isSendingSpeech = true;
|
||||
OnSpeechTransmission?.Invoke(true);
|
||||
ConvaiLogger.Info($"🔊 Starting speech transmission", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
|
||||
// Send this clip
|
||||
ConvaiLogger.Info($"🔊 TRANSMITTING CLIP to {targetIP}:{targetPort}", ConvaiLogger.LogCategory.Character);
|
||||
_ = TransmitAudioClip(lastClip, transcript);
|
||||
@ -322,7 +325,13 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.1f); // Check every 100ms
|
||||
// Log periodically to show we're still monitoring
|
||||
if (enableDebugLogging && checkCount % 100 == 0)
|
||||
{
|
||||
ConvaiLogger.DebugLog($"🔊 Monitoring... check #{checkCount}, current clip: {(audioSource?.clip != null ? audioSource.clip.name : "null")}, isTalking: {sourceNPC.IsCharacterTalking}", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.05f); // Check every 50ms for faster detection
|
||||
}
|
||||
|
||||
ConvaiLogger.Info($"🔊 Stopped monitoring audio clips (NPC stopped talking or was destroyed). Checks performed: {checkCount}", ConvaiLogger.LogCategory.Character);
|
||||
@ -440,15 +449,6 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
|
||||
try
|
||||
{
|
||||
// Start transmission if not already started
|
||||
if (!_isSendingSpeech)
|
||||
{
|
||||
_isSendingSpeech = true;
|
||||
OnSpeechTransmission?.Invoke(true);
|
||||
|
||||
ConvaiLogger.Info($"🔊 Starting speech transmission", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
|
||||
// Use the current speech sequence for this entire clip
|
||||
int clipSequence = _speechSequence;
|
||||
|
||||
|
||||
@ -115,15 +115,8 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Check for peer timeout
|
||||
if (_connectionState == ConnectionState.Connected)
|
||||
{
|
||||
TimeSpan timeSinceLastPacket = DateTime.UtcNow - _lastPeerPacketTime;
|
||||
if (timeSinceLastPacket.TotalSeconds > peerTimeoutSeconds)
|
||||
{
|
||||
HandlePeerLost();
|
||||
}
|
||||
}
|
||||
// No timeout checks - once connected, stay connected until restart
|
||||
// Removed: peer timeout monitoring
|
||||
}
|
||||
|
||||
private void StartDiscovery()
|
||||
@ -150,8 +143,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
// Start broadcasting discovery requests
|
||||
_ = BroadcastDiscoveryRequests(_cancellationTokenSource.Token);
|
||||
|
||||
// Start heartbeat when connected
|
||||
_ = SendHeartbeats(_cancellationTokenSource.Token);
|
||||
// NO HEARTBEATS - once connected, stay connected
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -376,23 +368,8 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
break;
|
||||
|
||||
case PACKET_TYPE_HEARTBEAT:
|
||||
// Heartbeat keeps connection alive
|
||||
if (_peerIP == senderIP)
|
||||
{
|
||||
// Same peer - update timestamp and reconnect if we were in Lost state
|
||||
if (_connectionState == ConnectionState.Lost || _connectionState == ConnectionState.Discovering)
|
||||
{
|
||||
// Reconnected to known peer
|
||||
SetConnectionState(ConnectionState.Connected);
|
||||
ConvaiLogger.Info($"🔄 Reconnected to peer Player {senderPlayerID} at {senderIP}", ConvaiLogger.LogCategory.Character);
|
||||
LogEvent($"🔄 Reconnected to peer Player {senderPlayerID}");
|
||||
}
|
||||
}
|
||||
else if (_connectionState != ConnectionState.Connected)
|
||||
{
|
||||
// Different peer IP than expected - this is a new peer
|
||||
HandlePeerDiscovered(senderIP, senderPlayerID);
|
||||
}
|
||||
// Ignore heartbeats - no reconnection logic
|
||||
// Once connected, stay connected until restart
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -420,18 +397,12 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
|
||||
private void HandlePeerLost()
|
||||
{
|
||||
ConvaiLogger.Warn($"⚠️ Peer connection lost (Player {_peerPlayerID} at {_peerIP}) - will keep trying to reconnect", ConvaiLogger.LogCategory.Character);
|
||||
LogEvent($"⚠️ Peer connection lost (Player {_peerPlayerID}) - reconnecting...");
|
||||
ConvaiLogger.Warn($"⚠️ Peer connection lost (Player {_peerPlayerID} at {_peerIP}) - but ignoring, keeping connection alive", ConvaiLogger.LogCategory.Character);
|
||||
LogEvent($"⚠️ Peer connection lost (Player {_peerPlayerID}) - IGNORING (stay connected until restart)");
|
||||
|
||||
SetConnectionState(ConnectionState.Lost);
|
||||
|
||||
// Notify listeners
|
||||
OnPeerLost?.Invoke();
|
||||
|
||||
// Don't clear peer IP - keep it and keep trying to reconnect
|
||||
// The heartbeat system will continue sending packets to the known peer
|
||||
// and will automatically reconnect when the peer comes back online
|
||||
ConvaiLogger.Info($"🔄 Will continue sending heartbeats to {_peerIP}", ConvaiLogger.LogCategory.Character);
|
||||
// DO NOTHING - keep the connection state as Connected
|
||||
// Once connected at the start of the session, stay connected forever
|
||||
// Senders will continue to send to the stored IP
|
||||
}
|
||||
|
||||
private void SetConnectionState(ConnectionState newState)
|
||||
|
||||
BIN
Unity-Master/ProjectSettings/EditorBuildSettings.asset
(Stored with Git LFS)
BIN
Unity-Master/ProjectSettings/EditorBuildSettings.asset
(Stored with Git LFS)
Binary file not shown.
BIN
Unity-Master/ProjectSettings/ProjectSettings.asset
(Stored with Git LFS)
BIN
Unity-Master/ProjectSettings/ProjectSettings.asset
(Stored with Git LFS)
Binary file not shown.
Reference in New Issue
Block a user