no00ob / LCSoundTool

Simple Lethal Company sound tool and debugger.
MIT License
11 stars 5 forks source link

Getting Overflow Exception when sending networked audio clips #10

Closed TimShaw1 closed 7 months ago

TimShaw1 commented 7 months ago

When sending audio clips (they are <10s in length), I get the following errors:

[Error  : Unity Log] OverflowException: Writing past the end of the buffer
Stack trace:
LCSoundTool.Networking.NetworkHandler.SendAudioClipServerRpc (System.String clipName, System.Byte[] audioData) (at <b8e1763cc71843d291f26feec0c04dd1>:IL_015F)
LCSoundTool.SoundTool.SendNetworkedAudioClip (UnityEngine.AudioClip audioClip) (at <b8e1763cc71843d291f26feec0c04dd1>:IL_0131)
Wendigos.Plugin+StartOfRoundConnectPatch.Postfix () (at <c2731a1c67ab4d32bca12a687b73d653>:IL_0035)
(wrapper dynamic-method) StartOfRound.DMD<StartOfRound::PlayerLoadedClientRpc>(StartOfRound,ulong)
StartOfRound.__rpc_handler_462348217 (Unity.Netcode.NetworkBehaviour target, Unity.Netcode.FastBufferReader reader, Unity.Netcode.__RpcParams rpcParams) (at <af9b1eec498a45aebd42601d6ab85015>:IL_003B)
Unity.Netcode.RpcMessageHelpers.Handle (Unity.Netcode.NetworkContext& context, Unity.Netcode.RpcMetadata& metadata, Unity.Netcode.FastBufferReader& payload, Unity.Netcode.__RpcParams& rpcParams) (at <895801699cfc4b4ab52267f31e2a4998>:IL_004E)
Rethrow as Exception: Unhandled RPC exception!
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.RpcMessageHelpers:Handle(NetworkContext&, RpcMetadata&, FastBufferReader&, __RpcParams&)
Unity.Netcode.ClientRpcMessage:Handle(NetworkContext&)
Unity.Netcode.NetworkBehaviour:__endSendClientRpc(FastBufferWriter&, UInt32, ClientRpcParams, RpcDelivery)
StartOfRound:DMD<StartOfRound::PlayerLoadedClientRpc>(StartOfRound, UInt64)
StartOfRound:PlayerLoadedServerRpc(UInt64)
StartOfRound:__rpc_handler_4249638645(NetworkBehaviour, FastBufferReader, __RpcParams)
Unity.Netcode.RpcMessageHelpers:Handle(NetworkContext&, RpcMetadata&, FastBufferReader&, __RpcParams&)
Unity.Netcode.ServerRpcMessage:Handle(NetworkContext&)
Unity.Netcode.NetworkBehaviour:__endSendServerRpc(FastBufferWriter&, UInt32, ServerRpcParams, RpcDelivery)
StartOfRound:PlayerLoadedServerRpc(UInt64)
StartOfRound:SceneManager_OnLoadComplete1(UInt64, String, LoadSceneMode)
Unity.Netcode.NetworkSceneManager:OnServerLoadedScene(UInt32, Scene)
Unity.Netcode.NetworkSceneManager:OnSceneLoaded(UInt32)
Unity.Netcode.SceneEventProgress:<SetAsyncOperation>b__37_0(AsyncOperation)
UnityEngine.AsyncOperation:InvokeCompletionEvent()

