Browse Source

Polling encoding fixed. #207

master
Nikita 10 years ago
parent
commit
c965ab9c9c
  1. 6
      src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java
  2. 38
      src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java

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

@ -251,7 +251,11 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter {
if (b64 != null && b64) {
Integer jsonpIndex = ctx.channel().attr(EncoderHandler.JSONP_INDEX).get();
encoder.encodeJsonP(jsonpIndex, queue, out, ctx.alloc(), 50);
sendMessage(msg, channel, out, "application/javascript");
String type = "application/javascript";
if (jsonpIndex == null) {
type = "text/plain";
}
sendMessage(msg, channel, out, type);
} else {
encoder.encodePackets(queue, out, ctx.alloc(), 50);
sendMessage(msg, channel, out, "application/octet-stream");

38
src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java

@ -27,7 +27,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Pattern;
import com.corundumstudio.socketio.Configuration;
@ -58,10 +57,7 @@ public class PacketEncoder {
public void encodeJsonP(Integer jsonpIndex, Queue<Packet> packets, ByteBuf out, ByteBufAllocator allocator, int limit) throws IOException {
boolean jsonpMode = jsonpIndex != null;
ByteBuf buf = out;
if (jsonpMode) {
buf = allocateBuffer(allocator);
}
ByteBuf buf = allocateBuffer(allocator);
int i = 0;
while (true) {
@ -95,17 +91,31 @@ public class PacketEncoder {
out.writeBytes(JSONP_HEAD);
out.writeBytes(toChars(jsonpIndex));
out.writeBytes(JSONP_START);
}
String packet = buf.toString(CharsetUtil.ISO_8859_1);
processUtf8(buf, out, jsonpMode);
buf.release();
// TODO optimize
packet = packet.replace("\\", "\\\\").replace("'", "\\'");
out.writeBytes(packet.getBytes(CharsetUtil.UTF_8));
if (jsonpMode) {
out.writeBytes(JSONP_END);
}
}
private void processUtf8(ByteBuf in, ByteBuf out, boolean jsonpMode) {
while (in.isReadable()) {
short value = (short) (in.readByte() & 0xFF);
if (value >>> 7 == 0) {
if (jsonpMode && (value == '\\' || value == '\'')) {
out.writeByte('\\');
}
out.writeByte(value);
} else {
out.writeByte(((value >>> 6) | 0xC0));
out.writeByte(((value & 0x3F) | 0x80));
}
}
}
public void encodePackets(Queue<Packet> packets, ByteBuf buffer, ByteBufAllocator allocator, int limit) throws IOException {
int i = 0;
while (true) {
@ -313,16 +323,6 @@ public class PacketEncoder {
}
}
private int count(ByteBuf buffer, ByteBuf searchValue) {
int count = 0;
for (int i = 0; i < buffer.readableBytes(); i++) {
if (isValueFound(buffer, i, searchValue)) {
count++;
}
}
return count;
}
public static int find(ByteBuf buffer, ByteBuf searchValue) {
for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) {
if (isValueFound(buffer, i, searchValue)) {

Loading…
Cancel
Save