Port dolphin to SFML 2.1

This commit is contained in:
James Cowgill 2014-11-09 22:30:06 +00:00
parent 11dbe6b6ce
commit a93f86330e
14 changed files with 157 additions and 140 deletions

View File

@ -600,16 +600,15 @@ if(LIBUSB_FOUND)
include_directories(${LIBUSB_INCLUDE_DIR}) include_directories(${LIBUSB_INCLUDE_DIR})
endif(LIBUSB_FOUND) endif(LIBUSB_FOUND)
set(SFML_FIND_VERSION TRUE) set(SFML_REQD_VERSION 2.1)
set(SFML_FIND_VERSION_MAJOR 1)
set(SFML_FIND_VERSION_MINOR 5)
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT ANDROID) if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT ANDROID)
include(FindSFML OPTIONAL) find_package(SFML ${SFML_REQD_VERSION} COMPONENTS network system)
endif() endif()
if(SFML_FOUND AND NOT SFML_VERSION_MAJOR) # SFML 1.x doesn't define SFML_VERSION_MAJOR if(SFML_FOUND)
message("Using shared SFML") message("Using shared SFML")
else() else()
message("Using static SFML ${SFML_FIND_VERSION_MAJOR}.${SFML_FIND_VERSION_MINOR} from Externals") message("Using static SFML ${SFML_REQD_VERSION} from Externals")
add_definitions(-DSFML_STATIC)
add_subdirectory(Externals/SFML) add_subdirectory(Externals/SFML)
include_directories(BEFORE Externals/SFML/include) include_directories(BEFORE Externals/SFML/include)
endif() endif()

View File

@ -23,6 +23,5 @@ set(SRC_SYSTEM
src/SFML/System/Time.cpp src/SFML/System/Time.cpp
) )
add_definitions(-DSFML_STATIC)
add_library(sfml-network ${SRC_NETWORK}) add_library(sfml-network ${SRC_NETWORK})
add_library(sfml-system ${SRC_SYSTEM}) add_library(sfml-system ${SRC_SYSTEM})

View File

