示例#1
0
int main(void)
{
	int s;
	int i;
	int ret;
        fd_set fds;
	struct clients cli;

	client_init(&cli);
	s = socket_setup(SOCK_PATH);
	cli.self = s;
	client_add(&cli, s);

	for (;;) {
		fds = cli.set;
		if (select(cli.max + 1, &fds, NULL, NULL, NULL) == -1) {
		    perror("select");
		    exit(1);
		}

		for (i = 0; i <= cli.max; i++) {
			if (!FD_ISSET(i, &fds))
				continue;

			if (i == cli.self) {
				client_accept(&cli, s);
				continue;
			}
			client_handle_request(&cli, i);
		}
	}

	return 0;
}
示例#2
0
// Accept a connection.  Create the client object, and then attach the socket handle to it.
static void accept_conn_cb(
	struct evconnlistener *listener,
	evutil_socket_t fd,
	struct sockaddr *address,
	int socklen,
	void *ctx)
{
	client_t *client;

	assert(listener);
	assert(fd > 0);
	assert(address && socklen > 0);
	assert(ctx == NULL);

	// create client object.
	// TODO: We should be pulling these client objects out of a mempool.
	client = client_new();
	client_accept(client, fd, address, socklen);

	_conncount ++;
}
示例#3
0
文件: main.c 项目: JayceYang/usbmuxd
static int main_loop(int listenfd)
{
	int to, cnt, i, dto;
	struct fdlist pollfds;
	struct timespec tspec;

	sigset_t empty_sigset;
	sigemptyset(&empty_sigset); // unmask all signals

	fdlist_create(&pollfds);
	while(!should_exit) {
		usbmuxd_log(LL_FLOOD, "main_loop iteration");
		to = usb_get_timeout();
		usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to);
		dto = device_get_timeout();
		usbmuxd_log(LL_FLOOD, "Device timeout is %d ms", dto);
		if(dto < to)
			to = dto;

		fdlist_reset(&pollfds);
		fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN);
		usb_get_fds(&pollfds);
		client_get_fds(&pollfds);
		usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count);

		tspec.tv_sec = to / 1000;
		tspec.tv_nsec = (to % 1000) * 1000000;
		cnt = ppoll(pollfds.fds, pollfds.count, &tspec, &empty_sigset);
		usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt);
		if(cnt == -1) {
			if(errno == EINTR) {
				if(should_exit) {
					usbmuxd_log(LL_INFO, "Event processing interrupted");
					break;
				}
				if(should_discover) {
					should_discover = 0;
					usbmuxd_log(LL_INFO, "Device discovery triggered");
					usb_discover();
				}
			}
		} else if(cnt == 0) {
			if(usb_process() < 0) {
				usbmuxd_log(LL_FATAL, "usb_process() failed");
				fdlist_free(&pollfds);
				return -1;
			}
			device_check_timeouts();
		} else {
			int done_usb = 0;
			for(i=0; i<pollfds.count; i++) {
				if(pollfds.fds[i].revents) {
					if(!done_usb && pollfds.owners[i] == FD_USB) {
						if(usb_process() < 0) {
							usbmuxd_log(LL_FATAL, "usb_process() failed");
							fdlist_free(&pollfds);
							return -1;
						}
						done_usb = 1;
					}
					if(pollfds.owners[i] == FD_LISTEN) {
						if(client_accept(listenfd) < 0) {
							usbmuxd_log(LL_FATAL, "client_accept() failed");
							fdlist_free(&pollfds);
							return -1;
						}
					}
					if(pollfds.owners[i] == FD_CLIENT) {
						client_process(pollfds.fds[i].fd, pollfds.fds[i].revents);
					}
				}
			}
		}
	}
	fdlist_free(&pollfds);
	return 0;
}
示例#4
0
int main(int argc, char *argv[]) 
{
	int servSock, client_sock;
	int port = 8084;
	struct sockaddr_in addr;
	pid_t pid;
	unsigned int process_count = 0;
	int reuse_addr = 1;
	char *local_ip = NULL;
	char *remote_ip = NULL;
	char *signal_port_name = NULL;

	if (argc > 0) {
		local_ip = argv[1];
	}
	if (argc > 1) {
		remote_ip = argv[2];
	}
	if (argc > 2) {
		signal_port_name = argv[3];
	}

	if (!local_ip) {
		local_ip = "127.0.0.1";
	}

	if (!remote_ip) {
		remote_ip = "127.0.0.1";
	}

	if (signal_port_name) {
		port = atoi(signal_port_name);
	}
	
	if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		die("Socket Error!\n");
	}
	setsockopt(servSock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
			   
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);
	
    if (bind(servSock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		die("Bind Error!\n");
		return -1;
	}

    if (listen(servSock, MAXPENDING) < 0) {
		die("Listen error\n");
		return -1;
	}
	for (;;) {
		int local_port = use_port++;
		int remote_port = use_port++;

		if (use_port++ >= PORT_MAX) {
			use_port = PORT_MIN;
		}
		
		client_sock = client_accept(servSock);

		if ((pid = fork()) < 0) {
			die("fork() failed");
		} else if (pid == 0) {

			close(servSock);   

			client_run(client_sock, local_ip, local_port, remote_ip, remote_port);
			exit(0);           
		}

#if SOCKET2ME_DEBUG
		printf("with child process: %d\n", (int) pid);
#endif
		close(client_sock);       
		process_count++;      

		while (process_count) {
			pid = waitpid((pid_t) -1, NULL, WNOHANG);  
			if (pid < 0) {
				die("waitpid() failed");
			} else if (pid == 0) {
				break;
			} else {
				process_count--;  
			}
		}
	}
}