2022-04-07 18:46:57 +02:00

192 lines
3.9 KiB
C

#include <string.h> // for memset
#include "network.h"
#include "engine.h"
#include "utils.h"
#define SSID "MES-NDS"
#define PROTOCOL_IP_TCP 6
#define PROTOCOL_IP_UDP 17
#define MSG_KEY "FRZN"
#define MSG_KEY_LEN 4
saddr_t sa_out, sa_in;
s32 sockfd = 01;
bool socket_init = false;
bool wlpif_init = false;
#define MAX_WLP_RETRIES 1024
s16 init_wlpif()
{
if (wlpif_init == true)
return 0;
Wifi_AccessPoint ap;
s32 found = 0, count = 0, i = 0;
Wifi_InitDefault(false);
Wifi_ScanMode();
u8 retries = 0;
while (found == 0) {
count = Wifi_GetNumAP();
for (i = 0; (i < count) && (found == 0); i++) {
Wifi_GetAPData(i, &ap);
if (strcmp(SSID, ap.ssid) == 0)
found = 1;
}
++retries;
if (retries > MAX_WLP_RETRIES)
return -1;
}
Wifi_SetIP(0, 0, 0, 0, 0);
Wifi_ConnectAP(&ap, WEPMODE_NONE, 0, 0);
s32 status = ASSOCSTATUS_DISCONNECTED;
while ((status != ASSOCSTATUS_ASSOCIATED) &&
(status != ASSOCSTATUS_CANNOTCONNECT)) {
status = Wifi_AssocStatus();
swiWaitForVBlank();
}
wlpif_init = (status == ASSOCSTATUS_ASSOCIATED);
return 0;
}
// ======================================================================
s16 init_socket(const s16 t)
{
if (socket_init == true) return 0;
if (wlpif_init == false) return -1;
sockfd = socket(AF_INET, SOCK_STREAM, PROTOCOL_IP_TCP);//IPPROTO_TCP);
//sockfd = socket(AF_INET, SOCK_DGRAM, PROTOCOL_IP_UDP);//IPPROTO_UDP);
if (sockfd == -1) return -1;
if (t >= 0) {
struct timeval timeout;
memset(&timeout, 0, sizeof(timeout));
timeout.tv_sec = t;
timeout.tv_usec = 100;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO,
&timeout, sizeof(timeout)) == -1) {
closesocket(sockfd);
return -1;
}
}
sa_in.sin_family = AF_INET;
sa_in.sin_port = htons(DEFAULT_PORT);
sa_in.sin_addr.s_addr = 0x00000000;
if (bind(sockfd, (struct sockaddr*)&sa_in, sizeof(sa_in)) < 0) {
closesocket(sockfd);
return -1;
}
//struct hostent* server_host = gethostbyname(DEFAULT_IP);
sa_out.sin_family = AF_INET;
sa_out.sin_port = htons(DEFAULT_PORT);
sa_out.sin_addr.s_addr = DEFAULT_IP_BIN; //*(server_host->h_addr_list[0]);//
//char nonblock = 1;
//ioctl(sockfd, FIONBIO, &nonblock);
if (connect(sockfd, (struct sockaddr*)&sa_out, sizeof(sa_out)) < 0) {
closesocket(sockfd);
return -1;
}
socket_init = true;
return 0;
}
void end_socket()
{
if (socket_init == false)
return;
shutdown(sockfd, SHUT_RDWR);
closesocket(sockfd);
socket_init = false;
}
void end_wlpif()
{
if (wlpif_init == false)
return;
Wifi_DisconnectAP();
wlpif_init = false;
}
s32 net_snd(char* data_buff, s32 bytes)
{
if (socket_init == false)
return -1;
sendto(sockfd, data_buff, bytes, 0, (struct sockaddr *)&sa_out, sizeof(sa_out));
return 1;
}
s32 net_rcv(char* data_buff, s32 bytes)
{
if (socket_init == false)
return -1;
s32 received_bytes;
s32 info_size = sizeof(sa_in);
received_bytes = recvfrom(sockfd, data_buff, bytes,
~MSG_PEEK,
(struct sockaddr *)&sa_in, (int*) &info_size);
return received_bytes;
}
bool rcv_room_from_server(char* data_buff, s32 bytes, gs_t* const game_status)
{
if (socket_init == false)
return -1;
memset(data_buff, 0, 1024);
s32 data_buff_len = net_rcv(data_buff, bytes);
if (data_buff_len < 0) {
return false;
} else if (data_buff_len == 0) {
*game_status = QUIT;
return false;
}
if ((s32)data_buff_len == strlen(data_buff)) {
return true;
} else {
return false;
}
}