|
@@ -42,9 +42,13 @@
|
|
|
int send_msg(const char *msg, const char *usrname);
|
|
int send_msg(const char *msg, const char *usrname);
|
|
|
int read_msg(struct vcc_request *buf);
|
|
int read_msg(struct vcc_request *buf);
|
|
|
|
|
|
|
|
|
|
+int do_cmd(char *s);
|
|
|
|
|
+int do_cmd_bh(struct vcc_request *);
|
|
|
|
|
|
|
|
-int fd;
|
|
|
|
|
|
|
+
|
|
|
|
|
+int fd, pid;
|
|
|
char *empty_package;
|
|
char *empty_package;
|
|
|
|
|
+char usrname[USRNAME_SIZE];
|
|
|
|
|
|
|
|
static int usage(void) {
|
|
static int usage(void) {
|
|
|
fprintf(stderr, "Usage: vcc [server]\n");
|
|
fprintf(stderr, "Usage: vcc [server]\n");
|
|
@@ -104,7 +108,18 @@ void alarm_handler(int nr) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static int setup_alarm(void) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+void usr1_handler(int nr) {
|
|
|
|
|
+ printf("bye. \n");
|
|
|
|
|
+
|
|
|
|
|
+ close(fd);
|
|
|
|
|
+ exit(0);
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+static int setup_signals(void) {
|
|
|
struct sigaction act;
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
if (unlikely(!(empty_package = malloc(MSG_SIZE)))) {
|
|
if (unlikely(!(empty_package = malloc(MSG_SIZE)))) {
|
|
@@ -120,6 +135,11 @@ static int setup_alarm(void) {
|
|
|
|
|
|
|
|
sigaction(SIGALRM, &act, NULL);
|
|
sigaction(SIGALRM, &act, NULL);
|
|
|
|
|
|
|
|
|
|
+ act.sa_handler = usr1_handler;
|
|
|
|
|
+ act.sa_flags = 0;
|
|
|
|
|
+
|
|
|
|
|
+ sigaction(SIGUSR1, &act, NULL);
|
|
|
|
|
+
|
|
|
alarm(1);
|
|
alarm(1);
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
@@ -129,8 +149,6 @@ static int setup_alarm(void) {
|
|
|
int main(int argc, const char **argv) {
|
|
int main(int argc, const char **argv) {
|
|
|
struct vcc_request req;
|
|
struct vcc_request req;
|
|
|
char *buf;
|
|
char *buf;
|
|
|
- char usrname[USRNAME_SIZE];
|
|
|
|
|
- int pid;
|
|
|
|
|
|
|
|
|
|
if (unlikely(argc != 1 && argc != 2))
|
|
if (unlikely(argc != 1 && argc != 2))
|
|
|
return usage();
|
|
return usage();
|
|
@@ -143,10 +161,10 @@ int main(int argc, const char **argv) {
|
|
|
fprintf(stderr, "login as: ");
|
|
fprintf(stderr, "login as: ");
|
|
|
scanf("%s", usrname);
|
|
scanf("%s", usrname);
|
|
|
|
|
|
|
|
- if ((pid = fork())) {
|
|
|
|
|
|
|
+ if (!(pid = fork())) {
|
|
|
/* receive messages */
|
|
/* receive messages */
|
|
|
|
|
|
|
|
- setup_alarm();
|
|
|
|
|
|
|
+ setup_signals();
|
|
|
|
|
|
|
|
for (;;) {
|
|
for (;;) {
|
|
|
if (read_msg(&req) == -1) {
|
|
if (read_msg(&req) == -1) {
|
|
@@ -155,6 +173,12 @@ int main(int argc, const char **argv) {
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (ntohl(req.reqtype) != REQ_MSG_NEW) {
|
|
|
|
|
+ do_cmd_bh(&req);
|
|
|
|
|
+
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
buf = req.msg;
|
|
buf = req.msg;
|
|
|
|
|
|
|
|
if (!*buf || *(unsigned char *) buf == 0xffu)
|
|
if (!*buf || *(unsigned char *) buf == 0xffu)
|
|
@@ -186,8 +210,11 @@ int main(int argc, const char **argv) {
|
|
|
if (*buf == '\n')
|
|
if (*buf == '\n')
|
|
|
continue;
|
|
continue;
|
|
|
|
|
|
|
|
- if (!strcmp(buf, "quit\n"))
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ if (*buf == '-') {
|
|
|
|
|
+ do_cmd(buf);
|
|
|
|
|
+
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
send_msg(buf, usrname);
|
|
send_msg(buf, usrname);
|
|
|
}
|
|
}
|