intirain 4 роки тому
батько
коміт
096b4bfa3c
6 змінених файлів з 49 додано та 24 видалено
  1. 2 2
      Makefile
  2. 14 8
      cmd.c
  3. 1 1
      include/vcc/vcc.h
  4. 2 2
      include/vcc/version.h
  5. 24 11
      main.c
  6. 6 0
      vcc.c

+ 2 - 2
Makefile

@@ -1,7 +1,7 @@
 VCCD_MAJOR 		= 1
-VCCD_MINOR 		= 0
+VCCD_MINOR 		= 1
 VCCD_PATCH 		= 0
-VCCD_STR 		= -release
+VCCD_STR 		= -resend 
 
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)

+ 14 - 8
cmd.c

@@ -62,6 +62,8 @@ struct cmdtable {
 	{ NULL, NULL, NULL }
 };
 
+#define clear_line() 	fprintf(stderr, "\033[2K\r")
+
 
 int do_cmd(char *s) {
 	struct cmdtable *t;
@@ -95,24 +97,28 @@ int do_cmd_quit(char *s) {
 
 
 int do_cmd_ls(char *s) {
-	struct vcc_request req;
-	int i, n;
-
 	get_users();
 
-	read_msg(&req);
-	n = ntohl(req.uid);
+	return 0;
+}
 
-	/* clear current line */
+int do_cmd_ls_bh(struct vcc_request *req) {
+	int n, i;
 
-	if (unlikely(ntohl(req.reqtype) != REQ_CTL_USRS)) {
+	n = ntohl(req->uid);
+
+	clear_line();
+
+	if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
 		fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
 
 		return 1;
 	}
 
 	for (i = 0; i < n; i++) 
-		printf("%s\n", req.msg + i * USRNAME_SIZE);
+		printf("%s\n", req->msg + i * USRNAME_SIZE);
+
+	pretty_prompt(usrname);
 
 	return 0;
 }

+ 1 - 1
include/vcc/vcc.h

@@ -41,7 +41,7 @@
  * server -> client. login ack  */
 #define REQ_CTL_LOGIN 	4
 
-#define REQ_SIZE 	4096
+#define REQ_SIZE 	1024
 #define USRNAME_SIZE 	32
 #define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)
 

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.0.0 Sat Mar 12 21:44:04 CST 2022 root"
-#define VCCD_SYMVER "vcc 1.0.0-release"
+#define VCCD_VERSION "vccd 1.1.0 Sun Mar 13 17:34:20 CST 2022 root"
+#define VCCD_SYMVER "vcc 1.1.0-resend "

+ 24 - 11
main.c

@@ -50,6 +50,8 @@ int do_bh(struct vcc_request *);
 
 int fd;
 char usrname[USRNAME_SIZE];
+static char *reading;
+static int size_read;
 
 static int usage(void) {
 	fprintf(stderr, "Usage: vcc [server]\n");
@@ -117,9 +119,9 @@ static int login(void) {
 
 int main(int argc, const char **argv) {
 	struct pollfd 		fds[2];
-	struct vcc_request 	req;
+	struct vcc_request 	*req;
 	char 			*buf;
-	int 			n;
+	int 			n, size;
 
 	if (unlikely(argc != 1 && argc != 2)) 
 		return usage();
@@ -146,8 +148,16 @@ int main(int argc, const char **argv) {
 		return 1;
 	}
 
+	if (unlikely(!(reading = malloc(REQ_SIZE)))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		return 1;
+	}
+
+	size_read = 0;
+
 	for (;;) {
-		n = poll(fds, 2, 1);
+		n = poll(fds, 2, 1000);
 
 		if (n == -1) {
 			if (likely(errno == EINTR)) 
@@ -190,24 +200,27 @@ int main(int argc, const char **argv) {
 		}
 
 		if (fds[1].revents & POLLIN) {
-			if (read_msg(&req) == -1) {
-				printf("Server closed. \n");
+			size = read(fd, reading + size_read, REQ_SIZE);
+			size_read += size;
 
-				return 1;
-			}
+			if (size_read < REQ_SIZE) 
+				continue;
+
+			size_read = 0;
+			req = (struct vcc_request *) reading;
 
-			if (ntohl(req.reqtype) != REQ_MSG_NEW) {
-				do_bh(&req);
+			if (ntohl(req->reqtype) != REQ_MSG_NEW) {
+				do_bh(req);
 
 				continue;
 			}
 
-			buf = req.msg;
+			buf = req->msg;
 
 			if (!*buf || *(unsigned char *) buf == 0xffu) 
 				continue;
 
-			pretty_new_msg(req.usrname, buf);
+			pretty_new_msg(req->usrname, buf);
 			pretty_prompt(usrname);
 		}
 	}

+ 6 - 0
vcc.c

@@ -41,6 +41,7 @@
 extern int fd;
 
 int vcc_login_bh(struct vcc_request *);
+int do_cmd_ls_bh(struct vcc_request *);
 
 
 int send_msg(const char *msg, const char *usrname) {
@@ -98,6 +99,11 @@ int do_bh(struct vcc_request *req) {
 
 		break;
 
+	case REQ_CTL_USRS:
+		do_cmd_ls_bh(req);
+
+		break;
+
 	default:
 		fprintf(stderr, "*** unexpected packet. \n");