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++;
|
checkCount++;
|
||||||
|
|
||||||
// Log periodically to show we're still monitoring
|
// SIMPLIFIED: Check EVERY frame if there's a new clip, don't wait for events
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (audioSource?.clip != null && audioSource.clip != lastClip)
|
if (audioSource?.clip != null && audioSource.clip != lastClip)
|
||||||
{
|
{
|
||||||
// New clip detected!
|
// New clip detected!
|
||||||
@ -312,6 +307,14 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
// Get the transcript from the most recent available transcript
|
// Get the transcript from the most recent available transcript
|
||||||
string transcript = GetRecentTranscript();
|
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
|
// Send this clip
|
||||||
ConvaiLogger.Info($"🔊 TRANSMITTING CLIP to {targetIP}:{targetPort}", ConvaiLogger.LogCategory.Character);
|
ConvaiLogger.Info($"🔊 TRANSMITTING CLIP to {targetIP}:{targetPort}", ConvaiLogger.LogCategory.Character);
|
||||||
_ = TransmitAudioClip(lastClip, transcript);
|
_ = 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);
|
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
|
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
|
// Use the current speech sequence for this entire clip
|
||||||
int clipSequence = _speechSequence;
|
int clipSequence = _speechSequence;
|
||||||
|
|
||||||
|
|||||||
@ -115,15 +115,8 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
// Check for peer timeout
|
// No timeout checks - once connected, stay connected until restart
|
||||||
if (_connectionState == ConnectionState.Connected)
|
// Removed: peer timeout monitoring
|
||||||
{
|
|
||||||
TimeSpan timeSinceLastPacket = DateTime.UtcNow - _lastPeerPacketTime;
|
|
||||||
if (timeSinceLastPacket.TotalSeconds > peerTimeoutSeconds)
|
|
||||||
{
|
|
||||||
HandlePeerLost();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartDiscovery()
|
private void StartDiscovery()
|
||||||
@ -150,8 +143,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
// Start broadcasting discovery requests
|
// Start broadcasting discovery requests
|
||||||
_ = BroadcastDiscoveryRequests(_cancellationTokenSource.Token);
|
_ = BroadcastDiscoveryRequests(_cancellationTokenSource.Token);
|
||||||
|
|
||||||
// Start heartbeat when connected
|
// NO HEARTBEATS - once connected, stay connected
|
||||||
_ = SendHeartbeats(_cancellationTokenSource.Token);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -376,23 +368,8 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PACKET_TYPE_HEARTBEAT:
|
case PACKET_TYPE_HEARTBEAT:
|
||||||
// Heartbeat keeps connection alive
|
// Ignore heartbeats - no reconnection logic
|
||||||
if (_peerIP == senderIP)
|
// Once connected, stay connected until restart
|
||||||
{
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -420,18 +397,12 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
|
|
||||||
private void HandlePeerLost()
|
private void HandlePeerLost()
|
||||||
{
|
{
|
||||||
ConvaiLogger.Warn($"⚠️ Peer connection lost (Player {_peerPlayerID} at {_peerIP}) - will keep trying to reconnect", ConvaiLogger.LogCategory.Character);
|
ConvaiLogger.Warn($"⚠️ Peer connection lost (Player {_peerPlayerID} at {_peerIP}) - but ignoring, keeping connection alive", ConvaiLogger.LogCategory.Character);
|
||||||
LogEvent($"⚠️ Peer connection lost (Player {_peerPlayerID}) - reconnecting...");
|
LogEvent($"⚠️ Peer connection lost (Player {_peerPlayerID}) - IGNORING (stay connected until restart)");
|
||||||
|
|
||||||
SetConnectionState(ConnectionState.Lost);
|
// DO NOTHING - keep the connection state as Connected
|
||||||
|
// Once connected at the start of the session, stay connected forever
|
||||||
// Notify listeners
|
// Senders will continue to send to the stored IP
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetConnectionState(ConnectionState newState)
|
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