controller_patcher/source/network/net.c

84 lines
1.9 KiB
C
Raw Normal View History

2020-12-16 01:59:00 +01:00
#include <coreinit/systeminfo.h>
#include <coreinit/thread.h>
2021-09-19 21:16:03 +02:00
#include <sys/socket.h>
2020-12-16 01:59:00 +01:00
static volatile int socket_lock __attribute__((section(".data"))) = 0;
2023-04-10 11:45:58 +02:00
void initNetwork() {
2020-12-16 01:59:00 +01:00
}
int32_t recvwait(int32_t sock, void *buffer, int32_t len) {
2023-04-10 11:45:58 +02:00
while (socket_lock) {
2020-12-16 01:59:00 +01:00
OSSleepTicks(OSMicrosecondsToTicks(1000));
}
int32_t ret;
while (len > 0) {
ret = recv(sock, buffer, len, 0);
2023-04-10 11:45:58 +02:00
if (ret < 0) {
2020-12-16 01:59:00 +01:00
socket_lock = 0;
return ret;
}
len -= ret;
2023-04-10 11:45:58 +02:00
buffer = (void *) (((char *) buffer) + ret);
2020-12-16 01:59:00 +01:00
}
socket_lock = 0;
return 0;
}
uint8_t recvbyte(int32_t sock) {
unsigned char buffer[1];
int32_t ret;
ret = recvwait(sock, buffer, 1);
if (ret < 0) return ret;
return buffer[0];
}
uint32_t recvword(int32_t sock) {
uint32_t result;
int32_t ret;
ret = recvwait(sock, &result, 4);
if (ret < 0) return ret;
return result;
}
int32_t checkbyte(int32_t sock) {
2023-04-10 11:45:58 +02:00
while (socket_lock) {
2020-12-16 01:59:00 +01:00
OSSleepTicks(OSMicrosecondsToTicks(1000));
}
unsigned char buffer[1];
int32_t ret;
2023-04-10 11:45:58 +02:00
ret = recv(sock, buffer, 1, MSG_DONTWAIT);
2020-12-16 01:59:00 +01:00
socket_lock = 0;
if (ret < 0) return ret;
if (ret == 0) return -1;
return buffer[0];
}
int32_t sendwait(int32_t sock, const void *buffer, int32_t len) {
2023-04-10 11:45:58 +02:00
while (socket_lock) {
2020-12-16 01:59:00 +01:00
OSSleepTicks(OSMicrosecondsToTicks(1000));
}
int32_t ret;
while (len > 0) {
// For some reason the send blocks/crashes if the buffer is too big..
int cur_length = len <= 0x30 ? len : 0x30;
2023-04-10 11:45:58 +02:00
ret = send(sock, buffer, cur_length, 0);
if (ret < 0) {
2020-12-16 01:59:00 +01:00
socket_lock = 0;
return ret;
}
len -= ret;
2023-04-10 11:45:58 +02:00
buffer = (void *) (((char *) buffer) + ret);
2020-12-16 01:59:00 +01:00
}
socket_lock = 0;
return 0;
}
int32_t sendbyte(int32_t sock, unsigned char byte) {
unsigned char buffer[1];
buffer[0] = byte;
return sendwait(sock, buffer, 1);
}