فهرست منبع

vccd: to-you-only relay message. it sounds nice.

intirain 4 سال پیش
والد
کامیت
fb22bb8c8b
4فایلهای تغییر یافته به همراه65 افزوده شده و 30 حذف شده
  1. 1 1
      Makefile
  2. 4 1
      include/vcc/vccd.h
  3. 2 2
      include/vcc/version.h
  4. 58 26
      relay.c

+ 1 - 1
Makefile

@@ -1,5 +1,5 @@
 VCCD_MAJOR 		= 1
-VCCD_MINOR 		= 10
+VCCD_MINOR 		= 11
 VCCD_PATCH 		= 0
 VCCD_STR 		= -rc 
 

+ 4 - 1
include/vcc/vccd.h

@@ -58,7 +58,10 @@ int get_user_info(int fd, struct vcc_request *);
 int incr_score(int fd, struct vcc_request *);
 
 int relay_new_msg(int fd, void *);
-int relay_send_to(const char *, int expect, struct vcc_relay_header *);
+int relay_broadcast(const char *restrict msg, int expect, 
+		const struct vcc_relay_header *restrict hdr);
+int relay_send_to(const char *restrict msg, const char *restrict visible, int expect_fd, 
+		const struct vcc_relay_header *restrict hdr);
 int read_until(int fd, void *, int);
 
 struct klist_node *find_connection(int fd);

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.10.0 Sat May 21 09:52:25 PM CST 2022 root"
-#define VCCD_SEMVER "vccd 1.10.0-rc "
+#define VCCD_VERSION "vccd 1.11.0 Sun May 22 11:09:46 AM CST 2022 root"
+#define VCCD_SEMVER "vccd 1.11.0-rc "

+ 58 - 26
relay.c

@@ -39,7 +39,7 @@
 #include <klist.h>
 
 
-extern struct klist_node connections;
+extern struct klist_node connections, logged_users;
 
 int relay_new_msg(int fd, void *t) {
 	struct vcc_relay_header 	*req;
@@ -56,49 +56,81 @@ int relay_new_msg(int fd, void *t) {
 	}
 
 	read_until(fd, msg, len);
-	relay_send_to(msg, 0, req);
+
+	if (!*req->visible) 
+		relay_broadcast(msg, fd, req);
+	else 
+		/* only send to somebody */
+		relay_send_to(msg, req->visible, fd, req);
 
 	return 0;
 }
 
 
-int relay_send_to(const char *msg, int expect_fd, struct vcc_relay_header *hdr) {
-	struct vcc_relay_header *req;
-	struct klist_node 	*n;
-	int 			i, len;
+int send_to(int fd, const char *restrict msg, int is_only_visible, 
+		const struct vcc_relay_header *restrict hdr, int size) {
+	struct vcc_relay_header req;
 
-	/* make sure about that the write() bottom will execute after these */
-	vcc_write_all();
+	memset(&req, 0, sizeof(struct vcc_relay_header));
 
-	i = 0;
-	len = strlen(msg);
+	req.magic = htonl(VCC_MAGIC_RL);
+	req.reqtype = htonl(REQ_REL_NEW);
+	req.uid = htonl(is_only_visible);
+	req.session = 0;
+	req.size = htonl(sizeof(struct vcc_relay_header) + size);
+
+	strncpy(req.usrname, hdr->usrname, USRNAME_SIZE);
+
+	/* direct writes here 
+	 * vcc_send_req(n, req); 
+	 * */
+
+	write(fd, &req, sizeof(struct vcc_relay_header));
+	write(fd, msg, size);
+
+	return 0;
+}
+
+int relay_send_to(const char *restrict msg, const char *restrict visible, int expect_fd, 
+		const struct vcc_relay_header *restrict hdr) {
+	struct klist_node 		*n;
+	struct user 			*u;
+
+	for (n = logged_users.next; n != &logged_users; n = n->next) {
+		u = n->data;
 
-	for (n = connections.next; n != &connections; n = n->next) {
 		if (n->value == expect_fd) 
 			continue;
 
-		if (unlikely(!(req = amalloc(sizeof(struct vcc_relay_header))))) {
-			vcc_log("*** malloc() failed\n");
+		if (!strcmp(visible, u->username)) {
+			send_to(n->value, msg, 1, hdr, strlen(msg));
+			vcc_log("Relay message sent to the visible user. \n");
 
-			return 1;
+			return 0;
 		}
+	}
 
-		memset(req, 0, sizeof(struct vcc_relay_header));
+	vcc_log("*** user not found. \n");
+
+	return 1;
+}
 
-		req->magic = htonl(VCC_MAGIC_RL);
-		req->reqtype = htonl(REQ_REL_NEW);
-		req->uid = 0;
-		req->session = 0;
-		req->size = htonl(sizeof(struct vcc_relay_header) + len);
 
-		strncpy(req->usrname, hdr->usrname, USRNAME_SIZE);
+int relay_broadcast(const char *restrict msg, int expect_fd, const struct vcc_relay_header *restrict hdr) {
+	struct klist_node 	*n;
+	int 			i, len;
 
-		/* direct writes here 
-		 * vcc_send_req(n, req); 
-		 * */
+	/* make sure about that the write() bottom will execute after these */
+	vcc_write_all();
+
+	len = strlen(msg);
+	i = 0;
+
+	for (n = connections.next; n != &connections; n = n->next) {
+		if (n->value == expect_fd) 
+			continue;
 
-		write(n->value, req, sizeof(struct vcc_relay_header));
-		write(n->value, msg, len);
+		send_to(n->value, msg, 0, hdr, len);
 
 		i++;
 	}