Android API rx/tx update

This commit is contained in:
Joseph Henry
2016-08-02 14:48:37 -07:00
parent e29bb41544
commit f96b254e94
4 changed files with 51 additions and 11 deletions

View File

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

View File

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

View File

@@ -305,6 +305,27 @@ int (*realclose)(CLOSE_SIG);
return fcntl(fd, F_SETFL, O_NONBLOCK); return fcntl(fd, F_SETFL, O_NONBLOCK);
} }
#endif #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() ------------------------------- // --------------------------------- setsockopt() -------------------------------

View File

@@ -52,5 +52,9 @@ int main(int argc , char *argv[])
for(int i=0; i<bytes_read; i++) { for(int i=0; i<bytes_read; i++) {
printf("%c", client_message[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; return 0;
} }