medokin / soundpad-connector

.NET Connector for remote controlling Soundpad
https://medokin.github.io/soundpad-connector/
MIT License
25 stars 5 forks source link

GetSoundlist fails if it has too many entries #13

Open feng51 opened 9 months ago

feng51 commented 9 months ago

The method Send in Soundpad.cs only reads _pipe.OutBufferSize. If a client has many sounds and GetSoundlist is called, then the response surpasses the fixed buffer size of 262144 bytes resulting in an incomplete xml response and parsing fails.

The method should read from the pipe until the read bytes are below that buffer size.

So instead of this code part:

var responseBuffer = new byte[_pipe.OutBufferSize];
await _pipe.ReadAsync(responseBuffer, 0, responseBuffer.Length);

it should be be something like that:

var tmpBuffer = new byte[_pipe.InBufferSize]; // use InBufferSize as we are reading, although they are of same size
var responseBuffer = new byte[0];

int bytesRead = 0;
do
{
    bytesRead = _pipe.Read(tmpBuffer, 0, tmpBuffer.Length);
    int previousLength = responseBuffer.Length;
    Array.Resize(ref responseBuffer, previousLength + bytesRead);
    Array.Copy(tmpBuffer, 0, responseBuffer, previousLength, bytesRead);
} while (bytesRead == _pipe.InBufferSize);

There's probably a more efficient way than resizing the response array, but I'm not too familiar with C#.