2 #include <SDL2/SDL_net.h>
12 static IPaddress addr
;
13 static UDPsocket sock
;
14 static char nickname
[32] = "Anonymous";
16 static void cl_kill_client(ProtocolMessage m
) {
17 SDL_Log("Connection refused by server: %.*s\n", (int) sizeof(m
.sv
.kill
.message
), m
.sv
.kill
.message
);
21 static void cl_update_svinfo(ProtocolMessage m
) {
22 SDL_Log("Connected to server %.*s\n", (int) sizeof(m
.sv
.info
.name
), m
.sv
.info
.name
);
23 cl_playerid
= m
.sv
.info
.clientid
;
24 assert(cl_playerid
< MAX_PLAYERS
);
25 SDL_Log("Player id%i\n", cl_playerid
);
28 static void cl_update_svplayer(ProtocolMessage m
) {
41 void cl_connect(const char * host
, uint16_t port
) {
42 if(SDLNet_ResolveHost(&addr
, host
, (port
) ? (port
) : (DEFAULT_PORT
)) != 0) {
43 SDL_Log("Unable to resolve host: %s\n", SDLNet_GetError());
48 SendMessage(sock
, addr
, cl_info(nickname
));
51 bool received
= WaitMessage(sock
, NULL
, &m
, 10000);
53 SDL_Log("Connection timeout");
58 if(m
.type
== SV_KILL
) {
60 } else if(m
.type
== SV_INFO
) {
63 SDL_Log("Invalid first message %i\n", m
.type
);
68 void cl_move(DoesCode code
) {
69 g_player_does(cl_playerid
, code
);
70 SendMessage(sock
, addr
, cl_does(code
));
76 if(!RecvMessage(sock
, &address
, &m
))
80 case SV_INFO
: cl_update_svinfo(m
); break;
81 case SV_KILL
: cl_kill_client(m
); break;
82 case SV_SPLR
: cl_update_svplayer(m
); break;
83 default: SDL_Log("invalid message %i", m
.type
);
87 void cl_disconnect(bool force
) {
92 SendMessage(sock
, addr
, cl_kill());