From a748461389d2e074126d99789574588de10e3ab9 Mon Sep 17 00:00:00 2001 From: Sergey Bushik Date: Wed, 19 Jul 2017 19:39:30 +0300 Subject: [PATCH] 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 --- .../socketio/handler/PacketListener.java | 3 +- .../socketio/protocol/PacketDecoder.java | 33 ++++++++++--------- .../socketio/protocol/PacketEncoder.java | 3 +- .../socketio/protocol/PacketType.java | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/corundumstudio/socketio/handler/PacketListener.java b/src/main/java/com/corundumstudio/socketio/handler/PacketListener.java index c3583ff..f442f81 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/PacketListener.java +++ b/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); } diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java index f6a0f89..4473bff 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java +++ b/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()); } } } diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java index fe6b602..eb51eef 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java +++ b/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); } } diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketType.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketType.java index 06d0289..e3295bd 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketType.java +++ b/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;