Skip to content

Commit

Permalink
Polling encoding fixed. #207
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Feb 9, 2015
1 parent d977d5e commit c965ab9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ private void handleHTTP(OutPacketMessage msg, ChannelHandlerContext ctx) throws
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Pattern;

import com.corundumstudio.socketio.Configuration;

Expand Down Expand Up @@ -58,10 +57,7 @@ public ByteBuf allocateBuffer(ByteBufAllocator allocator) {
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) {
Expand Down Expand Up @@ -95,17 +91,31 @@ public void encodeJsonP(Integer jsonpIndex, Queue<Packet> packets, ByteBuf out,
out.writeBytes(JSONP_HEAD);
out.writeBytes(toChars(jsonpIndex));
out.writeBytes(JSONP_START);
}

String packet = buf.toString(CharsetUtil.ISO_8859_1);
buf.release();
// TODO optimize
packet = packet.replace("\\", "\\\\").replace("'", "\\'");
out.writeBytes(packet.getBytes(CharsetUtil.UTF_8));
processUtf8(buf, out, jsonpMode);
buf.release();

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) {
Expand Down Expand Up @@ -313,16 +323,6 @@ public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocat
}
}

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)) {
Expand Down

0 comments on commit c965ab9

Please sign in to comment.