Bug 22704 - ClientWebSocket cannot handle reading subsets of the payload
Summary: ClientWebSocket cannot handle reading subsets of the payload
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: All All
: High major
Target Milestone: Untriaged
Assignee: Martin Baulig
Depends on:
Reported: 2014-09-05 14:00 UTC by Matthew Leibowitz
Modified: 2014-09-10 11:10 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Matthew Leibowitz 2014-09-05 14:00:53 UTC
If there is a message in the socket payload, and a request for only a subset is made, the result is inconsistent.

Here is a test case:

public void EchoWithSmallerBufferTest ()
	const string Payload = "This is a websocket test";
	const int BufferSize = 8;
	const int ExpectedMessages = 3;

	Assert.AreEqual (WebSocketState.None, socket.State);
	socket.ConnectAsync (new Uri (EchoServerUrl), CancellationToken.None).Wait ();
	Assert.AreEqual (WebSocketState.Open, socket.State);

	var sendBuffer = Encoding.ASCII.GetBytes (Payload);
	Assert.IsTrue (socket.SendAsync (new ArraySegment<byte> (sendBuffer), WebSocketMessageType.Text, true, CancellationToken.None).Wait (5000));

	byte[] receiveFullBuffer = new byte[Payload.Length];
	int iteration = 0;
	while (true) {
		var receiveBuffer = new byte[BufferSize];
		var resp = socket.ReceiveAsync (new ArraySegment<byte> (receiveBuffer), CancellationToken.None).Result;

		Assert.AreEqual (BufferSize, resp.Count);
		Assert.AreEqual (WebSocketMessageType.Text, resp.MessageType);

		Array.Copy (receiveBuffer, 0, receiveFullBuffer, iteration * BufferSize, BufferSize);

		Assert.AreEqual (resp.EndOfMessage, iteration == ExpectedMessages);

		if (resp.EndOfMessage)
	Assert.AreEqual (Payload, Encoding.ASCII.GetString (receiveFullBuffer, 0, receiveFullBuffer.Length));
	Assert.AreEqual (ExpectedMessages, iteration);

	Assert.IsTrue (socket.CloseAsync (WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).Wait (5000));
	Assert.AreEqual (WebSocketState.Closed, socket.State);

Using Mono on Mac (and Android), the loop only executes once:
 - Message size: 24
 - Type: Text
 - End OF Message: True
 - Data: "This is "

Using .NET on Windows, the loop executes 3 times:
 - First iteration
 -- Message size: 8
 -- Type: Text
 -- End OF Message: False
 -- Data: "This is "
 - Second iteration
 -- Message size: 8
 -- Type: Text
 -- End OF Message: False
 -- Data: "a websoc"
 - Third iteration
 -- Message size: 8
 -- Type: Text
 -- End OF Message: True
 -- Data: "ket test"
Comment 3 Martin Baulig 2014-09-10 11:10:23 UTC
Fixed; mono/master bbbcc73.