static void *nak_routine(void *arg) { /* This thread makes sure we get the NAKs from the receivers */ pgm_sock_t *pgm_sock = (pgm_sock_t*)arg; pgm_error_t* pgm_err = NULL; int fds = 0; fd_set readfds; char recv_buf[PGMBUF_SIZE]; size_t bytes_read = 0; int run_receiver = m_run_receiver; while (run_receiver) { memset(&recv_buf, 0, PGMBUF_SIZE); bytes_read = 0; struct timeval tv; struct pgm_sockaddr_t from; socklen_t from_sz = sizeof(from); const int pgm_status = pgm_recvfrom(pgm_sock, recv_buf, PGMBUF_SIZE, MSG_DONTWAIT, &bytes_read, &from, &from_sz, &pgm_err); //PRINT_ERR("pgm_status: %d", pgm_status); switch (pgm_status) { case PGM_IO_STATUS_TIMER_PENDING: { socklen_t optlen = sizeof(tv); pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_TIME_REMAIN, &tv, &optlen); if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000)) break; goto block; } case PGM_IO_STATUS_RATE_LIMITED: { socklen_t optlen = sizeof(tv); pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen); if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000)) break; /* No accidental fallthrough! */ } block: case PGM_IO_STATUS_WOULD_BLOCK: FD_ZERO(&readfds); pgm_select_info(pgm_sock, &readfds, NULL, &fds); fds = select(fds, &readfds, NULL, NULL, pgm_status == PGM_IO_STATUS_WOULD_BLOCK ? NULL : &tv); break; default : if (pgm_err) { fprintf(stderr, "%s\n", pgm_err->message); pgm_error_free(pgm_err); pgm_err = NULL; } break; } pthread_mutex_lock(&m_pftp_mutex); run_receiver = m_run_receiver; pthread_mutex_unlock(&m_pftp_mutex); } pthread_exit(NULL); }
int main ( int argc, char* argv[] ) { int e; pgm_error_t* pgm_err = NULL; setlocale (LC_ALL, ""); log_init (); g_message ("syncrecv"); if (!pgm_init (&pgm_err)) { g_error ("Unable to start PGM engine: %s", pgm_err->message); pgm_error_free (pgm_err); return EXIT_FAILURE; } /* parse program arguments */ const char* binary_name = strrchr (argv[0], '/'); int c; while ((c = getopt (argc, argv, "s:n:p:lh")) != -1) { switch (c) { case 'n': g_network = optarg; break; case 's': g_port = atoi (optarg); break; case 'p': g_udp_encap_port = atoi (optarg); break; case 'l': g_multicast_loop = TRUE; break; case 'h': case '?': usage (binary_name); } } g_quit = FALSE; #ifdef G_OS_UNIX e = pipe (g_quit_pipe); #else e = _pipe (g_quit_pipe, 4096, _O_BINARY | _O_NOINHERIT); #endif g_assert (0 == e); /* setup signal handlers */ signal(SIGSEGV, on_sigsegv); signal(SIGINT, on_signal); signal(SIGTERM, on_signal); #ifdef SIGHUP signal(SIGHUP, SIG_IGN); #endif if (!on_startup()) { g_error ("startup failed"); exit(1); } /* dispatch loop */ g_message ("entering PGM message loop ... "); do { struct timeval tv; int timeout; int n_fds = 2; struct pollfd fds[ 1 + n_fds ]; char buffer[4096]; gsize len; pgm_tsi_t from; const int status = pgm_recvfrom (g_transport, buffer, sizeof(buffer), 0, &len, &from, &pgm_err); switch (status) { case PGM_IO_STATUS_NORMAL: on_data (buffer, len, &from); break; case PGM_IO_STATUS_TIMER_PENDING: pgm_transport_get_timer_pending (g_transport, &tv); goto block; case PGM_IO_STATUS_RATE_LIMITED: pgm_transport_get_rate_remaining (g_transport, &tv); case PGM_IO_STATUS_WOULD_BLOCK: /* poll for next event */ block: timeout = PGM_IO_STATUS_WOULD_BLOCK == status ? -1 : ((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); memset (fds, 0, sizeof(fds)); fds[0].fd = g_quit_pipe[0]; fds[0].events = POLLIN; pgm_transport_poll_info (g_transport, &fds[1], &n_fds, POLLIN); poll (fds, 1 + n_fds, timeout /* ms */); break; default: if (pgm_err) { g_warning ("%s", pgm_err->message); pgm_error_free (pgm_err); pgm_err = NULL; } if (PGM_IO_STATUS_ERROR == status) break; } } while (!g_quit); g_message ("message loop terminated, cleaning up."); /* cleanup */ close (g_quit_pipe[0]); close (g_quit_pipe[1]); if (g_transport) { g_message ("destroying transport."); pgm_transport_destroy (g_transport, TRUE); g_transport = NULL; } g_message ("PGM engine shutdown."); pgm_shutdown (); g_message ("finished."); return EXIT_SUCCESS; }
int main ( int argc, char* argv[] ) { pgm_error_t* pgm_err = NULL; setlocale (LC_ALL, ""); #ifndef _WIN32 puts ("プリン プリン"); #else _putws (L"プリン プリン"); #endif if (!pgm_init (&pgm_err)) { fprintf (stderr, "Unable to start PGM engine: %s\n", pgm_err->message); pgm_error_free (pgm_err); return EXIT_FAILURE; } /* parse program arguments */ const char* binary_name = strrchr (argv[0], '/'); int c; while ((c = getopt (argc, argv, "s:n:p:f:K:N:lih")) != -1) { switch (c) { case 'n': network = optarg; break; case 's': port = atoi (optarg); break; case 'p': udp_encap_port = atoi (optarg); break; case 'f': use_fec = TRUE; break; case 'K': rs_k = atoi (optarg); break; case 'N': rs_n = atoi (optarg); break; case 'l': use_multicast_loop = TRUE; break; case 'i': pgm_if_print_all(); return EXIT_SUCCESS; case 'h': case '?': usage (binary_name); } } if (use_fec && ( !rs_n || !rs_k )) { fprintf (stderr, "Invalid Reed-Solomon parameters RS(%d,%d).\n", rs_n, rs_k); usage (binary_name); } /* setup signal handlers */ #ifdef SIGHUP signal (SIGHUP, SIG_IGN); #endif #ifndef _WIN32 int e = pipe (terminate_pipe); assert (0 == e); signal (SIGINT, on_signal); signal (SIGTERM, on_signal); #else terminate_event = CreateEvent (NULL, TRUE, FALSE, TEXT("TerminateEvent")); SetConsoleCtrlHandler ((PHANDLER_ROUTINE)on_console_ctrl, TRUE); #endif /* !_WIN32 */ if (!on_startup()) { fprintf (stderr, "Startup failed\n"); return EXIT_FAILURE; } /* dispatch loop */ #ifndef _WIN32 int fds; fd_set readfds; #else int n_handles = 3, recv_sock, pending_sock; HANDLE waitHandles[ 3 ]; DWORD dwTimeout, dwEvents; WSAEVENT recvEvent, pendingEvent; recvEvent = WSACreateEvent (); pgm_getsockopt (sock, PGM_RECV_SOCK, &recv_sock, sizeof(recv_sock)); WSAEventSelect (recv_sock, recvEvent, FD_READ); pendingEvent = WSACreateEvent (); pgm_getsockopt (sock, PGM_PENDING_SOCK, &pending_sock, sizeof(pending_sock)); WSAEventSelect (pending_sock, pendingEvent, FD_READ); waitHandles[0] = terminate_event; waitHandles[1] = recvEvent; waitHandles[2] = pendingEvent; #endif /* !_WIN32 */ puts ("Entering PGM message loop ... "); do { struct timeval tv; char buffer[4096]; size_t len; pgm_tsi_t from; const int status = pgm_recvfrom (sock, buffer, sizeof(buffer), 0, &len, &from, &pgm_err); switch (status) { case PGM_IO_STATUS_NORMAL: on_data (buffer, len, &from); break; case PGM_IO_STATUS_TIMER_PENDING: pgm_getsockopt (sock, PGM_TIME_REMAIN, &tv, sizeof(tv)); goto block; case PGM_IO_STATUS_RATE_LIMITED: pgm_getsockopt (sock, PGM_RATE_REMAIN, &tv, sizeof(tv)); case PGM_IO_STATUS_WOULD_BLOCK: /* select for next event */ block: #ifndef _WIN32 fds = terminate_pipe[0] + 1; FD_ZERO(&readfds); FD_SET(terminate_pipe[0], &readfds); pgm_select_info (sock, &readfds, NULL, &fds); fds = select (fds, &readfds, NULL, NULL, PGM_IO_STATUS_WOULD_BLOCK == status ? NULL : &tv); #else dwTimeout = PGM_IO_STATUS_WOULD_BLOCK == status ? INFINITE : (DWORD)((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); dwEvents = WaitForMultipleObjects (n_handles, waitHandles, FALSE, dwTimeout); switch (dwEvents) { case WAIT_OBJECT_0+1: WSAResetEvent (recvEvent); break; case WAIT_OBJECT_0+2: WSAResetEvent (pendingEvent); break; default: break; } #endif /* !_WIN32 */ break; default: if (pgm_err) { fprintf (stderr, "%s\n", pgm_err->message); pgm_error_free (pgm_err); pgm_err = NULL; } if (PGM_IO_STATUS_ERROR == status) break; } } while (!is_terminated); puts ("Message loop terminated, cleaning up."); /* cleanup */ #ifndef _WIN32 close (terminate_pipe[0]); close (terminate_pipe[1]); #else WSACloseEvent (recvEvent); WSACloseEvent (pendingEvent); CloseHandle (terminate_event); #endif /* !_WIN32 */ if (sock) { puts ("Destroying PGM socket."); pgm_close (sock, TRUE); sock = NULL; } puts ("PGM engine shutdown."); pgm_shutdown (); puts ("finished."); return EXIT_SUCCESS; }
int main ( int argc, char* argv[] ) { pgm_error_t* pgm_err = NULL; setlocale (LC_ALL, ""); log_init (); g_message ("blocksyncrecv"); if (!pgm_init (&pgm_err)) { g_error ("Unable to start PGM engine: %s", pgm_err->message); pgm_error_free (pgm_err); return EXIT_FAILURE; } /* parse program arguments */ const char* binary_name = strrchr (argv[0], '/'); int c; while ((c = getopt (argc, argv, "s:n:p:lh")) != -1) { switch (c) { case 'n': g_network = optarg; break; case 's': g_port = atoi (optarg); break; case 'p': g_udp_encap_port = atoi (optarg); break; case 'l': g_multicast_loop = TRUE; break; case 'h': case '?': usage (binary_name); } } /* setup signal handlers */ signal(SIGSEGV, on_sigsegv); #ifdef SIGHUP signal(SIGHUP, SIG_IGN); #endif #ifdef G_OS_UNIX signal(SIGINT, on_signal); signal(SIGTERM, on_signal); #else SetConsoleCtrlHandler ((PHANDLER_ROUTINE)on_console_ctrl, TRUE); setvbuf (stdout, (char *) NULL, _IONBF, 0); #endif on_startup(); /* dispatch loop */ g_message ("entering PGM message loop ... "); do { char buffer[4096]; size_t len; struct pgm_sockaddr_t from; socklen_t fromlen = sizeof(from); const int status = pgm_recvfrom (g_sock, buffer, sizeof(buffer), 0, &len, &from, &fromlen, &pgm_err); if (PGM_IO_STATUS_NORMAL == status) on_data (buffer, len, &from); else { if (pgm_err) { g_warning ("%s", pgm_err->message); pgm_error_free (pgm_err); pgm_err = NULL; } if (PGM_IO_STATUS_ERROR == status) break; } } while (!g_quit); g_message ("message loop terminated, cleaning up."); /* cleanup */ if (g_sock) { g_message ("closing PGM socket."); pgm_close (g_sock, TRUE); g_sock = NULL; } g_message ("PGM engine shutdown."); pgm_shutdown (); g_message ("finished."); return EXIT_SUCCESS; }
int main ( int argc, char* argv[] ) { pgm_error_t* pgm_err = NULL; setlocale (LC_ALL, ""); #if !defined(_WIN32) puts ("プリン プリン"); #else /* Windows consoles have incredibly limited Unicode support */ puts ("purin purin"); #endif if (!pgm_init (&pgm_err)) { fprintf (stderr, "Unable to start PGM engine: %s\n", pgm_err->message); pgm_error_free (pgm_err); return EXIT_FAILURE; } /* parse program arguments */ #ifdef _WIN32 const char* binary_name = strrchr (argv[0], '\\'); #else const char* binary_name = strrchr (argv[0], '/'); #endif if (NULL == binary_name) binary_name = argv[0]; else binary_name++; int c; while ((c = getopt (argc, argv, "s:n:p:cf:K:N:lih")) != -1) { switch (c) { case 'n': network = optarg; break; case 's': port = atoi (optarg); break; case 'p': udp_encap_port = atoi (optarg); break; case 'c': use_pgmcc = TRUE; break; case 'f': use_fec = TRUE; break; case 'K': rs_k = atoi (optarg); break; case 'N': rs_n = atoi (optarg); break; case 'l': use_multicast_loop = TRUE; break; case 'i': pgm_if_print_all(); return EXIT_SUCCESS; case 'h': case '?': usage (binary_name); } } if (use_fec && ( !rs_n || !rs_k )) { fprintf (stderr, "Invalid Reed-Solomon parameters RS(%d,%d).\n", rs_n, rs_k); usage (binary_name); } /* setup signal handlers */ #ifdef SIGHUP signal (SIGHUP, SIG_IGN); #endif #ifndef _WIN32 int e = pipe (terminate_pipe); assert (0 == e); signal (SIGINT, on_signal); signal (SIGTERM, on_signal); #else terminateEvent = WSACreateEvent(); SetConsoleCtrlHandler ((PHANDLER_ROUTINE)on_console_ctrl, TRUE); setvbuf (stdout, (char *) NULL, _IONBF, 0); #endif /* !_WIN32 */ if (!on_startup()) { fprintf (stderr, "Startup failed\n"); return EXIT_FAILURE; } /* dispatch loop */ #ifndef _WIN32 int fds; fd_set readfds; #else SOCKET recv_sock, pending_sock; DWORD cEvents = PGM_RECV_SOCKET_READ_COUNT + 1; WSAEVENT waitEvents[ PGM_RECV_SOCKET_READ_COUNT + 1 ]; socklen_t socklen = sizeof (SOCKET); waitEvents[0] = terminateEvent; waitEvents[1] = WSACreateEvent(); waitEvents[2] = WSACreateEvent(); assert (2 == PGM_RECV_SOCKET_READ_COUNT); pgm_getsockopt (sock, IPPROTO_PGM, PGM_RECV_SOCK, &recv_sock, &socklen); WSAEventSelect (recv_sock, waitEvents[1], FD_READ); pgm_getsockopt (sock, IPPROTO_PGM, PGM_PENDING_SOCK, &pending_sock, &socklen); WSAEventSelect (pending_sock, waitEvents[2], FD_READ); #endif /* !_WIN32 */ puts ("Entering PGM message loop ... "); do { struct timeval tv; #ifdef _WIN32 DWORD dwTimeout, dwEvents; #endif char buffer[4096]; size_t len; struct pgm_sockaddr_t from; socklen_t fromlen = sizeof (from); const int status = pgm_recvfrom (sock, buffer, sizeof(buffer), 0, &len, &from, &fromlen, &pgm_err); switch (status) { case PGM_IO_STATUS_NORMAL: on_data (buffer, len, &from); break; case PGM_IO_STATUS_TIMER_PENDING: { socklen_t optlen = sizeof (tv); pgm_getsockopt (sock, IPPROTO_PGM, PGM_TIME_REMAIN, &tv, &optlen); } goto block; case PGM_IO_STATUS_RATE_LIMITED: { socklen_t optlen = sizeof (tv); pgm_getsockopt (sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen); } case PGM_IO_STATUS_WOULD_BLOCK: /* select for next event */ block: #ifndef _WIN32 fds = terminate_pipe[0] + 1; FD_ZERO(&readfds); FD_SET(terminate_pipe[0], &readfds); pgm_select_info (sock, &readfds, NULL, &fds); fds = select (fds, &readfds, NULL, NULL, PGM_IO_STATUS_WOULD_BLOCK == status ? NULL : &tv); #else dwTimeout = PGM_IO_STATUS_WOULD_BLOCK == status ? WSA_INFINITE : (DWORD)((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); dwEvents = WSAWaitForMultipleEvents (cEvents, waitEvents, FALSE, dwTimeout, FALSE); switch (dwEvents) { case WSA_WAIT_EVENT_0+1: WSAResetEvent (waitEvents[1]); break; case WSA_WAIT_EVENT_0+2: WSAResetEvent (waitEvents[2]); break; default: break; } #endif /* !_WIN32 */ break; default: if (pgm_err) { fprintf (stderr, "%s\n", pgm_err->message); pgm_error_free (pgm_err); pgm_err = NULL; } if (PGM_IO_STATUS_ERROR == status) break; } } while (!is_terminated); puts ("Message loop terminated, cleaning up."); /* cleanup */ #ifndef _WIN32 close (terminate_pipe[0]); close (terminate_pipe[1]); #else WSACloseEvent (waitEvents[0]); WSACloseEvent (waitEvents[1]); WSACloseEvent (waitEvents[2]); #endif /* !_WIN32 */ if (sock) { puts ("Destroying PGM socket."); pgm_close (sock, TRUE); sock = NULL; } puts ("PGM engine shutdown."); pgm_shutdown (); puts ("finished."); return EXIT_SUCCESS; }
int main( int argc, char *argv[]) { int c = 0; int port = 0; int run_state = STATE_WAIT_CMD; char network[128]; char *iface = NULL; char *mcast_addr = NULL; network[0] = '\0'; pgm_error_t* pgm_err = NULL; pgm_sock_t *pgm_sock = NULL; signal(SIGINT, handle_sigint); while((c = getopt(argc, argv, "hm:i:p:")) != -1) { switch (c) { case 'm': mcast_addr = optarg; break; case 'i': iface = optarg; break; case 'p': port = atoi(optarg); break; case 'h': case '?': print_usage(); break; default: print_usage(); break; } } #ifdef UDPTEST int sockfd = 0; struct sockaddr_in servaddr; struct ip_mreqn mreq; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = port ? htons(port) : htons(PFTP_UDP_PORT); if (0 != bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) ) { PRINT_ERR("Couldn't bind socket!"); return -1; } memset(&mreq, 0, sizeof(mreq)); char *ifaddr = NULL; if (iface) { ifaddr = pftp_inet_iftoa(iface); } else { ifaddr = pftp_inet_iftoa("eth0"); } mreq.imr_address.s_addr = inet_addr(ifaddr); mreq.imr_ifindex = 0; if (ifaddr) free(ifaddr); if (mcast_addr) { mreq.imr_multiaddr.s_addr = inet_addr(mcast_addr); } else { mreq.imr_multiaddr.s_addr = inet_addr(PFTP_DEFAULT_MCAST_ADDR); } if (0 != setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) { PRINT_ERR("Couldn't join multicast group!: %s" ,strerror(errno)); } #else if (iface) { char *ifaddr = pftp_inet_iftoa(iface); if (strlen(ifaddr) > 0) { strncat(network, ifaddr, 128); strncat(network, ";", 128); } else { free(ifaddr); return -1; } free(ifaddr); } if (mcast_addr) { strncat(network, mcast_addr, 128); } else { strncat(network, PFTP_DEFAULT_MCAST_ADDR, 128); } if (0 != pftp_create(0, network, port, &pgm_sock)) { return -1; } #endif PRINT_DBG("Running receive loop"); int fds = 0; fd_set readfds; int file_desc = -1; int total_bytes = 0; void *cmd_info = NULL; m_run_receive_loop = 1; while (m_run_receive_loop) { char recv_buf[PGMBUF_SIZE]; memset(&recv_buf, 0, PGMBUF_SIZE); size_t bytes_read = 0; #ifdef UDPTEST fds = sockfd + 1; bytes_read = recv(sockfd, recv_buf, PGMBUF_SIZE, MSG_DONTWAIT); switch (bytes_read) { case -1: if (errno == EWOULDBLOCK || errno == EAGAIN) { FD_ZERO(&readfds); FD_SET(sockfd, &readfds); select(fds, &readfds, NULL, NULL, NULL); } break; default: { #else struct timeval tv; struct pgm_sockaddr_t from; socklen_t from_sz = sizeof(from); const int pgm_status = pgm_recvfrom(pgm_sock, recv_buf, PGMBUF_SIZE, MSG_DONTWAIT, &bytes_read, &from, &from_sz, &pgm_err); //PRINT_DBG("pgm_status: %d", pgm_status); switch (pgm_status) { case PGM_IO_STATUS_NORMAL : { #endif int cmd_parsed = pftp_parse_cmd(recv_buf, &cmd_info); switch(cmd_parsed) { case PFTP_CMD_UNKNOWN : if ( run_state == STATE_FILE_TRANSFER && file_desc > 0 ) { write(file_desc, recv_buf, bytes_read); total_bytes += bytes_read; if (total_bytes >= ((struct pftp_send_file_cmd*)cmd_info)->fsize) { PRINT_DBG("File %s received.", ((struct pftp_send_file_cmd*)cmd_info)->fname); close(file_desc); total_bytes = 0; pftp_free_cmd(cmd_info); run_state = STATE_WAIT_CMD; } } break; case PFTP_CMD_SEND_FILE_PARSED : { struct pftp_send_file_cmd *sf_cmd = (struct pftp_send_file_cmd *)cmd_info; file_desc = open(sf_cmd->fname, O_CREAT | O_WRONLY, S_IRWXU); if (file_desc < 0) { PRINT_ERR("Couldn't open file for writing! %s", strerror(errno)); pftp_free_cmd(cmd_info); run_state = STATE_WAIT_CMD; } else { run_state = STATE_FILE_TRANSFER; } } break; default : pftp_free_cmd(cmd_info); break; } #ifdef UDPTEST } break; } #else } break; case PGM_IO_STATUS_TIMER_PENDING: { socklen_t optlen = sizeof(tv); pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_TIME_REMAIN, &tv, &optlen); if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000)) break; goto block; } case PGM_IO_STATUS_RATE_LIMITED: { socklen_t optlen = sizeof(tv); pgm_getsockopt (pgm_sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen); if (0 == (tv.tv_sec * 1000) + ((tv.tv_usec + 500) / 1000)) break; /* No accidental fallthrough! */ } case PGM_IO_STATUS_WOULD_BLOCK: block: FD_ZERO(&readfds); pgm_select_info(pgm_sock, &readfds, NULL, &fds); fds = select(fds, &readfds, NULL, NULL, pgm_status == PGM_IO_STATUS_WOULD_BLOCK ? NULL : &tv); break; case PGM_IO_STATUS_RESET: if (pgm_err) { fprintf(stderr, "%s\n", pgm_err->message); pgm_error_free(pgm_err); pgm_err = NULL; } close(file_desc); total_bytes = 0; run_state = STATE_WAIT_CMD; break; default : if (pgm_err) { fprintf(stderr, "%s\n", pgm_err->message); pgm_error_free(pgm_err); pgm_err = NULL; } if (pgm_status == PGM_IO_STATUS_ERROR) { m_run_receive_loop = 0; } else { PRINT_ERR("Unknown status!"); m_run_receive_loop = 0; } break; } #endif } if (file_desc > 0) close(file_desc); PRINT_DBG("Receive loop finished"); #ifdef UDPTEST if (sockfd > 0) close(sockfd); #else if (pgm_sock) { pftp_stop(pgm_sock); } #endif return 0; }
void PGMNetwork::doWork() { /* dispatch loop */ #ifndef _WIN32 int fds; fd_set readfds; #else SOCKET recv_sock, pending_sock; DWORD cEvents = PGM_RECV_SOCKET_READ_COUNT + 1; WSAEVENT waitEvents[ PGM_RECV_SOCKET_READ_COUNT + 1 ]; socklen_t socklen = sizeof (SOCKET); waitEvents[0] = terminateEvent; waitEvents[1] = WSACreateEvent(); waitEvents[2] = WSACreateEvent(); assert (2 == PGM_RECV_SOCKET_READ_COUNT); pgm_getsockopt (sock, IPPROTO_PGM, PGM_RECV_SOCK, &recv_sock, &socklen); WSAEventSelect (recv_sock, waitEvents[1], FD_READ); pgm_getsockopt (sock, IPPROTO_PGM, PGM_PENDING_SOCK, &pending_sock, &socklen); WSAEventSelect (pending_sock, waitEvents[2], FD_READ); static WSAEVENT terminateEvent; #endif /* !_WIN32 */ pgm_error_t* pgm_err = NULL; struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; qDebug() << "Entering PGM message loop..." << QThread::currentThreadId(); do { #ifdef _WIN32 DWORD dwTimeout, dwEvents; #endif char buffer[4096]; size_t len; struct pgm_sockaddr_t from; socklen_t fromlen = sizeof (from); const int status = pgm_recvfrom (m_socket, buffer, 4096, 0, &len, &from, &fromlen, &pgm_err); switch (status) { case PGM_IO_STATUS_NORMAL: { QByteArray ba(buffer, len); emit rx(ba); break; } case PGM_IO_STATUS_TIMER_PENDING: { socklen_t optlen = sizeof (tv); pgm_getsockopt (m_socket, IPPROTO_PGM, PGM_TIME_REMAIN, &tv, &optlen); } goto block; case PGM_IO_STATUS_RATE_LIMITED: { socklen_t optlen = sizeof (tv); pgm_getsockopt (m_socket, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen); } case PGM_IO_STATUS_WOULD_BLOCK: /* select for next event */ block: #ifndef _WIN32 fds = m_terminate_pipe[0] + 1; FD_ZERO(&readfds); FD_SET(m_terminate_pipe[0], &readfds); pgm_select_info (m_socket, &readfds, NULL, &fds); fds = select (fds, &readfds, NULL, NULL, PGM_IO_STATUS_WOULD_BLOCK == status ? NULL : &tv); #else dwTimeout = PGM_IO_STATUS_WOULD_BLOCK == status ? WSA_INFINITE : (DWORD)((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); dwEvents = WSAWaitForMultipleEvents (cEvents, waitEvents, FALSE, dwTimeout, FALSE); switch (dwEvents) { case WSA_WAIT_EVENT_0+1: WSAResetEvent (waitEvents[1]); break; case WSA_WAIT_EVENT_0+2: WSAResetEvent (waitEvents[2]); break; default: break; } #endif /* !_WIN32 */ break; default: if (pgm_err) { qDebug() << "pgm error:" << pgm_err->message; pgm_error_free (pgm_err); pgm_err = NULL; } if (PGM_IO_STATUS_ERROR == status) break; } QCoreApplication::processEvents(); } while (!m_is_terminated); qDebug() << "Message loop terminated, cleaning up."; /* cleanup */ #ifndef _WIN32 close (m_terminate_pipe[0]); close (m_terminate_pipe[1]); #else WSACloseEvent (waitEvents[0]); WSACloseEvent (waitEvents[1]); WSACloseEvent (waitEvents[2]); #endif /* !_WIN32 */ this->thread()->quit(); }