salvage socket init code

This commit is contained in:
StapleButter 2017-05-11 19:57:49 +02:00
parent eb7154e426
commit 9a2e2998e9
6 changed files with 111 additions and 3 deletions

View File

@ -387,7 +387,7 @@ void GPU2D::DrawScanline(u32 line)
if (line > 192)
{
for (int i = 0; i < 256; i++)
dst[i] = 0xFF3F3F3F;
dst[i] = 0xFFFFFFFF;
return;
}

View File

@ -130,6 +130,7 @@ bool Init()
if (!SPU::Init()) return false;
if (!SPI::Init()) return false;
if (!RTC::Init()) return false;
if (!Wifi::Init()) return false;
return true;
}
@ -150,6 +151,7 @@ void DeInit()
SPU::DeInit();
SPI::DeInit();
RTC::DeInit();
Wifi::DeInit();
}

View File

@ -19,10 +19,13 @@
#ifndef PLATFORM_H
#define PLATFORM_H
#include "types.h"
namespace Platform
{
//
bool MP_Init();
void MP_DeInit();
}

View File

@ -21,6 +21,7 @@
#include "NDS.h"
#include "SPI.h"
#include "Wifi.h"
#include "Platform.h"
namespace Wifi
@ -47,6 +48,8 @@ u16 RFData1;
u16 RFData2;
u32 RFRegs[0x40];
bool MPInited;
// multiplayer host TX sequence:
// 1. preamble
@ -74,6 +77,19 @@ u32 RFRegs[0x40];
// * work out how power saving works, there are oddities
bool Init()
{
MPInited = false;
return true;
}
void DeInit()
{
if (MPInited)
Platform::MP_DeInit();
}
void Reset()
{
memset(RAM, 0, 0x2000);
@ -448,9 +464,18 @@ void Write(u32 addr, u16 val)
// schedule timer event when the clock is enabled
// TODO: check whether this resets USCOUNT (and also which other events can reset it)
if ((IOPORT(W_PowerUS) & 0x0001) && !(val & 0x0001))
{
NDS::ScheduleEvent(NDS::Event_Wifi, true, 33, USTimer, 0);
if (!MPInited)
{
Platform::MP_Init();
MPInited = true;
}
}
else if (!(IOPORT(W_PowerUS) & 0x0001) && (val & 0x0001))
{
NDS::CancelEvent(NDS::Event_Wifi);
}
break;
case W_USCountCnt: val &= 0x0001; break;

View File

@ -140,6 +140,8 @@ enum
};
bool Init();
void DeInit();
void Reset();
void USTimer(u32 param);

View File

@ -17,14 +17,90 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../Platform.h"
#ifdef __WXMSW__
#include <winsock2.h>
#include <ws2tcpip.h>
#define socket_t SOCKET
#define sockaddr_t SOCKADDR
#define pcap_dev_name description
#else
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define socket_t int
#define sockaddr_t struct sockaddr
#define closesocket close
#define pcap_dev_name name
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (socket_t)-1
#endif
namespace Platform
{
//
socket_t MPSocket;
sockaddr_t MPSendAddr;
bool MP_Init()
{
BOOL opt_true = TRUE;
int res;
MPSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (MPSocket < 0)
{
return false;
}
res = setsockopt(MPSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt_true, sizeof(BOOL));
if (res < 0)
{
closesocket(MPSocket);
MPSocket = INVALID_SOCKET;
return false;
}
sockaddr_t saddr;
saddr.sa_family = AF_INET;
*(u32*)&saddr.sa_data[2] = htonl(INADDR_ANY);
*(u16*)&saddr.sa_data[0] = htons(7064);
res = bind(MPSocket, &saddr, sizeof(sockaddr_t));
if (res < 0)
{
closesocket(MPSocket);
MPSocket = INVALID_SOCKET;
return false;
}
res = setsockopt(MPSocket, SOL_SOCKET, SO_BROADCAST, (const char*)&opt_true, sizeof(BOOL));
if (res < 0)
{
closesocket(MPSocket);
MPSocket = INVALID_SOCKET;
return false;
}
MPSendAddr.sa_family = AF_INET;
*(u32*)&MPSendAddr.sa_data[2] = htonl(INADDR_BROADCAST);
*(u16*)&MPSendAddr.sa_data[0] = htons(7064);
return true;
}
void MP_DeInit()
{
if (MPSocket >= 0)
closesocket(MPSocket);
}
}