int mrpw_run_once(void) { struct netif_thread_data wpcap_pkt; struct ctl_thread_params localhost_pkt; uint16_t length = 0; uint8_t *payload; uint16_t protocol; uint8_t *proto; DWORD dwEvent = WaitForMultipleObjects(sizeof(pkt_events) / sizeof(HANDLE), pkt_events, FALSE, 100); /* 100ms wait */ /* special exit case */ if (WAIT_OBJECT_0 + app_event_kill_all == dwEvent) return -1; switch (dwEvent) { case WAIT_TIMEOUT: case WAIT_OBJECT_0 + loop_time_tick: /* timeout - run protocols */ if (mmrp_enable) { if (mrpd_timer_timeout(MMRP_db->mrp_db.lva_timer)) mmrp_event(MRP_EVENT_LVATIMER, NULL); if (mrpd_timer_timeout(MMRP_db->mrp_db.lv_timer)) mmrp_event(MRP_EVENT_LVTIMER, NULL); if (mrpd_timer_timeout(MMRP_db->mrp_db.join_timer)) mmrp_event(MRP_EVENT_TX, NULL); } if (mvrp_enable) { if (mrpd_timer_timeout(MVRP_db->mrp_db.lva_timer)) mvrp_event(MRP_EVENT_LVATIMER, NULL); if (mrpd_timer_timeout(MVRP_db->mrp_db.lv_timer)) mvrp_event(MRP_EVENT_LVTIMER, NULL); if (mrpd_timer_timeout(MVRP_db->mrp_db.join_timer)) mvrp_event(MRP_EVENT_TX, NULL); } if (msrp_enable) { if (mrpd_timer_timeout(MSRP_db->mrp_db.lva_timer)) msrp_event(MRP_EVENT_LVATIMER, NULL); if (mrpd_timer_timeout(MSRP_db->mrp_db.lv_timer)) msrp_event(MRP_EVENT_LVTIMER, NULL); if (mrpd_timer_timeout(MSRP_db->mrp_db.join_timer)) msrp_event(MRP_EVENT_TX, NULL); } if (mrpd_timer_timeout(periodic_timer)) { //printf("mrpd_timer_timeout(periodic_timer)\n"); if (mmrp_enable) mmrp_event(MRP_EVENT_PERIODIC, NULL); if (mvrp_enable) mvrp_event(MRP_EVENT_PERIODIC, NULL); if (msrp_enable) msrp_event(MRP_EVENT_PERIODIC, NULL); mrpd_timer_restart(periodic_timer); } if (mrpd_timer_timeout(gc_timer)) { mrpd_reclaim(); } mrpd_timer_restart(timer_check_tick); break; case WAIT_OBJECT_0 + pkt_event_wpcap: que_pop_nowait(que_wpcap, &wpcap_pkt); proto = &wpcap_pkt.frame[12]; protocol = (uint16_t) proto[0] << 8 | (uint16_t) proto[1]; payload = proto + 2; last_pdu_buffer = wpcap_pkt.frame; last_pdu_buffer_size = wpcap_pkt.length; switch (protocol) { case MVRP_ETYPE: if (mvrp_enable) mvrp_recv_msg(); break; case MMRP_ETYPE: if (mmrp_enable) mmrp_recv_msg(); break; case MSRP_ETYPE: if (msrp_enable) msrp_recv_msg(); break; } if (mrpd_timer_timeout(&timer_check_tick)) { if (!SetEvent(pkt_events[loop_time_tick])) { printf("SetEvent loop_time_tick failed (%d)\n", GetLastError()); exit(-1); } } break; case WAIT_OBJECT_0 + pkt_event_wpcap_timeout: //printf("pkt_event_wpcap_timeout\n"); break; case WAIT_OBJECT_0 + pkt_event_localhost: que_pop_nowait(que_localhost, &localhost_pkt); process_ctl_msg(localhost_pkt.msgbuf, localhost_pkt.bytes, (struct sockaddr_in *) &localhost_pkt.client_addr); if (mrpd_timer_timeout(&timer_check_tick)) { if (!SetEvent(pkt_events[loop_time_tick])) { printf("SetEvent loop_time_tick failed (%d)\n", GetLastError()); exit(-1); } } break; case WAIT_OBJECT_0 + pkt_event_localhost_timeout: //printf("pkt_event_localhost_timeout\n"); break; default: printf("Unknown event %d\n", dwEvent); } return 0; }
void process_events(void) { HANDLE hThread1, hThread2; DWORD dwThreadID1, dwThreadID2; uint16_t length = 0; uint8_t *payload; uint16_t protocol; uint8_t *proto; struct avb_avtpdu *avtpdu = NULL; uint64_t src_mac_address = 0; struct ctl_thread_params localhost_pkt; struct netif_thread_data wpcap_pkt; int i; timer_check_tick = mrpd_timer_create(); mrpd_timer_start_interval(timer_check_tick, 100, 100); que_wpcap = que_new(256, sizeof(struct netif_thread_data)); que_localhost = que_new(256, sizeof(struct ctl_thread_params)); sem_kill_wpcap_thread = CreateSemaphore(NULL, 0, 32767, NULL); sem_kill_localhost_thread = CreateSemaphore(NULL, 0, 32767, NULL); for (i = pkt_event_wpcap_timeout; i <= loop_time_tick; i++) { pkt_events[i] = CreateEvent(NULL, FALSE, FALSE, NULL); if (pkt_events[i] == NULL) { fprintf(stderr, "CreateEvent error: %d\n", GetLastError()); ExitProcess(0); } } pkt_events[pkt_event_wpcap] = que_data_available_object(que_wpcap); pkt_events[pkt_event_localhost] = que_data_available_object(que_localhost); // Create threads hThread1 = CreateThread(NULL, // default security attributes 0, // default stack size (LPTHREAD_START_ROUTINE) netif_thread, NULL, // no thread function arguments 0, // default creation flags &dwThreadID1); // receive thread identifier if (hThread1 == NULL) { fprintf(stderr, "CreateThread error: %d\n", GetLastError()); ExitProcess(0); } hThread2 = CreateThread(NULL, // default security attributes 0, // default stack size (LPTHREAD_START_ROUTINE) ctl_thread, NULL, // no thread function arguments 0, // default creation flags &dwThreadID2); // receive thread identifier if (hThread2 == NULL) { fprintf(stderr, "CreateThread error: %d\n", GetLastError()); ExitProcess(0); } while (1) { DWORD dwEvent = WaitForMultipleObjects(sizeof(pkt_events) / sizeof(HANDLE), pkt_events, FALSE, 100); /* 100ms wait */ /* special exit case */ if (WAIT_OBJECT_0 + app_event_kill_all == dwEvent) break; switch (dwEvent) { case WAIT_TIMEOUT: case WAIT_OBJECT_0 + loop_time_tick: /* timeout - run protocols */ if (mmrp_enable) { if (mrpd_timer_timeout(MMRP_db->mrp_db.lva_timer)) mmrp_event(MRP_EVENT_LVATIMER, NULL); if (mrpd_timer_timeout(MMRP_db->mrp_db.lv_timer)) mmrp_event(MRP_EVENT_LVTIMER, NULL); if (mrpd_timer_timeout(MMRP_db->mrp_db.join_timer)) mmrp_event(MRP_EVENT_TX, NULL); } if (mvrp_enable) { if (mrpd_timer_timeout(MVRP_db->mrp_db.lva_timer)) mvrp_event(MRP_EVENT_LVATIMER, NULL); if (mrpd_timer_timeout(MVRP_db->mrp_db.lv_timer)) mvrp_event(MRP_EVENT_LVTIMER, NULL); if (mrpd_timer_timeout(MVRP_db->mrp_db.join_timer)) mvrp_event(MRP_EVENT_TX, NULL); } if (msrp_enable) { if (mrpd_timer_timeout(MSRP_db->mrp_db.lva_timer)) msrp_event(MRP_EVENT_LVATIMER, NULL); if (mrpd_timer_timeout(MSRP_db->mrp_db.lv_timer)) msrp_event(MRP_EVENT_LVTIMER, NULL); if (mrpd_timer_timeout(MSRP_db->mrp_db.join_timer)) msrp_event(MRP_EVENT_TX, NULL); } if (mrpd_timer_timeout(periodic_timer)) { //printf("mrpd_timer_timeout(periodic_timer)\n"); if (mmrp_enable) mmrp_event(MRP_EVENT_PERIODIC, NULL); if (mvrp_enable) mvrp_event(MRP_EVENT_PERIODIC, NULL); if (msrp_enable) msrp_event(MRP_EVENT_PERIODIC, NULL); mrpd_timer_restart(periodic_timer); } if (mrpd_timer_timeout(gc_timer)) { mrpd_reclaim(); } mrpd_timer_restart(timer_check_tick); break; case WAIT_OBJECT_0 + pkt_event_wpcap: que_pop_nowait(que_wpcap, &wpcap_pkt); proto = &wpcap_pkt.frame[12]; protocol = (uint16_t) proto[0] << 8 | (uint16_t) proto[1]; payload = proto + 2; last_pdu_buffer = wpcap_pkt.frame; last_pdu_buffer_size = wpcap_pkt.length; switch (protocol) { case MVRP_ETYPE: if (mvrp_enable) mvrp_recv_msg(); break; case MMRP_ETYPE: if (mmrp_enable) mmrp_recv_msg(); break; case MSRP_ETYPE: if (msrp_enable) msrp_recv_msg(); break; } if (mrpd_timer_timeout(&timer_check_tick)) { if (!SetEvent(pkt_events[loop_time_tick])) { printf ("SetEvent loop_time_tick failed (%d)\n", GetLastError()); exit(-1); } } break; case WAIT_OBJECT_0 + pkt_event_wpcap_timeout: //printf("pkt_event_wpcap_timeout\n"); break; case WAIT_OBJECT_0 + pkt_event_localhost: que_pop_nowait(que_localhost, &localhost_pkt); process_ctl_msg(localhost_pkt.msgbuf, localhost_pkt.bytes, (struct sockaddr_in *) &localhost_pkt.client_addr); if (mrpd_timer_timeout(&timer_check_tick)) { if (!SetEvent(pkt_events[loop_time_tick])) { printf ("SetEvent loop_time_tick failed (%d)\n", GetLastError()); exit(-1); } } break; case WAIT_OBJECT_0 + pkt_event_localhost_timeout: //printf("pkt_event_localhost_timeout\n"); break; default: printf("Unknown event %d\n", dwEvent); } } }