开发者

TCP Socket frames 'queuing' up on Windows. How can I force each message in it's own frame?

开发者 https://www.devze.com 2023-04-04 03:44 出处:网络
I use Actionscript 3 TCP sockets to c开发者_如何转开发onnect with Javascript websockets. Sending data is primarily from the websocket to the AS socket.

I use Actionscript 3 TCP sockets to c开发者_如何转开发onnect with Javascript websockets. Sending data is primarily from the websocket to the AS socket.

On Mac OS X, no problem. On Windows however, successive TCP messages seem to queue up somewhere. This causes the ProgressEvent.SOCKET_DATA event to fire with quite a large time interval, which creates noticeable lag.

I used Wireshark to monitor the TCP packets on both OS X and Windows. The difference I see is that on OS X each message comes in it's own packet, while on Windows successive messages are 'concatenated' into one packet.

Is this just the way the socket is implemented, or is there any way I can improve on this?

EDIT 1: I found this post on actionscript.org which outlines the same problem

EDIT 2: I found a way to go around the problem. I pad every message with dummy text to increase the frame size. This causes the TCP stack to send every message in it's own frame instead if queuing them. This works, even though it's really, really ugly...


This is the code in the SOCKET_DATA event.

while(this.socket.bytesAvailable) {
    var byte:uint = this.socket.readUnsignedByte();
    if(byte == 0x00) {
        trace("Start byte found. - " + new Date().time);
        this.incomingMessageBytes = new ByteArray();
    } else if (byte == 0xFF) {
        trace("End byte found. Dispatching. - " + new Date().time);
        this.incomingMessageBytes.position = 0;
        var msg:String = incomingMessageBytes.readUTFBytes(incomingMessageBytes.bytesAvailable);
        var decodedMessage:Object = JSON.decode(msg, false);
        var message = new Message(decodedMessage.clientId, decodedMessage.command, decodedMessage.data);

        this.dispatchEvent(new MessageReceivedEvent(MessageReceivedEvent.RECEIVED_MESSAGE, message));
    } else {
        //trace("Appending.");
        this.incomingMessageBytes.writeByte(byte);
    }
}


It sounds like you might be seeing the effects of Nagle's algorithm. I don't know if there is a way to disable Nagle's algorithm (aka setting the TCP_NODELAY flag) under ActionScript, but if there is, you might try doing that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号