Skip to content

Commit b415884

Browse files
author
Elias Adum
committedAug 29, 2013
Merge pull request #1 from uken/perf-improvements
improve performance of WebSocket for large messages
2 parents 07db347 + b6fbf5f commit b415884

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed
 

‎src/com/strumsoft/websocket/phonegap/WebSocket.java

+57-29
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ public enum Draft {
208208

209209
private final WebSocket instance;
210210

211+
private ByteBuffer bigBuffer = ByteBuffer.allocate(1024 * 500);
212+
private byte[] tokenByteBuffer = new byte[1024 * 500];
213+
private int tokenByteBufferCounter = 0;
214+
211215
/**
212216
* Constructor.
213217
*
@@ -530,52 +534,76 @@ private void _write(byte[] bytes) throws IOException {
530534
}
531535

532536
private void _read() throws IOException, NoSuchAlgorithmException {
533-
this.buffer.rewind();
534-
535537
int bytesRead = -1;
536538
try {
537-
bytesRead = this.socketChannel.read(this.buffer);
539+
if (!handshakeComplete) {
540+
buffer.rewind();
541+
bytesRead = socketChannel.read(this.buffer);
542+
buffer.rewind();
543+
} else {
544+
bigBuffer.rewind();
545+
bytesRead = socketChannel.read(this.bigBuffer);
546+
bigBuffer.rewind();
547+
}
548+
538549
} catch (Exception ex) {
550+
Log.v("websocket", "Could not read data from socket channel, ex=" + ex.toString());
539551
}
540552

541553
if (bytesRead == -1) {
554+
Log.v("websocket", "All Bytes readed");
542555
close();
543556
} else if (bytesRead > 0) {
544-
this.buffer.rewind();
545-
546557
if (!this.handshakeComplete) {
547558
_readHandshake();
548559
} else {
549-
_readFrame();
560+
_readFrame(bytesRead);
550561
}
551562
}
552563
}
553564

554-
private void _readFrame() throws UnsupportedEncodingException {
555-
byte newestByte = this.buffer.get();
565+
private void _readFrame(int bytesRead) throws UnsupportedEncodingException {
566+
byte[] data = bigBuffer.array();
556567

557-
if (newestByte == DATA_START_OF_FRAME) { // Beginning of Frame
558-
this.currentFrame = null;
568+
int length = bytesRead;
569+
if (length > 2000) length = 2000;
559570

560-
} else if (newestByte == DATA_END_OF_FRAME) { // End of Frame
561-
String textFrame = null;
562-
// currentFrame will be null if END_OF_FRAME was send directly after
563-
// START_OF_FRAME, thus we will send 'null' as the sent message.
564-
if (this.currentFrame != null) {
565-
textFrame = new String(this.currentFrame.array(), UTF8_CHARSET.toString());
566-
}
567-
// fire onMessage method
568-
this.onMessage(textFrame);
569-
570-
} else { // Regular frame data, add to current frame buffer
571-
ByteBuffer frame = ByteBuffer.allocate((this.currentFrame != null ? this.currentFrame.capacity() : 0)
572-
+ this.buffer.capacity());
573-
if (this.currentFrame != null) {
574-
this.currentFrame.rewind();
575-
frame.put(this.currentFrame);
576-
}
577-
frame.put(newestByte);
578-
this.currentFrame = frame;
571+
Log.v("websocket", "_readFrame - bytesRead: " + bytesRead + ", data: " + new String(data, 0, length));
572+
573+
// Get tokens
574+
for (int i=0; i < bytesRead; i++) {
575+
576+
byte readByte = data[i];
577+
578+
// Token message is finished
579+
if (readByte == DATA_END_OF_FRAME) {
580+
581+
// Make token public
582+
this.onMessage(new String(tokenByteBuffer, 0, tokenByteBufferCounter));
583+
584+
// Reset counter for byte buffer
585+
tokenByteBufferCounter = 0;
586+
587+
// Bytes to read as token message, skip start frame byte
588+
} else if (readByte != DATA_START_OF_FRAME) {
589+
tokenByteBufferCounter++;
590+
591+
// Array is out of bounds, make it greater
592+
if (tokenByteBufferCounter == tokenByteBuffer.length) {
593+
594+
byte[] newTokenByteBuffer = new byte[tokenByteBuffer.length*2];
595+
596+
Log.v("websocket", "expand token byte buffer, new size=" + newTokenByteBuffer.length);
597+
598+
// Copy old data
599+
for (int i2=0; i2<tokenByteBuffer.length; i2++) {
600+
newTokenByteBuffer[i2] = tokenByteBuffer[i2];
601+
}
602+
603+
tokenByteBuffer = newTokenByteBuffer;
604+
}
605+
606+
tokenByteBuffer[tokenByteBufferCounter-1] = readByte;
579607
}
580608
}
581609

0 commit comments

Comments
 (0)
Failed to load comments.