int net_host_broadcast_thread(void *arg) { char ip[32],err_str[256]; unsigned char *uc_ptr; unsigned long r_addr; d3socket sock; // use broadcast err_str to flag if errors occured broadcast_listen_err_str[0]=0x0; // create host socket broadcast_listen_socket=net_udp_open_socket(); if (broadcast_listen_socket==D3_NULL_SOCKET) { strcpy(broadcast_listen_err_str,"Networking: Unable to open socket"); broadcast_listen_complete=TRUE; return(0); } net_socket_blocking(broadcast_listen_socket,TRUE); // bind to the "any" IP to gather // any broadcast messages if (!net_udp_bind_broadcast(broadcast_listen_socket,net_port_host_broadcast,broadcast_listen_err_str)) { net_close_socket(&broadcast_listen_socket); broadcast_listen_complete=TRUE; return(0); } // listener is OK, free thread to run independantly broadcast_listen_complete=TRUE; // start listening while (TRUE) { r_addr=net_udp_receive_broadcast(broadcast_listen_socket); if (r_addr==-1) break; // connect and reply that we are a server uc_ptr=(unsigned char*)&r_addr; sock=net_open_socket(); if (sock==D3_NULL_SOCKET) continue; uc_ptr=(unsigned char*)&r_addr; sprintf(ip,"%d.%d.%d.%d",uc_ptr[0],uc_ptr[1],uc_ptr[2],uc_ptr[3]); if (net_connect_block(sock,ip,net_port_host_broadcast_reply,client_timeout_wait_seconds,err_str)) { net_host_client_handle_info(sock); } net_close_socket(&sock); } return(0); }
int net_host_thread(void *arg) { // use host err_str to flag if errors occured host_err_str[0]=0x0; // create host socket host_socket=net_open_udp_socket(); if (host_socket==D3_NULL_SOCKET) { strcpy(host_err_str,"Hosting: Unable to open socket"); host_complete=TRUE; return(0); } // we'll let the socket block for us net_socket_blocking(host_socket,TRUE); // bind -- use any IP on this machine to // get traffic if (!net_bind_any(host_socket,net_port_host,host_err_str)) { net_close_socket(&host_socket); host_complete=TRUE; return(0); } // host is OK, free thread to run independantly host_complete=TRUE; // begin waiting for messages while (TRUE) { // exiting? if (host_start_shutdown) break; // feed the queues from the socket // if there's an error, break out of loop // and cancel network game if (!net_queue_feed(host_socket,&host_queue)) break; // always wait a little to not flood this thread usleep(100000); } return(0); }
IoT_Error_t setSocketToNonBlocking(int server_fd) { net_socket_blocking(server_fd, NET_BLOCKING_OFF); return 0; }