Эх сурвалжийг харах

vccd: support changing score, and add an user cache

intirain 4 жил өмнө
parent
commit
cf27287489
6 өөрчлөгдсөн 142 нэмэгдсэн , 41 устгасан
  1. 1 1
      Makefile
  2. 3 0
      include/vcc/vcc.h
  3. 1 0
      include/vcc/vccd.h
  4. 2 2
      include/vcc/version.h
  5. 130 38
      users.c
  6. 5 0
      vcc.c

+ 1 - 1
Makefile

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

+ 3 - 0
include/vcc/vcc.h

@@ -55,6 +55,9 @@
 /* client -> server. get user info */
 #define REQ_CTL_UINFO 	8
 
+/* client -> server. increase one's score */
+#define REQ_SYS_SCRINC 	9
+
 #define REQ_SIZE 	512
 #define USRNAME_SIZE 	32
 

+ 1 - 0
include/vcc/vccd.h

@@ -54,6 +54,7 @@ int send_sessions(int fd);
 int join_session(int sid, int fd);
 
 int get_user_info(int fd, struct vcc_request *);
+int incr_score(int fd, struct vcc_request *);
 
 struct klist_node *find_connection(int fd);
 

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.7.0 Tue Apr  5 14:32:50 CST 2022 root"
-#define VCCD_SEMVER "vccd 1.7.0-rc "
+#define VCCD_VERSION "vccd 1.7.1 Tue Apr  5 17:09:06 CST 2022 root"
+#define VCCD_SEMVER "vccd 1.7.1-rc "

+ 130 - 38
users.c

@@ -37,7 +37,7 @@
 
 
 char 			*vccd_dir;
-int 			nusers = 0;
+int 			nusers = 0, system_fd = 0;
 struct klist_node 	logged_users = KLIST_NODE_INIT(&logged_users);
 
 
@@ -69,6 +69,37 @@ int users_init(void) {
 	return 0;
 }
 
