DEADSOFTWARE

Изменены физические константы
[netwar.git] / network.c
1 #include "network.h"
3 void SendMessage(UDPsocket sock, IPaddress address, ProtocolMessage m) {
4 UDPpacket packet = {
5 .channel = -1,
6 .data = (void *) &m,
7 .len = MessageSize(m),
8 .maxlen = sizeof(ProtocolMessage),
9 .status = 0,
10 .address = address,
11 };
13 if(!SDLNet_UDP_Send(sock, -1, &packet)) {
14 SDL_Log("Messge not sended: %s\n", SDLNet_GetError());
15 exit(1);
16 }
17 }
19 bool RecvMessage(UDPsocket sock, IPaddress * address, ProtocolMessage * m) {
20 UDPpacket packet = {
21 .channel = -1,
22 .data = (void *) m,
23 .len = sizeof(ProtocolMessage),
24 .maxlen = sizeof(ProtocolMessage),
25 .status = 0,
26 .address = (IPaddress) { 0, 0 },
27 };
29 int status;
30 status = SDLNet_UDP_Recv(sock, &packet);
31 if(status < 0)
32 goto error_recv;
33 else if(status == 0)
34 goto error_norecv;
36 if(address)
37 *address = packet.address;
39 return true;
41 error_recv:
42 SDL_Log("Messge not received: %s\n", SDLNet_GetError());
43 exit(1);
44 error_norecv:
45 return false;
46 }
48 bool WaitMessage(UDPsocket sock, IPaddress * address, ProtocolMessage * m, uint32_t timeout) {
49 uint32_t lastTime = SDL_GetTicks();
50 while(SDL_GetTicks() - lastTime < timeout) {
51 if(RecvMessage(sock, address, m))
52 return true;
53 SDL_Delay(1);
54 }
55 return false;
56 }