Skip to content

Commit c934d3e

Browse files
committed
Convert more stuff to ByteBuffer
1 parent f0f1928 commit c934d3e

File tree

8 files changed

+28
-20
lines changed

8 files changed

+28
-20
lines changed

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* A connection used to invoke hub methods on a SignalR Server.
2929
*/
3030
public class HubConnection implements AutoCloseable {
31-
private static final String RECORD_SEPARATOR = "\u001e";
31+
private static final byte RECORD_SEPARATOR = 0x1e;
3232
private static final List<Class<?>> emptyArray = new ArrayList<>();
3333
private static final int MAX_NEGOTIATE_ATTEMPTS = 100;
3434

@@ -136,7 +136,7 @@ Transport getTransport() {
136136
}
137137

138138
this.baseUrl = url;
139-
this.protocol = new JsonHubProtocol();
139+
this.protocol = new MessagePackHubProtocol();
140140

141141
if (accessTokenProvider != null) {
142142
this.accessTokenProvider = accessTokenProvider;
@@ -167,8 +167,10 @@ Transport getTransport() {
167167
this.callback = (payload) -> {
168168
resetServerTimeout();
169169
if (!handshakeReceived) {
170-
int handshakeLength = payload.indexOf(RECORD_SEPARATOR) + 1;
171-
String handshakeResponseString = payload.substring(0, handshakeLength - 1);
170+
// The handshake will always be a UTF8 Json string, so we can convert the ByteBuffer to that to read the beginning of the payload
171+
String payloadStr = new String(payload.array(), StandardCharsets.UTF_8);
172+
int handshakeLength = payloadStr.indexOf(RECORD_SEPARATOR) + 1;
173+
String handshakeResponseString = payloadStr.substring(0, handshakeLength - 1);
172174
HandshakeResponseMessage handshakeResponse;
173175
try {
174176
handshakeResponse = HandshakeProtocol.parseHandshakeResponse(handshakeResponseString);
@@ -186,15 +188,18 @@ Transport getTransport() {
186188
}
187189
handshakeReceived = true;
188190
handshakeResponseSubject.onComplete();
191+
192+
// Increment the ByteBuffer payload by the byte length of the handshake + the byte length of the record separator (1)
193+
int readBytes = handshakeResponseString.getBytes(StandardCharsets.UTF_8).length + 1;
194+
payload = payload.position(payload.position() + readBytes);
189195

190-
payload = payload.substring(handshakeLength);
191196
// The payload only contained the handshake response so we can return.
192-
if (payload.length() == 0) {
197+
if (!payload.hasRemaining()) {
193198
return;
194199
}
195200
}
196201

197-
List<HubMessage> messages = protocol.parseMessages(ByteBuffer.wrap(payload.getBytes(StandardCharsets.UTF_8)), connectionState);
202+
List<HubMessage> messages = protocol.parseMessages(payload, connectionState);
198203

199204
for (HubMessage message : messages) {
200205
logger.debug("Received message of type {}.", message.getMessageType());

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/JsonHubProtocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public TransferFormat getTransferFormat() {
3939

4040
@Override
4141
public List<HubMessage> parseMessages(ByteBuffer payload, InvocationBinder binder) {
42-
String payloadStr = new String(payload.array(), StandardCharsets.UTF_8);
42+
String payloadStr = new String(payload.array(), StandardCharsets.UTF_8);
4343
if (payloadStr.length() == 0) {
4444
return null;
4545
}

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/LongPollingTransport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.microsoft.signalr;
55

66
import java.nio.ByteBuffer;
7+
import java.nio.charset.StandardCharsets;
78
import java.util.Map;
89
import java.util.concurrent.ExecutorService;
910
import java.util.concurrent.Executors;
@@ -101,7 +102,7 @@ private Completable poll(String url) {
101102
} else {
102103
if (response.getContent() != null) {
103104
logger.debug("Message received.");
104-
onReceiveThread.submit(() ->this.onReceive(response.getContent()));
105+
onReceiveThread.submit(() ->this.onReceive(ByteBuffer.wrap(response.getContent().getBytes(StandardCharsets.UTF_8))));
105106
} else {
106107
logger.debug("Poll timed out, reissuing.");
107108
}
@@ -139,7 +140,7 @@ public void setOnReceive(OnReceiveCallBack callback) {
139140
}
140141

141142
@Override
142-
public void onReceive(String message) {
143+
public void onReceive(ByteBuffer message) {
143144
this.onReceiveCallBack.invoke(message);
144145
logger.debug("OnReceived callback has been invoked.");
145146
}

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/MessagePackHubProtocol.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ public List<HubMessage> parseMessages(ByteBuffer payload, InvocationBinder binde
9090
break;
9191
}
9292
// Make sure that we actually read the right number of bytes
93-
long readBytes = unpacker.getTotalReadBytes();
93+
int readBytes = (int) unpacker.getTotalReadBytes();
9494
if (readBytes != length) {
95-
throw new RuntimeException(String.format("MessagePack message was length %d but claimed to be length %d.", readBytes, length));
95+
throw new RuntimeException(String.format("MessagePack message was length %d but claimed to be length %d.", readBytes, length));
9696
}
9797
unpacker.close();
98-
payload = payload.position(payload.position() + (int) readBytes);
98+
payload = payload.position(payload.position() + readBytes);
9999
}
100100
} catch (MessagePackException | IOException ex) {
101101
throw new RuntimeException("Error reading MessagePack data.", ex);
@@ -160,7 +160,7 @@ private HubMessage createInvocationMessage(MessageUnpacker unpacker, InvocationB
160160
// invocationId may be nil
161161
String invocationId = null;
162162
if (!unpacker.tryUnpackNil()) {
163-
invocationId = unpacker.unpackString();
163+
invocationId = unpacker.unpackString();
164164
}
165165

166166
// For MsgPack, we represent an empty invocation ID as an empty string,

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/OkHttpWebSocketWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Completable stop() {
6464

6565
@Override
6666
public Completable send(ByteBuffer message) {
67-
ByteString bs = ByteString.of(message);
67+
ByteString bs = ByteString.of(message);
6868
websocketClient.send(bs);
6969
return Completable.complete();
7070
}
@@ -87,12 +87,12 @@ public void onOpen(WebSocket webSocket, Response response) {
8787

8888
@Override
8989
public void onMessage(WebSocket webSocket, String message) {
90-
onReceive.invoke(message);
90+
onReceive.invoke(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)));
9191
}
9292

9393
@Override
9494
public void onMessage(WebSocket webSocket, ByteString bytes) {
95-
onReceive.invoke(new String(bytes.toByteArray(), StandardCharsets.ISO_8859_1));
95+
onReceive.invoke(bytes.asByteBuffer());
9696
}
9797

9898
@Override

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/OnReceiveCallBack.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package com.microsoft.signalr;
55

6+
import java.nio.ByteBuffer;
7+
68
interface OnReceiveCallBack {
7-
void invoke(String message);
9+
void invoke(ByteBuffer message);
810
}

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/Transport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Transport {
1111
Completable start(String url);
1212
Completable send(ByteBuffer message);
1313
void setOnReceive(OnReceiveCallBack callback);
14-
void onReceive(String message);
14+
void onReceive(ByteBuffer message);
1515
void setOnClose(TransportOnClosedCallback onCloseCallback);
1616
Completable stop();
1717
}

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/WebSocketTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void setOnReceive(OnReceiveCallBack callback) {
7272
}
7373

7474
@Override
75-
public void onReceive(String message) {
75+
public void onReceive(ByteBuffer message) {
7676
this.onReceiveCallBack.invoke(message);
7777
}
7878

0 commit comments

Comments
 (0)