Forráskód Böngészése

vccd: support getting users

intirain 4 éve
szülő
commit
8870421370
6 módosított fájl, 84 hozzáadás és 10 törlés
  1. 1 1
      Makefile
  2. 2 0
      include/klist.h
  3. 3 0
      include/vcc/vcc.h
  4. 1 1
      include/vcc/version.h
  5. 3 0
      klist.c
  6. 74 8
      vcc.c

+ 1 - 1
Makefile

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

+ 2 - 0
include/klist.h

@@ -28,6 +28,7 @@
 struct klist_node {
 	long 			value;
 	void 			*data;
+	char 			*usrname;
 	struct klist_node 	*sending;
 
 	struct klist_node 	*next, *prev;
@@ -48,6 +49,7 @@ void klist_del(struct klist_node *head, struct klist_node *will_del);
 	.value = 0, \
 	.data = NULL, \
 	.sending = NULL, \
+	.usrname = NULL, \
 	.next = __x, \
 	.prev = __x, \
 }

+ 3 - 0
include/vcc/vcc.h

@@ -34,6 +34,9 @@
 /* server -> client. sent when received a message */
 #define REQ_MSG_NEW 	2
 
+/* client -> server. get users logged-in */
+#define REQ_CTL_USRS 	3
+
 #define REQ_SIZE 	4096
 #define USRNAME_SIZE 	32
 #define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.2.0 Sun Mar  6 13:00:08 CST 2022 root"
+#define VCCD_VERSION "vccd 0.3.1 Sun Mar  6 14:51:32 CST 2022 root"

+ 3 - 0
klist.c

@@ -39,8 +39,11 @@ struct klist_node *klist_find_value(struct klist_node *head, int value) {
 void klist_init(struct klist_node *head) {
 	head->next = head;
 	head->prev = head;
+
 	head->value = 0;
 	head->data = NULL;
+	head->sending = NULL;
+	head->usrname = NULL;
 
 	return;
 }

+ 74 - 8
vcc.c

@@ -187,6 +187,10 @@ int vcc_message_broadcast(char *msg, char *usrname) {
 			continue;
 		}
 
+		req->magic = htonl(VCC_MAGIC);
+		req->reqtype = htonl(REQ_MSG_NEW);
+		req->uid = 0;
+
 		strcpy(req->msg, msg);
 		strcpy(req->usrname, usrname);
 
@@ -199,6 +203,57 @@ int vcc_message_broadcast(char *msg, char *usrname) {
 }
 
 
+int vcc_update_usrname(int fd, const char *usrname) {
+	struct klist_node *n = find_connection(fd);
+
+	if (unlikely(!n)) 
+		return -1;
+
+	if (likely(n->usrname)) 
+		return 0;
+
+	n->usrname = strdup(usrname);
+	vcc_log("user name %s updated for %d\n", usrname, fd);
+
+	return 0;
+}
+
+
+int send_users(int fd) {
+	struct vcc_request *req;
+	struct klist_node *n;
+	int i;
+
+	if (unlikely(!(req = malloc(sizeof(struct vcc_request))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	memset(req, 0, REQ_SIZE);
+
+	req->magic = htonl(VCC_MAGIC);
+	req->reqtype = htonl(REQ_CTL_USRS);
+
+	for (n = connections.next, i = 0; n != &connections; n = n->next, i++) {
+		strcpy(req->msg + i * USRNAME_SIZE, n->usrname ? n->usrname : "unnamed");
+
+		if (unlikely(i >= MSG_SIZE / USRNAME_SIZE)) {
+			vcc_log("--- too many users\n");
+
+			break;
+		}
+	}
+
+	req->uid = htonl(i);
+
+	vcc_send_req(find_connection(fd), req);
+	vcc_log("%d user names sent to %d\n", i, fd);
+
+	return 0;
+}
+
+
 int vcc_new_msg(int fd, struct vcc_request *req) {
 	char *msg = req->msg;
 
@@ -208,17 +263,28 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 		return 0;
 	}
 
-	if (unlikely(htonl(req->reqtype) != REQ_MSG_SEND)) {
-		vcc_log("--- request's type is not REQ_MSG_SEND from %d\n", fd);
+	switch (htonl(req->reqtype)) {
+	case REQ_MSG_SEND:
+		if (((unsigned char *) msg)[0] == 0xffu || !*msg) 
+			return 0;
 
-		return 0;
-	}
+		vcc_update_usrname(fd, req->usrname);
 
-	if (((unsigned char *) msg)[0] == 0xffu || !*msg) 
-		return 0;
+		vcc_log("New message: %s\n", msg);
+		vcc_message_broadcast(msg, req->usrname);
 
-	vcc_log("New message: %s\n", msg);
-	vcc_message_broadcast(msg, req->usrname);
+		break;
+
+	case REQ_CTL_USRS:
+		send_users(fd);
+
+		break;
+
+	default:
+		vcc_log("--- bad request type from %d\n", fd);
+
+		break;
+	}
 
 	return 0;
 }