浏览代码

vccd: support sessions, and now we can chat happily :-)

intirain 4 年之前
父节点
当前提交
0ac837fe8c
共有 8 个文件被更改,包括 243 次插入 和 16 次删除
  1. 3 3
      Makefile
  2. 20 7
      include/klist.h
  3. 10 1
      include/vcc/vcc.h
  4. 4 0
      include/vcc/vccd.h
  5. 2 2
      include/vcc/version.h
  6. 0 1
      monitor.c
  7. 184 0
      session.c
  8. 20 2
      vcc.c

+ 3 - 3
Makefile

@@ -1,5 +1,5 @@
 VCCD_MAJOR 		= 1
-VCCD_MINOR 		= 5
+VCCD_MINOR 		= 6
 VCCD_PATCH 		= 0
 VCCD_STR 		= -rc 
 
@@ -7,8 +7,8 @@ VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 VCCD_SEMVER 		= vccd $(VCCD_MAJOR).$(VCCD_MINOR).$(VCCD_PATCH)$(VCCD_STR)
 
-srcs 			= main.c vcc.c klist.c users.c monitor.c 
-objs 			= main.o vcc.o klist.o users.o monitor.o 
+srcs 			= main.c vcc.c klist.c users.c monitor.c session.c 
+objs 			= main.o vcc.o klist.o users.o monitor.o session.o 
 target 			= vccd
 CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g 
 MAKEFLAGS 		= --no-print-directory

+ 20 - 7
include/klist.h

@@ -25,16 +25,29 @@
 #include <vcc/vcc.h>
 
 
+/* before, i think using a 'container_of' in things we did in kernel 
+ * is silly, but now i realize that, i'm the stupid one, there are 
+ * too many things in the structure. */
+
 struct klist_node {
-	long 			value;
+	long 				value;
+
+	union {
+		void 			*data;
+		int 			nusers;
+	};
+
+	union {
+		char 			*usrname;
+		struct klist_node 	*connection;
+	};
 
-	/* only used by monitor.c, amalloc() */
-	long 			time;
-	void 			*data;
-	char 			*usrname;
-	struct klist_node 	*sending;
+	union {
+		struct klist_node 	*sending;
+		struct klist_node 	*session;
+	};
 
-	struct klist_node 	*next, *prev;
+	struct klist_node 		*next, *prev;
 };
 
 

+ 10 - 1
include/vcc/vcc.h

@@ -41,12 +41,19 @@
  * server -> client. login ack  */
 #define REQ_CTL_LOGIN 	4
 
+/* client -> server. create a new session */
+#define REQ_CTL_NEWSE 	5
+
+/* client -> server: request (get sessions present). 
+ * server -> client: response */
+#define REQ_CTL_SESS 	6
+
 #define REQ_SIZE 	512
 #define USRNAME_SIZE 	32
 
 /* we'll not warn you anyway.  */
 #define PASSWD_SIZE 	64
