浏览代码

!3 support quiting sessions and no TIME_WAIT
Merge pull request !3 from 3swordman/master

rain 4 年之前
父节点
当前提交
70840bc6a1
共有 8 个文件被更改,包括 74 次插入 和 16 次删除
  1. 2 2
      Makefile
  2. 3 0
      include/vcc/vcc.h
  3. 1 0
      include/vcc/vccd.h
  4. 2 2
      include/vcc/version.h
  5. 17 1
      main.c
  6. 37 0
      session.c
  7. 4 2
      users.c
  8. 8 9
      vcc.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCCD_MAJOR 		= 1
-VCCD_MINOR 		= 19
-VCCD_PATCH 		= 1
+VCCD_MINOR 		= 20
+VCCD_PATCH 		= 0
 VCCD_STR 		= -rc 
 
 VCCD_COMPILE_TIME 	= $(shell date)

+ 3 - 0
include/vcc/vcc.h

@@ -71,6 +71,9 @@
 #define REQ_SYS_INFO 	13
 #define REQ_CTL_SENAME 	14
 
+/* client -> server. quit session */
+#define REQ_CTL_QUITS	15
+
 #define REQ_MAX 	15
 
 #define REQ_SIZE 	512

+ 1 - 0
include/vcc/vccd.h

@@ -73,6 +73,7 @@ int create_session(int fd, char *name);
 int session_broadcast(int sid, char *msg, char *usr, int except_fd, int flags);
 int send_sessions(int fd);
 int join_session(int sid, int fd);
+int quit_session(int sid, int fd);
 int session_name(int fd, struct vcc_request *req);
 
 int get_user_info(int fd, struct vcc_request *);

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.19.1 Sun 19 Jun 2022 07:11:11 PM CST rain"
-#define VCCD_SEMVER "vccd 1.19.1-rc "
+#define VCCD_VERSION "vccd 1.20.0 Tue 28 Jun 2022 01:33:02 PM CST ubuntu"
+#define VCCD_SEMVER "vccd 1.20.0-rc "

+ 17 - 1
main.c

@@ -112,7 +112,9 @@ int becomes_daemon(void) {
 
 static int init_socket(void) {
 	struct sockaddr_in addr;
-	int i;
+	int i, enable;
+
+	enable = 1;
 
 	if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
 		perror("socket()");
@@ -126,6 +128,20 @@ static int init_socket(void) {
 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
 	addr.sin_port = htons(VCC_PORT);
 
+	if (unlikely(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)) {
+		perror("setsockopt()");
+
+		exit(1);
+	}
+
+#ifdef SO_REUSEPORT
+	if (unlikely(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0)) {
+		perror("setsockopt()");
+
+		exit(1);
+	}
+#endif
+
 	if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
 		perror("bind()");
 

+ 37 - 0
session.c

@@ -156,6 +156,43 @@ int join_session(int sid, int fd) {
 	return 0;
 }
 
+int quit_session(int sid, int fd) {
+	struct klist_node *session, *n;
+	static const char *leave_msg = "%s left session %s, now there are %d users in this session. \n";
+	char *t;
+
+	if (unlikely(!(session = find_session(sid))))
+		return 1;
+	
+	if (!fd_in_session(session, fd)) {
+		vcc_log("%d isn't in the session %d yet\n", fd, sid);
+
+		return 0;
+	}
+
+	if (unlikely(!(t = amalloc(128)))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	for (n = session->session->next; n != session->session; n = n->next)
+		if (n->value == fd)
+			break;
+
+	sprintf(t, leave_msg, n->connection->usrname, session->usrname, --session->session->nusers);
+	session_broadcast(sid, t, "system", fd, 0);
+
+	vcc_log("%d left session %d. \n", fd, sid);
+
+	klist_del(session->session, n);
+	afree(n, sizeof(struct klist_node));
+
+	afree(t, 128);
+	
+	return 0;
+}
+
 
 int session_broadcast(int sid, char *msg, char *usrname, int except, int flags) {
 	struct klist_node 	*session, *n;

+ 4 - 2
users.c

@@ -201,7 +201,7 @@ int do_user_login(int fd, struct vcc_request *req) {
 		do_login_failed(fd, out);
 		afree(usr, sizeof(struct user));
 
-		return 0;
+		return 1;
 	}
 
 	init_user_info(fd, req->usrname, usr);
@@ -287,7 +287,7 @@ int init_user_info(int fd, char *usrname, struct user *u) {
 	out->uid = htonl(1);
 	vcc_send_req(p, out);
 
-	/* nusers already inc'ed when it connects */
+	nusers++;
 
 	sprintf(t, join_msg, usrname, nusers);
 	vcc_message_broadcast(t, "system", 0, 0);
@@ -347,6 +347,8 @@ int do_user_logout(int fd) {
 
 	vcc_log("Bye, %s. \n", usr->username);
 
+	nusers--;
+
 	sprintf(t, leave_msg, usr->username, nusers);
 	vcc_message_broadcast(t, "system", fd, 0);
 

+ 8 - 9
vcc.c

@@ -69,10 +69,6 @@ int vcc_new_connection(int fd, struct in_addr *addr) {
 	klist_init(n->sending);
 	klist_add(&connections, n);
 
-	/* if we kill vcc before it logs in, the nusers will dec but it 
-	 * didn't login the value will be something bad. */
-
-	nusers++;
 
 	return 0;
 }
@@ -83,8 +79,6 @@ int vcc_close_connection(int fd) {
 	if (unlikely(!(n = find_connection(fd)))) 
 		return 1;
 
-	nusers--;
-
 	do_user_logout(fd);
 	klist_del(&connections, n);
 
@@ -325,13 +319,12 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 		break;
 
 	case REQ_CTL_LOGIN:
-		do_user_login(fd, req);
-		vcc_update_usrname(fd, req->usrname);
+		if (!do_user_login(fd, req))
+			vcc_update_usrname(fd, req->usrname);
 
 		break;
 
 	case REQ_CTL_IALOG:
-		check_logged(fd);
 		do_ia_login(fd, req);
 		vcc_update_usrname(fd, req->usrname);
 
@@ -379,6 +372,12 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 
 		break;
 
+	case REQ_CTL_QUITS:
+		check_logged(fd);
+		quit_session(ntohl(req->session), fd);
+
+		break;
+
 	default:
 		vcc_log("--- bad request type from %d\n", fd);
 		vcc_info("WBADREQ %d\n", fd);