Browse Source

XHRPolling "write once" optimization.

master
Nikita 12 years ago
parent
commit
89ed6103d7
  1. 5
      src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java
  2. 10
      src/main/java/com/corundumstudio/socketio/transport/XHRPollingClient.java

5
src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java

@ -54,12 +54,11 @@ import com.corundumstudio.socketio.messages.XHRErrorMessage;
import com.corundumstudio.socketio.messages.XHROutMessage;
import com.corundumstudio.socketio.messages.XHRSendPacketsMessage;
import com.corundumstudio.socketio.parser.Encoder;
import com.corundumstudio.socketio.transport.XHRPollingClient;
@Sharable
public class EncoderHandler extends ChannelOutboundHandlerAdapter {
private static final AttributeKey<Boolean> WRITE_ONCE = AttributeKey.<Boolean>valueOf("writeOnce");
private final Logger log = LoggerFactory.getLogger(getClass());
private final Encoder encoder;
@ -70,7 +69,7 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter {
private void write(XHRSendPacketsMessage msg, ChannelHandlerContext ctx, ByteBuf out) throws IOException {
Channel channel = ctx.channel();
Attribute<Boolean> attr = channel.attr(WRITE_ONCE);
Attribute<Boolean> attr = channel.attr(XHRPollingClient.WRITE_ONCE);
if (!channel.isActive()
|| msg.getPacketQueue().isEmpty()

10
src/main/java/com/corundumstudio/socketio/transport/XHRPollingClient.java

@ -17,6 +17,7 @@ package com.corundumstudio.socketio.transport;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.util.AttributeKey;
import java.util.Queue;
import java.util.UUID;
@ -31,6 +32,8 @@ import com.corundumstudio.socketio.parser.Packet;
public class XHRPollingClient extends MainBaseClient {
public static final AttributeKey<Boolean> WRITE_ONCE = AttributeKey.<Boolean>valueOf("writeOnce");
private final Queue<Packet> packetQueue = new ConcurrentLinkedQueue<Packet>();
private String origin;
@ -48,9 +51,12 @@ public class XHRPollingClient extends MainBaseClient {
return origin;
}
public ChannelFuture send(final Packet packet) {
public ChannelFuture send(Packet packet) {
packetQueue.add(packet);
return getChannel().write(new XHRSendPacketsMessage(getSessionId(), origin, packetQueue));
if (getChannel().attr(WRITE_ONCE).get() == null) {
return getChannel().write(new XHRSendPacketsMessage(getSessionId(), origin, packetQueue));
}
return getChannel().newSucceededFuture();
}
}
Loading…
Cancel
Save