src/server.c

changeset 94
864f59271974
parent 92
84e0dec5db16
equal deleted inserted replaced
93:9b64437262a2 94:864f59271974
33 #include "chess/pgn.h" 33 #include "chess/pgn.h"
34 #include <ncurses.h> 34 #include <ncurses.h>
35 #include <errno.h> 35 #include <errno.h>
36 #include <string.h> 36 #include <string.h>
37 #include <stdlib.h> 37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <signal.h>
38 40
39 static int server_open(Server *server, short port) { 41 static int server_fd = -1;
42 static void interrupt_listen(int sig) {
43 if (server_fd > -1) {
44 // this interrupts
45 close(server_fd);
46 }
47 }
48
49 static int server_open(Server *server, Settings *settings) {
40 printw("\nListening for client...\n"); 50 printw("\nListening for client...\n");
41 refresh(); 51 refresh();
42 if (net_create(server, port)) { 52 if (settings->usedomainsocket
53 ? net_create_sock(server, settings->serverhost)
54 : net_create_tcp(server, settings->port)) {
43 addstr("Server creation failed"); 55 addstr("Server creation failed");
44 return 1; 56 return 1;
45 } 57 }
46 58
59 // allow Ctrl+C to interrupt the listening process
60 server_fd = server->fd;
61 signal(SIGINT, interrupt_listen);
62
47 if (net_listen(server)) { 63 if (net_listen(server)) {
48 addstr("Listening for client failed"); 64 addstr("Listening for client failed or interrupted");
49 return 1; 65 return 1;
50 } 66 }
67
68 // restore default action
69 signal(SIGINT, SIG_DFL);
51 70
52 return 0; 71 return 0;
53 } 72 }
54 73
55 static int server_handshake(Client *client) { 74 static int server_handshake(Client *client) {
95 printw("Can't read PGN file (%s)\n", strerror(errno)); 114 printw("Can't read PGN file (%s)\n", strerror(errno));
96 return 1; 115 return 1;
97 } 116 }
98 } 117 }
99 118
100 if (server_open(&server, settings->port)) { 119 if (server_open(&server, settings)) {
101 net_destroy(&server); 120 net_destroy(&server);
102 return 1; 121 return 1;
103 } 122 }
104 123
105 if (server_handshake(server.client)) { 124 if (server_handshake(server.client)) {

mercurial