From 89ab08e3b28f6790f4a6c5740cd161adfa8b1165 Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Mon, 18 Jul 2016 12:24:24 -0700 Subject: [PATCH] Unity3D tweaks, test app updates --- .../Assets/ZeroTierNetworkInterface.cs | 19 +- make-mac.mk | 1 - tests/httpstub.c | 542 ------------------ tests/tcp_client.c | 18 +- tests/tcp_server.c | 18 +- tests/udp_client.c | 6 +- tests/udp_server.c | 23 +- 7 files changed, 39 insertions(+), 588 deletions(-) delete mode 100644 tests/httpstub.c diff --git a/integrations/Unity3D/Assets/ZeroTierNetworkInterface.cs b/integrations/Unity3D/Assets/ZeroTierNetworkInterface.cs index dcd6a61..0080251 100755 --- a/integrations/Unity3D/Assets/ZeroTierNetworkInterface.cs +++ b/integrations/Unity3D/Assets/ZeroTierNetworkInterface.cs @@ -208,35 +208,22 @@ public class ZeroTierNetworkInterface { } // Initialize the ZeroTier service with a given path - public ZeroTierNetworkInterface(string path, string nwid) - { + public ZeroTierNetworkInterface(string path, string nwid) { this.path = path; this.nwid = nwid; Init(); } - public ZeroTierNetworkInterface (string path) - { + public ZeroTierNetworkInterface (string path) { this.path = path; Init(); } // Initialize the ZeroTier service - public ZeroTierNetworkInterface() - { + public ZeroTierNetworkInterface() { Init(); } - // Initialize the ZeroTier service - // Use the GlobalConfig to set things like the max packet size - /* - public ZeroTierNetworkInterface(GlobalConfig gConfig) - { - MaxPacketSize = gConfig.MaxPacketSize; // TODO: Do something with this! - Init(); - } - */ - #region Network Handling // Joins a ZeroTier virtual network public bool JoinNetwork(string nwid) diff --git a/make-mac.mk b/make-mac.mk index e16d3e0..8da0580 100644 --- a/make-mac.mk +++ b/make-mac.mk @@ -156,7 +156,6 @@ clean: -find . -type f \( -name '*.o' -o -name '*.so' -o -name '*.o.d' -o -name '*.out' -o -name '*.log' \) -delete # android JNI lib project - test -s /usr/bin/javac || { echo "Javac not found"; exit 1; } -cd $(INT)/android/android_jni_lib/proj; ./gradlew clean diff --git a/tests/httpstub.c b/tests/httpstub.c deleted file mode 100644 index e5efaec..0000000 --- a/tests/httpstub.c +++ /dev/null @@ -1,542 +0,0 @@ -/* A simple http server for performance test. - Copyright (C) 2013 Sun, Junyi */ - -/* https://github.com/fxsjy/httpstub */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAX_EPOLL_FD 4096 -#define MAX_BUF_SIZE (1<<20) -#define WORKER_COUNT 2 - -int ep_fd[WORKER_COUNT],listen_fd; -int g_delay; -int g_shutdown_flag; -int g_quiet; -FILE *g_logger; -int g_pipe[WORKER_COUNT][2]; - -enum version_t { - HTTP_1_0 = 10, - HTTP_1_1 = 11 -}; -struct io_data_t { - int fd; - struct sockaddr_in addr; - char *in_buf; - char *out_buf; - int in_buf_cur; - int out_buf_cur; - int out_buf_total; - int keep_alive; - enum version_t version; -}; - -struct slice_t { - char *begin; - size_t size; -}; - -struct thread_data_t{ - struct slice_t data_from_file; - int myep_fd; - int mypipe_fd; -}; - -static void *handle_io_loop(void *param); - -static void httpstub_log(const char *fmt, ...); - -static void setnonblocking(int fd) -{ - int opts; - opts = fcntl(fd, F_GETFL); - if (opts < 0) { - fprintf(stderr, "fcntl failed\n"); - return; - } - opts = opts | O_NONBLOCK; - if (fcntl(fd, F_SETFL, opts) < 0) { - fprintf(stderr, "fcntl failed\n"); - return; - } - return; -} - -static void usage() -{ - printf("usage: httpstub -p -f -d [-q quiet] \n"); -} - -static struct slice_t load_data(char *fname) -{ - struct stat buf; - char *bin = NULL; - FILE *fptr; - int ret; - struct slice_t result; - ret = stat(fname, &buf); - if (ret < 0) { - printf("open %s failed\n", fname); - perror(""); - exit(1); - } - printf(">> size of %s is %d\n", fname, (int)buf.st_size); - if (buf.st_size <= 0) { - printf("the file is empty or broken\n"); - exit(1); - } - if (buf.st_size <= 0 || buf.st_size > MAX_BUF_SIZE) { - printf("file is too large\n"); - exit(1); - } - bin = (char *)malloc(sizeof(char) * buf.st_size + 1); - bin[buf.st_size] = '\0'; - result.size = buf.st_size; - result.begin = bin; - fptr = fopen(fname, "rb"); - if(fread(bin, buf.st_size, 1, fptr)<=0){ - perror("failed to read file"); - exit(1); - }; - fclose(fptr); - return result; -} - -static struct io_data_t * alloc_io_data(int client_fd, struct sockaddr_in *client_addr) -{ - struct io_data_t *io_data_ptr = (struct io_data_t *)malloc(sizeof(struct io_data_t)); - io_data_ptr->fd = client_fd; - io_data_ptr->in_buf = (char *)malloc(4096); - io_data_ptr->out_buf = (char *)malloc(MAX_BUF_SIZE); - io_data_ptr->in_buf_cur = 0; - io_data_ptr->out_buf_cur = 0; - io_data_ptr->keep_alive = 1; - if (client_addr) - io_data_ptr->addr = *client_addr; - return io_data_ptr; -} - -static void destroy_io_data(struct io_data_t *io_data_ptr) -{ - if(NULL == io_data_ptr)return; - if(io_data_ptr->in_buf)free(io_data_ptr->in_buf); - if(io_data_ptr->out_buf)free(io_data_ptr->out_buf); - io_data_ptr->in_buf = NULL; - io_data_ptr->out_buf = NULL; - free(io_data_ptr); -} - -void exit_hook(int number) -{ - close(listen_fd); - g_shutdown_flag=1; - printf(">> [%d]will shutdown...[%d]\n", getpid(),number); -} - -int main(int argc, char **argv) -{ - const char *ip_binding = "0.0.0.0"; - int port_listening = 8402; - char *data_file=NULL; - int opt; - int on = 1; - - int client_fd=0; - int worker_count=WORKER_COUNT,i; - register int worker_pointer = 0; - - struct sockaddr_in server_addr; - struct slice_t data_from_file; - - pthread_t tid[WORKER_COUNT]; - pthread_attr_t tattr[WORKER_COUNT]; - struct thread_data_t tdata[WORKER_COUNT]; - - char ip_buf[256] = { 0 }; - struct sockaddr_in client_addr; - socklen_t client_n; - - - g_delay = 0; - g_shutdown_flag = 0; - if (argc == 1) { - usage(); - return 1; - } - g_quiet = 0; - while ((opt = getopt(argc, argv, "l:p:f:d:hq")) != -1) { - switch (opt) { - case 'l': - ip_binding = strdup(optarg); - break; - case 'p': - port_listening = atoi(optarg); - if (port_listening == 0) { - printf(">> invalid port : %s\n", optarg); - exit(1); - } - break; - case 'f': - data_file = strdup(optarg); - break; - case 'd': - g_delay = atoi(optarg); - break; - case 'q': - g_quiet = 1; - break; - case 'h': - usage(); - return 1; - } - - } - printf(">> IP listening:%s\n", ip_binding); - printf(">> port: %d\n", port_listening); - printf(">> data_file: %s\n", data_file); - printf(">> reponse delay(MS): %d\n", g_delay); - printf(">> quite:%d\n",g_quiet); - - if (NULL == data_file || strlen(data_file) == 0) { - printf("\033[31m-data file is needed!~ \033[0m\n"); - usage(); - return 1; - } - - g_logger = fopen("stub.log", "a"); - if (g_logger ==NULL) { - perror("create log file stub.log failed."); - exit(1); - } - - data_from_file = load_data(data_file); - - signal(SIGPIPE, SIG_IGN); - signal(SIGINT, exit_hook); - signal(SIGKILL, exit_hook); - signal(SIGQUIT, exit_hook); - signal(SIGTERM, exit_hook); - signal(SIGHUP, exit_hook); - - for(i=0;i 0) { - if(write(g_pipe[worker_pointer][1],(char*)&client_fd,4)<0){ - perror("failed to write pipe"); - exit(1); - } - inet_ntop(AF_INET, &client_addr.sin_addr, ip_buf, sizeof(ip_buf)); - httpstub_log("[CONN]Connection from %s", ip_buf); - worker_pointer++; - if(worker_pointer == worker_count) worker_pointer=0; - } - else if(errno == EBADF && g_shutdown_flag){ - break; - } - else{ - if(0 == g_shutdown_flag){ - perror("please check ulimit -n"); - sleep(1); - } - } - } - - free(data_from_file.begin); - - for(i=0; i< worker_count; i++){ - close(ep_fd[i]); - } - - if(client_fd<0 && 0==g_shutdown_flag){ - perror("Accep failed, try ulimit -n"); - httpstub_log("[ERROR]too many fds open, try ulimit -n"); - g_shutdown_flag = 1; - } - fclose(g_logger); - printf(">> [%d]waiting worker thread....\n",getpid()); - - for(i=0; i< worker_count; i++) - pthread_join(tid[i], NULL); - - printf(">> [%d]Bye~\n",getpid()); - return 0; -} - -static void destroy_fd(int myep_fd, int client_fd, struct io_data_t *data_ptr, int case_no) -{ - struct epoll_event ev; - ev.data.ptr = data_ptr; - epoll_ctl(myep_fd, EPOLL_CTL_DEL, client_fd, &ev); - shutdown(client_fd, SHUT_RDWR); - close(client_fd); - destroy_io_data(data_ptr); - httpstub_log("[DEBUG] close case %d",case_no); -} - -static void httpstub_log(const char *fmt, ...) -{ - if(0 == g_quiet){ - char msg[4096]; - char buf[64]; - time_t now = time(NULL); - va_list ap; - va_start(ap, fmt); - vsnprintf(msg, sizeof(msg), fmt, ap); - va_end(ap); - strftime(buf, sizeof(buf), "%d %b %H:%M:%S", localtime(&now)); - fprintf(g_logger, "[%d] %s %s\n", (int)getpid(), buf, msg); - fflush(g_logger); - } -} - -static void handle_output(int myep_fd, struct io_data_t *client_io_ptr) -{ - int cfd, ret, case_no; - struct epoll_event ev; - - cfd = client_io_ptr->fd; - ret = send(cfd, client_io_ptr->out_buf + client_io_ptr->out_buf_cur, client_io_ptr->out_buf_total - client_io_ptr->out_buf_cur, MSG_NOSIGNAL); - if (ret >= 0) - client_io_ptr->out_buf_cur += ret; - - httpstub_log("[DEBUG]out_buf_cur %d", client_io_ptr->out_buf_cur); - httpstub_log("[DEBUG]out_buf_total %d", client_io_ptr->out_buf_total); - - //printf("ret:%d\n",ret); - //printf("errno:%d\n", errno); - if (0 == ret || (ret < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { - //printf("loose 2\n"); - case_no = 2; - //perror("send"); - //printf("cfd: %d\n", cfd); - destroy_fd(myep_fd, cfd, client_io_ptr, case_no); - return; - } - if (client_io_ptr->out_buf_cur == client_io_ptr->out_buf_total) { //have sent all - httpstub_log("[NOTICE] all messages have been sent.(%d bytes)", client_io_ptr->out_buf_total); - //printf("alive: %d\n", client_io_ptr->keep_alive); - if (client_io_ptr->version == HTTP_1_0 && 0 == client_io_ptr->keep_alive) { - case_no = 4; - destroy_fd(myep_fd, cfd, client_io_ptr, case_no); - return; - } - ev.data.ptr = client_io_ptr; - ev.events = EPOLLIN; - epoll_ctl(myep_fd, EPOLL_CTL_MOD, cfd, &ev); - } - -} - - -static void handle_input(int myep_fd, struct io_data_t *client_io_ptr, struct slice_t data_from_file, const char *rsps_msg_fmt, int delay) -{ - int npos = 0; - int total = 0; - int ret = 0; - int case_no = 0; - char headmsg[256]; - char *sep = NULL; - const char *CRLF = "\r\n\r\n"; - const char *LF = "\n\n"; - const char *sep_flag=NULL; - - struct epoll_event ev; - int cfd = client_io_ptr->fd; - int pkg_len = 0; - - assert(client_io_ptr->in_buf_cur >= 0); - ret = recv(cfd, client_io_ptr->in_buf + client_io_ptr->in_buf_cur, 512, MSG_DONTWAIT); - //printf("%u\n",(unsigned int)pthread_self()); - if (0 == ret || (ret < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { - case_no = 1; - //perror("++++++++"); - destroy_fd(myep_fd, cfd, client_io_ptr, case_no); - return; - } - - client_io_ptr->in_buf_cur += ret; - client_io_ptr->in_buf[client_io_ptr->in_buf_cur] = '\0'; - - sep = strstr(client_io_ptr->in_buf, CRLF); - if (NULL == sep) { - sep = strstr(client_io_ptr->in_buf, LF); - if (NULL == sep) - return; - else - sep_flag = LF; - } else { - sep_flag = CRLF; - } - - if (strstr(client_io_ptr->in_buf, "GET ") == client_io_ptr->in_buf) { - if (strstr(client_io_ptr->in_buf, "HTTP/1.0") != NULL) { - client_io_ptr->version = HTTP_1_0; - if (NULL == strstr(client_io_ptr->in_buf, "Connection: Keep-Alive")) { - client_io_ptr->keep_alive = 0; - } - } else { - client_io_ptr->version = HTTP_1_1; - } - } - npos = strcspn(client_io_ptr->in_buf, "\r\n"); - if (npos > 250) - npos = 250; - memcpy(headmsg, client_io_ptr->in_buf, npos); - headmsg[npos] = '\0'; - httpstub_log("[RECV] %s ", headmsg); - - pkg_len = sep - client_io_ptr->in_buf + strlen(sep_flag); - - assert(pkg_len >= 0); - assert(client_io_ptr->in_buf_cur - pkg_len >= 0); - memmove(client_io_ptr->in_buf, sep + strlen(sep_flag), client_io_ptr->in_buf_cur - pkg_len); - client_io_ptr->in_buf_cur -= pkg_len; - - client_io_ptr->out_buf_cur = 0; - total = snprintf(client_io_ptr->out_buf, MAX_BUF_SIZE, rsps_msg_fmt, data_from_file.size); - memcpy(client_io_ptr->out_buf + total, data_from_file.begin, data_from_file.size); - total += data_from_file.size; - httpstub_log("[DEBUG]total:%d", total); - client_io_ptr->out_buf_total = total; - - ev.data.ptr = client_io_ptr; - ev.events = EPOLLOUT; - epoll_ctl(myep_fd, EPOLL_CTL_MOD, cfd, &ev); - if (delay > 0) { - //printf("usleep: %d\n",(int)(g_delay*2000/nfds) ); - usleep(delay); - } -} - -static void * handle_io_loop(void *param) -{ - register int i; - int cfd, nfds, case_no, new_sock_fd; - struct epoll_event events[MAX_EPOLL_FD],ev; - - const char *rsps_msg_fmt = "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nConnection: Keep-Alive\r\nContent-Type: text/plain\r\n\r\n"; - - struct io_data_t *client_io_ptr; - - struct thread_data_t my_tdata = *(struct thread_data_t*)param; - - ev.data.fd = my_tdata.mypipe_fd; - ev.events = EPOLLIN; - epoll_ctl(my_tdata.myep_fd,EPOLL_CTL_ADD,my_tdata.mypipe_fd,&ev); - - while (1) { - nfds = epoll_wait(my_tdata.myep_fd, events, MAX_EPOLL_FD, 1000); - //printf("nfds:%d, epoll fd:%d\n",nfds,my_tdata.myep_fd); - if(nfds<=0 && 0!=g_shutdown_flag){ - break; - } - for (i = 0; i < nfds && nfds>0; i++) { - if( (events[i].data.fd == my_tdata.mypipe_fd) && (events[i].events & EPOLLIN)){ - if(read(my_tdata.mypipe_fd,&new_sock_fd,4)==-1){ - perror("faild to read pipe"); - exit(1); - } - setnonblocking(new_sock_fd); - ev.data.ptr = alloc_io_data(new_sock_fd, (struct sockaddr_in *)NULL); - ev.events = EPOLLIN; - epoll_ctl(my_tdata.myep_fd, EPOLL_CTL_ADD, new_sock_fd, &ev); - continue; - } - client_io_ptr = (struct io_data_t *)events[i].data.ptr; - if(client_io_ptr->fd<=0) continue; - - if (events[i].events & EPOLLIN) { - handle_input(my_tdata.myep_fd, client_io_ptr, my_tdata.data_from_file, rsps_msg_fmt, (int)(g_delay * 1000 / nfds)); - - } else if (events[i].events & EPOLLOUT) { - handle_output(my_tdata.myep_fd, client_io_ptr); - - } else if (events[i].events & EPOLLERR) { - cfd = client_io_ptr->fd; - case_no = 3; - destroy_fd(my_tdata.myep_fd, cfd, client_io_ptr, case_no); - } - } - } - return NULL; -} diff --git a/tests/tcp_client.c b/tests/tcp_client.c index 94034d2..4e18c21 100644 --- a/tests/tcp_client.c +++ b/tests/tcp_client.c @@ -5,6 +5,9 @@ #include #include +int atoi(const char *str); +int close(int filedes); + int main(int argc , char *argv[]) { if(argc < 3) { @@ -18,7 +21,7 @@ int main(int argc , char *argv[]) sock = socket(AF_INET , SOCK_STREAM , 0); if (sock == -1) { - printf("Could not create socket"); + printf("could not create socket"); } server.sin_addr.s_addr = inet_addr(argv[1]); server.sin_family = AF_INET; @@ -29,29 +32,28 @@ int main(int argc , char *argv[]) perror("connect failed. Error"); return 1; } - puts("Connected\n"); - + printf("connected\n"); char *msg = "welcome to the machine!"; while(1) { - printf("Enter message : "); + printf("enter message : "); scanf("%s" , message); // TX if(send(sock, msg, sizeof(msg), 0) < 0) { - puts("Send failed"); + printf("send failed"); return 1; } else { - printf("len = %d\n", strlen(message)); + printf("len = %ld\n", strlen(message)); } // RX if(recv(sock , server_reply , 2000 , 0) < 0) { - puts("recv failed"); + printf("recv failed"); break; } - puts("Server reply :"); + printf("server reply :"); puts(server_reply); } close(sock); diff --git a/tests/tcp_server.c b/tests/tcp_server.c index 5a557c3..011f5cb 100644 --- a/tests/tcp_server.c +++ b/tests/tcp_server.c @@ -6,11 +6,13 @@ #include #include +int atoi(const char *str); + int main(int argc , char *argv[]) { if(argc < 2) { printf("usage: tcp_server \n"); - exit(0); + return 0; } int socket_desc, client_sock, c, read_size, port = atoi(argv[1]); @@ -19,8 +21,8 @@ int main(int argc , char *argv[]) socket_desc = socket(AF_INET , SOCK_STREAM , 0); if (socket_desc == -1) { - printf("Could not create socket"); - exit(0); + printf("could not create socket"); + return 0; } server.sin_family = AF_INET; @@ -30,23 +32,23 @@ int main(int argc , char *argv[]) printf("binding on port %d\n", port); if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) { perror("bind failed. Error"); - exit(0); + return 0; } printf("listening\n"); listen(socket_desc , 3); - puts("waiting to accept\n"); + printf("waiting to accept\n"); c = sizeof(struct sockaddr_in); client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); if (client_sock < 0) { perror("accept failed"); - exit(0); + return 0; } - puts("connection accepted\n reading...\n"); + printf("connection accepted\n reading...\n"); // RX int bytes_read = recv(client_sock , client_message , 2000 , 0); - printf("Read (%d) bytes\n", bytes_read); + printf("read (%d) bytes\n", bytes_read); for(int i=0; i #include +int atoi(const char *str); + int main(int argc, char * argv[]) { if(argc < 3) { @@ -49,8 +51,8 @@ int main(int argc, char * argv[]) return 1; } if (n_sent!=sizeof(msg)) - printf("Sendto sent %d bytes\n",(int)n_sent); - printf("n_sent = %d, count = %d\n", n_sent,count); + printf("sendto sent %d bytes\n",(int)n_sent); + printf("n_sent = %ld, count = %d\n", n_sent,count); } // RX from server diff --git a/tests/udp_server.c b/tests/udp_server.c index fd092ab..c6bd4b3 100755 --- a/tests/udp_server.c +++ b/tests/udp_server.c @@ -12,10 +12,10 @@ #define MAXBUF 1024*1024 void echo( int sd ) { - int len,n; char bufin[MAXBUF]; struct sockaddr_in remote; - len = sizeof(remote); + int n; + socklen_t len = sizeof(remote); long count = 0; while (1) { @@ -29,7 +29,7 @@ void echo( int sd ) { if (n<0) { perror("Error receiving data"); } else { - printf("GOT %d BYTES (count = %d)\n",n, count); + printf("GOT %d BYTES (count = %ld)\n", n, count); // Got something, just send it back // sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len); } @@ -40,16 +40,17 @@ int main(int argc, char *argv[]) { if(argc < 2) { printf("usage: udp_server \n"); - exit(0); + return 0; } - int ld, length, port = atoi(argv[1]); + int ld, port = atoi(argv[1]); + socklen_t len; struct sockaddr_in skaddr; struct sockaddr_in skaddr2; // Create socket if ((ld = socket( PF_INET, SOCK_DGRAM, 0)) < 0) { printf("error creating socket\n"); - exit(1); + return 0; } // Create address skaddr.sin_family = AF_INET; @@ -58,13 +59,13 @@ int main(int argc, char *argv[]) { // Bind to address if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) { printf("error binding\n"); - exit(0); + return 0; } // find out what port we were assigned - length = sizeof( skaddr2 ); - if (getsockname(ld, (struct sockaddr *) &skaddr2, &length)<0) { + len = sizeof( skaddr2 ); + if (getsockname(ld, (struct sockaddr *) &skaddr2, &len)<0) { printf("error getsockname\n"); - exit(1); + return 0; } // Display address:port to verify it was sent over RPC correctly port = ntohs(skaddr2.sin_port); @@ -74,7 +75,7 @@ int main(int argc, char *argv[]) { d[1] = (ip >> 8) & 0xFF; d[2] = (ip >> 16) & 0xFF; d[3] = (ip >> 24) & 0xFF; - printf("Bound to address: %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port); + printf("bound to address: %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port); // RX echo(ld);