+
+static struct klist_node *find_user(int fd) {
+	struct klist_node *n;
+
+	for (n = logged_users.next; n != &logged_users; n = n->next) 
+		if (n->value == fd) 
+			return n;
+
+	vcc_log("--- user not found: %d\n", fd);
+
+	return NULL;
+}
+
+
+static struct user *find_user_name(char *usrname) {
+	struct klist_node *n;
+	struct user *u;
+
+	for (n = logged_users.next; n != &logged_users; n = n->next) {
+		u = n->data;
+
+		if (!strcmp(u->username, usrname)) 
+			return u;
+	}
+
+	vcc_log("--- find_user_name: not found %s. \n", usrname);
+
+	return NULL;
+}
+
+
 int open_user_file(char *usr) {
 	char 	*fname;
 	int 	fdt;
@@ -81,7 +112,7 @@ int open_user_file(char *usr) {
 
 	sprintf(fname, "%s/users/%s", vccd_dir, usr);
 
-	if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
+	if (unlikely((fdt = open(fname, O_RDWR)) < 0)) {
 		perror("open()");
 
 		return -1;
@@ -94,7 +125,14 @@ int open_user_file(char *usr) {
 
 
 int get_user(char *usr, struct user *buf) {
-	int fdt;
+	int 		fdt;
+	struct user	*u;
+
+	if ((u = find_user_name(usr))) {
+		memcpy(buf, u, sizeof(struct user));
+
+		return 0;
+	}
 
 	if (unlikely(((fdt = open_user_file(usr)) == -1))) {
 		vcc_log("*** user not found. \n");
@@ -108,6 +146,25 @@ int get_user(char *usr, struct user *buf) {
 	return 0;
 }
 
+int sync_user(char *usr, struct user *buf) {
+	struct user *u;
+	int fdt;
+
+	if ((u = find_user_name(usr))) 
+		memcpy(u, buf, sizeof(struct user));
+
+	if (unlikely((fdt = open_user_file(usr)) == -1)) {
+		vcc_log("*** user not found. \n");
+
+		return -1;
+	}
+
+	write(fdt, buf, sizeof(struct user));
+	close(fdt);
+
+	return 0;
+}
+
 
 int do_user_login(int fd, struct vcc_request *req) {
 	struct vcc_request 	*out;
@@ -142,6 +199,21 @@ int do_user_login(int fd, struct vcc_request *req) {
 		return 0;
 	}
 
+	if (!strcmp(req->usrname, "system")) {
+		if (system_fd) {
+			vcc_log("system already logged-in. \n");
+
+			out->uid = 0;
+			vcc_send_req(find_connection(fd), out);
+
+			afree(usr, sizeof(struct user));
+
+			return 0;
+		}
+
+		system_fd = fd;
+	}
+
 	vcc_log("%s logged in. \n", req->usrname);
 
 	if (unlikely(!(t = amalloc(128)))) {
@@ -177,19 +249,6 @@ int do_user_login(int fd, struct vcc_request *req) {
 }
 
 
-static struct klist_node *find_user(int fd) {
-	struct klist_node *n;
-
-	for (n = logged_users.next; n != &logged_users; n = n->next) 
-		if (n->value == fd) 
-			return n;
-
-	vcc_log("--- user not found: %d\n", fd);
-
-	return NULL;
-}
-
-
 int get_user_info(int fd, struct vcc_request *req) {
 	struct vcc_request 	*out;
 	struct user 		*u;
@@ -204,51 +263,39 @@ int get_user_info(int fd, struct vcc_request *req) {
 	out->reqtype = htonl(REQ_CTL_UINFO);
 	u = (struct user *) out->msg;
 
-	if (get_user(req->msg, u) == -1) 
-		out->uid = htonl(-1);
-
-	else {
-		out->uid = 0;
-
-		u->score = htonl(u->score);
-		u->level = htonl(u->level);
-	}
-
-	memset(((struct user *) out->msg)->passwd, 0, PASSWD_SIZE);
+	out->uid = htonl(get_user(req->msg, u));
+	u->score = htonl(u->score);
+	u->level = htonl(u->level);
 
+	memset(u->passwd, 0, PASSWD_SIZE);
 	vcc_send_req(find_connection(fd), out);
 
 	return 0;
 }
 
 
-
-
 int do_user_logout(int fd) {
 	struct klist_node 	*n;
 	struct user 		*usr;
 	static const char 	*leave_msg = "%s left, now there are %d users here. \n";
 	char 			*t;
-	int 			fdt;
 
 	if (unlikely(!(n = find_user(fd)))) 
 		return 1;
 
 	usr = n->data;
 
-	if (unlikely(((fdt = open_user_file(usr->username)) == -1))) {
-		vcc_log("*** user not found: %s. \n", usr->username);
-
-		return 1;
-	}
-
 	if (unlikely(!(t = amalloc(128)))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
 	}
 
-	write(fdt, usr, sizeof(struct user));
+	sync_user(usr->username, usr);
+
+	if (!strcmp(usr->username, "system")) 
+		system_fd = 0;
+
 	vcc_log("Bye, %s. \n", usr->username);
 
 	sprintf(t, leave_msg, usr->username, nusers);
@@ -259,7 +306,52 @@ int do_user_logout(int fd) {
 	afree(usr, sizeof(struct user));
 	afree(n, sizeof(struct klist_node));
 
-	close(fdt);
+	return 0;
+}
+
+
+int incr_score(int fd, struct vcc_request *req) {
+	struct vcc_request 	*out;
+	struct user 		u;
+
+	if (unlikely(!(out = amalloc(sizeof(struct vcc_request ))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	out->magic = htonl(VCC_MAGIC);
+	out->reqtype = htonl(REQ_SYS_SCRINC);
+
+	if (fd != system_fd) {
+		out->uid = htonl(-1);
+		vcc_send_req(find_connection(fd), out);
+
+		vcc_log("%d tried to modify the score. \n", fd);
+
+		return 1;
+	}
+
+	if (unlikely(get_user(req->usrname, &u) == -1)) {
+		out->uid = htonl(-1);
+		vcc_send_req(find_connection(fd), out);
+
+		return 1;
+	}
+
+	u.score += ntohl(req->session);
+
+	if (u.score > 65535) {
+		u.level -= u.score >> 16;
+		u.score %= 65535;
+	}
+
+	sync_user(req->usrname, &u);
+
+	out->uid = 0;
+	vcc_send_req(find_connection(fd), out);
+
+	vcc_log("%s is %d:%d scores now. \n", req->usrname, u.level, u.score);
 
 	return 0;
 }

+ 5 - 0
vcc.c

@@ -312,6 +312,11 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 
 		break;
 
+	case REQ_SYS_SCRINC:
+		incr_score(fd, req);
+
+		break;
+
 	default:
 		vcc_log("--- bad request type from %d\n", fd);