|
|
@@ -31,6 +31,7 @@
|
|
|
#include <sys/socket.h>
|
|
|
#include <netinet/in.h>
|
|
|
#include <arpa/inet.h>
|
|
|
+#include <poll.h>
|
|
|
|
|
|
#include <vcc/vcc.h>
|
|
|
#include <vcc/version.h>
|
|
|
@@ -47,6 +48,10 @@
|
|
|
/* this is the shortest name i think. it stores the fd of the server
|
|
|
* socket */
|
|
|
int fd;
|
|
|
+int next_fd;
|
|
|
+
|
|
|
+#define POLLFDS 1024
|
|
|
+struct pollfd *fds;
|
|
|
|
|
|
static int usage(void) {
|
|
|
fprintf(stderr, "Usage: vccd (no argument)\n");
|
|
|
@@ -54,7 +59,7 @@ static int usage(void) {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
-static int becomes_daemon(void) {
|
|
|
+/*static */int becomes_daemon(void) {
|
|
|
int pid;
|
|
|
|
|
|
if ((pid = fork()))
|
|
|
@@ -95,6 +100,7 @@ static int becomes_daemon(void) {
|
|
|
|
|
|
static int init_socket(void) {
|
|
|
struct sockaddr_in addr;
|
|
|
+ int i;
|
|
|
|
|
|
if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
|
|
|
perror("socket()");
|
|
|
@@ -120,43 +126,114 @@ static int init_socket(void) {
|
|
|
exit(1);
|
|
|
}
|
|
|
|
|
|
+ if (!(fds = malloc(sizeof(struct pollfd) * POLLFDS))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (i = 0; i < POLLFDS; i++)
|
|
|
+ fds[i].fd = -1;
|
|
|
+
|
|
|
+ fds[0].fd = fd;
|
|
|
+ fds[0].events = POLLIN;
|
|
|
+
|
|
|
+ next_fd = 1;
|
|
|
+
|
|
|
return fd;
|
|
|
}
|
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
|
|
__uint32_t size;
|
|
|
- int usr_fd;
|
|
|
+ int usr_fd, nfds_total, nfds_ready, i;
|
|
|
char *buf;
|
|
|
+ const char msg[] = {"Hello, I'm the server. \n"};
|
|
|
|
|
|
struct sockaddr_in usr_addr;
|
|
|
|
|
|
if (unlikely(argc != 1))
|
|
|
return usage();
|
|
|
|
|
|
- becomes_daemon();
|
|
|
+// becomes_daemon();
|
|
|
init_socket();
|
|
|
|
|
|
+ nfds_total = 1;
|
|
|
+
|
|
|
for (;;) {
|
|
|
- size = sizeof(struct sockaddr_in);
|
|
|
+ nfds_ready = poll(fds, nfds_total, -1);
|
|
|
|
|
|
- if (unlikely(usr_fd = accept(fd, (struct sockaddr *) &usr_addr, &size)) < 0) {
|
|
|
- perror("accept()");
|
|
|
+ if (unlikely(nfds_ready == -1)) {
|
|
|
+ perror("poll()");
|
|
|
|
|
|
- continue;
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
- vcc_log("New connection from %s\n", inet_ntoa(usr_addr.sin_addr));
|
|
|
-
|
|
|
- if (unlikely(!(buf = malloc(REQ_SIZE)))) {
|
|
|
- fprintf(stderr, "malloc() failed\n");
|
|
|
+ if (fds[0].revents & POLLIN) {
|
|
|
+ size = sizeof(struct sockaddr_in);
|
|
|
+
|
|
|
+ if (unlikely(usr_fd = accept(fd, (struct sockaddr *) &usr_addr, &size)) < 0) {
|
|
|
+ perror("accept()");
|
|
|
+
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ vcc_log("New connection from %s\n", inet_ntoa(usr_addr.sin_addr));
|
|
|
+
|
|
|
+ if (next_fd >= POLLFDS) {
|
|
|
+ for (next_fd = 1; fds[next_fd].fd == -1 && next_fd < POLLFDS; next_fd++)
|
|
|
+ ;
|
|
|
+
|
|
|
+ if (next_fd == POLLFDS)
|
|
|
+ vcc_log("*** connection queue full. \n");
|
|
|
+ }
|
|
|
|
|
|
- continue;
|
|
|
+ fds[next_fd].fd = usr_fd;
|
|
|
+ fds[next_fd].events = POLLIN;
|
|
|
+
|
|
|
+ next_fd++;
|
|
|
+ nfds_total++;
|
|
|
}
|
|
|
|
|
|
- size = read(usr_fd, buf, REQ_SIZE);
|
|
|
+ for (i = 1; i < nfds_total; i++) {
|
|
|
+ if (fds[i].fd == -1)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ if (fds[i].revents & POLLIN) {
|
|
|
+ if (!(buf = malloc(REQ_SIZE))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ size = read(fds[i].fd, buf, REQ_SIZE);
|
|
|
+
|
|
|
+ if (!size) {
|
|
|
+ vcc_log("Closed. \n");
|
|
|
+
|
|
|
+ fds[i].fd = -1;
|
|
|
+ free(buf);
|
|
|
+
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- vcc_log("package size %u: %s\n", size, buf);
|
|
|
+ vcc_log("message received: %s\n", buf);
|
|
|
+
|
|
|
+ free(buf);
|
|
|
+
|
|
|
+ fds[i].events = POLLOUT;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fds[i].revents & POLLOUT) {
|
|
|
+ write(fds[i].fd, msg, sizeof(msg));
|
|
|
+
|
|
|
+ vcc_log("message sent. \n");
|
|
|
+
|
|
|
+ fds[i].events = POLLIN;
|
|
|
+ }
|
|
|
+
|
|
|
+ fds[i].revents = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return 0;
|