|
@@ -187,6 +187,10 @@ int vcc_message_broadcast(char *msg, char *usrname) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ req->magic = htonl(VCC_MAGIC);
|
|
|
|
|
+ req->reqtype = htonl(REQ_MSG_NEW);
|
|
|
|
|
+ req->uid = 0;
|
|
|
|
|
+
|
|
|
strcpy(req->msg, msg);
|
|
strcpy(req->msg, msg);
|
|
|
strcpy(req->usrname, usrname);
|
|
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) {
|
|
int vcc_new_msg(int fd, struct vcc_request *req) {
|
|
|
char *msg = req->msg;
|
|
char *msg = req->msg;
|
|
|
|
|
|
|
@@ -208,17 +263,28 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
|
|
|
return 0;
|
|
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;
|
|
return 0;
|
|
|
}
|
|
}
|