Replace network code with new tcp implementation

This commit is contained in:
Ian Curtis 2020-06-16 11:55:38 +00:00
parent 8071efbf79
commit daef27be37
9 changed files with 106 additions and 131 deletions

View file

@ -78,7 +78,7 @@ const char *CJTAG::s_state[] =
static void SaveBitRegister(CBlockFile *SaveState, const Util::BitRegister &reg)
{
uint16_t size = reg.Size() + 1; // include null terminator
uint16_t size = (uint16_t)reg.Size() + 1; // include null terminator
SaveState->Write(&size, sizeof(size));
SaveState->Write(reg.ToBinaryString());
}

View file

@ -2471,9 +2471,6 @@ void CModel3::DumpTimings(void)
timings.syncTicks, (timings.syncTicks > 1 ? '!' : ','),
timings.sndTicks, (timings.sndTicks > 10 ? '!' : ','),
timings.drvTicks, (timings.drvTicks > 10 ? '!' : ','),
#ifdef NET_BOARD
timings.netTicks, (timings.netTicks > 10 ? '!' : ','), // TODO fix this line 'printf' : too many arguments passed for format string
#endif
timings.frameTicks, (timings.frameTicks > 16 ? '!' : ' '));
}
@ -3125,7 +3122,6 @@ bool CModel3::Init(void)
#ifdef NET_BOARD
netRAM = &memoryPool[OFFSET_NETRAM];
netBuffer = &memoryPool[OFFSET_NETBUFFER];
m_runNetBoard = m_game.stepping != "1.0" && (NetBoard.IsAttached() && (m_config["EmulateNet"].ValueAs<bool>()));
#endif
SetCROMBank(0xFF);
@ -3143,8 +3139,11 @@ bool CModel3::Init(void)
if (OKAY != SoundBoard.Init(soundROM,sampleROM))
return FAIL;
#ifdef NET_BOARD
if (OKAY != NetBoard.Init(netRAM, netBuffer))
if (OKAY != NetBoard.Init(netRAM, netBuffer)) {
return FAIL;
}
m_runNetBoard = m_game.stepping != "1.0" && (NetBoard.IsAttached() && (m_config["EmulateNet"].ValueAs<bool>()));
#endif
PCIBridge.AttachPCIBus(&PCIBus);

View file

@ -76,9 +76,8 @@
#include "NetBoard.h"
#include "Util/Format.h"
#include "Util/ByteSwap.h"
#include <thread>
#include "UDPSend.h"
#include "UDPReceive.h"
#include "TCPSend.h"
#include "TCPReceive.h"
// few macros to make debugging a bit less painful
// if NET_DEBUG is defined, printf works normally, otherwise it's compiled to nothing (ie removed)
@ -98,8 +97,6 @@
#define SAFE_ARRAY_DELETE(x) delete[] x; x = NULL;
#endif
using namespace SMUDP;
static int(*Runnet68kCB)(int cycles);
static void(*Intnet68kCB)(int irq);
@ -602,15 +599,10 @@ void CNetBoard::Write8(UINT32 a, UINT8 d)
//printf("-> nb recu : %x\n", recv_data.size());
//memcpy(CommRAM + recv_offset, recv_data.data(), recv_data.size());
//DPRINTF("receive enable off=%x size=%x\n", recv_offset, recv_size);
UINT16 s = 0;
std::vector <uint8_t> recv_data;
do
{
recv_data = udpReceive.ReadData(500);
DPRINTF("-> nb recu : %x\n", recv_data.size());
s = recv_data.size();
DPRINTF("receive enable off=%x size=%x\n", recv_offset, recv_size);
} while (s == 0);
auto &recv_data = netr->Receive();
memcpy(CommRAM + recv_offset, recv_data.data(), recv_data.size());
}
else
@ -629,7 +621,8 @@ void CNetBoard::Write8(UINT32 a, UINT8 d)
//printf("receive enable off=%x size=%x slot=%x\n", recv_offset, recv_size, slot);
auto recv_data = udpReceive.ReadData(5000);
//auto recv_data = udpReceive.ReadData(5000);
auto &recv_data = netr->Receive();
DPRINTF("-> nb recu : %x\n", recv_data.size());
memcpy(CommRAM + recv_offset, recv_data.data(), recv_data.size());
}
@ -708,7 +701,8 @@ void CNetBoard::Write8(UINT32 a, UINT8 d)
//if (send_offset > 0x1000)
if (send_size < 0x0011) // must find a better condition
{
udpSend.SendAsync(addr_out.data(), port_out, send_size, (const char*)CommRAM + send_offset, 1000);
//udpSend.SendAsync(addr_out.data(), port_out, send_size, (const char*)CommRAM + send_offset, 1000);
nets->Send((const char*)CommRAM + send_offset, send_size);
DPRINTF("send enable off=%x size=%x\n", send_offset, send_size);
}
@ -738,7 +732,8 @@ void CNetBoard::Write8(UINT32 a, UINT8 d)
}
send_offset = (send_offset << 8) | (send_offset >> 8);
udpSend.SendAsync(addr_out.data(), port_out, send_size, (const char*)CommRAM + send_offset, 1000);
//udpSend.SendAsync(addr_out.data(), port_out, send_size, (const char*)CommRAM + send_offset, 1000);
nets->Send((const char*)CommRAM + send_offset, send_size);
DPRINTF("send enable off=%x size=%x slot=%x\n", send_offset, send_size, slot);
}
@ -1230,7 +1225,7 @@ bool CNetBoard::Init(UINT8 * netRAMPtr, UINT8 *netBufferPtr)
ctrlrw = ct;
printf("Init netboard netbuffer=%x netram=%x CommRAM=%x ioreg=%x ctrlrw=%x bank=%x\n", netBuffer, netRAM, CommRAM, ioreg, ctrlrw,bank);
printf("Init netboard\n");
// Initialize 68K core
@ -1247,7 +1242,15 @@ bool CNetBoard::Init(UINT8 * netRAMPtr, UINT8 *netBufferPtr)
port_in = m_config["port_in"].ValueAs<unsigned>();
port_out = m_config["port_out"].ValueAs<unsigned>();
addr_out = m_config["addr_out"].ValueAs<std::string>();
udpReceive.Bind(port_in);
nets = std::make_unique<TCPSend>(addr_out, port_out);
netr = std::make_unique<TCPReceive>(port_in);
while (!nets->Connect()) {
printf("Connecting to %s:%i ..\n", addr_out.c_str(), port_out);
}
printf("Successfully connected.\n");
return OKAY;
}