-#define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)
+#define MSG_SIZE 	(REQ_SIZE - 4 * sizeof(int) - USRNAME_SIZE)
 
 struct vcc_request {
 	int 		magic;
@@ -54,6 +61,8 @@ struct vcc_request {
 
 	/* to make server easy and fast */
 	int 		uid;
+	int 		session;
+
 	char 		usrname[USRNAME_SIZE];
 	char 		msg[MSG_SIZE];
 };

+ 4 - 0
include/vcc/vccd.h

@@ -50,6 +50,10 @@ int vcc_message_broadcast(char *msg, char *usr, int except_fd);
 int do_user_login(int fd, struct vcc_request *buf);
 int do_user_logout(int fd);
 
+int create_session(int fd, char *name);
+int session_broadcast(int sid, char *msg, char *usr, int except_fd);
+int send_sessions(int fd);
+
 struct klist_node *find_connection(int fd);
 
 #define LJEST_LEVEL 	16

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.5.0 Fri Mar 25 22:45:25 CST 2022 root"
-#define VCCD_SEMVER "vccd 1.5.0-rc "
+#define VCCD_VERSION "vccd 1.6.0 Sat Mar 26 21:44:33 CST 2022 root"
+#define VCCD_SEMVER "vccd 1.6.0-rc "

+ 0 - 1
monitor.c

@@ -99,7 +99,6 @@ void *__amalloc(int nbytes, const char *fn) {
 	n->data = (char *) fn;
 	n->sending = (void *) p;
 	n->value = nbytes;
-	n->time = times(NULL);
 
 	klist_add(&amalloc_log, n);
 #else

+ 184 - 0
session.c

@@ -0,0 +1,184 @@
+/* vccd/session.c 
+ * 
+ * This file is part of vccd. 
+ * 
+ * vccd is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * vccd is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/times.h>
+
+#include <config.h>
+#include <vcc/vccd.h>
+
+
+
+struct klist_node sessions = KLIST_NODE_INIT(&sessions);
+int next_sid = 1;
+
+
+/* it takes 3 * sizeof(struct klist_node) size to create a new session, it's 
+ * not a small one. */
+
+int create_session(int fd, char *name) {
+	struct klist_node *n, *s, *p;
+
+	if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	if (unlikely(!(s = amalloc(sizeof(struct klist_node))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	if (unlikely(!(p = amalloc(sizeof(struct klist_node))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	klist_init(n);
+	klist_init(s);
+	klist_init(p);
+
+	n->value = next_sid++;
+
+	if (unlikely(!(n->usrname = strdup(name)))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+	
+	klist_add(&sessions, n);
+
+	s->value = fd;
+	s->nusers = 1;
+	n->session = s;
+
+	p->value = fd;
+	p->connection = find_connection(fd);
+	
+	klist_add(s, p);
+
+	vcc_log("New session '%s' created by %d. \n", name, fd);
+
+	return 0;
+}
+
+
+struct klist_node *find_session(int sid) {
+	struct klist_node *n;
+
+	for (n = sessions.next; n != &sessions; n = n->next) 
+		if (n->value == sid) 
+			return n;
+
+	vcc_log("--- session not found. \n");
+
+	return NULL;
+}
+
+
+int session_broadcast(int sid, char *msg, char *usrname, int except) {
+	struct klist_node 	*session, *n;
+	struct vcc_request 	req, *out;
+	int 			i;
+
+	if (unlikely(!(session = find_session(sid)))) 
+		return 1;
+
+	memset(&req, 0, REQ_SIZE);
+
+	req.magic = htonl(VCC_MAGIC);
+	req.reqtype = htonl(REQ_MSG_NEW);
+	req.uid = 0;
+
+	strcpy(req.msg, msg);
+	strcpy(req.usrname, usrname);
+
+	i = 0;
+	
+	for (n = session->session->next; n != session->session; n = n->next) {
+		if (n->value == except) 
+			continue;
+
+		if (unlikely(!(out = amalloc(REQ_SIZE)))) {
+			vcc_log("*** malloc() failed\n");
+
+			return 1;
+		}
+
+		memcpy(out, &req, REQ_SIZE);
+		vcc_send_req(n->connection, out);
+
+		i++;
+	}
+
+	vcc_log("(%d) Message sent to %d users. \n", sid, i);
+
+	return 0;
+}
+
+
+int send_sessions(int fd) {
+	struct klist_node 	*c, *n;
+	struct vcc_request 	*req;
+	int 			i;
+
+	if (unlikely(!(c = find_connection(fd)))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	if (unlikely(!(req = amalloc(REQ_SIZE)))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	req->magic = htonl(VCC_MAGIC);
+	req->reqtype = htonl(REQ_CTL_SESS);
+
+	for (n = sessions.next, i = 0; n != &sessions; n = n->next, i++) {
+		strcpy(req->msg + i * USRNAME_SIZE, n->usrname);
+
+		if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
+			vcc_log("--- too many sessions. \n");
+
+			break;
+		}
+	}
+
+	req->uid = htonl(i);
+	
+	vcc_send_req(find_connection(fd), req);
+	vcc_log("%d session names sent to %d. \n", i, fd);
+
+	return 0;
+}
+
+
+

+ 20 - 2
vcc.c

@@ -242,6 +242,7 @@ int send_users(int fd) {
 int vcc_new_msg(int fd, struct vcc_request *req) {
 	struct klist_node *n = find_connection(fd);
 	char *msg;
+	int sid;
 
 	if (unlikely(!n)) 
 		return 1;
@@ -264,8 +265,15 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 		if (((unsigned char *) msg)[0] == 0xffu || !*msg) 
 			return 0;
 
-		vcc_log("New message: %s\n", msg);
-		vcc_message_broadcast(msg, req->usrname, fd);
+		sid = ntohl(req->session);
+
+		vcc_log("(%d) New message: %s\n", sid, msg);
+
+		if (!sid) 
+			vcc_message_broadcast(msg, req->usrname, fd);
+
+		else 
+			session_broadcast(sid, msg, req->usrname, fd);
 
 		break;
 
@@ -280,6 +288,16 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 
 		break;
 
+	case REQ_CTL_NEWSE:
+		create_session(fd, req->usrname);
+
+		break;
+
+	case REQ_CTL_SESS:
+		send_sessions(fd);
+
+		break;
+
 	default:
 		vcc_log("--- bad request type from %d\n", fd);