[Error  : Unity Log] OverflowException: Writing past the end of the buffer
Stack trace:
LCSoundTool.Networking.NetworkHandler.SendAudioClipServerRpc (System.String clipName, System.Byte[] audioData) (at <b8e1763cc71843d291f26feec0c04dd1>:IL_015F)
LCSoundTool.SoundTool.SendNetworkedAudioClip (UnityEngine.AudioClip audioClip) (at <b8e1763cc71843d291f26feec0c04dd1>:IL_0131)
Wendigos.Plugin+StartOfRoundConnectPatch.Postfix () (at <c2731a1c67ab4d32bca12a687b73d653>:IL_0035)
(wrapper dynamic-method) StartOfRound.DMD<StartOfRound::PlayerLoadedClientRpc>(StartOfRound,ulong)
StartOfRound.PlayerLoadedServerRpc (System.UInt64 clientId) (at <af9b1eec498a45aebd42601d6ab85015>:IL_00D4)
StartOfRound.__rpc_handler_4249638645 (Unity.Netcode.NetworkBehaviour target, Unity.Netcode.FastBufferReader reader, Unity.Netcode.__RpcParams rpcParams) (at <af9b1eec498a45aebd42601d6ab85015>:IL_003B)
Unity.Netcode.RpcMessageHelpers.Handle (Unity.Netcode.NetworkContext& context, Unity.Netcode.RpcMetadata& metadata, Unity.Netcode.FastBufferReader& payload, Unity.Netcode.__RpcParams& rpcParams) (at <895801699cfc4b4ab52267f31e2a4998>:IL_004E)
Rethrow as Exception: Unhandled RPC exception!
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.RpcMessageHelpers:Handle(NetworkContext&, RpcMetadata&, FastBufferReader&, __RpcParams&)
Unity.Netcode.ServerRpcMessage:Handle(NetworkContext&)
Unity.Netcode.NetworkBehaviour:__endSendServerRpc(FastBufferWriter&, UInt32, ServerRpcParams, RpcDelivery)
StartOfRound:PlayerLoadedServerRpc(UInt64)
StartOfRound:SceneManager_OnLoadComplete1(UInt64, String, LoadSceneMode)
Unity.Netcode.NetworkSceneManager:OnServerLoadedScene(UInt32, Scene)
Unity.Netcode.NetworkSceneManager:OnSceneLoaded(UInt32)
Unity.Netcode.SceneEventProgress:<SetAsyncOperation>b__37_0(AsyncOperation)
UnityEngine.AsyncOperation:InvokeCompletionEvent()

The clips are WAV files loaded from the disk using UnityWebRequestMultimedia.GetAudioClip().

no00ob commented 7 months ago

Hey, not sure what is causing this but I have a rework of the networking planned for the next update so hopefully I can get this fixed.

TimShaw1 commented 7 months ago

To add to this, the first audioclip I try to load works fine. Every subsequent audioclip I try to load throws this error.

no00ob commented 7 months ago

Should be fixed in the 1.5.1 update now. Let me know if further problems appear!

TimShaw1 commented 7 months ago

Still getting this issue on 1.5.1

I am able to send my audio clips using my own custom networking class when I increase the buffer size and use ReliableFragmentedSequenced. This works over LAN but I am currently dealing with Steam saying my messages are too large.

private void SendMessage(byte[] audioClipFragment)
{
    var messageContent = audioClipFragment;
    //WriteToConsole("Writing message...");

    // Steam has max size of 512kb (C)
    var writer = new FastBufferWriter(messageContent.Length, Unity.Collections.Allocator.Temp, 512000);
    //WriteToConsole("Wrote Message");
    var customMessagingManager = NetworkManager.Singleton.CustomMessagingManager;

    using (writer)
    {
        //WriteToConsole($"Writing {messageContent.Length} bytes of data...");
        // Issue is here
        writer.WriteValueSafe(messageContent);
        //WriteToConsole("Wrote data");
        if (NetworkManager.Singleton.IsServer)
        {
            // This is a server-only method that will broadcast the named message.
            // Caution: Invoking this method on a client will throw an exception!
            //WriteToConsole("Sending Message...");
            customMessagingManager.SendNamedMessageToAll(MessageName, writer, NetworkDelivery.ReliableFragmentedSequenced);
            //WriteToConsole("Sent Message");
        }
        else
        {
            // This is a client or server method that sends a named message to one target destination
            // (client to server or server to client)
            //WriteToConsole("Sending Message...");
            customMessagingManager.SendNamedMessage(MessageName, NetworkManager.ServerClientId, writer, NetworkDelivery.ReliableFragmentedSequenced);
            //WriteToConsole("Sent Message");
        }
    }
}