Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/com/codebutler/android_websockets/HybiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@

package com.codebutler.android_websockets;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.net.TrafficStats;
import android.os.Build;
import android.util.Log;

import java.io.*;
import java.util.Arrays;
import java.util.List;

@TargetApi(9)
public class HybiParser {
private static final String TAG = "HybiParser";

Expand Down Expand Up @@ -250,7 +255,8 @@ public void close(int code, String reason) {
mClosed = true;
}

private void emitFrame() throws IOException {
@SuppressLint("NewApi")
private void emitFrame() throws IOException {
byte[] payload = mask(mPayload, mMask, 0);
int opcode = mOpcode;

Expand All @@ -267,6 +273,8 @@ private void emitFrame() throws IOException {
mClient.getListener().onMessage(message);
}
reset();
if(Build.VERSION.SDK_INT >= 14)
TrafficStats.incrementOperationCount(1);
}

} else if (opcode == OP_TEXT) {
Expand All @@ -277,6 +285,8 @@ private void emitFrame() throws IOException {
mMode = MODE_TEXT;
mBuffer.write(payload);
}
if(Build.VERSION.SDK_INT >= 14)
TrafficStats.incrementOperationCount(1);

} else if (opcode == OP_BINARY) {
if (mFinal) {
Expand All @@ -285,6 +295,8 @@ private void emitFrame() throws IOException {
mMode = MODE_BINARY;
mBuffer.write(payload);
}
if(Build.VERSION.SDK_INT >= 14)
TrafficStats.incrementOperationCount(1);

} else if (opcode == OP_CLOSE) {
int code = (payload.length >= 2) ? 256 * payload[0] + payload[1] : 0;
Expand Down
39 changes: 34 additions & 5 deletions src/com/codebutler/android_websockets/WebSocketClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.codebutler.android_websockets;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.net.TrafficStats;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;
Expand All @@ -20,13 +24,16 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@TargetApi(8)
public class WebSocketClient {
private static final String TAG = "WebSocketClient";
private int mSocketTag = -1;

private URI mURI;
private Listener mListener;
Expand Down Expand Up @@ -66,8 +73,8 @@ public void connect() {
}

mThread = new Thread(new Runnable() {
@Override
public void run() {
@SuppressLint("NewApi")
public void run() {
try {
int port = (mURI.getPort() != -1) ? mURI.getPort() : (mURI.getScheme().equals("wss") ? 443 : 80);

Expand All @@ -81,6 +88,10 @@ public void run() {

SocketFactory factory = mURI.getScheme().equals("wss") ? getSSLSocketFactory() : SocketFactory.getDefault();
mSocket = factory.createSocket(mURI.getHost(), port);
if(Build.VERSION.SDK_INT >= 14 && mSocketTag > 0) {
TrafficStats.setThreadStatsTag(mSocketTag);
TrafficStats.tagSocket(mSocket);
}

PrintWriter out = new PrintWriter(mSocket.getOutputStream());
out.print("GET " + path + " HTTP/1.1\r\n");
Expand Down Expand Up @@ -142,7 +153,6 @@ public void run() {
public void disconnect() {
if (mSocket != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
try {
mSocket.close();
Expand Down Expand Up @@ -205,13 +215,15 @@ private String createSecret() {

void sendFrame(final byte[] frame) {
mHandler.post(new Runnable() {
@Override
public void run() {
@SuppressLint("NewApi")
public void run() {
try {
synchronized (mSendLock) {
OutputStream outputStream = mSocket.getOutputStream();
outputStream.write(frame);
outputStream.flush();
if(Build.VERSION.SDK_INT >= 14)
TrafficStats.incrementOperationCount(1);
}
} catch (IOException e) {
mListener.onError(e);
Expand All @@ -220,6 +232,23 @@ public void run() {
});
}

public void setSocketTag(int tag) {
mSocketTag = tag;
if(Build.VERSION.SDK_INT >= 14 && mSocketTag > 0 && mSocket != null) {
mHandler.post(new Runnable() {
@TargetApi(14)
public void run() {
try {
TrafficStats.setThreadStatsTag(mSocketTag);
TrafficStats.tagSocket(mSocket);
} catch (SocketException e) {
mListener.onError(e);
}
}
});
}
}

public interface Listener {
public void onConnect();
public void onMessage(String message);
Expand Down