mirror of
https://github.com/Maschell/controller_patcher.git
synced 2024-11-28 22:44:24 +01:00
88 lines
2.0 KiB
C
88 lines
2.0 KiB
C
|
#include <network/net.h>
|
||
|
#include <nsysnet/socket.h>
|
||
|
#include <coreinit/systeminfo.h>
|
||
|
#include <coreinit/thread.h>
|
||
|
#include <nn/ac/ac_c.h>
|
||
|
|
||
|
static uint32_t hostIpAddress __attribute__((section(".data"))) = 0;
|
||
|
static volatile int socket_lock __attribute__((section(".data"))) = 0;
|
||
|
|
||
|
void initNetwork(){
|
||
|
|
||
|
}
|
||
|
|
||
|
int32_t recvwait(int32_t sock, void *buffer, int32_t len) {
|
||
|
while(socket_lock) {
|
||
|
OSSleepTicks(OSMicrosecondsToTicks(1000));
|
||
|
}
|
||
|
int32_t ret;
|
||
|
while (len > 0) {
|
||
|
ret = recv(sock, buffer, len, 0);
|
||
|
if(ret < 0) {
|
||
|
socket_lock = 0;
|
||
|
return ret;
|
||
|
}
|
||
|
len -= ret;
|
||
|
buffer = (void *)(((char *) buffer) + ret);
|
||
|
}
|
||
|
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) {
|
||
|
while(socket_lock) {
|
||
|
OSSleepTicks(OSMicrosecondsToTicks(1000));
|
||
|
}
|
||
|
unsigned char buffer[1];
|
||
|
int32_t ret;
|
||
|
|
||
|
ret = recv(sock, buffer, 1, MSG_DONTWAIT);
|
||
|
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) {
|
||
|
while(socket_lock) {
|
||
|
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;
|
||
|
ret = send(sock, buffer, cur_length, 0);
|
||
|
if(ret < 0) {
|
||
|
socket_lock = 0;
|
||
|
return ret;
|
||
|
}
|
||
|
len -= ret;
|
||
|
buffer = (void *)(((char *) buffer) + ret);
|
||
|
}
|
||
|
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);
|
||
|
}
|