@ -238,6 +238,7 @@ set(LIBS
inputcommon inputcommon
${LZO} ${LZO}
sfml-network sfml-network
sfml-system
videoogl videoogl
videosoftware videosoftware
z z

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2 // Licensed under GPLv2
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "Common/StdMakeUnique.h"
#include "Core/Core.h" #include "Core/Core.h"
#include "Core/HW/EXI_Device.h" #include "Core/HW/EXI_Device.h"
#include "Core/HW/EXI_DeviceGecko.h" #include "Core/HW/EXI_DeviceGecko.h"
@ -10,8 +11,8 @@ u16 GeckoSockServer::server_port;
int GeckoSockServer::client_count; int GeckoSockServer::client_count;
std::thread GeckoSockServer::connectionThread; std::thread GeckoSockServer::connectionThread;
volatile bool GeckoSockServer::server_running; volatile bool GeckoSockServer::server_running;
std::queue<sf::SocketTCP> GeckoSockServer::waiting_socks;
std::mutex GeckoSockServer::connection_lock; std::mutex GeckoSockServer::connection_lock;
std::queue<std::unique_ptr<sf::TcpSocket>> GeckoSockServer::waiting_socks;
GeckoSockServer::GeckoSockServer() GeckoSockServer::GeckoSockServer()
: client_running(false) : client_running(false)
@ -41,11 +42,12 @@ void GeckoSockServer::GeckoConnectionWaiter()
{ {
Common::SetCurrentThreadName("Gecko Connection Waiter"); Common::SetCurrentThreadName("Gecko Connection Waiter");
sf::SocketTCP server; sf::TcpListener server;
server_port = 0xd6ec; // "dolphin gecko" server_port = 0xd6ec; // "dolphin gecko"
for (int bind_tries = 0; bind_tries <= 10 && !server_running; bind_tries++) for (int bind_tries = 0; bind_tries <= 10 && !server_running; bind_tries++)
{ {
if (!(server_running = server.Listen(server_port))) server_running = server.listen(server_port) == sf::Socket::Done;
if (!server_running)
server_port++; server_port++;
} }
@ -56,22 +58,23 @@ void GeckoSockServer::GeckoConnectionWaiter()
StringFromFormat("USBGecko: Listening on TCP port %u", server_port), StringFromFormat("USBGecko: Listening on TCP port %u", server_port),
5000); 5000);
server.SetBlocking(false); server.setBlocking(false);
sf::SocketTCP new_client; auto new_client = std::make_unique<sf::TcpSocket>();
while (server_running) while (server_running)
{ {
if (server.Accept(new_client) == sf::Socket::Done) if (server.accept(*new_client) == sf::Socket::Done)
{ {
std::lock_guard<std::mutex> lk(connection_lock); std::lock_guard<std::mutex> lk(connection_lock);
waiting_socks.push(new_client); waiting_socks.push(std::move(new_client));
new_client = std::make_unique<sf::TcpSocket>();
} }
SLEEP(1); SLEEP(1);
} }
server.Close();
} }
bool GeckoSockServer::GetAvailableSock(sf::SocketTCP &sock_to_fill) bool GeckoSockServer::GetAvailableSock()
{ {
bool sock_filled = false; bool sock_filled = false;
@ -79,7 +82,7 @@ bool GeckoSockServer::GetAvailableSock(sf::SocketTCP &sock_to_fill)
if (!waiting_socks.empty()) if (!waiting_socks.empty())
{ {
sock_to_fill = waiting_socks.front(); client = std::move(waiting_socks.front());
if (clientThread.joinable()) if (clientThread.joinable())
{ {
client_running = false; client_running = false;
@ -103,7 +106,7 @@ void GeckoSockServer::ClientThread()
Common::SetCurrentThreadName("Gecko Client"); Common::SetCurrentThreadName("Gecko Client");
client.SetBlocking(false); client->setBlocking(false);
while (client_running) while (client_running)
{ {
@ -116,7 +119,7 @@ void GeckoSockServer::ClientThread()
char data[128]; char data[128];
std::size_t got = 0; std::size_t got = 0;
if (client.Receive(&data[0], ArraySize(data), got) == sf::Socket::Disconnected) if (client->receive(&data[0], ArraySize(data), got) == sf::Socket::Disconnected)
client_running = false; client_running = false;
if (got != 0) if (got != 0)
@ -133,7 +136,7 @@ void GeckoSockServer::ClientThread()
std::vector<char> packet(send_fifo.begin(), send_fifo.end()); std::vector<char> packet(send_fifo.begin(), send_fifo.end());
send_fifo.clear(); send_fifo.clear();
if (client.Send(&packet[0], packet.size()) == sf::Socket::Disconnected) if (client->send(&packet[0], packet.size()) == sf::Socket::Disconnected)
client_running = false; client_running = false;
} }
} // unlock transfer } // unlock transfer
@ -142,7 +145,7 @@ void GeckoSockServer::ClientThread()
Common::YieldCPU(); Common::YieldCPU();
} }
client.Close(); client->disconnect();
} }
void CEXIGecko::ImmReadWrite(u32 &_uData, u32 _uSize) void CEXIGecko::ImmReadWrite(u32 &_uData, u32 _uSize)
@ -150,8 +153,8 @@ void CEXIGecko::ImmReadWrite(u32 &_uData, u32 _uSize)
// We don't really care about _uSize // We don't really care about _uSize
(void)_uSize; (void)_uSize;
if (!client.IsValid()) if (!client || client->getLocalPort() == 0)
GetAvailableSock(client); GetAvailableSock();
switch (_uData >> 28) switch (_uData >> 28)
{ {

View File

@ -14,15 +14,14 @@
#include "Core/HW/EXI_Device.h" #include "Core/HW/EXI_Device.h"
class GeckoSockServer class GeckoSockServer
: public sf::SocketTCP
{ {
public: public:
GeckoSockServer(); GeckoSockServer();
~GeckoSockServer(); ~GeckoSockServer();
bool GetAvailableSock(sf::SocketTCP &sock_to_fill); bool GetAvailableSock();
// Client for this server object // Client for this server object
sf::SocketTCP client; std::unique_ptr<sf::TcpSocket> client;
void ClientThread(); void ClientThread();
std::thread clientThread; std::thread clientThread;
std::mutex transfer_lock; std::mutex transfer_lock;
@ -40,8 +39,8 @@ private:
static u16 server_port; static u16 server_port;
static volatile bool server_running; static volatile bool server_running;
static std::thread connectionThread; static std::thread connectionThread;
static std::queue<sf::SocketTCP> waiting_socks;
static std::mutex connection_lock; static std::mutex connection_lock;
static std::queue<std::unique_ptr<sf::TcpSocket>> waiting_socks;
}; };
class CEXIGecko class CEXIGecko

View File

@ -5,6 +5,7 @@
#include <queue> #include <queue>
#include "Common/CommonFuncs.h" #include "Common/CommonFuncs.h"
#include "Common/StdMakeUnique.h"
#include "Common/Thread.h" #include "Common/Thread.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Core/HW/SI_Device.h" #include "Core/HW/SI_Device.h"
@ -13,7 +14,7 @@
#include "SFML/Network.hpp" #include "SFML/Network.hpp"
static std::thread connectionThread; static std::thread connectionThread;
static std::queue<sf::SocketTCP> waiting_socks; static std::queue<std::unique_ptr<sf::TcpSocket>> waiting_socks;
static std::mutex cs_gba; static std::mutex cs_gba;
namespace { volatile bool server_running; } namespace { volatile bool server_running; }
@ -25,25 +26,25 @@ static void GBAConnectionWaiter()
Common::SetCurrentThreadName("GBA Connection Waiter"); Common::SetCurrentThreadName("GBA Connection Waiter");
sf::SocketTCP server; sf::TcpListener server;
// "dolphin gba" // "dolphin gba"
if (!server.Listen(0xd6ba)) if (server.listen(0xd6ba) != sf::Socket::Done)
return; return;
server.SetBlocking(false); server.setBlocking(false);
sf::SocketTCP new_client; auto new_client = std::make_unique<sf::TcpSocket>();
while (server_running) while (server_running)
{ {
if (server.Accept(new_client) == sf::Socket::Done) if (server.accept(*new_client) == sf::Socket::Done)
{ {
std::lock_guard<std::mutex> lk(cs_gba); std::lock_guard<std::mutex> lk(cs_gba);
waiting_socks.push(new_client); waiting_socks.push(std::move(new_client));
new_client = std::make_unique<sf::TcpSocket>();
} }
SLEEP(1); SLEEP(1);
} }
server.Close();
return;
} }
void GBAConnectionWaiter_Shutdown() void GBAConnectionWaiter_Shutdown()
@ -53,7 +54,7 @@ void GBAConnectionWaiter_Shutdown()
connectionThread.join(); connectionThread.join();
} }
static bool GetAvailableSock(sf::SocketTCP& sock_to_fill) static bool GetAvailableSock(std::unique_ptr<sf::TcpSocket>& sock_to_fill)
{ {
bool sock_filled = false; bool sock_filled = false;
@ -61,7 +62,7 @@ static bool GetAvailableSock(sf::SocketTCP& sock_to_fill)
if (!waiting_socks.empty()) if (!waiting_socks.empty())
{ {
sock_to_fill = waiting_socks.front(); sock_to_fill = std::move(waiting_socks.front());
waiting_socks.pop(); waiting_socks.pop();
sock_filled = true; sock_filled = true;
} }
@ -77,13 +78,12 @@ GBASockServer::GBASockServer()
GBASockServer::~GBASockServer() GBASockServer::~GBASockServer()
{ {
client.Close();
} }
// Blocking, since GBA must always send lower byte of REG_JOYSTAT // Blocking, since GBA must always send lower byte of REG_JOYSTAT
void GBASockServer::Transfer(char* si_buffer) void GBASockServer::Transfer(char* si_buffer)
{ {
if (!client.IsValid()) if (!client || client->getLocalPort() == 0)
if (!GetAvailableSock(client)) if (!GetAvailableSock(client))
return; return;
@ -93,9 +93,9 @@ void GBASockServer::Transfer(char* si_buffer)
u8 cmd = *current_data; u8 cmd = *current_data;
if (cmd == CMD_WRITE) if (cmd == CMD_WRITE)
client.Send(current_data, sizeof(current_data)); client->send(current_data, sizeof(current_data));
else else
client.Send(current_data, 1); client->send(current_data, 1);
DEBUG_LOG(SERIALINTERFACE, "> command %02x %02x%02x%02x%02x", DEBUG_LOG(SERIALINTERFACE, "> command %02x %02x%02x%02x%02x",
(u8)current_data[0], (u8)current_data[1], (u8)current_data[2], (u8)current_data[0], (u8)current_data[1], (u8)current_data[2],
@ -103,8 +103,8 @@ void GBASockServer::Transfer(char* si_buffer)
memset(current_data, 0, sizeof(current_data)); memset(current_data, 0, sizeof(current_data));
size_t num_received = 0; size_t num_received = 0;
if (client.Receive(current_data, sizeof(current_data), num_received) == sf::Socket::Disconnected) if (client->receive(current_data, sizeof(current_data), num_received) == sf::Socket::Disconnected)
client.Close(); client->disconnect();
DEBUG_LOG(SERIALINTERFACE, "< %02x%02x%02x%02x%02x", DEBUG_LOG(SERIALINTERFACE, "< %02x%02x%02x%02x%02x",
(u8)current_data[0], (u8)current_data[1], (u8)current_data[2], (u8)current_data[0], (u8)current_data[1], (u8)current_data[2],

View File

@ -12,7 +12,7 @@
void GBAConnectionWaiter_Shutdown(); void GBAConnectionWaiter_Shutdown();
class GBASockServer : public sf::SocketTCP class GBASockServer
{ {
public: public:
GBASockServer(); GBASockServer();
@ -29,7 +29,7 @@ private:
CMD_WRITE = 0x15 CMD_WRITE = 0x15
}; };
sf::SocketTCP client; std::unique_ptr<sf::TcpSocket> client;
char current_data[5]; char current_data[5];
}; };

View File

@ -203,10 +203,10 @@ bool Wiimote::Read()
if (SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort > 0 && if (SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort > 0 &&
index == WIIMOTE_BALANCE_BOARD) index == WIIMOTE_BALANCE_BOARD)
{ {
static sf::SocketUDP Socket; static sf::UdpSocket Socket;
Socket.Send((char*)rpt.data(), Socket.send((char*)rpt.data(),
rpt.size(), rpt.size(),
sf::IPAddress::LocalHost, sf::IpAddress::LocalHost,
SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort); SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort);
} }
@ -236,8 +236,8 @@ bool Wiimote::Write()
{ {
if (SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort > 0 && index == WIIMOTE_BALANCE_BOARD) if (SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort > 0 && index == WIIMOTE_BALANCE_BOARD)
{ {
static sf::SocketUDP Socket; static sf::UdpSocket Socket;
Socket.Send((char*)rpt.data(), rpt.size(), sf::IPAddress::LocalHost, SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort); Socket.send((char*)rpt.data(), rpt.size(), sf::IpAddress::LocalHost, SConfig::GetInstance().m_LocalCoreStartupParameter.iBBDumpPort);
} }
IOWrite(rpt.data(), rpt.size()); IOWrite(rpt.data(), rpt.size());

View File

@ -43,18 +43,18 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay
is_connected = false; is_connected = false;
if (m_socket.Connect(port, address, 5) == sf::Socket::Done) if (m_socket.connect(address, port, sf::seconds(5)) == sf::Socket::Done)
{ {
// send connect message // send connect message
sf::Packet spac; sf::Packet spac;
spac << NETPLAY_VERSION; spac << NETPLAY_VERSION;
spac << netplay_dolphin_ver; spac << netplay_dolphin_ver;
spac << name; spac << name;
m_socket.Send(spac); m_socket.send(spac);
sf::Packet rpac; sf::Packet rpac;
// TODO: make this not hang // TODO: make this not hang
m_socket.Receive(rpac); m_socket.receive(rpac);
MessageId error; MessageId error;
rpac >> error; rpac >> error;
@ -76,7 +76,7 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay
PanicAlertT("The server sent an unknown error message!"); PanicAlertT("The server sent an unknown error message!");
break; break;
} }
m_socket.Close(); m_socket.disconnect();
} }
else else
{ {
@ -96,7 +96,7 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay
//PanicAlertT("Connection successful: assigned player id: %d", m_pid); //PanicAlertT("Connection successful: assigned player id: %d", m_pid);
is_connected = true; is_connected = true;
m_selector.Add(m_socket); m_selector.add(m_socket);
m_thread = std::thread(&NetPlayClient::ThreadFunc, this); m_thread = std::thread(&NetPlayClient::ThreadFunc, this);
} }
} }
@ -286,7 +286,7 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
spac << ping_key; spac << ping_key;
std::lock_guard<std::recursive_mutex> lks(m_crit.send); std::lock_guard<std::recursive_mutex> lks(m_crit.send);
m_socket.Send(spac); m_socket.send(spac);
} }
break; break;
@ -319,10 +319,10 @@ void NetPlayClient::ThreadFunc()
{ {
while (m_do_loop) while (m_do_loop)
{ {
if (m_selector.Wait(0.01f)) if (m_selector.wait(sf::milliseconds(10)))
{ {
sf::Packet rpac; sf::Packet rpac;
switch (m_socket.Receive(rpac)) switch (m_socket.receive(rpac))
{ {
case sf::Socket::Done : case sf::Socket::Done :
OnData(rpac); OnData(rpac);
@ -340,7 +340,7 @@ void NetPlayClient::ThreadFunc()
} }
} }
m_socket.Close(); m_socket.disconnect();
return; return;
} }
@ -403,7 +403,7 @@ void NetPlayClient::SendChatMessage(const std::string& msg)
spac << msg; spac << msg;
std::lock_guard<std::recursive_mutex> lks(m_crit.send); std::lock_guard<std::recursive_mutex> lks(m_crit.send);
m_socket.Send(spac); m_socket.send(spac);
} }
// called from ---CPU--- thread // called from ---CPU--- thread
@ -416,7 +416,7 @@ void NetPlayClient::SendPadState(const PadMapping in_game_pad, const GCPadStatus
spac << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX << pad.substickY << pad.triggerLeft << pad.triggerRight; spac << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY << pad.substickX << pad.substickY << pad.triggerLeft << pad.triggerRight;
std::lock_guard<std::recursive_mutex> lks(m_crit.send); std::lock_guard<std::recursive_mutex> lks(m_crit.send);
m_socket.Send(spac); m_socket.send(spac);
} }
// called from ---CPU--- thread // called from ---CPU--- thread
@ -433,7 +433,7 @@ void NetPlayClient::SendWiimoteState(const PadMapping in_game_pad, const NetWiim
} }
std::lock_guard<std::recursive_mutex> lks(m_crit.send); std::lock_guard<std::recursive_mutex> lks(m_crit.send);
m_socket.Send(spac); m_socket.send(spac);
} }
// called from ---GUI--- thread // called from ---GUI--- thread
@ -448,7 +448,7 @@ bool NetPlayClient::StartGame(const std::string &path)
spac << (char *)&g_NetPlaySettings; spac << (char *)&g_NetPlaySettings;
std::lock_guard<std::recursive_mutex> lks(m_crit.send); std::lock_guard<std::recursive_mutex> lks(m_crit.send);
m_socket.Send(spac); m_socket.send(spac);
if (m_is_running) if (m_is_running)
{ {
@ -751,7 +751,7 @@ void NetPlayClient::Stop()
{ {
sf::Packet spac; sf::Packet spac;
spac << (MessageId)NP_MSG_STOP_GAME; spac << (MessageId)NP_MSG_STOP_GAME;
m_socket.Send(spac); m_socket.send(spac);
} }
} }

