Skip to content

Commit 3c3a76b

Browse files
committed
Reduce the impact
1 parent c1119e2 commit 3c3a76b

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
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: 10 additions & 3 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);
@@ -277,6 +275,15 @@ public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocat
277275
jsonSupport.writeValue(out, values);
278276

279277
if (!jsonSupport.getArrays().isEmpty()) {
278+
// If the Packet is sent by BroadcastOperations,
279+
// there is a problem of concurrent initialization for the same Packet.
280+
// Please initAttachment when creating the Packet to avoid this problem.
281+
if (!packet.hasAttachments()) {
282+
packet.initAttachments(jsonSupport.getArrays().size());
283+
for (byte[] array : jsonSupport.getArrays()) {
284+
packet.addAttachment(Unpooled.wrappedBuffer(array));
285+
}
286+
}
280287
packet.setSubType(packet.getSubType() == PacketType.ACK
281288
? PacketType.BINARY_ACK : PacketType.BINARY_EVENT);
282289
}

0 commit comments

Comments
 (0)