View file

@ -6,9 +6,9 @@
#include "OSD/Thread.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <thread>
#include "UDPSend.h"
#include "UDPReceive.h"
#include <memory>
#include "TCPSend.h"
#include "TCPReceive.h"
//#define NET_BUF_SIZE 32800 // 16384 not enough
@ -72,8 +72,8 @@ private:
UINT16 port_out = 0;
std::string addr_out = "";
SMUDP::UDPSend udpSend;
SMUDP::UDPReceive udpReceive;
std::unique_ptr<TCPSend> nets;
std::unique_ptr<TCPReceive> netr;
//game info
Game Gameinfo;

View file

@ -3,6 +3,13 @@
using namespace std::chrono_literals;
#if defined(_DEBUG)
#include <stdio.h>
#define DPRINTF printf
#else
#define DPRINTF(a, ...)
#endif
TCPReceive::TCPReceive(int port) :
m_listenSocket(nullptr),
m_receiveSocket(nullptr)
@ -45,7 +52,7 @@ TCPReceive::~TCPReceive()
std::vector<char>& TCPReceive::Receive()
{
if (!m_receiveSocket) {
printf("return here because no socket\n");
DPRINTF("Can't receive because no socket.\n");
m_recBuffer.clear();
return m_recBuffer;
}
@ -54,7 +61,7 @@ std::vector<char>& TCPReceive::Receive()
int result = 0;
result = SDLNet_TCP_Recv(m_receiveSocket, &size, sizeof(int));
printf("received %i\n", result);
DPRINTF("Received %i bytes\n", result);
if (result <= 0) {
SDLNet_TCP_Close(m_receiveSocket);
m_receiveSocket = nullptr;
@ -66,7 +73,7 @@ std::vector<char>& TCPReceive::Receive()
while (size) {
result = SDLNet_TCP_Recv(m_receiveSocket, m_recBuffer.data() + (m_recBuffer.size() - size), size);
printf("received %i\n", result);
DPRINTF("Received %i bytes\n", result);
if (result <= 0) {
SDLNet_TCP_Close(m_receiveSocket);
m_receiveSocket = nullptr;
@ -90,7 +97,7 @@ void TCPReceive::ListenFunc()
if (socket) {
m_receiveSocket = socket;
printf("accepted a connectio\n");
DPRINTF("Accepted connection.\n");
}
}

View file

@ -1,11 +1,17 @@
#include "TCPSend.h"
#if defined(_DEBUG)
#include <stdio.h>
#define DPRINTF printf
#else
#define DPRINTF(a, ...)
#endif
static const int RETRY_COUNT = 10; // shrugs
TCPSend::TCPSend(std::string& ip, int port) :
m_ip(ip),
m_port(port),
m_tryCount(0),
m_socket(nullptr)
{
SDLNet_Init();
@ -23,19 +29,14 @@ TCPSend::~TCPSend()
bool TCPSend::Send(const void * data, int length)
{
printf("trying to send data %i\n", length);
// If we aren't connected make a connection
if (!Connected()) {
Connect();
printf("trying to connect\n");
}
// If we failed bail out
if (!Connected()) {
printf("not connected\n");
DPRINTF("Not connected\n");
return false;
}
DPRINTF("Sending %i bytes\n", length);
if (!length) {
return true; // 0 sized packet will blow our connex
}
@ -50,7 +51,7 @@ bool TCPSend::Send(const void * data, int length)
m_socket = nullptr;
}
return false;
return true;
}
bool TCPSend::Connected()
@ -58,12 +59,8 @@ bool TCPSend::Connected()
return m_socket != 0;
}
void TCPSend::Connect()
bool TCPSend::Connect()
{
if (m_tryCount >= RETRY_COUNT) {
return; // already tried and failed so bail out. We do this as instances might load diff times
}
IPaddress ip;
int result = SDLNet_ResolveHost(&ip, m_ip.c_str(), m_port);
@ -71,5 +68,5 @@ void TCPSend::Connect()
m_socket = SDLNet_TCP_Open(&ip);
}
m_tryCount++;
return Connected();
}

View file

@ -11,15 +11,12 @@ public:
~TCPSend();
bool Send(const void* data, int length);
bool Connect();
bool Connected();
private:
void Connect();
std::string m_ip;
int m_port;
bool m_connected;
int m_tryCount;
TCPsocket m_socket; // sdl socket
};

