c1c9a557c7368229b647ebea1467f47c2d6c0207
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 static void cl_update_svbullet(ProtocolMessage m
) {
54 void cl_connect(const char * host
, uint16_t port
) {
55 if(SDLNet_ResolveHost(&addr
, host
, (port
) ? (port
) : (DEFAULT_PORT
)) != 0) {
56 SDL_Log("Unable to resolve host: %s\n", SDLNet_GetError());
61 SendMessage(sock
, addr
, cl_info(nickname
));
64 bool received
= WaitMessage(sock
, NULL
, &m
, 10000);
66 SDL_Log("Connection timeout");
71 if(m
.type
== SV_KILL
) {
73 } else if(m
.type
== SV_INFO
) {
76 SDL_Log("Invalid first message %i\n", m
.type
);
81 void cl_move(DoesCode code
) {
82 g_player_does(cl_playerid
, code
);
83 SendMessage(sock
, addr
, cl_does(code
));
89 if(!RecvMessage(sock
, &address
, &m
))
93 case SV_INFO
: cl_update_svinfo(m
); break;
94 case SV_KILL
: cl_kill_client(m
); break;
95 case SV_SPLR
: cl_update_svplayer(m
); break;
96 case SV_SBUL
: cl_update_svbullet(m
); break;
97 default: SDL_Log("invalid message %i", m
.type
);
101 void cl_disconnect(bool force
) {
106 SendMessage(sock
, addr
, cl_kill());