|
@@ -46,7 +46,7 @@ int do_cmd(char *s);
|
|
|
int do_cmd_bh(struct vcc_request *);
|
|
int do_cmd_bh(struct vcc_request *);
|
|
|
|
|
|
|
|
|
|
|
|
|
-int fd, pid;
|
|
|
|
|
|
|
+int fd;
|
|
|
char *empty_package;
|
|
char *empty_package;
|
|
|
char usrname[USRNAME_SIZE];
|
|
char usrname[USRNAME_SIZE];
|
|
|
|
|
|
|
@@ -109,17 +109,7 @@ void alarm_handler(int nr) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-void usr1_handler(int nr) {
|
|
|
|
|
- printf("bye. \n");
|
|
|
|
|
-
|
|
|
|
|
- close(fd);
|
|
|
|
|
- exit(0);
|
|
|
|
|
-
|
|
|
|
|
- return;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static int setup_signals(void) {
|
|
|
|
|
|
|
+int setup_signals(void) {
|
|
|
struct sigaction act;
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
if (unlikely(!(empty_package = malloc(MSG_SIZE)))) {
|
|
if (unlikely(!(empty_package = malloc(MSG_SIZE)))) {
|
|
@@ -135,11 +125,6 @@ static int setup_signals(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;
|
|
@@ -147,8 +132,10 @@ static int setup_signals(void) {
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
|
int main(int argc, const char **argv) {
|
|
|
- struct vcc_request req;
|
|
|
|
|
- char *buf;
|
|
|
|
|
|
|
+ struct pollfd fds[2];
|
|
|
|
|
+ struct vcc_request req;
|
|
|
|
|
+ char *buf;
|
|
|
|
|
+ int n;
|
|
|
|
|
|
|
|
if (unlikely(argc != 1 && argc != 2))
|
|
if (unlikely(argc != 1 && argc != 2))
|
|
|
return usage();
|
|
return usage();
|
|
@@ -156,74 +143,86 @@ int main(int argc, const char **argv) {
|
|
|
printf(VCCD_VERSION "\n");
|
|
printf(VCCD_VERSION "\n");
|
|
|
|
|
|
|
|
init_socket(argv[1] ? argv[1] : "124.223.105.230");
|
|
init_socket(argv[1] ? argv[1] : "124.223.105.230");
|
|
|
|
|
+ setup_signals();
|
|
|
|
|
|
|
|
printf("Connected to server. \n");
|
|
printf("Connected to server. \n");
|
|
|
|
|
+
|
|
|
fprintf(stderr, "login as: ");
|
|
fprintf(stderr, "login as: ");
|
|
|
scanf("%s", usrname);
|
|
scanf("%s", usrname);
|
|
|
|
|
|
|
|
- if (!(pid = fork())) {
|
|
|
|
|
- /* receive messages */
|
|
|
|
|
|
|
+ fds[0].fd = 0;
|
|
|
|
|
+ fds[0].events = POLLIN;
|
|
|
|
|
+ fds[0].revents = 0;
|
|
|
|
|
|
|
|
- setup_signals();
|
|
|
|
|
|
|
+ fds[1].fd = fd;
|
|
|
|
|
+ fds[1].events = POLLIN;
|
|
|
|
|
+ fds[1].revents = 0;
|
|
|
|
|
|
|
|
- for (;;) {
|
|
|
|
|
- if (read_msg(&req) == -1) {
|
|
|
|
|
- printf("Server closed\n");
|
|
|
|
|
|
|
+ if (unlikely(!(buf = malloc(MSG_SIZE)))) {
|
|
|
|
|
+ fprintf(stderr, "*** malloc() failed\n");
|
|
|
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (ntohl(req.reqtype) != REQ_MSG_NEW) {
|
|
|
|
|
- do_cmd_bh(&req);
|
|
|
|
|
|
|
+ for (;;) {
|
|
|
|
|
+ n = poll(fds, 2, -1);
|
|
|
|
|
|
|
|
|
|
+ if (n == -1) {
|
|
|
|
|
+ if (likely(errno == EINTR))
|
|
|
continue;
|
|
continue;
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- buf = req.msg;
|
|
|
|
|
|
|
|
|
|
- if (!*buf || *(unsigned char *) buf == 0xffu)
|
|
|
|
|
- continue;
|
|
|
|
|
|
|
+ perror("poll()");
|
|
|
|
|
|
|
|
- pretty_new_msg(req.usrname, buf);
|
|
|
|
|
- pretty_prompt(usrname);
|
|
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- else {
|
|
|
|
|
- if (unlikely(!(buf = malloc(MSG_SIZE)))) {
|
|
|
|
|
- fprintf(stderr, "*** malloc() failed\n");
|
|
|
|
|
|
|
|
|
|
- goto exit;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (fds[0].revents & POLLIN) {
|
|
|
|
|
+ /* read keyboard */
|
|
|
|
|
|
|
|
- for (;;) {
|
|
|
|
|
memset(buf, 0, MSG_SIZE);
|
|
memset(buf, 0, MSG_SIZE);
|
|
|
|
|
|
|
|
|
|
+ if (read(0, buf, MSG_SIZE) < 0) {
|
|
|
|
|
+ perror("read()");
|
|
|
|
|
+
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (*buf == '-')
|
|
|
|
|
+ do_cmd(buf);
|
|
|
|
|
+
|
|
|
|
|
+ else if (*buf != '\n') {
|
|
|
|
|
+ send_msg(buf, usrname);
|
|
|
|
|
+
|
|
|
|
|
+ pretty_new_msg(usrname, buf);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
pretty_prompt(usrname);
|
|
pretty_prompt(usrname);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (read(0, buf, MSG_SIZE) <= 0) {
|
|
|
|
|
- fprintf(stderr, "--- read() failed. \n");
|
|
|
|
|
|
|
+ if (fds[1].revents & POLLIN) {
|
|
|
|
|
+ if (read_msg(&req) == -1) {
|
|
|
|
|
+ printf("Server closed. \n");
|
|
|
|
|
|
|
|
- continue;
|
|
|
|
|
|
|
+ return 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (*buf == '\n')
|
|
|
|
|
|
|
+ if (ntohl(req.reqtype) != REQ_MSG_NEW) {
|
|
|
|
|
+ do_cmd_bh(&req);
|
|
|
|
|
+
|
|
|
continue;
|
|
continue;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (*buf == '-') {
|
|
|
|
|
- do_cmd(buf);
|
|
|
|
|
|
|
+ buf = req.msg;
|
|
|
|
|
|
|
|
|
|
+ if (!*buf || *(unsigned char *) buf == 0xffu)
|
|
|
continue;
|
|
continue;
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- send_msg(buf, usrname);
|
|
|
|
|
|
|
+ pretty_new_msg(req.usrname, buf);
|
|
|
|
|
+ pretty_prompt(usrname);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-exit: close(fd);
|
|
|
|
|
- kill(pid, 9);
|
|
|
|
|
-
|
|
|
|
|
- printf("exited\n");
|
|
|
|
|
|
|
+ printf("bye. \n");
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|