View file

@ -341,9 +341,6 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config"</Command>
<ClCompile Include="..\Src\Network\NetBoard.cpp" />
<ClCompile Include="..\Src\Network\TCPReceive.cpp" />
<ClCompile Include="..\Src\Network\TCPSend.cpp" />
<ClCompile Include="..\Src\Network\UDPReceive.cpp" />
<ClCompile Include="..\Src\Network\UDPSend.cpp" />
<ClCompile Include="..\Src\Network\WinSockWrap.cpp" />
<ClCompile Include="..\Src\OSD\Logger.cpp" />
<ClCompile Include="..\Src\OSD\Outputs.cpp" />
<ClCompile Include="..\Src\OSD\SDL\Audio.cpp" />
@ -515,10 +512,6 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config"</Command>
<ClInclude Include="..\Src\Network\NetBoard.h" />
<ClInclude Include="..\Src\Network\TCPReceive.h" />
<ClInclude Include="..\Src\Network\TCPSend.h" />
<ClInclude Include="..\Src\Network\UDPPacket.h" />
<ClInclude Include="..\Src\Network\UDPReceive.h" />
<ClInclude Include="..\Src\Network\UDPSend.h" />
<ClInclude Include="..\Src\Network\WinSockWrap.h" />
<ClInclude Include="..\Src\OSD\Audio.h" />
<ClInclude Include="..\Src\OSD\Logger.h" />
<ClInclude Include="..\Src\OSD\Outputs.h" />

View file

@ -416,15 +416,6 @@
<ClCompile Include="..\Src\Model3\JTAG.cpp">
<Filter>Source Files\Model3</Filter>
</ClCompile>
<ClCompile Include="..\Src\Network\UDPReceive.cpp">
<Filter>Source Files\Network</Filter>
</ClCompile>
<ClCompile Include="..\Src\Network\UDPSend.cpp">
<Filter>Source Files\Network</Filter>
</ClCompile>
<ClCompile Include="..\Src\Network\WinSockWrap.cpp">
<Filter>Source Files\Network</Filter>
</ClCompile>
<ClCompile Include="..\Src\Network\NetBoard.cpp">
<Filter>Source Files\Network</Filter>
</ClCompile>
@ -778,18 +769,6 @@
<ClInclude Include="..\Src\Util\BitRegister.h">
<Filter>Header Files\Util</Filter>
</ClInclude>
<ClInclude Include="..\Src\Network\UDPPacket.h">
<Filter>Header Files\Network</Filter>
</ClInclude>
<ClInclude Include="..\Src\Network\UDPReceive.h">
<Filter>Header Files\Network</Filter>
</ClInclude>
<ClInclude Include="..\Src\Network\UDPSend.h">
<Filter>Header Files\Network</Filter>
</ClInclude>
<ClInclude Include="..\Src\Network\WinSockWrap.h">
<Filter>Header Files\Network</Filter>
</ClInclude>
<ClInclude Include="..\Src\Network\NetBoard.h">
<Filter>Header Files\Network</Filter>
</ClInclude>