client.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netdb.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <errno.h>
  11. #include <signal.h>
  12. #include <ncurses.h>
  13. #include "common.h"
  14. #include "network.h"
  15. #define ctrl(key) (0x1f & (key))
  16. #define ORIGIN_X 1
  17. #define ORIGIN_Y 2
  18. #define clamp_yx(y, x) \
  19. do { \
  20. if ((y) <= ORIGIN_Y) \
  21. y = ORIGIN_Y+1; \
  22. if ((x) <= ORIGIN_X) \
  23. x = ORIGIN_X+1; \
  24. if ((x) > ORIGIN_X+WIDTH) \
  25. x = ORIGIN_X+WIDTH; \
  26. if ((y) > ORIGIN_Y+HEIGHT) \
  27. y = ORIGIN_Y+HEIGHT; \
  28. } while(0)
  29. char *g_host, *g_port;
  30. int g_sockfd;
  31. int toosmall;
  32. volatile sig_atomic_t running;
  33. void int_handler(int s) {
  34. (void)s;
  35. running = false;
  36. }
  37. int connect_socket(char *node, char *service) {
  38. int sockfd;
  39. struct addrinfo hints, *result, *rp;
  40. int s;
  41. memset(&hints, 0, sizeof(hints));
  42. hints.ai_family = AF_UNSPEC;
  43. hints.ai_socktype = SOCK_STREAM;
  44. if ((s = getaddrinfo(node, service, &hints, &result)) != 0) {
  45. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  46. exit(EXIT_FAILURE);
  47. }
  48. for (rp = result; rp != NULL; rp = rp->ai_next) {
  49. if ((sockfd = socket(rp->ai_family, rp->ai_socktype,
  50. rp->ai_protocol)) == -1) {
  51. perror("socket");
  52. continue;
  53. }
  54. if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == -1) {
  55. close(sockfd);
  56. perror("connect");
  57. continue;
  58. }
  59. break;
  60. }
  61. if (rp == NULL) {
  62. exit(EXIT_FAILURE);
  63. }
  64. freeaddrinfo(result);
  65. g_host = node;
  66. g_port = service;
  67. return sockfd;
  68. }
  69. void send_hello() {
  70. uint8_t packet[3];
  71. *((uint16_t*)packet) = htons(3);
  72. packet[2] = PKT_TYPE_HELLO;
  73. if (send_packet(g_sockfd, packet, sizeof packet) < (sizeof packet)) {
  74. perror("send_packet");
  75. exit(EXIT_FAILURE);
  76. }
  77. }
  78. char FIELD[(WIDTH+1)*HEIGHT];
  79. /**
  80. * Put a character in the local buffer and put it on screen, if it is currently
  81. * not toosmall. The cursor position is clobbered.
  82. *
  83. * x and y are in screen coordinates
  84. */
  85. void put_char_raw(int y, int x, char ch) {
  86. clamp_yx(y, x);
  87. if (!toosmall)
  88. mvaddch(y, x, ch);
  89. y = y - ORIGIN_Y - 1;
  90. x = x - ORIGIN_X - 1;
  91. FIELD[y*(WIDTH+1)+x] = ch;
  92. }
  93. /**
  94. * Calls put_char_raw with translated coordinates and prevents the cursor
  95. * from moving.
  96. * Called on received network events.
  97. *
  98. * x and y are in buffer coordinates
  99. */
  100. void put_char_net(int y, int x, char ch) {
  101. // Translate to screen coordinates
  102. y = y + ORIGIN_Y + 1;
  103. x = x + ORIGIN_X + 1;
  104. int oldy, oldx;
  105. // Position of the cursor is stored, as put_char_raw moves it.
  106. getyx(stdscr, oldy, oldx);
  107. put_char_raw(y, x, ch);
  108. move(oldy, oldx);
  109. }
  110. /**
  111. * Calls put_char_raw and sends event over network. Prevents cursor from moving.
  112. * Called on locally generated events (user interactions).
  113. * Does nothing if toosmall is set, in order to prevent user from doing
  114. * what they can't see.
  115. *
  116. * x and y are in screen coordinates.
  117. */
  118. void put_char_usr(int y, int x, char ch) {
  119. if (toosmall)
  120. return;
  121. put_char_raw(y, x, ch);
  122. // Prevent cursor from moving. While this is contraproductive in some cases,
  123. // for example drawing vertical lines becomes slightly easier.
  124. // A better solution would be to enable to user to select one of several
  125. // movement modes.
  126. move(y, x);
  127. struct packet_update_t packet = {
  128. .size = htons(sizeof(struct packet_update_t)),
  129. .type = PKT_TYPE_UPDATE,
  130. .y = (uint8_t)(y-ORIGIN_Y-1),
  131. .x = (uint8_t)(x-ORIGIN_X-1),
  132. .ch = ch
  133. };
  134. size_t len = sizeof(struct packet_update_t);
  135. if (send_packet(g_sockfd, (uint8_t*)&packet, len) < len) {
  136. perror("send_packet");
  137. exit(EXIT_FAILURE);
  138. }
  139. }
  140. void redraw_field() {
  141. for (int i = 0; i < HEIGHT; ++i) {
  142. FIELD[i*(WIDTH+1)+WIDTH] = 0;
  143. mvprintw(ORIGIN_Y+i+1, ORIGIN_X+1, "%s", &FIELD[i*(WIDTH+1)]);
  144. }
  145. }
  146. void configure_curses() {
  147. initscr();
  148. cbreak();
  149. keypad(stdscr, TRUE);
  150. noecho();
  151. nodelay(stdscr, TRUE);
  152. }
  153. void draw_rectangle(int y1, int x1, int y2, int x2) {
  154. mvhline(y1, x1, 0, x2-x1);
  155. mvhline(y2, x1, 0, x2-x1);
  156. mvvline(y1, x1, 0, y2-y1);
  157. mvvline(y1, x2, 0, y2-y1);
  158. mvaddch(y1, x1, ACS_ULCORNER);
  159. mvaddch(y1, x2, ACS_URCORNER);
  160. mvaddch(y2, x1, ACS_LLCORNER);
  161. mvaddch(y2, x2, ACS_LRCORNER);
  162. }
  163. int clamped_move(int y, int x) {
  164. if (toosmall)
  165. return 0;
  166. clamp_yx(y, x);
  167. return move(y, x);
  168. }
  169. void redraw_all() {
  170. int maxy, maxx;
  171. getmaxyx(stdscr, maxy, maxx);
  172. if (maxy < ORIGIN_Y+HEIGHT+2 || maxx < ORIGIN_X+WIDTH+2) {
  173. clear();
  174. printw("Terminal too small.");
  175. toosmall = true;
  176. return;
  177. } else {
  178. toosmall = false;
  179. }
  180. int y, x;
  181. getyx(stdscr, y, x);
  182. clear();
  183. draw_rectangle(ORIGIN_Y, ORIGIN_X, ORIGIN_Y+HEIGHT+1, ORIGIN_X+WIDTH+1);
  184. mvprintw(0, 0, "Connected to %s:%s.", g_host, g_port);
  185. redraw_field();
  186. clamped_move(y, x);
  187. refresh();
  188. }
  189. void process_stdin() {
  190. int ch = getch();
  191. if (ch == ERR)
  192. return;
  193. int y, x;
  194. getyx(stdscr, y, x);
  195. if (ch >= 0x20 && ch < 0x7f) {
  196. put_char_usr(y, x, ch);
  197. } else switch (ch) {
  198. case KEY_UP:
  199. clamped_move(y-1, x);
  200. break;
  201. case KEY_DOWN:
  202. clamped_move(y+1, x);
  203. break;
  204. case KEY_LEFT:
  205. clamped_move(y, x-1);
  206. break;
  207. case KEY_RIGHT:
  208. clamped_move(y, x+1);
  209. break;
  210. case KEY_HOME:
  211. clamped_move(y, 0);
  212. break;
  213. case KEY_END:
  214. clamped_move(y, ORIGIN_X+WIDTH);
  215. break;
  216. case KEY_PPAGE:
  217. clamped_move(0, x);
  218. break;
  219. case KEY_NPAGE:
  220. clamped_move(ORIGIN_Y+HEIGHT, x);
  221. break;
  222. case KEY_RESIZE:
  223. case ctrl('l'):
  224. redraw_all();
  225. break;
  226. case ctrl('r'):
  227. send_hello();
  228. break;
  229. case ctrl('x'):
  230. running = false;
  231. return;
  232. default:
  233. break;
  234. }
  235. refresh();
  236. }
  237. void process_network() {
  238. uint8_t buf[PKT_SIZE_MAX];
  239. size_t len = sizeof buf;
  240. int type = recv_packet(g_sockfd, buf, &len);
  241. if (type < 0) {
  242. perror("recv_packet");
  243. exit(EXIT_FAILURE);
  244. }
  245. if (type == 0) {
  246. fprintf(stderr, "Connection closed by server.\n");
  247. running = false;
  248. return;
  249. }
  250. if (type == PKT_TYPE_UPDATE) {
  251. struct packet_update_t *pkt = (struct packet_update_t *)buf;
  252. if (len < sizeof(struct packet_update_t)) {
  253. fprintf(stderr, "Received truncated packet.\n");
  254. exit(EXIT_FAILURE);
  255. }
  256. put_char_net(pkt->y, pkt->x, pkt->ch);
  257. refresh();
  258. } else if (type == PKT_TYPE_INIT) {
  259. struct packet_init_t *pkt = (struct packet_init_t *)buf;
  260. if (len < sizeof(struct packet_init_t)) {
  261. fprintf(stderr, "Received truncated packet.\n");
  262. exit(EXIT_FAILURE);
  263. }
  264. memcpy(FIELD, pkt->data, sizeof FIELD);
  265. redraw_all();
  266. }
  267. }
  268. void cleanup() {
  269. if (!isendwin())
  270. endwin();
  271. }
  272. int main(int argc, char *argv[]) {
  273. if (argc != 3 && argc != 2) {
  274. fprintf(stderr, "USAGE: %s SERVER [PORT]\n", argv[0]);
  275. exit(EXIT_FAILURE);
  276. }
  277. memset(FIELD, ' ', (WIDTH+1)*HEIGHT);
  278. for (int i = 0; i < HEIGHT; ++i) {
  279. FIELD[i*(WIDTH+1)+WIDTH] = '\0';
  280. }
  281. char *port;
  282. if (argc == 2)
  283. port = PORT;
  284. if (argc == 3)
  285. port = argv[2];
  286. g_sockfd = connect_socket(argv[1], port);
  287. send_hello();
  288. configure_curses();
  289. redraw_all();
  290. atexit(cleanup);
  291. fd_set fds_main, rfds;
  292. FD_ZERO(&fds_main);
  293. FD_SET(STDIN_FILENO, &fds_main);
  294. FD_SET(g_sockfd, &fds_main);
  295. int maxfd = (STDIN_FILENO > g_sockfd) ? STDIN_FILENO : g_sockfd;
  296. running = true;
  297. struct sigaction act;
  298. act.sa_handler = int_handler;
  299. if (sigaction(SIGINT, &act, NULL) == -1) {
  300. perror("sigaction");
  301. }
  302. process_stdin();
  303. while (running)
  304. {
  305. rfds = fds_main;
  306. if (select(maxfd+1, &rfds, NULL, NULL, NULL) == -1) {
  307. if (errno == EINTR) {
  308. // e.g. SIGWINCH pushes a KEY_RESIZE to the ncurses
  309. // input queue and needs to be processed.
  310. // We are using nodelay, so this does not block in case of
  311. // other signals or ncurses deciding not to generate
  312. // a KEY_RESIZE for whatever reason.
  313. process_stdin();
  314. continue;
  315. }
  316. perror("select");
  317. exit(EXIT_FAILURE);
  318. } else {
  319. if (FD_ISSET(g_sockfd, &rfds)) {
  320. process_network();
  321. }
  322. if (FD_ISSET(STDIN_FILENO, &rfds)) {
  323. process_stdin();
  324. }
  325. }
  326. }
  327. endwin();
  328. puts("Shutting down");
  329. if (shutdown(g_sockfd, SHUT_WR) == -1) {
  330. perror("shutdown");
  331. exit(EXIT_FAILURE);
  332. }
  333. if (recv_dump_all(g_sockfd) == -1) {
  334. perror("recv_dump_all");
  335. exit(EXIT_FAILURE);
  336. }
  337. close(g_sockfd);
  338. return EXIT_SUCCESS;
  339. }