void NLSocket::start_receive() { socket_.async_receive_from (asio::buffer(recv_buffer_), remote_endpoint_, [this](const std::error_code &error, std::size_t bytes_xferred) { process_receive(bytes_xferred, 0, 0); start_receive(); }); }
void NLSocket::request_addrs() { int fd = socket_.native(); auto addr_seq = nlseq_++; if (nl_sendgetaddrs(fd, addr_seq) < 0) { fmt::print(stderr, "nlsocket: failed to get initial rtaddr state\n"); std::exit(EXIT_FAILURE); } std::size_t bytes_xferred; std::error_code ec; while ((bytes_xferred = socket_.receive(asio::buffer(recv_buffer_), 0, ec))) process_receive(bytes_xferred, addr_seq, 0); }
void fddi_isr(struct s_smc *smc) { u_long is ; /* ISR source */ u_short stu, stl ; SMbuf *mb ; #ifdef USE_BREAK_ISR int force_irq ; #endif #ifdef ODI2 if (smc->os.hwm.rx_break) { mac_drv_fill_rxd(smc) ; if (smc->hw.fp.rx_q[QUEUE_R1].rx_used > 0) { smc->os.hwm.rx_break = 0 ; process_receive(smc) ; } else { smc->os.hwm.detec_count = 0 ; smt_force_irq(smc) ; } } #endif smc->os.hwm.isr_flag = TRUE ; #ifdef USE_BREAK_ISR force_irq = TRUE ; if (smc->os.hwm.leave_isr) { smc->os.hwm.leave_isr = FALSE ; process_receive(smc) ; } #endif while ((is = GET_ISR() & ISR_MASK)) { NDD_TRACE("CH0B",is,0,0) ; DB_GEN("ISA = 0x%x",is,0,7) ; if (is & IMASK_SLOW) { NDD_TRACE("CH1b",is,0,0) ; if (is & IS_PLINT1) { /* PLC1 */ plc1_irq(smc) ; } if (is & IS_PLINT2) { /* PLC2 */ plc2_irq(smc) ; } if (is & IS_MINTR1) { /* FORMAC+ STU1(U/L) */ stu = inpw(FM_A(FM_ST1U)) ; stl = inpw(FM_A(FM_ST1L)) ; DB_GEN("Slow transmit complete",0,0,6) ; mac1_irq(smc,stu,stl) ; } if (is & IS_MINTR2) { /* FORMAC+ STU2(U/L) */ stu= inpw(FM_A(FM_ST2U)) ; stl= inpw(FM_A(FM_ST2L)) ; DB_GEN("Slow receive complete",0,0,6) ; DB_GEN("stl = %x : stu = %x",stl,stu,7) ; mac2_irq(smc,stu,stl) ; } if (is & IS_MINTR3) { /* FORMAC+ STU3(U/L) */ stu= inpw(FM_A(FM_ST3U)) ; stl= inpw(FM_A(FM_ST3L)) ; DB_GEN("FORMAC Mode Register 3",0,0,6) ; mac3_irq(smc,stu,stl) ; } if (is & IS_TIMINT) { /* Timer 82C54-2 */ timer_irq(smc) ; #ifdef NDIS_OS2 force_irq_pending = 0 ; #endif /* * out of RxD detection */ if (++smc->os.hwm.detec_count > 4) { /* * check out of RxD condition */ process_receive(smc) ; } } if (is & IS_TOKEN) { /* Restricted Token Monitor */ rtm_irq(smc) ; } if (is & IS_R1_P) { /* Parity error rx queue 1 */ /* clear IRQ */ outpd(ADDR(B4_R1_CSR),CSR_IRQ_CL_P) ; SMT_PANIC(smc,HWM_E0004,HWM_E0004_MSG) ; } if (is & IS_R1_C) { /* Encoding error rx queue 1 */ /* clear IRQ */ outpd(ADDR(B4_R1_CSR),CSR_IRQ_CL_C) ; SMT_PANIC(smc,HWM_E0005,HWM_E0005_MSG) ; } if (is & IS_XA_C) { /* Encoding error async tx q */ /* clear IRQ */ outpd(ADDR(B5_XA_CSR),CSR_IRQ_CL_C) ; SMT_PANIC(smc,HWM_E0006,HWM_E0006_MSG) ; } if (is & IS_XS_C) { /* Encoding error sync tx q */ /* clear IRQ */ outpd(ADDR(B5_XS_CSR),CSR_IRQ_CL_C) ; SMT_PANIC(smc,HWM_E0007,HWM_E0007_MSG) ; } } /* * Fast Tx complete Async/Sync Queue (BMU service) */ if (is & (IS_XS_F|IS_XA_F)) { DB_GEN("Fast tx complete queue",0,0,6) ; /* * clear IRQ, Note: no IRQ is lost, because * we always service both queues */ outpd(ADDR(B5_XS_CSR),CSR_IRQ_CL_F) ; outpd(ADDR(B5_XA_CSR),CSR_IRQ_CL_F) ; mac_drv_clear_txd(smc) ; llc_restart_tx(smc) ; } /* * Fast Rx Complete (BMU service) */ if (is & IS_R1_F) { DB_GEN("Fast receive complete",0,0,6) ; /* clear IRQ */ #ifndef USE_BREAK_ISR outpd(ADDR(B4_R1_CSR),CSR_IRQ_CL_F) ; process_receive(smc) ; #else process_receive(smc) ; if (smc->os.hwm.leave_isr) { force_irq = FALSE ; } else { outpd(ADDR(B4_R1_CSR),CSR_IRQ_CL_F) ; process_receive(smc) ; } #endif } #ifndef NDIS_OS2 while ((mb = get_llc_rx(smc))) { smt_to_llc(smc,mb) ; } #else if (offDepth) post_proc() ; while (!offDepth && (mb = get_llc_rx(smc))) { smt_to_llc(smc,mb) ; } if (!offDepth && smc->os.hwm.rx_break) { process_receive(smc) ; } #endif if (smc->q.ev_get != smc->q.ev_put) { NDD_TRACE("CH2a",0,0,0) ; ev_dispatcher(smc) ; } #ifdef NDIS_OS2 post_proc() ; if (offDepth) { /* leave fddi_isr because */ break ; /* indications not allowed */ } #endif #ifdef USE_BREAK_ISR if (smc->os.hwm.leave_isr) { break ; /* leave fddi_isr */ } #endif /* NOTE: when the isr is left, no rx is pending */ } /* end of interrupt source polling loop */ #ifdef USE_BREAK_ISR if (smc->os.hwm.leave_isr && force_irq) { smt_force_irq(smc) ; } #endif smc->os.hwm.isr_flag = FALSE ; NDD_TRACE("CH0E",0,0,0) ; }
int main(int argc, char *argv[]) { WSADATA wsa; struct sockaddr_in server, address; int max_clients = 30, activity, addrlen, i, valread; //size of our receive buffer, this is string length. int MAXRECV = 1024; //set of socket descriptors fd_set readfds; //1 extra for null character, string termination char *buffer; buffer = (char*)malloc((MAXRECV + 1) * sizeof(char)); for (i = 0; i < TOTAL_POSSIBLE_NEIGHBOURS; i++) { client_socket[i] = 0; } printf("\nInitialising Winsock..."); if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } printf("Initialised.\n"); //Create a socket if ((master = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { printf("Could not create socket : %d", WSAGetLastError()); exit(EXIT_FAILURE); } printf("Socket created.\n"); //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(MY_PORT); //Bind if (bind(master, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) { printf("Bind failed with error code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } puts("Bind done"); //Listen to incoming connections listen(master, 3); //Accept and incoming connection puts("Waiting for incoming connections...\n\n"); addrlen = sizeof(struct sockaddr_in); extend_network(bootstrap_ip, port, activeNeighbours); myTimer(hTimerQueue); while (TRUE) { //clear the socket fd set FD_ZERO(&readfds); //add master socket to fd set FD_SET(master, &readfds); //add child sockets to fd set for (i = 0; i < 5; i++) { s = client_socket[i]; if (s > 0) { FD_SET(s, &readfds); } } //wait for an activity on any of the sockets, timeout is NULL , so wait indefinitely activity = select(0, &readfds, NULL, NULL, NULL); if (activity == SOCKET_ERROR) { printf("[activity] select call failed with error code : %d", WSAGetLastError()); getchar(); exit(EXIT_FAILURE); } //If something happened on the master socket , then its an incoming connection if (FD_ISSET(master, &readfds)) { if ((new_socket = accept(master, (struct sockaddr *)&address, (int *)&addrlen)) < 0) { perror("accept"); getchar(); exit(EXIT_FAILURE); } //inform user of socket number - used in send and receive commands printf("New connection , socket fd is %d , ip is : %s , port : %d \n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port)); //add new socket to array of sockets for (i = 0; i < max_clients; i++) { if (client_socket[i] == 0) { client_socket[i] = new_socket; printf("Adding to list of sockets at index %d \n", i); break; } } } //else its some IO operation on some other socket :) for (i = 0; i < max_clients; i++) { s = client_socket[i]; //if client presend in read sockets if (FD_ISSET(s, &readfds)) { //get details of the client getpeername(s, (struct sockaddr*)&address, (int*)&addrlen); //Check if it was for closing , and also read the incoming message //recv does not place a null terminator at the end of the string (whilst printf %s assumes there is one). valread = recv(s, buffer, MAXRECV, 0); if (valread == SOCKET_ERROR) { int error_code = WSAGetLastError(); if (error_code == WSAECONNRESET) { //Somebody disconnected , get his details and print printf("Host disconnected unexpectedly , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); //removeFromNeighboursList(i, inet_ntoa(address.sin_addr)); //Close the socket and mark as 0 in list for reuse closesocket(s); client_socket[i] = 0; } else { printf("recv failed with error code : %d", error_code); } } if (valread == 0) { //Somebody disconnected , get his details and print printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); //removeFromNeighboursList(i, inet_ntoa(address.sin_addr)); //Close the socket and mark as 0 in list for reuse closesocket(s); client_socket[i] = 0; } //Echo back the message that came in else { //add null character, if you want to use with printf/puts or other string handling functions process_receive(buffer, i); } } } } if (WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0) printf("WaitForSingleObject failed (%d)\n", GetLastError()); closesocket(s); WSACleanup(); getchar(); return 0; }