Browse Source

vcc: now use single task by poll()

intirain 4 năm trước cách đây
mục cha
commit
745357ddd1
5 tập tin đã thay đổi với 61 bổ sung và 65 xóa
  1. 1 1
      Makefile
  2. 3 6
      cmd.c
  3. 1 1
      include/vcc/version.h
  4. 55 56
      main.c
  5. 1 1
      pretty.c

+ 1 - 1
Makefile

@@ -1,6 +1,6 @@
 VCCD_MAJOR 		= 0
 VCCD_MINOR 		= 5
-VCCD_PATCH 		= 0
+VCCD_PATCH 		= 1
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 

+ 3 - 6
cmd.c

@@ -108,8 +108,6 @@ int do_cmd_help(char *s) {
 
 int do_cmd_quit(char *s) {
 	printf("bye. \n");
-
-	kill(pid, SIGUSR1);
 	exit(0);
 
 	return 0;
@@ -119,14 +117,15 @@ int do_cmd_quit(char *s) {
 int do_cmd_ls_bh(struct vcc_request *req) {
 	int i, n = ntohl(req->uid);
 
+	/* clear current line */
+	printf("\033[2K\r");
+
 	if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
 		fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
 
 		return 1;
 	}
 
-	printf("\n");
-
 	for (i = 0; i < n; i++) 
 		printf("%s\n", req->msg + i * USRNAME_SIZE);
 
@@ -141,5 +140,3 @@ int do_cmd_ls(char *s) {
 }
 
 
-
-

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.5.0 Sun Mar  6 14:51:00 CST 2022 root"
+#define VCCD_VERSION "vccd 0.5.1 Fri Mar 11 20:02:49 CST 2022 root"

+ 55 - 56
main.c

@@ -46,7 +46,7 @@ int do_cmd(char *s);
 int do_cmd_bh(struct vcc_request *);
 
 
-int fd, pid;
+int fd;
 char *empty_package;
 char usrname[USRNAME_SIZE];
 
@@ -109,17 +109,7 @@ void alarm_handler(int nr) {
 }
 
 
-void usr1_handler(int nr) {
-	printf("bye. \n");
-
-	close(fd);
-	exit(0);
-
-	return;
-}
-
-
-static int setup_signals(void) {
+int setup_signals(void) {
 	struct sigaction act;
 
 	if (unlikely(!(empty_package = malloc(MSG_SIZE)))) {
@@ -135,11 +125,6 @@ static int setup_signals(void) {
 
 	sigaction(SIGALRM, &act, NULL);
 
-	act.sa_handler = usr1_handler;
-	act.sa_flags = 0;
-
-	sigaction(SIGUSR1, &act, NULL);
-
 	alarm(1);
 
 	return 0;
@@ -147,8 +132,10 @@ static int setup_signals(void) {
 
 
 int main(int argc, const char **argv) {
-	struct vcc_request req;
-	char *buf;
+	struct pollfd 		fds[2];
+	struct vcc_request 	req;
+	char 			*buf;
+	int 			n;
 
 	if (unlikely(argc != 1 && argc != 2)) 
 		return usage();
@@ -156,74 +143,86 @@ int main(int argc, const char **argv) {
 	printf(VCCD_VERSION "\n");
 
 	init_socket(argv[1] ? argv[1] : "124.223.105.230");
+	setup_signals();
 
 	printf("Connected to server. \n");
+
 	fprintf(stderr, "login as: ");
 	scanf("%s", usrname);
 
-	if (!(pid = fork())) {
-		/* receive messages */
+	fds[0].fd = 0;
+	fds[0].events = POLLIN;
+	fds[0].revents = 0;
 
-		setup_signals();
+	fds[1].fd = fd;
+	fds[1].events = POLLIN;
+	fds[1].revents = 0;
 
-		for (;;) {
-			if (read_msg(&req) == -1) {
-				printf("Server closed\n");
+	if (unlikely(!(buf = malloc(MSG_SIZE)))) {
+		fprintf(stderr, "*** malloc() failed\n");
 
-				break;
-			}
+		return 1;
+	}
 
-			if (ntohl(req.reqtype) != REQ_MSG_NEW) {
-				do_cmd_bh(&req);
+	for (;;) {
+		n = poll(fds, 2, -1);
 
+		if (n == -1) {
+			if (likely(errno == EINTR)) 
 				continue;
-			}
-
-			buf = req.msg;
 
-			if (!*buf || *(unsigned char *) buf == 0xffu) 
-				continue;
+			perror("poll()");
 
-			pretty_new_msg(req.usrname, buf);
-			pretty_prompt(usrname);
+			break;
 		}
-	}
-
-	else {
-		if (unlikely(!(buf = malloc(MSG_SIZE)))) {
-			fprintf(stderr, "*** malloc() failed\n");
 
-			goto exit;
-		}
+		if (fds[0].revents & POLLIN) {
+			/* read keyboard */
 
-		for (;;) {
 			memset(buf, 0, MSG_SIZE);
 
+			if (read(0, buf, MSG_SIZE) < 0) {
+				perror("read()");
+
+				continue;
+			}
+
+			if (*buf == '-') 
+				do_cmd(buf);
+
+			else if (*buf != '\n') {
+				send_msg(buf, usrname);
+
+				pretty_new_msg(usrname, buf);
+			}
+
 			pretty_prompt(usrname);
+		}
 
-			if (read(0, buf, MSG_SIZE) <= 0) {
-				fprintf(stderr, "--- read() failed. \n");
+		if (fds[1].revents & POLLIN) {
+			if (read_msg(&req) == -1) {
+				printf("Server closed. \n");
 
-				continue;
+				return 1;
 			}
 
-			if (*buf == '\n') 
+			if (ntohl(req.reqtype) != REQ_MSG_NEW) {
+				do_cmd_bh(&req);
+
 				continue;
+			}
 
-			if (*buf == '-') {
-				do_cmd(buf);
+			buf = req.msg;
 
+			if (!*buf || *(unsigned char *) buf == 0xffu) 
 				continue;
-			}
 
-			send_msg(buf, usrname);
+			pretty_new_msg(req.usrname, buf);
+			pretty_prompt(usrname);
 		}
 	}
 
-exit:	close(fd);
-	kill(pid, 9);
-
-	printf("exited\n");
+	printf("bye. \n");
 
 	return 0;
 }

+ 1 - 1
pretty.c

@@ -141,7 +141,7 @@ int pretty_cmd_help(char *cmdname, char *descr) {
 	printf("%s: ", cmdname);
 
 	setcolor(stdout, curr_helpmsg);
-	printf("%s\n", descr);
+	printf("\t%s\n", descr);
 
 	ansi_csi_ed(stdout);