|
@ -27,7 +27,6 @@ import java.io.IOException; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Queue; |
|
|
import java.util.Queue; |
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
|
|
import com.corundumstudio.socketio.Configuration; |
|
|
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 { |
|
|
public void encodeJsonP(Integer jsonpIndex, Queue<Packet> packets, ByteBuf out, ByteBufAllocator allocator, int limit) throws IOException { |
|
|
boolean jsonpMode = jsonpIndex != null; |
|
|
boolean jsonpMode = jsonpIndex != null; |
|
|
|
|
|
|
|
|
ByteBuf buf = out; |
|
|
|
|
|
if (jsonpMode) { |
|
|
|
|
|
buf = allocateBuffer(allocator); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
ByteBuf buf = allocateBuffer(allocator); |
|
|
|
|
|
|
|
|
int i = 0; |
|
|
int i = 0; |
|
|
while (true) { |
|
|
while (true) { |
|
@ -95,17 +91,31 @@ public class PacketEncoder { |
|
|
out.writeBytes(JSONP_HEAD); |
|
|
out.writeBytes(JSONP_HEAD); |
|
|
out.writeBytes(toChars(jsonpIndex)); |
|
|
out.writeBytes(toChars(jsonpIndex)); |
|
|
out.writeBytes(JSONP_START); |
|
|
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); |
|
|
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 { |
|
|
public void encodePackets(Queue<Packet> packets, ByteBuf buffer, ByteBufAllocator allocator, int limit) throws IOException { |
|
|
int i = 0; |
|
|
int i = 0; |
|
|
while (true) { |
|
|
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) { |
|
|
public static int find(ByteBuf buffer, ByteBuf searchValue) { |
|
|
for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { |
|
|
for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { |
|
|
if (isValueFound(buffer, i, searchValue)) { |
|
|
if (isValueFound(buffer, i, searchValue)) { |
|
|