Sfoglia il codice sorgente

vcc: now using structures instead of direct messages

intirain 4 anni fa
parent
commit
233a7d4aab
4 ha cambiato i file con 21 aggiunte e 16 eliminazioni
  1. 1 1
      Makefile
  2. 4 1
      include/vcc/vcc.h
  3. 1 1
      include/vcc/version.h
  4. 15 13
      main.c

+ 1 - 1
Makefile

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

+ 4 - 1
include/vcc/vcc.h

@@ -35,6 +35,8 @@
 #define REQ_MSG_NEW 	2
 
 #define REQ_SIZE 	4096
+#define USRNAME_SIZE 	32
+#define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)
 
 struct vcc_request {
 	int 		magic;
@@ -42,7 +44,8 @@ struct vcc_request {
 
 	/* to make server easy and fast */
 	int 		uid;
-	char 		*msg;
+	char 		usrname[USRNAME_SIZE];
+	char 		msg[MSG_SIZE];
 };
 
 #define to_boolean(c) 	(!!(c))

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.1.3 Sat Mar  5 16:23:32 CST 2022 root"
+#define VCCD_VERSION "vccd 0.1.4 Sun Mar  6 07:59:54 CST 2022 root"

+ 15 - 13
main.c

@@ -93,25 +93,27 @@ static int init_socket(const char *server_addr) {
 
 
 static int send_msg(const char *msg) {
-	char *buf;
+	struct vcc_request req;
        
-	if (unlikely(!(buf = malloc(REQ_SIZE)))) {
-		fprintf(stderr, "*** malloc() failed\n");
-
-		exit(1);
-	}
-
-	strcpy(buf, msg);
-	write(fd, buf, REQ_SIZE);
-
-	free(buf);
+	strcpy(req.msg, msg);
+	write(fd, &req, REQ_SIZE);
 
 	return 0;
 }
 
 
 static int read_msg(char *buf) {
-	return read(fd, buf, REQ_SIZE);
+	struct vcc_request req;
+	int n;
+
+	n = read(fd, &req, REQ_SIZE);
+
+	if (n < 0) 
+		return n;
+
+	memcpy(buf, req.msg, MSG_SIZE);
+
+	return 0;
 }
 
 
@@ -151,7 +153,7 @@ int main(int argc, const char **argv) {
 
 	printf("Connected to server. \n");
 
-	if (!(pid = fork())) {
+	if ((pid = fork())) {
 		/* receive messages */
 
 		setup_alarm();