Browse Source

Add support for BINARY_ACK(6, true) packet, which is used when the arguments for an ACK function contain binary data; encodes packet in the BINARY_EVENT style documented in https://github.com/socketio/socket.io-protocol

master
Sergey Bushik 8 years ago
parent
commit
a748461389
  1. 3
      src/main/java/com/corundumstudio/socketio/handler/PacketListener.java
  2. 33
      src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java
  3. 3
      src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java
  4. 2
      src/main/java/com/corundumstudio/socketio/protocol/PacketType.java

3
src/main/java/com/corundumstudio/socketio/handler/PacketListener.java

@ -89,7 +89,8 @@ public class PacketListener {
client.getBaseClient().send(packet, transport);
}
if (packet.getSubType() == PacketType.ACK) {
if (packet.getSubType() == PacketType.ACK
|| packet.getSubType() == PacketType.BINARY_ACK) {
ackManager.onAck(client, packet);
}

33
src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java

@ -15,6 +15,9 @@
*/
package com.corundumstudio.socketio.protocol;
import com.corundumstudio.socketio.AckCallback;
import com.corundumstudio.socketio.ack.AckManager;
import com.corundumstudio.socketio.handler.ClientHead;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.Unpooled;
@ -26,10 +29,6 @@ import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.UUID;
import com.corundumstudio.socketio.AckCallback;
import com.corundumstudio.socketio.ack.AckManager;
import com.corundumstudio.socketio.handler.ClientHead;
public class PacketDecoder {
private final UTF8CharsScanner utf8scanner = new UTF8CharsScanner();
@ -190,7 +189,8 @@ public class PacketDecoder {
int attachmentsDividerIndex = frame.bytesBefore(endIndex, (byte)'-');
boolean hasAttachments = attachmentsDividerIndex != -1;
if (hasAttachments && PacketType.BINARY_EVENT.equals(innerType)) {
if (hasAttachments && (PacketType.BINARY_EVENT.equals(innerType)
|| PacketType.BINARY_ACK.equals(innerType))) {
int attachments = (int) readLong(frame, attachmentsDividerIndex);
packet.initAttachments(attachments);
frame.readerIndex(frame.readerIndex() + 1);
@ -286,7 +286,14 @@ public class PacketDecoder {
packet.setNsp(readString(frame));
}
if (packet.getSubType() == PacketType.ACK) {
if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) {
packet.setDataSource(Unpooled.copiedBuffer(frame));
frame.readerIndex(frame.readableBytes());
head.setLastBinaryPacket(packet);
}
if (packet.getSubType() == PacketType.ACK
|| packet.getSubType() == PacketType.BINARY_ACK) {
ByteBufInputStream in = new ByteBufInputStream(frame);
AckCallback<?> callback = ackManager.getCallback(head.getSessionId(), packet.getAckId());
AckArgs args = jsonSupport.readAckArgs(in, callback);
@ -295,16 +302,10 @@ public class PacketDecoder {
if (packet.getSubType() == PacketType.EVENT
|| packet.getSubType() == PacketType.BINARY_EVENT) {
if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) {
packet.setDataSource(Unpooled.copiedBuffer(frame));
frame.readerIndex(frame.readableBytes());
head.setLastBinaryPacket(packet);
} else {
ByteBufInputStream in = new ByteBufInputStream(frame);
Event event = jsonSupport.readValue(packet.getNsp(), in, Event.class);
packet.setName(event.getName());
packet.setData(event.getArgs());
}
ByteBufInputStream in = new ByteBufInputStream(frame);
Event event = jsonSupport.readValue(packet.getNsp(), in, Event.class);
packet.setName(event.getName());
packet.setData(event.getArgs());
}
}
}

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

@ -280,7 +280,8 @@ public class PacketEncoder {
for (byte[] array : jsonSupport.getArrays()) {
packet.addAttachment(Unpooled.wrappedBuffer(array));
}
packet.setSubType(PacketType.BINARY_EVENT);
packet.setSubType(packet.getSubType() == PacketType.ACK
? PacketType.BINARY_ACK : PacketType.BINARY_EVENT);
}
}

2
src/main/java/com/corundumstudio/socketio/protocol/PacketType.java

@ -20,7 +20,7 @@ public enum PacketType {
OPEN(0), CLOSE(1), PING(2), PONG(3), MESSAGE(4), UPGRADE(5), NOOP(6),
CONNECT(0, true), DISCONNECT(1, true), EVENT(2, true), ACK(3, true), ERROR(4, true), BINARY_EVENT(5, true);
CONNECT(0, true), DISCONNECT(1, true), EVENT(2, true), ACK(3, true), ERROR(4, true), BINARY_EVENT(5, true), BINARY_ACK(6, true);
public static final PacketType[] VALUES = values();
private final int value;

Loading…
Cancel
Save