assaultfish/client/net.c

43 lines
1.1 KiB
C
Raw Normal View History

2025-12-20 00:32:49 +09:00
#include <af_common.h>
static fishsoup_client_t* client = NULL;
static void net_return(MwWidget handle, void* user, void* client) {
scene_change(AF_SCENE_MAIN);
}
2025-12-20 05:15:27 +09:00
static void on_packet(fishsoup_client_t* client, fishsoup_packet_t* pkt) {
}
2025-12-20 00:32:49 +09:00
void net_connect(const char* username, const char* hostname, int port) {
2025-12-20 11:06:31 +09:00
fishsoup_packet_t pkt;
af_packet_join_t join;
2025-12-20 00:32:49 +09:00
scene_change(AF_SCENE_CONNECTING);
client = fishsoup_client(hostname, port == 0 ? AF_DEFAULT_PORT : port);
if(client == NULL) {
MwAddUserHandler(ui_message("Failed to connect"), MwNactivateHandler, net_return, NULL);
return;
}
2025-12-20 05:15:27 +09:00
2025-12-20 11:06:31 +09:00
pkt.header.type = AF_PACKET_JOIN;
pkt.header.length = sizeof(join);
pkt.data = &join;
2025-12-20 05:15:27 +09:00
fishsoup_client_on_packet(client, on_packet);
2025-12-20 11:06:31 +09:00
fishsoup_packet_send(client->fd, &pkt);
2025-12-20 00:32:49 +09:00
}
void net_poll(void) {
int ret;
if(client == NULL) return;
while((ret = fishsoup_client_poll(client)) > 0);
if(ret < 0) {
MwAddUserHandler(ui_message("Connection destroyed"), MwNactivateHandler, net_return, NULL);
fishsoup_client_destroy(client);
client = NULL;
return;
}
}