View File

@ -87,9 +87,9 @@ protected:
Common::FifoQueue<NetWiimote> m_wiimote_buffer[4]; Common::FifoQueue<NetWiimote> m_wiimote_buffer[4];
NetPlayUI* m_dialog; NetPlayUI* m_dialog;
sf::SocketTCP m_socket; sf::TcpSocket m_socket;
std::thread m_thread; std::thread m_thread;
sf::Selector<sf::SocketTCP> m_selector; sf::SocketSelector m_selector;
std::string m_selected_game; std::string m_selected_game;
volatile bool m_is_running; volatile bool m_is_running;

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Common/StdMakeUnique.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "Core/NetPlayServer.h" #include "Core/NetPlayServer.h"
#include "InputCommon/GCPadStatus.h" #include "InputCommon/GCPadStatus.h"
@ -15,7 +16,7 @@ NetPlayServer::~NetPlayServer()
{ {
m_do_loop = false; m_do_loop = false;
m_thread.join(); m_thread.join();
m_socket.Close(); m_socket.close();
} }
#ifdef USE_UPNP #ifdef USE_UPNP
@ -31,11 +32,11 @@ NetPlayServer::NetPlayServer(const u16 port) : is_connected(false), m_is_running
{ {
memset(m_pad_map, -1, sizeof(m_pad_map)); memset(m_pad_map, -1, sizeof(m_pad_map));
memset(m_wiimote_map, -1, sizeof(m_wiimote_map)); memset(m_wiimote_map, -1, sizeof(m_wiimote_map));
if (m_socket.Listen(port)) if (m_socket.listen(port) == sf::Socket::Done)
{ {
is_connected = true; is_connected = true;
m_do_loop = true; m_do_loop = true;
m_selector.Add(m_socket); m_selector.add(m_socket);
m_thread = std::thread(&NetPlayServer::ThreadFunc, this); m_thread = std::thread(&NetPlayServer::ThreadFunc, this);
m_target_buffer_size = 5; m_target_buffer_size = 5;
} }
@ -64,17 +65,14 @@ void NetPlayServer::ThreadFunc()
m_update_pings = false; m_update_pings = false;
} }
// check which sockets need attention // check if any sockets need attention
const unsigned int num = m_selector.Wait(0.01f); if (m_selector.wait(sf::milliseconds(10)))
for (unsigned int i=0; i<num; ++i)
{ {
sf::SocketTCP ready_socket = m_selector.GetSocketReady(i);
// listening socket // listening socket
if (ready_socket == m_socket) if (m_selector.isReady(m_socket))
{ {
sf::SocketTCP accept_socket; auto accept_socket = std::make_unique<sf::TcpSocket>();
m_socket.Accept(accept_socket); m_socket.accept(*accept_socket);
unsigned int error; unsigned int error;
{ {
@ -87,29 +85,34 @@ void NetPlayServer::ThreadFunc()
sf::Packet spac; sf::Packet spac;
spac << (MessageId)error; spac << (MessageId)error;
// don't need to lock, this client isn't in the client map // don't need to lock, this client isn't in the client map
accept_socket.Send(spac); accept_socket->send(spac);
accept_socket->disconnect();
// TODO: not sure if client gets the message if i close right away
accept_socket.Close();
} }
} }
// client socket // client sockets
else for (auto it = m_players.begin(); it != m_players.end();)
{ {
sf::Packet rpac; // move iterator on immediately so client can be removed
switch (ready_socket.Receive(rpac)) Client& client = *it;
{ it++;
case sf::Socket::Done :
// if a bad packet is received, disconnect the client
if (0 == OnData(rpac, ready_socket))
break;
//case sf::Socket::Disconnected : if (m_selector.isReady(*client.socket))
default : {
sf::Packet rpac;
switch (client.socket->receive(rpac))
{ {
std::lock_guard<std::recursive_mutex> lkg(m_crit.game); case sf::Socket::Done :
OnDisconnect(ready_socket); // if a bad packet is received, disconnect the client
break; if (0 == OnData(rpac, client))
break;
//case sf::Socket::Disconnected :
default :
{
std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
OnDisconnect(client);
break;
}
} }
} }
} }
@ -118,15 +121,15 @@ void NetPlayServer::ThreadFunc()
// close listening socket and client sockets // close listening socket and client sockets
for (auto& player_entry : m_players) for (auto& player_entry : m_players)
player_entry.second.socket.Close(); player_entry.socket->disconnect();
} }
// called from ---NETPLAY--- thread // called from ---NETPLAY--- thread
unsigned int NetPlayServer::OnConnect(sf::SocketTCP& socket) unsigned int NetPlayServer::OnConnect(std::unique_ptr<sf::TcpSocket>& socket)
{ {
sf::Packet rpac; sf::Packet rpac;
// TODO: make this not hang / check if good packet // TODO: make this not hang / check if good packet
socket.Receive(rpac); socket->receive(rpac);
std::string npver; std::string npver;
rpac >> npver; rpac >> npver;
@ -146,7 +149,7 @@ unsigned int NetPlayServer::OnConnect(sf::SocketTCP& socket)
m_update_pings = true; m_update_pings = true;
Client player; Client player;
player.socket = socket; player.socket = std::move(socket);
rpac >> player.revision; rpac >> player.revision;
rpac >> player.name; rpac >> player.name;
@ -154,7 +157,7 @@ unsigned int NetPlayServer::OnConnect(sf::SocketTCP& socket)
PlayerId pid = 1; PlayerId pid = 1;
for (auto i = m_players.begin(); i != m_players.end(); ++i) for (auto i = m_players.begin(); i != m_players.end(); ++i)
{ {
if (i->second.pid == pid) if (i->pid == pid)
{ {
pid++; pid++;
i = m_players.begin(); i = m_players.begin();
@ -182,57 +185,56 @@ unsigned int NetPlayServer::OnConnect(sf::SocketTCP& socket)
SendToClients(spac); SendToClients(spac);
// send new client success message with their id // send new client success message with their id
spac.Clear(); spac.clear();
spac << (MessageId)0; spac << (MessageId)0;
spac << player.pid; spac << player.pid;
socket.Send(spac); player.socket->send(spac);
// send new client the selected game // send new client the selected game
if (m_selected_game != "") if (m_selected_game != "")
{ {
spac.Clear(); spac.clear();
spac << (MessageId)NP_MSG_CHANGE_GAME; spac << (MessageId)NP_MSG_CHANGE_GAME;
spac << m_selected_game; spac << m_selected_game;
socket.Send(spac); player.socket->send(spac);
} }
// send the pad buffer value // send the pad buffer value
spac.Clear(); spac.clear();
spac << (MessageId)NP_MSG_PAD_BUFFER; spac << (MessageId)NP_MSG_PAD_BUFFER;
spac << (u32)m_target_buffer_size; spac << (u32)m_target_buffer_size;
socket.Send(spac); player.socket->send(spac);
// sync values with new client // sync values with new client
for (const auto& p : m_players) for (const auto& p : m_players)
{ {
spac.Clear(); spac.clear();
spac << (MessageId)NP_MSG_PLAYER_JOIN; spac << (MessageId)NP_MSG_PLAYER_JOIN;
spac << p.second.pid << p.second.name << p.second.revision; spac << p.pid << p.name << p.revision;
socket.Send(spac); player.socket->send(spac);
} }
} // unlock send } // unlock send
// add client to selector/ used for receiving
m_selector.add(*player.socket);
// add client to the player list // add client to the player list
{ {
std::lock_guard<std::recursive_mutex> lkp(m_crit.players); std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
m_players[socket] = player; m_players.push_back(std::move(player));
std::lock_guard<std::recursive_mutex> lks(m_crit.send); std::lock_guard<std::recursive_mutex> lks(m_crit.send);
UpdatePadMapping(); // sync pad mappings with everyone UpdatePadMapping(); // sync pad mappings with everyone
UpdateWiimoteMapping(); UpdateWiimoteMapping();
} }
// add client to selector/ used for receiving
m_selector.Add(socket);
return 0; return 0;
} }
// called from ---NETPLAY--- thread // called from ---NETPLAY--- thread
unsigned int NetPlayServer::OnDisconnect(sf::SocketTCP& socket) unsigned int NetPlayServer::OnDisconnect(Client& player)
{ {
PlayerId pid = m_players[socket].pid; PlayerId pid = player.pid;
if (m_is_running) if (m_is_running)
{ {
@ -258,10 +260,10 @@ unsigned int NetPlayServer::OnDisconnect(sf::SocketTCP& socket)
spac << (MessageId)NP_MSG_PLAYER_LEAVE; spac << (MessageId)NP_MSG_PLAYER_LEAVE;
spac << pid; spac << pid;
m_selector.Remove(socket); m_selector.remove(*player.socket);
std::lock_guard<std::recursive_mutex> lkp(m_crit.players); std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
m_players.erase(m_players.find(socket)); m_players.remove(player);
// alert other players of disconnect // alert other players of disconnect
std::lock_guard<std::recursive_mutex> lks(m_crit.send); std::lock_guard<std::recursive_mutex> lks(m_crit.send);
@ -359,14 +361,13 @@ void NetPlayServer::AdjustPadBufferSize(unsigned int size)
} }
// called from ---NETPLAY--- thread // called from ---NETPLAY--- thread
unsigned int NetPlayServer::OnData(sf::Packet& packet, sf::SocketTCP& socket) unsigned int NetPlayServer::OnData(sf::Packet& packet, Client& player)
{ {
MessageId mid; MessageId mid;
packet >> mid; packet >> mid;
// don't need lock because this is the only thread that modifies the players // don't need lock because this is the only thread that modifies the players
// only need locks for writes to m_players in this thread // only need locks for writes to m_players in this thread
Client& player = m_players[socket];
switch (mid) switch (mid)
{ {
@ -568,12 +569,11 @@ bool NetPlayServer::StartGame()
// called from multiple threads // called from multiple threads
void NetPlayServer::SendToClients(sf::Packet& packet, const PlayerId skip_pid) void NetPlayServer::SendToClients(sf::Packet& packet, const PlayerId skip_pid)
{ {
for (std::pair<const sf::SocketTCP, Client>& p : m_players) for (auto& p : m_players)
{ {
if (p.second.pid && if (p.pid && p.pid != skip_pid)
p.second.pid != skip_pid)
{ {
p.second.socket.Send(packet); p.socket->send(packet);
} }
} }
} }
@ -582,9 +582,9 @@ void NetPlayServer::KickPlayer(u8 player)
{ {
for (auto& current_player : m_players) for (auto& current_player : m_players)
{ {
if (current_player.second.pid == player) if (current_player.pid == player)
{ {
current_player.second.socket.Close(); current_player.socket->disconnect();
return; return;
} }
} }
@ -613,7 +613,7 @@ void NetPlayServer::TryPortmapping(u16 port)
// UPnP thread: try to map a port // UPnP thread: try to map a port
void NetPlayServer::mapPortThread(const u16 port) void NetPlayServer::mapPortThread(const u16 port)
{ {
std::string ourIP = sf::IPAddress::GetLocalAddress().ToString(); std::string ourIP = sf::IpAddress::getLocalAddress().toString();
if (!m_upnp_inited) if (!m_upnp_inited)
if (!initUPnP()) if (!initUPnP())

View File

@ -56,15 +56,30 @@ private:
std::string name; std::string name;
std::string revision; std::string revision;
sf::SocketTCP socket; std::unique_ptr<sf::TcpSocket> socket;
u32 ping; u32 ping;
u32 current_game; u32 current_game;
// VS2013 does not generate the right constructors here automatically
// like GCC does, so we implement them manually
Client() = default;
Client(const Client& other) = delete;
Client(Client&& other)
: pid(other.pid), name(std::move(other.name)), revision(std::move(other.revision)),
socket(std::move(other.socket)), ping(other.ping), current_game(other.current_game)
{
}
bool operator==(const Client& other) const
{
return this == &other;
}
}; };
void SendToClients(sf::Packet& packet, const PlayerId skip_pid = 0); void SendToClients(sf::Packet& packet, const PlayerId skip_pid = 0);
unsigned int OnConnect(sf::SocketTCP& socket); unsigned int OnConnect(std::unique_ptr<sf::TcpSocket>& socket);
unsigned int OnDisconnect(sf::SocketTCP& socket); unsigned int OnDisconnect(Client& player);
unsigned int OnData(sf::Packet& packet, sf::SocketTCP& socket); unsigned int OnData(sf::Packet& packet, Client& player);
void UpdatePadMapping(); void UpdatePadMapping();
void UpdateWiimoteMapping(); void UpdateWiimoteMapping();
@ -80,7 +95,7 @@ private:
PadMapping m_pad_map[4]; PadMapping m_pad_map[4];
PadMapping m_wiimote_map[4]; PadMapping m_wiimote_map[4];
std::map<sf::SocketTCP, Client> m_players; std::list<Client> m_players;
struct struct
{ {
@ -91,9 +106,9 @@ private:
std::string m_selected_game; std::string m_selected_game;
sf::SocketTCP m_socket; sf::TcpListener m_socket;
std::thread m_thread; std::thread m_thread;
sf::Selector<sf::SocketTCP> m_selector; sf::SocketSelector m_selector;
#ifdef USE_UPNP #ifdef USE_UPNP
static void mapPortThread(const u16 port); static void mapPortThread(const u16 port);

View File

@ -172,20 +172,20 @@ void CodeConfigPanel::DownloadCodes(wxCommandEvent&)
} }
sf::Http::Request req; sf::Http::Request req;
req.SetURI("/txt.php?txt=" + gameid); req.setUri("/txt.php?txt=" + gameid);
sf::Http http; sf::Http http;
http.SetHost("geckocodes.org"); http.setHost("geckocodes.org");
const sf::Http::Response resp = http.SendRequest(req, 5.0f); const sf::Http::Response resp = http.sendRequest(req, sf::seconds(5));
if (sf::Http::Response::Ok == resp.GetStatus()) if (sf::Http::Response::Ok == resp.getStatus())
{ {
// temp vector containing parsed codes // temp vector containing parsed codes
std::vector<GeckoCode> gcodes; std::vector<GeckoCode> gcodes;
// parse the codes // parse the codes
std::istringstream ss(resp.GetBody()); std::istringstream ss(resp.getBody());
std::string line; std::string line;

View File

@ -50,6 +50,7 @@
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>USE_UPNP;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>USE_UPNP;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>PSAPI_VERSION=1;_M_X86=1;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>PSAPI_VERSION=1;_M_X86=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SFML_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Platform)'=='x64'">_ARCH_64=1;_M_X86_64=1;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions Condition="'$(Platform)'=='x64'">_ARCH_64=1;_M_X86_64=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError> <TreatWarningAsError>true</TreatWarningAsError>