users@websocket-spec.java.net

[jsr356-users] Re: RemoteEndpoint.getSendWriter() and getSendStream() sugguestions

From: Mark Thomas <markt_at_apache.org>
Date: Fri, 08 Feb 2013 12:44:12 +0000

On 08/02/2013 08:28, Norman Maurer wrote:
> Hi all,
>
> while working on my implementation of RemoteEndpoint I encountered a
> few issues. Well not really issues but a few things I would like to
> start a discussion about..
>
> Most important I wonder why RemoteEndpoint.getSendWriter() and
> getSendStream() don't take a payload size as argument. The problem
> at the moment is that the whole message needs to be buffered till the
> close() method was called. This is because the length of the message
> (websocket frame) is encoded in the "header" of the message (at least
> for all the versions > 00) .

No. The length of the fragment is set in the header but a message can
consist of many fragments and the total message size is never
communicated directly. (It can be derived by adding the sizes of each of
the fragments).

> So without knowing the size up-front it
> is not possible to start the transfer as it needs to get calculated
> from the whole message. I think this is very inefficient and can
> result in a lot of memory usage on the implementation, as the user
> may send a very big message. If the method signature would be changed
> to take the payload size it would be possible to directly start the
> transfer of the message and send data in "chunks" over the wire.
>
> Feedback welcome…

The way I implemented this in Tomcat was to use a small local buffer and
write to that. When that fills up send a message fragment (without the
FIN bit set) and when the Stream / Writer is closed flush whatever is
left in the buffer with the FIN bit set.

I have this sat on my local drive waiting for some test cases to be
written before I commit it. I should be in a position to do that later
today.

Mark