network.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* $Id: network.c,v 1.14 2001/04/29 22:24:37 kilobug Exp $ */
  2. #include "client.h"
  3. void send_goodbye(conf_t *conf)
  4. {
  5. if ((conf->connected) && (conf->debug))
  6. {
  7. net_wr_string(conf->file, "END");
  8. fflush(conf->file);
  9. net_wait_ack(conf->socket);
  10. net_chk_string(conf->socket, "BYE");
  11. net_send_ack(conf->file);
  12. }
  13. if (conf->connected)
  14. close_socket(conf);
  15. }
  16. void network_error(net_type_t get, net_type_t wanted, void *data)
  17. {
  18. char buf[100];
  19. conf_t *conf;
  20. conf = data;
  21. if ((conf == NULL) || (!conf->connected))
  22. return;
  23. conf->connected = FALSE;
  24. g_snprintf(buf, 100,
  25. "Erreur réseau: `wanted (%s), get (%s)'. Connexion perdue.\n",
  26. net_type_name(wanted), net_type_name(get));
  27. set_status(conf, buf);
  28. }
  29. void do_connect(conf_t *conf)
  30. {
  31. char buf[420];
  32. send_goodbye(conf);
  33. conf->terminated = FALSE;
  34. snprintf(buf, 420, "Connexion en cours sur %s:%d...", conf->host,
  35. conf->port);
  36. set_status(conf, buf);
  37. conf->socket = net_connect_to(conf->host, conf->port, buf, 420, NULL);
  38. if (conf->socket == -1)
  39. {
  40. set_status(conf, buf);
  41. return;
  42. }
  43. snprintf(buf, 420,
  44. "Connexion établie avec %s:%d... authentification en cours.",
  45. conf->host, conf->port);
  46. set_status(conf, buf);
  47. conf->connected = TRUE;
  48. conf->file = fdopen(conf->socket, "w");
  49. net_wr_string(conf->file, "HELLO 42");
  50. fflush(conf->file);
  51. if (!net_chk_string(conf->socket, "HELLO 42"))
  52. {
  53. snprintf(buf, 420, "%s:%d n'est pas un serveur Prologin.",
  54. conf->host, conf->port);
  55. set_status(conf, buf);
  56. conf->socket = -1;
  57. return;
  58. }
  59. net_wr_int(conf->file, PROTOCOL_VERSION);
  60. fflush(conf->file);
  61. if (!net_wait_ack(conf->socket))
  62. {
  63. snprintf(buf, 420, "%s:%d connexion refusée (mauvaise version).",
  64. conf->host, conf->port);
  65. set_status(conf, buf);
  66. conf->socket = -1;
  67. return;
  68. }
  69. net_wr_flag(conf->file, conf->debug);
  70. fflush(conf->file);
  71. if (!net_wait_ack(conf->socket))
  72. {
  73. snprintf(buf, 420, "%s:%d connexion refusée (mauvais type de client).",
  74. conf->host, conf->port);
  75. set_status(conf, buf);
  76. conf->socket = -1;
  77. return;
  78. }
  79. net_send_ack(conf->file);
  80. load_map(conf);
  81. conf->gdk_io_id = gdk_input_add(conf->socket, GDK_INPUT_READ,
  82. update_map_callback, conf);
  83. snprintf(buf, 420,
  84. "Connexion établie avec %s:%d.",
  85. conf->host, conf->port);
  86. set_status(conf, buf);
  87. if (conf->conf != NULL)
  88. gtk_widget_hide(conf->conf);
  89. }
  90. void protocol_error(conf_t *conf, const char *msg)
  91. {
  92. char buf[420];
  93. net_send_abort(conf->file);
  94. close_socket(conf);
  95. fprintf(stderr, "Erreur fatale de protocole avec %s:%d: %s.\n",
  96. conf->host, conf->port, msg);
  97. snprintf(buf, 420, "Erreur fatale de protocole avec %s:%d: %s.",
  98. conf->host, conf->port, msg);
  99. set_status(conf, buf);
  100. conf->ready = FALSE;
  101. }
  102. void close_socket(conf_t *conf)
  103. {
  104. if (conf->socket > 0)
  105. {
  106. if (conf->gdk_io_id >= 0)
  107. gdk_input_remove(conf->gdk_io_id);
  108. conf->connected = FALSE;
  109. fclose(conf->file);
  110. conf->socket = -1;
  111. conf->file = NULL;
  112. }
  113. }
  114. void allow_new_turn(conf_t *conf)
  115. {
  116. if (!conf->connected)
  117. return;
  118. if (!conf->ready)
  119. return;
  120. conf->ready = FALSE;
  121. if (conf->gdk_io_id >= 0)
  122. gdk_input_remove(conf->gdk_io_id);
  123. if (conf->terminated)
  124. {
  125. send_goodbye(conf);
  126. return;
  127. }
  128. else
  129. net_wr_string(conf->file, "GO");
  130. fflush(conf->file);
  131. if (!net_wait_ack(conf->socket))
  132. protocol_error(conf, "expected ACK.");
  133. conf->gdk_io_id = gdk_input_add(conf->socket, GDK_INPUT_READ,
  134. update_map_callback, conf);
  135. }