int main (int argc, const char *argv[]) { int sock; int i = 0; if (argc >1){ ip = malloc(sizeof(char) * strlen(argv[1])); strcpy(ip,argv[1]); if (argc >2){ port = malloc(sizeof(char) * strlen(argv[2])); strcpy(port,argv[2]); if (argc >3){ wsName = malloc(sizeof(char) * strlen(argv[3])); strcpy(wsName,argv[3]); } } } fullDir = buildAddress(); trace(1, fullDir); memset(glob, 0, sizeof(*glob)); if (!create_listen_sock(FPM_DEFAULT_PORT, &glob->server_sock)) { exit(1); } /* * Server forever. */ while (1) { glob->sock = accept_conn(glob->server_sock); fpm_serve(); trace(1, "Done serving client"); } }
void arpModel() { int n; // puts("----->ARP: Ready for requests..."); while (1) { // rset = allset; FD_ZERO(&rset); FD_SET(un_listenfd, &rset); FD_SET(if_sockfd, &rset); maxfd = max(un_listenfd, if_sockfd); if (un_connfd != -1){ printf("Add un_connfd to fs_set %d\n", un_connfd); FD_SET(un_connfd, &rset); maxfd = (maxfd, un_connfd); } Select(maxfd + 1, &rset, NULL, NULL, NULL); if (FD_ISSET(un_listenfd, &rset)) { // puts("----->ARP: Request to establish domain socket connection."); accept_conn(un_listenfd); } if (un_connfd != -1 && FD_ISSET(un_connfd, &rset)) { struct hwaddr hwaddr_info; bzero(&hwaddr_info, sizeof(struct hwaddr)); if ((n = recv_un(un_connfd, &hwaddr_info)) > 0) { // puts("----->ARP: Incoming un_packet. Handing over..."); // printf("Requested IP is: %s\n", hwaddr_info.sll_ip); process_un(un_connfd, &hwaddr_info); } else if(n == 0) { puts("----->ARP: Time out."); close(un_connfd); un_connfd = -1; } } if (FD_ISSET(if_sockfd, &rset)) { struct arp_msg msg; bzero(&msg, sizeof(struct arp_msg)); if ((n = recv_arp(if_sockfd, &msg)) == 0) { // puts("*********MSG received***********"); // print_msg(&msg); if (msg.op == ARP_REQ) { // puts("----->ARP: Incoming PF_PACKET: ARP request. Handing over..."); process_arp_request(if_sockfd, &msg); } else if (msg.op == ARP_REP) { // puts("----->ARP: Incoming PF_PACKET: ARP reply. Handing over..."); process_arp_reply(if_sockfd, &msg); } } } } }
int main(int argc, char **argv) { int port = 34034; int sock; fd_set fdset; int nfds; printf("Usage: %s [PORT]\n", argv[0]); if (argc > 1) { port = atoi(argv[1]); if (port < 1) port = 1; if (port > 0xFFFF) port = 0xFFFF; } printf("PORT %d\n", port); sock = socket(PF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("Could not create socket"); return 1; } if (!bind_listen(sock, port)) { close(sock); return 1; } signal(SIGINT, sighandler); signal(SIGPIPE, sighandler); while (num_clients <= MAX_CLIENTS) { FD_ZERO(&fdset); FD_SET(sock, &fdset); nfds = sock+1; if (!num_clients) puts("Listening for connections..."); else nfds = fd_set_clients(&fdset, nfds); if (select(nfds, &fdset, 0,0,0) < 0 && errno != EINTR) { perror("select"); return 1; } read_from_clients(&fdset); if (FD_ISSET(sock, &fdset) && num_clients < MAX_CLIENTS) { accept_conn(sock); num_clients++; } } close(sock); return 0; }
static void ns_mgr_handle_connection(struct ns_connection *nc, int fd_flags, time_t now) { DBG(("%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, fd_flags, nc->flags, (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); if (fd_flags != 0) nc->last_io_time = now; if (nc->flags & NSF_CONNECTING) { if (fd_flags != 0) { ns_read_from_socket(nc); } return; } if (nc->flags & NSF_LISTENING) { /* * We're not looping here, and accepting just one connection at * a time. The reason is that eCos does not respect non-blocking * flag on a listening socket and hangs in a loop. */ if (fd_flags & _NSF_FD_CAN_READ) accept_conn(nc); return; } if (fd_flags & _NSF_FD_CAN_READ) { if (nc->flags & NSF_UDP) { ns_handle_udp(nc); } else { ns_read_from_socket(nc); } if (nc->flags & NSF_CLOSE_IMMEDIATELY) return; } if ((fd_flags & _NSF_FD_CAN_WRITE) && !(nc->flags & NSF_DONT_SEND) && !(nc->flags & NSF_UDP)) { /* Writes to UDP sockets are not buffered. */ ns_write_to_socket(nc); } if (!(fd_flags & (_NSF_FD_CAN_READ | _NSF_FD_CAN_WRITE))) { ns_call(nc, NS_POLL, &now); } DBG(("%p after fd=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, nc->flags, (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); }
int main(int argc, char *argv[]) { int server_sock; int client_sock; if (argc < 2) { errx(1, "Please enter socket parameter!"); } signal(SIGPIPE, SIG_IGN); setlinebuf(stdout); server_sock = create_listening_socket(argv[1]); do { client_sock = accept_conn(server_sock); } while (read_write_loop(client_sock, stdout) != -1); return (0); }
/* 接受另外一个神经元的连接 */ int Neuron::accept_conn(Neuron *neuron) { assert(neuron != NULL); return accept_conn(neuron->get_axone()); }
/* * This function performs the actual IO, and must be called in a loop * (an event loop). Returns the current timestamp. */ time_t ns_mgr_poll(struct ns_mgr *mgr, int milli) { struct ns_connection *nc, *tmp; struct timeval tv; fd_set read_set, write_set, err_set; sock_t max_fd = INVALID_SOCKET; time_t current_time = time(NULL); FD_ZERO(&read_set); FD_ZERO(&write_set); FD_ZERO(&err_set); ns_add_to_set(mgr->ctl[1], &read_set, &max_fd); for (nc = mgr->active_connections; nc != NULL; nc = tmp) { tmp = nc->next; if (!(nc->flags & (NSF_LISTENING | NSF_CONNECTING))) { ns_call(nc, NS_POLL, ¤t_time); } /* * NS_POLL handler could have signaled us to close the connection * by setting NSF_CLOSE_IMMEDIATELY flag. In this case, we don't want to * trigger any other events on that connection, but close it right away. */ if (nc->flags & NSF_CLOSE_IMMEDIATELY) { /* NOTE(lsm): this call removes nc from the mgr->active_connections */ ns_close_conn(nc); continue; } if (!(nc->flags & NSF_WANT_WRITE)) { /*DBG(("%p read_set", nc)); */ ns_add_to_set(nc->sock, &read_set, &max_fd); } if (((nc->flags & NSF_CONNECTING) && !(nc->flags & NSF_WANT_READ)) || (nc->send_iobuf.len > 0 && !(nc->flags & NSF_CONNECTING) && !(nc->flags & NSF_BUFFER_BUT_DONT_SEND))) { /*DBG(("%p write_set", nc)); */ ns_add_to_set(nc->sock, &write_set, &max_fd); ns_add_to_set(nc->sock, &err_set, &max_fd); } } tv.tv_sec = milli / 1000; tv.tv_usec = (milli % 1000) * 1000; if (select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv) > 0) { /* select() might have been waiting for a long time, reset current_time * now to prevent last_io_time being set to the past. */ current_time = time(NULL); /* Read wakeup messages */ if (mgr->ctl[1] != INVALID_SOCKET && FD_ISSET(mgr->ctl[1], &read_set)) { struct ctl_msg ctl_msg; int len = (int) recv(mgr->ctl[1], (char *) &ctl_msg, sizeof(ctl_msg), 0); send(mgr->ctl[1], ctl_msg.message, 1, 0); if (len >= (int) sizeof(ctl_msg.callback) && ctl_msg.callback != NULL) { struct ns_connection *c; for (c = ns_next(mgr, NULL); c != NULL; c = ns_next(mgr, c)) { ctl_msg.callback(c, NS_POLL, ctl_msg.message); } } } for (nc = mgr->active_connections; nc != NULL; nc = tmp) { tmp = nc->next; /* Windows reports failed connect() requests in err_set */ if (FD_ISSET(nc->sock, &err_set) && (nc->flags & NSF_CONNECTING)) { nc->last_io_time = current_time; ns_read_from_socket(nc); } if (FD_ISSET(nc->sock, &read_set)) { nc->last_io_time = current_time; if (nc->flags & NSF_LISTENING) { if (nc->flags & NSF_UDP) { ns_handle_udp(nc); } else { /* We're not looping here, and accepting just one connection at * a time. The reason is that eCos does not respect non-blocking * flag on a listening socket and hangs in a loop. */ accept_conn(nc); } } else { ns_read_from_socket(nc); } } if (FD_ISSET(nc->sock, &write_set)) { nc->last_io_time = current_time; if (nc->flags & NSF_CONNECTING) { ns_read_from_socket(nc); } else if (!(nc->flags & NSF_BUFFER_BUT_DONT_SEND) && !(nc->flags & NSF_CLOSE_IMMEDIATELY)) { ns_write_to_socket(nc); } } } } for (nc = mgr->active_connections; nc != NULL; nc = tmp) { tmp = nc->next; if ((nc->flags & NSF_CLOSE_IMMEDIATELY) || (nc->send_iobuf.len == 0 && (nc->flags & NSF_FINISHED_SENDING_DATA))) { ns_close_conn(nc); } } return current_time; }
static void main_loop(int serial_fd, int low_listener, int high_listener) { fd_set rdfds; int low_poll = 0, high_poll = 0, serial_poll = 0; int low_fd = -1, high_fd = -1; while(1) { char buf[1024]; ssize_t bytes; int max; FD_ZERO(&rdfds); FD_SET(low_fd < 0 ? low_listener : low_fd, &rdfds); FD_SET(high_fd < 0 ? high_listener : high_fd, &rdfds); FD_SET(serial_fd, &rdfds); max = MAX(low_fd, low_listener); max = MAX(max, high_fd); max = MAX(max, high_listener); max = MAX(max, serial_fd); if (select(max + 1, &rdfds, NULL, NULL, NULL) < 0) { perror("select"); continue; } if (FD_ISSET(low_listener, &rdfds)) { assert(low_fd < 0); low_fd = accept_conn(low_listener); } if (FD_ISSET(high_listener, &rdfds)) { assert(high_fd < 0); high_fd = accept_conn(high_listener); } if (low_poll || (low_fd >= 0 && FD_ISSET(low_fd, &rdfds))) { if ((bytes = receive_data(&low_fd, &buf[0], sizeof(buf), &low_poll)) > 0) { clear_high_bit(&buf[0], bytes); if (write(serial_fd, &buf[0], bytes) < 0) perror("write"); } } if (high_poll || (high_fd >= 0 && FD_ISSET(high_fd, &rdfds))) { if ((bytes = receive_data(&high_fd, &buf[0], sizeof(buf), &high_poll)) > 0) { set_high_bit(&buf[0], bytes); if (write(serial_fd, &buf[0], bytes) < 0) perror("write"); } } if (serial_poll || FD_ISSET(serial_fd, &rdfds)) { if ((bytes = receive_data(&serial_fd, &buf[0], sizeof(buf), &serial_poll)) > 0) { ssize_t i; for (i = 0; i < bytes; ++ i) { if (buf[i] & 0x80) { if (high_fd >= 0) { buf[i] &= 0x7f; if ((write(high_fd, &buf[i], 1)) < 0) { perror("write"); close_conn(&high_fd); high_poll = 0; } } } else { if (low_fd >= 0) { if ((write(low_fd, &buf[i], 1)) < 0) { perror("write"); close_conn(&low_fd); low_poll = 0; } } } } } } } }
int main(int argc, char *argv[]) { int stcp = -1; int sudp = -1; int rc = -1; if (argc != 2) { fprintf(stderr, "Usage: %s [port]\n", argv[0]); exit(1); } /* Limit memory usage by SQLite to a reasonable, relatively small level. */ #ifdef WWDEBUG printf("Current SQLite heap size limit is %ld. Setting it to 8MB.\n", sqlite3_soft_heap_limit(-1)); #endif sqlite3_soft_heap_limit(8*1024*1024); #ifdef WWDEBUG printf("New SQLite heap size limit is %ld.\n", sqlite3_soft_heap_limit(-1)); #endif /* Include this only if we are *not* compiling a debug version */ #ifndef WWDEBUG /* Fork and background (basically) */ pid_t pid = fork(); if (pid != 0) { if (pid < 0) { perror("AGGREGATOR: Failed to fork"); exit(1); } exit(0); } #endif bzero(sock_data, sizeof(sock_data)); // Get the database ready // Attempt to open database & check for failure #ifdef WWDEBUG printf("Attempting to open database: %s\n", SQLITE_DB_FNAME); #endif rc = sqlite3_open(SQLITE_DB_FNAME, &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); exit(1); } else { // Now check & create tables if required createTable(db, 1); createTable(db, 2); #ifdef WWDEBUG printf("Database ready for reading and writing...\n"); #endif } // Prepare to accept clients FD_ZERO(&rfds); FD_ZERO(&wfds); // Open TCP (SOCK_STREAM) & UDP (SOCK_DGRAM) sockets, bind to the port // given and listen on TCP sock for connections if ((setup_sockets(atoi(argv[1]), &stcp, &sudp)) < 0) { perror("WWAGGREGATOR: Error setting up Sockets\n"); exit(1); } #ifdef WWDEBUG printf("Our listen sock # is - %d & UDP sock # is - %d \n", stcp, sudp); #endif //printf("FD_SETSIZE - %d\n",FD_SETSIZE); //Add the created TCP & UDP sockets to the read set of file descriptors FD_SET(stcp, &rfds); FD_SET(sudp, &rfds); // Event loop while (1) { int n = 0; fd_set _rfds, _wfds; memcpy(&_rfds, &rfds, sizeof(fd_set)); memcpy(&_wfds, &wfds, sizeof(fd_set)); // Block until there's an event to handle // Select function call is made; return value 'n' gives the number of FDs ready to be serviced if (((n = select(FD_SETSIZE, &_rfds, &_wfds, NULL, 0)) < 0) && (errno != EINTR)) { perror("select"); exit(1); } // Handle events for (int i = 0; (i < FD_SETSIZE) && n; i++) { if (FD_ISSET(i, &_rfds)) { // Handle our main mother, TCP listening socket differently if (i == stcp) { if (accept_conn(stcp) < 0) exit(1); // Handle our UDP socket differently } else if (i == sudp) { read_and_dump_data(sudp); } else { #ifdef WWDEBUG fprintf(stderr, "File descriptor %d is ready for reading .. call'g readHLR\n", i); #endif read_handler(i); } n--; } if (FD_ISSET(i, &_wfds)) { #ifdef WWDEBUG fprintf(stderr, "File descriptor %d is ready for writing .. call'g writeHLR\n", i); #endif write_handler(i); n--; } } } }
void main_loop(int listen_socket) { int i, changed_fds; int kernel_ok, mps_ok, new_ctrl, new_shortcut; int poll_timeout = POLL_TIMEOUT; time_t now, previous_now; for (i = 0; i < OPEN_MAX; i++) fds[i].fd = -1; fds[0].fd = mpc_control.kernel_socket; /* mpcd <--> kernel socket */ fds[0].events = POLLIN; socket_type[0]= KERNEL; if(!mpc_control.mps_ctrl_addr_set) /* Can't do much without the MPS control ATM addr */ wait_for_mps_ctrl_addr(); connect_to_MPS(); fds[1].fd = mpc_control.MPS_socket; /* we opened this to MPS */ fds[1].events = POLLIN; socket_type[1]= (OUTGOING_CTRL | CONNECTED); fds[2].fd = mpc_control.MPS_listen_socket; /* for incoming control calls */ fds[2].events = POLLIN; socket_type[2]= LISTENING_CTRL; fds[3].fd = listen_socket; /* for incoming shortcuts */ fds[3].events = POLLIN; socket_type[3]= LISTENING_DATA; fds_used = first_empty = 4; now = previous_now = time(NULL); while (1) { kernel_ok = mps_ok = new_ctrl = new_shortcut = 1; fflush(stdout); #ifdef BROKEN_POLL changed_fds = poll2select(fds, fds_used, poll_timeout); #else changed_fds = poll(fds, fds_used, poll_timeout); #endif #if 0 printf("\nio.c: main_loop() poll returns %d\n", changed_fds); for (i = 0; i < OPEN_MAX; i++) { if (fds[i].fd < 0 ) continue; /* slot not in use */ if ( fds[i].revents == 0) { printf("check1: fd %d slot %d not changed\n", fds[i].fd, i); } else printf("check1: fd %d slot %d changed\n", fds[i].fd, i); } #endif switch(changed_fds) { case -1: printf("mpcd: io.c: main_loop: poll error: %s\n", strerror(errno)); if(errno == EINTR) continue; goto out; /* return to main() */ break; /* not reached */ case 0: keep_alive_sm(0, -1); /* (keepalive_liftime, seq_num) */ clear_expired(); /* id_list.c */ poll_timeout = POLL_TIMEOUT; previous_now = time(NULL); continue; break; /* not reached */ } /* It was not a timeout. Adjust poll_timeout */ now = time(NULL); poll_timeout -= now - previous_now; if (poll_timeout < 0) poll_timeout = 0; /* Since we are here something happened to the fds */ if (fds[0].revents) { dprintf("mpcd: io.c: main_loop() msg_from_kernel\n"); kernel_ok = msg_from_kernel(fds[0].fd); changed_fds--; } if (fds[1].revents) { ddprintf("mpcd: io.c: main_loop() msg_from_mps1\n"); mps_ok = msg_from_mps(1); changed_fds--; } if (fds[2].revents) { new_ctrl = accept_conn(2); changed_fds--; if( new_ctrl < 0 ) break; socket_type[new_ctrl] = INCOMING_CTRL | CONNECTED; dprintf("mpcd: io.c main_loop() accepted INCOMING_CTRL slot %d\n", new_ctrl); } if (fds[3].revents) { new_shortcut = accept_conn(3); dprintf("mpcd: io.c main_loop() accepted INCOMING_SHORTCUT slot %d\n", new_shortcut); changed_fds--; if( new_shortcut < 0 ) break; socket_type[new_shortcut] = INCOMING_SHORTCUT; if (add_shortcut(new_shortcut, MPC_SOCKET_EGRESS) < 0) break; } #if 0 if (changed_fds == 0) /* see if we can already go back to poll() */ continue; #endif for (i = first_empty; i < fds_used; i++) { if (fds[i].fd < 0 ) continue; /* slot not in use */ if ( fds[i].revents == 0) { ddprintf("fd %d slot %d not changed\n", fds[i].fd, i); continue; } ddprintf("about to process fd %d slot %d\n", fds[i].fd, i); if (socket_type[i] & INCOMING_CTRL) { ddprintf("mpcd: io.c: main_loop() msg_from_mps2\n"); mps_ok = msg_from_mps(i); } else { ddprintf("mpcd: io.c: main_loop() checking connection fd %d\n", fds[i].fd); if (check_connection(i) < 0) { printf("mpcd: io.c: main_loop: check_connection returned < 0\n"); break; /* this will cause break from while(1) too */ } } if (--changed_fds == 0) break; /* no more changed fds, leave for() */ } if (changed_fds != 0){ printf("mpcd: changed_fds = %d\n", changed_fds); /* break; */ /* leave while(1) */ } if (kernel_ok && mps_ok >= 0 && new_ctrl >= 0 && new_shortcut >= 0) continue; /* back to poll() */ else break; /* leave main_loop */ } out: /* clean up, close the sockets */ for (i = 0; i < fds_used; i++) { if (fds[i].fd < 0) continue; close(fds[i].fd); socket_type[i] = NOT_USED; } printf("mpcd: io.c: exiting main_loop()\n"); return; }