Skip to content

Commit 115cf42

Browse files
committed
Reduce the impact
1 parent c1119e2 commit 115cf42

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

src/main/java/com/corundumstudio/socketio/BroadcastOperations.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@
2121

2222
/**
2323
* broadcast interface
24-
*
2524
*/
2625
public interface BroadcastOperations extends ClientOperations {
2726

2827
Collection<SocketIOClient> getClients();
2928

29+
/**
30+
* {@link Packet#attachments} needs to be filled when sending byte[].
31+
* Using {@link io.netty.buffer.Unpooled#wrappedBuffer(byte[])} to
32+
* fill byte[] into {@link Packet#attachments} is the recommended way.
33+
* Before using {@link Packet#addAttachment(io.netty.buffer.ByteBuf)},
34+
* be sure to initialize the number of attachments with
35+
* {@link Packet#initAttachments(int)})}
36+
*
37+
* @param packet
38+
* @param ackCallback
39+
* @param <T>
40+
*/
3041
<T> void send(Packet packet, BroadcastAckCallback<T> ackCallback);
3142

3243
void sendEvent(String name, SocketIOClient excludedClient, Object... data);

src/main/java/com/corundumstudio/socketio/ClientOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface ClientOperations {
2727
* Send custom packet.
2828
* But {@link ClientOperations#sendEvent} method
2929
* usage is enough for most cases.
30+
* If the Packet is sent by BroadcastOperations,
3031
* {@link Packet#attachments} needs to be filled when sending byte[].
3132
* Using {@link io.netty.buffer.Unpooled#wrappedBuffer(byte[])} to
3233
* fill byte[] into {@link Packet#attachments} is the recommended way.

src/main/java/com/corundumstudio/socketio/SingleRoomBroadcastOperations.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ public void disconnect() {
8585
}
8686

8787
@Override
88-
public void sendEvent(String name, SocketIOClient excludedClient, Object... data) {
88+
public void sendEvent(String name, SocketIOClient excludedClient, @NonNull Object... data) {
8989
Packet packet = new Packet(PacketType.MESSAGE, EngineIOVersion.UNKNOWN);
9090
packet.setSubType(PacketType.EVENT);
9191
packet.setName(name);
9292
packet.setData(Arrays.asList(data));
9393

94+
// handle byte[] data
95+
handleBytes(packet, data);
96+
9497
for (SocketIOClient client : clients) {
9598
packet.setEngineIOVersion(client.getEngineIOVersion());
9699
if (client.getSessionId().equals(excludedClient.getSessionId())) {
@@ -109,6 +112,12 @@ public void sendEvent(String name, @NonNull Object... data) {
109112
packet.setData(Arrays.asList(data));
110113

111114
// handle byte[] data
115+
handleBytes(packet, data);
116+
117+
send(packet);
118+
}
119+
120+
private static void handleBytes(Packet packet, Object[] data) {
112121
List<byte[]> bytes = Arrays.stream(data)
113122
.filter(o -> o instanceof byte[])
114123
.map(b -> (byte[]) b)
@@ -119,8 +128,6 @@ public void sendEvent(String name, @NonNull Object... data) {
119128
packet.initAttachments(bytes.size());
120129
bytes.stream().peek(b -> packet.addAttachment(Unpooled.wrappedBuffer(b)));
121130
}
122-
123-
send(packet);
124131
}
125132

126133
@Override

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.corundumstudio.socketio.protocol;
1717

18-
import com.corundumstudio.socketio.handler.ClientHead;
18+
import com.corundumstudio.socketio.Configuration;
1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.ByteBufAllocator;
2121
import io.netty.buffer.ByteBufOutputStream;
@@ -29,8 +29,6 @@
2929
import java.util.List;
3030
import java.util.Queue;
3131

32-
import com.corundumstudio.socketio.Configuration;
33-
3432
public class PacketEncoder {
3533

3634
private static final byte[] BINARY_HEADER = "b4".getBytes(CharsetUtil.UTF_8);
@@ -254,18 +252,20 @@ public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocat
254252

255253
ByteBuf encBuf = null;
256254

257-
if (packet.getSubType() == PacketType.ERROR) {
255+
PacketType subType = packet.getSubType();
256+
if (subType == PacketType.ERROR) {
258257
encBuf = allocateBuffer(allocator);
259258

260259
ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
261260
jsonSupport.writeValue(out, packet.getData());
262261
}
263262

264-
if (packet.getSubType() == PacketType.EVENT
265-
|| packet.getSubType() == PacketType.ACK) {
263+
PacketType tmpSubType = subType;
264+
if (subType == PacketType.EVENT
265+
|| subType == PacketType.ACK) {
266266

267267
List<Object> values = new ArrayList<Object>();
268-
if (packet.getSubType() == PacketType.EVENT) {
268+
if (subType == PacketType.EVENT) {
269269
values.add(packet.getName());
270270
}
271271

@@ -277,21 +277,30 @@ public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocat
277277
jsonSupport.writeValue(out, values);
278278

279279
if (!jsonSupport.getArrays().isEmpty()) {
280-
packet.setSubType(packet.getSubType() == PacketType.ACK
280+
// If the Packet is sent by BroadcastOperations,
281+
// there is a problem of concurrent initialization for the same Packet.
282+
// Please initAttachment when creating the Packet to avoid this problem.
283+
if (!packet.hasAttachments()) {
284+
packet.initAttachments(jsonSupport.getArrays().size());
285+
for (byte[] array : jsonSupport.getArrays()) {
286+
packet.addAttachment(Unpooled.wrappedBuffer(array));
287+
}
288+
}
289+
tmpSubType = (subType == PacketType.ACK
281290
? PacketType.BINARY_ACK : PacketType.BINARY_EVENT);
282291
}
283292
}
284293

285-
byte subType = toChar(packet.getSubType().getValue());
286-
buf.writeByte(subType);
294+
byte subTypeByte = toChar(tmpSubType.getValue());
295+
buf.writeByte(subTypeByte);
287296

288297
if (packet.hasAttachments()) {
289298
byte[] ackId = toChars(packet.getAttachments().size());
290299
buf.writeBytes(ackId);
291300
buf.writeByte('-');
292301
}
293302

294-
if (packet.getSubType() == PacketType.CONNECT) {
303+
if (subType == PacketType.CONNECT) {
295304
if (!packet.getNsp().isEmpty()) {
296305
buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
297306
}

0 commit comments

Comments
 (0)