Compare commits

..
12 Commits
10 changed files with 175 additions and 21 deletions

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
[submodule "app/src/main/jni/jnienet/enet"]
path = app/src/main/jni/jnienet/enet
url = https://github.com/cgutman/enet.git
+3 -3
View File
@@ -11,8 +11,8 @@ android {
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 23 targetSdkVersion 23
versionName "4.5.3" versionName "4.5.4"
versionCode = 88 versionCode = 91
} }
productFlavors { productFlavors {
@@ -26,7 +26,7 @@ android {
} }
lintOptions { lintOptions {
abortOnError false disable 'MissingTranslation'
} }
buildTypes { buildTypes {
Binary file not shown.
Binary file not shown.
+5 -1
View File
@@ -50,6 +50,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
private String lastRawApplist; private String lastRawApplist;
private int lastRunningAppId; private int lastRunningAppId;
private boolean suspendGridUpdates; private boolean suspendGridUpdates;
private boolean inForeground;
private final static int START_OR_RESUME_ID = 1; private final static int START_OR_RESUME_ID = 1;
private final static int QUIT_ID = 2; private final static int QUIT_ID = 2;
@@ -108,7 +109,8 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
}; };
private void startComputerUpdates() { private void startComputerUpdates() {
if (managerBinder == null) { // Don't start polling if we're not bound or in the foreground
if (managerBinder == null || !inForeground) {
return; return;
} }
@@ -252,6 +254,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
inForeground = true;
startComputerUpdates(); startComputerUpdates();
} }
@@ -259,6 +262,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
inForeground = false;
stopComputerUpdates(); stopComputerUpdates();
} }
+14 -16
View File
@@ -53,7 +53,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
private RelativeLayout noPcFoundLayout; private RelativeLayout noPcFoundLayout;
private PcGridAdapter pcGridAdapter; private PcGridAdapter pcGridAdapter;
private ComputerManagerService.ComputerManagerBinder managerBinder; private ComputerManagerService.ComputerManagerBinder managerBinder;
private boolean freezeUpdates, runningPolling, hasResumed; private boolean freezeUpdates, runningPolling, inForeground;
private final ServiceConnection serviceConnection = new ServiceConnection() { private final ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) { public void onServiceConnected(ComponentName className, IBinder binder) {
final ComputerManagerService.ComputerManagerBinder localBinder = final ComputerManagerService.ComputerManagerBinder localBinder =
@@ -161,11 +161,9 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
} }
private void startComputerUpdates() { private void startComputerUpdates() {
if (managerBinder != null) { // Only allow polling to start if we're bound to CMS, polling is not already running,
if (runningPolling) { // and our activity is in the foreground.
return; if (managerBinder != null && !runningPolling && inForeground) {
}
freezeUpdates = false; freezeUpdates = false;
managerBinder.startPolling(new ComputerManagerListener() { managerBinder.startPolling(new ComputerManagerListener() {
@Override @Override
@@ -215,7 +213,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
hasResumed = true; inForeground = true;
startComputerUpdates(); startComputerUpdates();
} }
@@ -223,7 +221,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
hasResumed = false; inForeground = false;
stopComputerUpdates(false); stopComputerUpdates(false);
} }
@@ -271,10 +269,9 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
@Override @Override
public void onContextMenuClosed(Menu menu) { public void onContextMenuClosed(Menu menu) {
// For some reason, this gets called again _after_ onPause() is called on this activity. // For some reason, this gets called again _after_ onPause() is called on this activity.
// We don't want to start computer updates again, so we need to keep track of whether we're paused. // startComputerUpdates() manages this and won't actual start polling until the activity
if (hasResumed) { // returns to the foreground.
startComputerUpdates(); startComputerUpdates();
}
} }
private void doPair(final ComputerDetails computer) { private void doPair(final ComputerDetails computer) {
@@ -368,14 +365,15 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
} }
if (toastSuccess) { if (toastSuccess) {
// Open the app list after a successful pairing attemp // Open the app list after a successful pairing attempt
doAppList(computer); doAppList(computer);
} }
else {
// Start polling again if we're still in the foreground
startComputerUpdates();
}
} }
}); });
// Start polling again
startComputerUpdates();
} }
}).start(); }).start();
} }
+25
View File
@@ -0,0 +1,25 @@
# Android.mk for Moonlight's ENet JNI binding
MY_LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
LOCAL_PATH := $(MY_LOCAL_PATH)
include $(CLEAR_VARS)
LOCAL_MODULE := jnienet
LOCAL_SRC_FILES := jnienet.c \
enet/callbacks.c \
enet/compress.c \
enet/host.c \
enet/list.c \
enet/packet.c \
enet/peer.c \
enet/protocol.c \
enet/unix.c \
enet/win32.c \
LOCAL_CFLAGS := -DHAS_SOCKLEN_T=1
LOCAL_C_INCLUDES := $(LOCAL_PATH)/enet/include
include $(BUILD_SHARED_LIBRARY)
+123
View File
@@ -0,0 +1,123 @@
#include "enet/enet.h"
#include <stdlib.h>
#include <jni.h>
#define CLIENT_TO_LONG(x) ((intptr_t)(x))
#define LONG_TO_CLIENT(x) ((ENetHost*)(intptr_t)(x))
#define PEER_TO_LONG(x) ((intptr_t)(x))
#define LONG_TO_PEER(x) ((ENetPeer*)(intptr_t)(x))
JNIEXPORT jint JNICALL
Java_com_limelight_nvstream_enet_EnetConnection_initializeEnet(JNIEnv *env, jobject class) {
return enet_initialize();
}
JNIEXPORT jlong JNICALL
Java_com_limelight_nvstream_enet_EnetConnection_createClient(JNIEnv *env, jobject class) {
// Create a client that can use 1 outgoing connection and 1 channel
return CLIENT_TO_LONG(enet_host_create(NULL, 1, 1, 0, 0));
}
JNIEXPORT jlong JNICALL
Java_com_limelight_nvstream_enet_EnetConnection_connectToPeer(JNIEnv *env, jobject class, jlong client, jstring address, jint port, jint timeout) {
ENetPeer* peer;
ENetAddress enetAddress;
ENetEvent event;
const char *addrStr;
// Initialize the ENet address
addrStr = (*env)->GetStringUTFChars(env, address, 0);
enet_address_set_host(&enetAddress, addrStr);
enet_address_set_port(&enetAddress, port);
(*env)->ReleaseStringUTFChars(env, address, addrStr);
// Start the connection
peer = enet_host_connect(LONG_TO_CLIENT(client), &enetAddress, 1, 0);
if (peer == NULL) {
return PEER_TO_LONG(NULL);
}
// Wait for the connect to complete
if (enet_host_service(LONG_TO_CLIENT(client), &event, timeout) <= 0 || event.type != ENET_EVENT_TYPE_CONNECT) {
enet_peer_reset(peer);
return PEER_TO_LONG(NULL);
}
// Ensure the connect verify ACK is sent immediately
enet_host_flush(LONG_TO_CLIENT(client));
// Set the max peer timeout to 10 seconds
enet_peer_timeout(peer, ENET_PEER_TIMEOUT_LIMIT, ENET_PEER_TIMEOUT_MINIMUM, 10000);
return PEER_TO_LONG(peer);
}
JNIEXPORT jint JNICALL
Java_com_limelight_nvstream_enet_EnetConnection_readPacket(JNIEnv *env, jobject class, jlong client, jbyteArray data, jint length, jint timeout) {
jint err;
jbyte* dataPtr;
ENetEvent event;
// Wait for a receive event, timeout, or disconnect
err = enet_host_service(LONG_TO_CLIENT(client), &event, timeout);
if (err <= 0) {
return err;
}
else if (event.type != ENET_EVENT_TYPE_RECEIVE) {
return -1;
}
// Check that the packet isn't too large
if (event.packet->dataLength > length) {
enet_packet_destroy(event.packet);
return event.packet->dataLength;
}
// Copy the packet data into the caller's buffer
dataPtr = (*env)->GetByteArrayElements(env, data, 0);
memcpy(dataPtr, event.packet->data, event.packet->dataLength);
err = event.packet->dataLength;
(*env)->ReleaseByteArrayElements(env, data, dataPtr, 0);
// Free the packet
enet_packet_destroy(event.packet);
return err;
}
JNIEXPORT jboolean JNICALL
Java_com_limelight_nvstream_enet_EnetConnection_writePacket(JNIEnv *env, jobject class, jlong client, jlong peer, jbyteArray data, jint length, jint packetFlags) {
ENetPacket* packet;
jboolean ret;
jbyte* dataPtr;
dataPtr = (*env)->GetByteArrayElements(env, data, 0);
// Create the reliable packet that describes our outgoing message
packet = enet_packet_create(dataPtr, length, packetFlags);
if (packet != NULL) {
// Send the message to the peer
enet_peer_send(LONG_TO_PEER(peer), 0, packet);
enet_host_flush(LONG_TO_CLIENT(client));
ret = JNI_TRUE;
}
else {
ret = JNI_FALSE;
}
(*env)->ReleaseByteArrayElements(env, data, dataPtr, JNI_ABORT);
return ret;
}
JNIEXPORT jint JNICALL
Java_com_limelight_nvstream_enet_EnetConnection_destroyClient(JNIEnv *env, jobject class, jlong client) {
enet_host_destroy(LONG_TO_CLIENT(client));
}
JNIEXPORT jint JNICALL
Java_com_limelight_nvstream_enet_EnetConnection_disconnectPeer(JNIEnv *env, jobject class, jlong peer) {
enet_peer_disconnect_now(LONG_TO_PEER(peer), 0);
}
+1 -1
View File
@@ -4,7 +4,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta2' classpath 'com.android.tools.build:gradle:2.0.0-beta6'
} }
} }