Browse Source

Android API rx/tx update

pull/1/head
Joseph Henry 10 years ago
parent
commit
f96b254e94
  1. 29
      integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java
  2. 8
      src/SDK_EthernetTap.cpp
  3. 21
      src/SDK_Sockets.c
  4. 4
      tests/tcp_server.c

29
integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java

@ -29,22 +29,37 @@ public class MainActivity extends AppCompatActivity {
// Create ZeroTier socket
int sock = zt.zt_socket(SDK.AF_INET, SDK.SOCK_STREAM, 0);
/*
try {
Thread.sleep(5000);
Thread.sleep(25000);
}
catch(java.lang.InterruptedException e) { }
*/
int mode = 0; // client/server mode toggle
// Establish outgoing connection
if(mode==0)
{
int err = zt.zt_connect(sock, "10.9.9.203", 7000);
Log.d("TEST", "err = " + err + "\n");
SDK.zt_write(sock, "Welcome to the machine".getBytes(), 16);
byte[] buffer = null;
SDK.zt_read(sock, buffer, 16);
Log.d("TEST", "buffer = " + buffer);
int err = -1;
while(err < 0) {
try {
Thread.sleep(1000);
}
catch(java.lang.InterruptedException e) { }
err = zt.zt_connect(sock, "10.9.9.100", 7003);
Log.d("TEST", "err = " + err + "\n");
}
// TX
zt.zt_write(sock, "Welcome to the machine".getBytes(), 16);
// RX
byte[] buffer = new byte[12];
zt.zt_read(sock, buffer, 12);
String bufStr = new String(buffer);
Log.d("TEST", "response = " + bufStr);
}
// Listen to incoming connections

8
src/SDK_EthernetTap.cpp

@ -794,11 +794,11 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *
tot += len;
}
if(tot) {
#if defined(USE_SOCKS_PROXY)
l->tap->phyOnTcpWritable(l->conn->sock, NULL, true);
#else
//#if defined(USE_SOCKS_PROXY)
// l->tap->phyOnTcpWritable(l->conn->sock, NULL, true);
//#else
l->tap->phyOnUnixWritable(l->conn->sock, NULL, true);
#endif
//#endif
}
l->tap->lwipstack->__pbuf_free(q);
return ERR_OK;

21
src/SDK_Sockets.c

@ -305,6 +305,27 @@ int (*realclose)(CLOSE_SIG);
return fcntl(fd, F_SETFL, O_NONBLOCK);
}
#endif
// ------------------------------------------------------------------------------
// ----------------------- Exposed RX/TX API for Java JNI -----------------------
// ------------------------------------------------------------------------------
// TX
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len)
{
jbyte *body = (*env)->GetByteArrayElements(env, buf, 0);
int written_bytes = write(fd, body, len);
(*env)->ReleaseByteArrayElements(env, buf, body, 0);
return written_bytes;
}
// RX
JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1read(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len)
{
jbyte *body = (*env)->GetByteArrayElements(env, buf, 0);
int read_bytes = read(fd, body, len);
(*env)->ReleaseByteArrayElements(env, buf, body, 0);
return read_bytes;
}
// ------------------------------------------------------------------------------
// --------------------------------- setsockopt() -------------------------------

4
tests/tcp_server.c

@ -52,5 +52,9 @@ int main(int argc , char *argv[])
for(int i=0; i<bytes_read; i++) {
printf("%c", client_message[i]);
}
// TX
int bytes_written = write(client_sock, "Server here!", 12);
printf("bytes_written = %d\n", bytes_written);
return 0;
}

Loading…
Cancel
Save