net_loop.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // net_loop.c
  16. #include "quakedef.h"
  17. #include "net_loop.h"
  18. qboolean localconnectpending = false;
  19. qsocket_t *loop_client = NULL;
  20. qsocket_t *loop_server = NULL;
  21. int Loop_Init (void)
  22. {
  23. if (cls.state == ca_dedicated)
  24. return -1;
  25. return 0;
  26. }
  27. void Loop_Shutdown (void)
  28. {
  29. }
  30. void Loop_Listen (qboolean state)
  31. {
  32. }
  33. void Loop_SearchForHosts (qboolean xmit)
  34. {
  35. if (!sv.active)
  36. return;
  37. hostCacheCount = 1;
  38. if (Q_strcmp(hostname.string, "UNNAMED") == 0)
  39. Q_strcpy(hostcache[0].name, "local");
  40. else
  41. Q_strcpy(hostcache[0].name, hostname.string);
  42. Q_strcpy(hostcache[0].map, sv.name);
  43. hostcache[0].users = net_activeconnections;
  44. hostcache[0].maxusers = svs.maxclients;
  45. hostcache[0].driver = net_driverlevel;
  46. Q_strcpy(hostcache[0].cname, "local");
  47. }
  48. qsocket_t *Loop_Connect (char *host)
  49. {
  50. if (Q_strcmp(host,"local") != 0)
  51. return NULL;
  52. localconnectpending = true;
  53. if (!loop_client)
  54. {
  55. if ((loop_client = NET_NewQSocket ()) == NULL)
  56. {
  57. Con_Printf("Loop_Connect: no qsocket available\n");
  58. return NULL;
  59. }
  60. Q_strcpy (loop_client->address, "localhost");
  61. }
  62. loop_client->receiveMessageLength = 0;
  63. loop_client->sendMessageLength = 0;
  64. loop_client->canSend = true;
  65. if (!loop_server)
  66. {
  67. if ((loop_server = NET_NewQSocket ()) == NULL)
  68. {
  69. Con_Printf("Loop_Connect: no qsocket available\n");
  70. return NULL;
  71. }
  72. Q_strcpy (loop_server->address, "LOCAL");
  73. }
  74. loop_server->receiveMessageLength = 0;
  75. loop_server->sendMessageLength = 0;
  76. loop_server->canSend = true;
  77. loop_client->driverdata = (void *)loop_server;
  78. loop_server->driverdata = (void *)loop_client;
  79. return loop_client;
  80. }
  81. qsocket_t *Loop_CheckNewConnections (void)
  82. {
  83. if (!localconnectpending)
  84. return NULL;
  85. localconnectpending = false;
  86. loop_server->sendMessageLength = 0;
  87. loop_server->receiveMessageLength = 0;
  88. loop_server->canSend = true;
  89. loop_client->sendMessageLength = 0;
  90. loop_client->receiveMessageLength = 0;
  91. loop_client->canSend = true;
  92. return loop_server;
  93. }
  94. static int IntAlign(int value)
  95. {
  96. return (value + (sizeof(int) - 1)) & (~(sizeof(int) - 1));
  97. }
  98. int Loop_GetMessage (qsocket_t *sock)
  99. {
  100. int ret;
  101. int length;
  102. if (sock->receiveMessageLength == 0)
  103. return 0;
  104. ret = sock->receiveMessage[0];
  105. length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8);
  106. // alignment byte skipped here
  107. SZ_Clear (&net_message);
  108. SZ_Write (&net_message, &sock->receiveMessage[4], length);
  109. length = IntAlign(length + 4);
  110. sock->receiveMessageLength -= length;
  111. if (sock->receiveMessageLength)
  112. Q_memcpy(sock->receiveMessage, &sock->receiveMessage[length], sock->receiveMessageLength);
  113. if (sock->driverdata && ret == 1)
  114. ((qsocket_t *)sock->driverdata)->canSend = true;
  115. return ret;
  116. }
  117. int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data)
  118. {
  119. byte *buffer;
  120. int *bufferLength;
  121. if (!sock->driverdata)
  122. return -1;
  123. bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
  124. if ((*bufferLength + data->cursize + 4) > NET_MAXMESSAGE)
  125. Sys_Error("Loop_SendMessage: overflow\n");
  126. buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
  127. // message type
  128. *buffer++ = 1;
  129. // length
  130. *buffer++ = data->cursize & 0xff;
  131. *buffer++ = data->cursize >> 8;
  132. // align
  133. buffer++;
  134. // message
  135. Q_memcpy(buffer, data->data, data->cursize);
  136. *bufferLength = IntAlign(*bufferLength + data->cursize + 4);
  137. sock->canSend = false;
  138. return 1;
  139. }
  140. int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data)
  141. {
  142. byte *buffer;
  143. int *bufferLength;
  144. if (!sock->driverdata)
  145. return -1;
  146. bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
  147. if ((*bufferLength + data->cursize + sizeof(byte) + sizeof(short)) > NET_MAXMESSAGE)
  148. return 0;
  149. buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
  150. // message type
  151. *buffer++ = 2;
  152. // length
  153. *buffer++ = data->cursize & 0xff;
  154. *buffer++ = data->cursize >> 8;
  155. // align
  156. buffer++;
  157. // message
  158. Q_memcpy(buffer, data->data, data->cursize);
  159. *bufferLength = IntAlign(*bufferLength + data->cursize + 4);
  160. return 1;
  161. }
  162. qboolean Loop_CanSendMessage (qsocket_t *sock)
  163. {
  164. if (!sock->driverdata)
  165. return false;
  166. return sock->canSend;
  167. }
  168. qboolean Loop_CanSendUnreliableMessage (qsocket_t *sock)
  169. {
  170. return true;
  171. }
  172. void Loop_Close (qsocket_t *sock)
  173. {
  174. if (sock->driverdata)
  175. ((qsocket_t *)sock->driverdata)->driverdata = NULL;
  176. sock->receiveMessageLength = 0;
  177. sock->sendMessageLength = 0;
  178. sock->canSend = true;
  179. if (sock == loop_client)
  180. loop_client = NULL;
  181. else
  182. loop_server = NULL;
  183. }