Remove any wx-specific code from Core.

Move NetPlay code from DolphinWX to Core.
Even NetPlay with nowx should be possible now if anyone felt like implementing a CLI for it ;)


git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7119 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
NeoBrainX
2011-02-08 15:36:15 +00:00
parent dbd381755b
commit 42908c5b2e
23 changed files with 152 additions and 130 deletions

View File

@ -34,9 +34,6 @@ if(wxWidgets_FOUND)
Src/Main.cpp
Src/MemcardManager.cpp
Src/MemoryCards/GCMemcard.cpp
Src/NetPlay.cpp
Src/NetPlayClient.cpp
Src/NetPlayServer.cpp
Src/NetWindow.cpp
Src/PatchAddEdit.cpp
Src/PHackSettings.cpp

View File

@ -242,9 +242,6 @@ xcopy "$(SolutionDir)..\Externals\SDL\$(PlatformName)\*.dll" "$(TargetDir)" /e /
<ClCompile Include="Src\MemcardManager.cpp" />
<ClCompile Include="Src\MemoryCards\GCMemcard.cpp" />
<ClCompile Include="Src\MemoryCards\WiiSaveCrypted.cpp" />
<ClCompile Include="Src\NetPlay.cpp" />
<ClCompile Include="Src\NetPlayClient.cpp" />
<ClCompile Include="Src\NetPlayServer.cpp" />
<ClCompile Include="Src\NetWindow.cpp" />
<ClCompile Include="Src\PatchAddEdit.cpp" />
<ClCompile Include="Src\PHackSettings.cpp" />
@ -281,7 +278,6 @@ xcopy "$(SolutionDir)..\Externals\SDL\$(PlatformName)\*.dll" "$(TargetDir)" /e /
<ClInclude Include="Src\MemcardManager.h" />
<ClInclude Include="Src\MemoryCards\GCMemcard.h" />
<ClInclude Include="Src\MemoryCards\WiiSaveCrypted.h" />
<ClInclude Include="Src\NetPlay.h" />
<ClInclude Include="Src\NetWindow.h" />
<ClInclude Include="Src\PatchAddEdit.h" />
<ClInclude Include="Src\PHackSettings.h" />

View File

@ -943,22 +943,6 @@
<Filter
Name="NetPlay"
>
<File
RelativePath=".\Src\NetPlay.cpp"
>
</File>
<File
RelativePath=".\Src\NetPlay.h"
>
</File>
<File
RelativePath=".\Src\NetPlayClient.cpp"
>
</File>
<File
RelativePath=".\Src\NetPlayServer.cpp"
>
</File>
<File
RelativePath=".\Src\NetWindow.cpp"
>

View File

@ -48,7 +48,8 @@
bool rendererHasFocus = true;
bool running = true;
void Host_NotifyMapLoaded(){}
void Host_NotifyMapLoaded() {}
void Host_RefreshDSPDebuggerWindow() {}
void Host_ShowJitResults(unsigned int address){}

View File

@ -1,396 +0,0 @@
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "NetPlay.h"
#include "NetWindow.h"
// for wiimote
#include "IPC_HLE/WII_IPC_HLE_Device_usb.h"
#include "IPC_HLE/WII_IPC_HLE_WiiMote.h"
// for gcpad
#include "HW/SI_DeviceGCController.h"
// for gctime
#include "HW/EXI_DeviceIPL.h"
// to start/stop game
#include "Frame.h"
// for wiimote/ OSD messages
#include "Core.h"
Common::CriticalSection crit_netplay_ptr;
static NetPlay* netplay_ptr = NULL;
extern CFrame* main_frame;
#define RPT_SIZE_HACK (1 << 16)
DEFINE_EVENT_TYPE(wxEVT_THREAD)
// called from ---GUI--- thread
NetPlay::NetPlay()
: m_is_running(false), m_do_loop(true)
{
m_target_buffer_size = 20;
ClearBuffers();
}
void NetPlay_Enable(NetPlay* const np)
{
CritLocker crit(::crit_netplay_ptr); // probably safe without a lock
::netplay_ptr = np;
}
void NetPlay_Disable()
{
CritLocker crit(::crit_netplay_ptr);
::netplay_ptr = NULL;
}
// called from ---GUI--- thread
NetPlay::~NetPlay()
{
CritLocker crit(crit_netplay_ptr);
::netplay_ptr = NULL;
// not perfect
if (m_is_running)
StopGame();
}
NetPlay::Player::Player()
{
memset(pad_map, -1, sizeof(pad_map));
}
// called from ---NETPLAY--- thread
void NetPlay::UpdateGUI()
{
if (m_dialog)
{
wxCommandEvent evt(wxEVT_THREAD, 1);
m_dialog->GetEventHandler()->AddPendingEvent(evt);
}
}
// called from ---NETPLAY--- thread
void NetPlay::AppendChatGUI(const std::string& msg)
{
if (m_dialog)
{
m_dialog->chat_msgs.Push(msg);
// silly
UpdateGUI();
}
}
// called from ---GUI--- thread
std::string NetPlay::Player::ToString() const
{
std::ostringstream ss;
ss << name << '[' << (char)(pid+'0') << "] : " << revision << " |";
for (unsigned int i=0; i<4; ++i)
ss << (pad_map[i]>=0 ? (char)(pad_map[i]+'1') : '-');
ss << '|';
return ss.str();
}
NetPad::NetPad()
{
nHi = 0x00808080;
nLo = 0x80800000;
}
NetPad::NetPad(const SPADStatus* const pad_status)
{
nHi = (u32)((u8)pad_status->stickY);
nHi |= (u32)((u8)pad_status->stickX << 8);
nHi |= (u32)((u16)pad_status->button << 16);
nHi |= 0x00800000;
nLo = (u8)pad_status->triggerRight;
nLo |= (u32)((u8)pad_status->triggerLeft << 8);
nLo |= (u32)((u8)pad_status->substickY << 16);
nLo |= (u32)((u8)pad_status->substickX << 24);
}
// called from ---NETPLAY--- thread
void NetPlay::ClearBuffers()
{
// clear pad buffers, Clear method isn't thread safe
for (unsigned int i=0; i<4; ++i)
{
while (m_pad_buffer[i].Size())
m_pad_buffer[i].Pop();
while (m_wiimote_buffer[i].Size())
m_wiimote_buffer[i].Pop();
m_wiimote_input[i].clear();
}
}
// called from ---CPU--- thread
bool NetPlay::GetNetPads(const u8 pad_nb, const SPADStatus* const pad_status, NetPad* const netvalues)
{
m_crit.players.Enter(); // lock players
// in game mapping for this local pad
unsigned int in_game_num = m_local_player->pad_map[pad_nb];
// does this local pad map in game?
if (in_game_num < 4)
{
NetPad np(pad_status);
// adjust the buffer either up or down
// inserting multiple padstates or dropping states
while (m_pad_buffer[in_game_num].Size() <= m_target_buffer_size)
{
// add to buffer
m_pad_buffer[in_game_num].Push(np);
// send
SendPadState(pad_nb, np);
}
}
m_crit.players.Leave();
//Common::Timer bufftimer;
//bufftimer.Start();
// get padstate from buffer and send to game
while (!m_pad_buffer[pad_nb].Pop(*netvalues))
{
// wait for receiving thread to push some data
Common::SleepCurrentThread(1);
if (false == m_is_running)
return false;
// TODO: check the time of bufftimer here,
// if it gets pretty high, ask the user if they want to disconnect
}
//u64 hangtime = bufftimer.GetTimeElapsed();
//if (hangtime > 10)
//{
// std::ostringstream ss;
// ss << "Pad " << (int)pad_nb << ": Had to wait " << hangtime << "ms for pad data. (increase pad Buffer maybe)";
// Core::DisplayMessage(ss.str(), 1000);
//}
return true;
}
// called from ---CPU--- thread
void NetPlay::WiimoteInput(int _number, u16 _channelID, const void* _pData, u32 _Size)
{
//// in game mapping for this local wiimote
unsigned int in_game_num = m_local_player->pad_map[_number]; // just using gc pad_map for now
// does this local pad map in game?
if (in_game_num < 4)
{
m_wiimote_input[_number].resize(m_wiimote_input[_number].size() + 1);
m_wiimote_input[_number].back().assign((char*)_pData, (char*)_pData + _Size);
m_wiimote_input[_number].back().channel = _channelID;
}
m_crit.players.Leave();
}
// called from ---CPU--- thread
void NetPlay::WiimoteUpdate(int _number)
{
m_crit.players.Enter(); // lock players
// in game mapping for this local wiimote
unsigned int in_game_num = m_local_player->pad_map[_number]; // just using gc pad_map for now
// does this local pad map in game?
if (in_game_num < 4)
{
m_wiimote_buffer[in_game_num].Push(m_wiimote_input[_number]);
// TODO: send it
m_wiimote_input[_number].clear();
}
m_crit.players.Leave();
if (0 == m_wiimote_buffer[_number].Size())
{
//PanicAlert("PANIC");
return;
}
NetWiimote nw;
m_wiimote_buffer[_number].Pop(nw);
NetWiimote::const_iterator
i = nw.begin(), e = nw.end();
for ( ; i!=e; ++i)
Core::Callback_WiimoteInterruptChannel(_number, i->channel, &(*i)[0], (u32)i->size() + RPT_SIZE_HACK);
}
// called from ---GUI--- thread
bool NetPlay::StartGame(const std::string &path)
{
if (m_is_running)
{
PanicAlertT("Game is already running!");
return false;
}
AppendChatGUI(" -- STARTING GAME -- ");
m_is_running = true;
NetPlay_Enable(this);
ClearBuffers();
// boot game
::main_frame->BootGame(path);
// temporary
NetWiimote nw;
for (unsigned int i = 0; i<4; ++i)
for (unsigned int f = 0; f<2; ++f)
m_wiimote_buffer[i].Push(nw);
return true;
}
// called from ---GUI--- thread and ---NETPLAY--- thread (client side)
bool NetPlay::StopGame()
{
CritLocker game_lock(m_crit.game); // lock game state
if (false == m_is_running)
{
PanicAlertT("Game isn't running!");
return false;
}
AppendChatGUI(" -- STOPPING GAME -- ");
m_is_running = false;
NetPlay_Disable();
// stop game
::main_frame->DoStop();
return true;
}
// called from ---CPU--- thread
u8 NetPlay::GetPadNum(u8 numPAD)
{
// TODO: i don't like that this loop is running everytime there is rumble
unsigned int i = 0;
for (; i<4; ++i)
if (numPAD == m_local_player->pad_map[i])
break;
return i;
}
// stuff hacked into dolphin
// called from ---CPU--- thread
// Actual Core function which is called on every frame
bool CSIDevice_GCController::NetPlay_GetInput(u8 numPAD, SPADStatus PadStatus, u32 *PADStatus)
{
CritLocker crit(::crit_netplay_ptr);
if (::netplay_ptr)
return netplay_ptr->GetNetPads(numPAD, &PadStatus, (NetPad*)PADStatus);
else
return false;
}
// called from ---CPU--- thread
// so all players' games get the same time
u32 CEXIIPL::NetPlay_GetGCTime()
{
CritLocker crit(::crit_netplay_ptr);
if (::netplay_ptr)
return 1272737767; // watev
else
return 0;
}
// called from ---CPU--- thread
// return the local pad num that should rumble given a ingame pad num
u8 CSIDevice_GCController::NetPlay_GetPadNum(u8 numPAD)
{
CritLocker crit(::crit_netplay_ptr);
if (::netplay_ptr)
return ::netplay_ptr->GetPadNum(numPAD);
else
return numPAD;
}
// called from ---CPU--- thread
// wiimote update / used for frame counting
//void CWII_IPC_HLE_Device_usb_oh1_57e_305::NetPlay_WiimoteUpdate(int _number)
void CWII_IPC_HLE_Device_usb_oh1_57e_305::NetPlay_WiimoteUpdate(int)
{
//CritLocker crit(::crit_netplay_ptr);
//if (::netplay_ptr)
// ::netplay_ptr->WiimoteUpdate(_number);
}
// called from ---CPU--- thread
//
int CWII_IPC_HLE_WiiMote::NetPlay_GetWiimoteNum(int _number)
{
//CritLocker crit(::crit_netplay_ptr);
//if (::netplay_ptr)
// return ::netplay_ptr->GetPadNum(_number); // just using gcpad mapping for now
//else
return _number;
}
// called from ---CPU--- thread
// intercept wiimote input callback
//bool CWII_IPC_HLE_WiiMote::NetPlay_WiimoteInput(int _number, u16 _channelID, const void* _pData, u32& _Size)
bool CWII_IPC_HLE_WiiMote::NetPlay_WiimoteInput(int, u16, const void*, u32&)
{
CritLocker crit(::crit_netplay_ptr);
if (::netplay_ptr)
//{
// if (_Size >= RPT_SIZE_HACK)
// {
// _Size -= RPT_SIZE_HACK;
// return false;
// }
// else
// {
// ::netplay_ptr->WiimoteInput(_number, _channelID, _pData, _Size);
// // don't use this packet
return true;
// }
//}
else
return false;
}

View File

@ -1,243 +0,0 @@
#ifndef _NETPLAY_H
#define _NETPLAY_H
#include "Common.h"
#include "CommonTypes.h"
#include "Thread.h"
#include "Timer.h"
#include <SFML/Network.hpp>
#include "GCPadStatus.h"
#include <functional>
#include <map>
#include <queue>
#include <sstream>
#include "FifoQueue.h"
class NetPlayDiag;
class NetPad
{
public:
NetPad();
NetPad(const SPADStatus* const);
u32 nHi;
u32 nLo;
};
struct Rpt : public std::vector<u8>
{
u16 channel;
};
typedef std::vector<Rpt> NetWiimote;
#define NETPLAY_VERSION "Dolphin NetPlay r6423"
// messages
enum
{
NP_MSG_PLAYER_JOIN = 0x10,
NP_MSG_PLAYER_LEAVE = 0x11,
NP_MSG_CHAT_MESSAGE = 0x30,
NP_MSG_PAD_DATA = 0x60,
NP_MSG_PAD_MAPPING = 0x61,
NP_MSG_PAD_BUFFER = 0x62,
NP_MSG_WIIMOTE_DATA = 0x70,
NP_MSG_WIIMOTE_MAPPING = 0x71, // just using pad mapping for now
NP_MSG_START_GAME = 0xA0,
NP_MSG_CHANGE_GAME = 0xA1,
NP_MSG_STOP_GAME = 0xA2,
NP_MSG_DISABLE_GAME = 0xA3,
NP_MSG_READY = 0xD0,
NP_MSG_NOT_READY = 0xD1,
NP_MSG_PING = 0xE0,
NP_MSG_PONG = 0xE1,
};
typedef u8 MessageId;
typedef u8 PlayerId;
typedef s8 PadMapping;
typedef u32 FrameNum;
enum
{
CON_ERR_SERVER_FULL = 1,
CON_ERR_GAME_RUNNING,
CON_ERR_VERSION_MISMATCH
};
// something like this should be in Common stuff
class CritLocker
{
public:
//CritLocker(const CritLocker&);
CritLocker& operator=(const CritLocker&);
CritLocker(Common::CriticalSection& crit) : m_crit(crit) { m_crit.Enter(); }
~CritLocker() { m_crit.Leave(); }
private:
Common::CriticalSection& m_crit;
};
class NetPlay
{
public:
NetPlay();
virtual ~NetPlay();
//virtual void ThreadFunc() = 0;
bool is_connected;
// Send and receive pads values
void WiimoteInput(int _number, u16 _channelID, const void* _pData, u32 _Size);
void WiimoteUpdate(int _number);
bool GetNetPads(const u8 pad_nb, const SPADStatus* const, NetPad* const netvalues);
virtual bool ChangeGame(const std::string& game) = 0;
virtual void GetPlayerList(std::string& list, std::vector<int>& pid_list) = 0;
virtual void SendChatMessage(const std::string& msg) = 0;
virtual bool StartGame(const std::string &path);
virtual bool StopGame();
//void PushPadStates(unsigned int count);
u8 GetPadNum(u8 numPAD);
protected:
//void GetBufferedPad(const u8 pad_nb, NetPad* const netvalues);
void ClearBuffers();
void UpdateGUI();
void AppendChatGUI(const std::string& msg);
virtual void SendPadState(const PadMapping local_nb, const NetPad& np) = 0;
struct
{
Common::CriticalSection game;
// lock order
Common::CriticalSection players, send;
} m_crit;
class Player
{
public:
Player();
std::string ToString() const;
PlayerId pid;
std::string name;
PadMapping pad_map[4];
std::string revision;
};
Common::FifoQueue<NetPad> m_pad_buffer[4];
Common::FifoQueue<NetWiimote> m_wiimote_buffer[4];
NetWiimote m_wiimote_input[4];
NetPlayDiag* m_dialog;
sf::SocketTCP m_socket;
std::thread m_thread;
sf::Selector<sf::SocketTCP> m_selector;
std::string m_selected_game;
volatile bool m_is_running;
volatile bool m_do_loop;
unsigned int m_target_buffer_size;
Player* m_local_player;
u32 m_current_game;
private:
};
void NetPlay_Enable(NetPlay* const np);
void NetPlay_Disable();
class NetPlayServer : public NetPlay
{
public:
void ThreadFunc();
NetPlayServer(const u16 port, const std::string& name, NetPlayDiag* const npd = NULL, const std::string& game = "");
~NetPlayServer();
void GetPlayerList(std::string& list, std::vector<int>& pid_list);
// Send and receive pads values
//bool GetNetPads(const u8 pad_nb, const SPADStatus* const, NetPad* const netvalues);
bool ChangeGame(const std::string& game);
void SendChatMessage(const std::string& msg);
bool StartGame(const std::string &path);
bool StopGame();
bool GetPadMapping(const int pid, int map[]);
bool SetPadMapping(const int pid, const int map[]);
u64 CalculateMinimumBufferTime();
void AdjustPadBufferSize(unsigned int size);
private:
class Client : public Player
{
public:
Client() : ping(0), current_game(0) {}
sf::SocketTCP socket;
u64 ping;
u32 current_game;
};
void SendPadState(const PadMapping local_nb, const NetPad& np);
void SendToClients(sf::Packet& packet, const PlayerId skip_pid = 0);
unsigned int OnConnect(sf::SocketTCP& socket);
unsigned int OnDisconnect(sf::SocketTCP& socket);
unsigned int OnData(sf::Packet& packet, sf::SocketTCP& socket);
void UpdatePadMapping();
std::map<sf::SocketTCP, Client> m_players;
Common::Timer m_ping_timer;
u32 m_ping_key;
bool m_update_pings;
};
class NetPlayClient : public NetPlay
{
public:
void ThreadFunc();
NetPlayClient(const std::string& address, const u16 port, const std::string& name, NetPlayDiag* const npd = NULL);
~NetPlayClient();
void GetPlayerList(std::string& list, std::vector<int>& pid_list);
// Send and receive pads values
//bool GetNetPads(const u8 pad_nb, const SPADStatus* const, NetPad* const netvalues);
bool StartGame(const std::string &path);
bool ChangeGame(const std::string& game);
void SendChatMessage(const std::string& msg);
private:
void SendPadState(const PadMapping local_nb, const NetPad& np);
unsigned int OnData(sf::Packet& packet);
PlayerId m_pid;
std::map<PlayerId, Player> m_players;
};
#endif

View File

@ -1,337 +0,0 @@
#include "NetPlay.h"
#include "NetWindow.h"
// called from ---GUI--- thread
NetPlayClient::~NetPlayClient()
{
if (is_connected)
{
m_do_loop = false;
m_thread.join();
}
}
// called from ---GUI--- thread
NetPlayClient::NetPlayClient(const std::string& address, const u16 port, const std::string& name, NetPlayDiag* const npd)
{
m_dialog = npd;
is_connected = false;
// why is false successful? documentation says true is
if (0 == m_socket.Connect(port, address, 5))
{
// send connect message
sf::Packet spac;
spac << NETPLAY_VERSION;
spac << netplay_dolphin_ver;
spac << name;
m_socket.Send(spac);
sf::Packet rpac;
// TODO: make this not hang
m_socket.Receive(rpac);
MessageId error;
rpac >> error;
// got error message
if (error)
{
switch (error)
{
case CON_ERR_SERVER_FULL :
PanicAlertT("The server is full!");
break;
case CON_ERR_VERSION_MISMATCH :
PanicAlertT("The server and client's NetPlay versions are incompatible!");
break;
case CON_ERR_GAME_RUNNING :
PanicAlertT("The server responded: the game is currently running!");
break;
default :
PanicAlertT("The server sent an unknown error message!");
break;
}
m_socket.Close();
}
else
{
rpac >> m_pid;
Player player;
player.name = name;
player.pid = m_pid;
player.revision = netplay_dolphin_ver;
// add self to player list
m_players[m_pid] = player;
m_local_player = &m_players[m_pid];
UpdateGUI();
//PanicAlertT("Connection successful: assigned player id: %d", m_pid);
is_connected = true;
m_selector.Add(m_socket);
m_thread = std::thread(std::mem_fun(&NetPlayClient::ThreadFunc), this);
}
}
else
PanicAlertT("Failed to Connect!");
}
// called from ---NETPLAY--- thread
unsigned int NetPlayClient::OnData(sf::Packet& packet)
{
MessageId mid;
packet >> mid;
switch (mid)
{
case NP_MSG_PLAYER_JOIN :
{
Player player;
packet >> player.pid;
packet >> player.name;
packet >> player.revision;
m_crit.players.Enter(); // lock players
m_players[player.pid] = player;
m_crit.players.Leave();
UpdateGUI();
}
break;
case NP_MSG_PLAYER_LEAVE :
{
PlayerId pid;
packet >> pid;
m_crit.players.Enter(); // lock players
m_players.erase(m_players.find(pid));
m_crit.players.Leave();
UpdateGUI();
}
break;
case NP_MSG_CHAT_MESSAGE :
{
PlayerId pid;
packet >> pid;
std::string msg;
packet >> msg;
// don't need lock to read in this thread
const Player& player = m_players[pid];
// add to gui
std::ostringstream ss;
ss << player.name << '[' << (char)(pid+'0') << "]: " << msg;
AppendChatGUI(ss.str());
}
break;
case NP_MSG_PAD_MAPPING :
{
PlayerId pid;
packet >> pid;
m_crit.players.Enter(); // lock players
Player& player = m_players[pid];
for (unsigned int i=0; i<4; ++i)
packet >> player.pad_map[i];
m_crit.players.Leave();
UpdateGUI();
}
break;
case NP_MSG_PAD_DATA :
{
PadMapping map = 0;
NetPad np;
packet >> map >> np.nHi >> np.nLo;
// trusting server for good map value (>=0 && <4)
// add to pad buffer
m_pad_buffer[(unsigned)map].Push(np);
}
break;
case NP_MSG_PAD_BUFFER :
{
u32 size = 0;
packet >> size;
m_target_buffer_size = size;
}
break;
case NP_MSG_CHANGE_GAME :
{
// lock here?
m_crit.game.Enter(); // lock game state
packet >> m_selected_game;
m_crit.game.Leave();
// update gui
wxCommandEvent evt(wxEVT_THREAD, NP_GUI_EVT_CHANGE_GAME);
// TODO: using a wxString in AddPendingEvent from another thread is unsafe i guess?
evt.SetString(wxString(m_selected_game.c_str(), *wxConvCurrent));
m_dialog->GetEventHandler()->AddPendingEvent(evt);
}
break;
case NP_MSG_START_GAME :
{
m_crit.game.Enter(); // lock buffer
packet >> m_current_game;
m_crit.game.Leave();
wxCommandEvent evt(wxEVT_THREAD, NP_GUI_EVT_START_GAME);
m_dialog->GetEventHandler()->AddPendingEvent(evt);
}
break;
case NP_MSG_STOP_GAME :
{
wxCommandEvent evt(wxEVT_THREAD, NP_GUI_EVT_STOP_GAME);
m_dialog->GetEventHandler()->AddPendingEvent(evt);
}
break;
case NP_MSG_DISABLE_GAME :
{
PanicAlertT("Other client disconnected while game is running!! NetPlay is disabled. You manually stop the game.");
CritLocker game_lock(m_crit.game); // lock game state
m_is_running = false;
NetPlay_Disable();
}
break;
case NP_MSG_PING :
{
u32 ping_key = 0;
packet >> ping_key;
sf::Packet spac;
spac << (MessageId)NP_MSG_PONG;
spac << ping_key;
CritLocker send_lock(m_crit.send);
m_socket.Send(spac);
}
break;
default :
PanicAlertT("Unknown message received with id : %d", mid);
break;
}
return 0;
}
// called from ---NETPLAY--- thread
void NetPlayClient::ThreadFunc()
{
while (m_do_loop)
{
if (m_selector.Wait(0.01f))
{
sf::Packet rpac;
switch (m_socket.Receive(rpac))
{
case sf::Socket::Done :
OnData(rpac);
break;
//case sf::Socket::Disconnected :
default :
m_is_running = false;
NetPlay_Disable();
AppendChatGUI("< LOST CONNECTION TO SERVER >");
PanicAlertT("Lost connection to server!");
m_do_loop = false;
break;
}
}
}
m_socket.Close();
return;
}
// called from ---GUI--- thread
void NetPlayClient::GetPlayerList(std::string& list, std::vector<int>& pid_list)
{
CritLocker player_lock(m_crit.players); // lock players
std::ostringstream ss;
std::map<PlayerId, Player>::const_iterator
i = m_players.begin(),
e = m_players.end();
for ( ; i!=e; ++i)
{
ss << i->second.ToString() << '\n';
pid_list.push_back(i->second.pid);
}
list = ss.str();
}
// called from ---GUI--- thread
void NetPlayClient::SendChatMessage(const std::string& msg)
{
sf::Packet spac;
spac << (MessageId)NP_MSG_CHAT_MESSAGE;
spac << msg;
CritLocker send_lock(m_crit.send); // lock send
m_socket.Send(spac);
}
// called from ---CPU--- thread
void NetPlayClient::SendPadState(const PadMapping local_nb, const NetPad& np)
{
// send to server
sf::Packet spac;
spac << (MessageId)NP_MSG_PAD_DATA;
spac << local_nb; // local pad num
spac << np.nHi << np.nLo;
CritLocker send_lock(m_crit.send); // lock send
m_socket.Send(spac);
}
// called from ---GUI--- thread
bool NetPlayClient::StartGame(const std::string &path)
{
CritLocker game_lock(m_crit.game); // lock game state
if (false == NetPlay::StartGame(path))
return false;
// tell server i started the game
sf::Packet spac;
spac << (MessageId)NP_MSG_START_GAME;
spac << m_current_game;
CritLocker send_lock(m_crit.send); // lock send
m_socket.Send(spac);
return true;
}
// called from ---GUI--- thread
bool NetPlayClient::ChangeGame(const std::string&)
{
return true;
}

View File

@ -1,629 +0,0 @@
#include "NetPlay.h"
#include "NetWindow.h"
// called from ---GUI--- thread
NetPlayServer::~NetPlayServer()
{
if (is_connected)
{
m_do_loop = false;
m_thread.join();
}
}
// called from ---GUI--- thread
NetPlayServer::NetPlayServer(const u16 port, const std::string& name, NetPlayDiag* const npd, const std::string& game)
{
m_dialog = npd;
m_selected_game = game;
m_update_pings = true;
if (m_socket.Listen(port))
{
Client player;
player.pid = 0;
player.revision = netplay_dolphin_ver;
player.socket = m_socket;
player.name = name;
// map local pad 1 to game pad 1
player.pad_map[0] = 0;
// add self to player list
m_players[m_socket] = player;
m_local_player = &m_players[m_socket];
//PanicAlertT("Listening");
UpdateGUI();
is_connected = true;
m_selector.Add(m_socket);
m_thread = std::thread(std::mem_fun(&NetPlayServer::ThreadFunc), this);
}
else
is_connected = false;
}
// called from ---NETPLAY--- thread
void NetPlayServer::ThreadFunc()
{
while (m_do_loop)
{
// update pings every so many seconds
if ((m_ping_timer.GetTimeElapsed() > (10 * 1000)) || m_update_pings)
{
//PanicAlertT("sending pings");
m_ping_key = Common::Timer::GetTimeMs();
sf::Packet spac;
spac << (MessageId)NP_MSG_PING;
spac << m_ping_key;
//CritLocker player_lock(m_crit.players);
CritLocker send_lock(m_crit.send);
m_ping_timer.Start();
SendToClients(spac);
m_update_pings = false;
}
// check which sockets need attention
const unsigned int num = m_selector.Wait(0.01f);
for (unsigned int i=0; i<num; ++i)
{
sf::SocketTCP ready_socket = m_selector.GetSocketReady(i);
// listening socket
if (ready_socket == m_socket)
{
sf::SocketTCP accept_socket;
m_socket.Accept(accept_socket);
m_crit.game.Enter(); // lock game state
const unsigned int error = OnConnect(accept_socket);
m_crit.game.Leave();
if (error)
{
sf::Packet spac;
spac << (MessageId)error;
// don't need to lock, this client isn't in the client map
accept_socket.Send(spac);
// TODO: not sure if client gets the message if i close right away
accept_socket.Close();
}
}
// client socket
else
{
sf::Packet rpac;
switch (ready_socket.Receive(rpac))
{
case sf::Socket::Done :
// if a bad packet is recieved, disconnect the client
if (0 == OnData(rpac, ready_socket))
break;
//case sf::Socket::Disconnected :
default :
m_crit.game.Enter(); // lock game state
OnDisconnect(ready_socket);
m_crit.game.Leave();
break;
}
}
}
}
// close listening socket and client sockets
{
std::map<sf::SocketTCP, Client>::reverse_iterator
i = m_players.rbegin(),
e = m_players.rend();
for ( ; i!=e; ++i)
i->second.socket.Close();
}
return;
}
// called from ---NETPLAY--- thread
unsigned int NetPlayServer::OnConnect(sf::SocketTCP& socket)
{
sf::Packet rpac;
// TODO: make this not hang / check if good packet
socket.Receive(rpac);
std::string npver;
rpac >> npver;
// dolphin netplay version
if (npver != NETPLAY_VERSION)
return CON_ERR_VERSION_MISMATCH;
// game is currently running
if (m_is_running)
return CON_ERR_GAME_RUNNING;
// too many players
if (m_players.size() >= 255)
return CON_ERR_SERVER_FULL;
// cause pings to be updated
m_update_pings = true;
Client player;
player.socket = socket;
rpac >> player.revision;
rpac >> player.name;
// give new client first available id
player.pid = 0;
std::map<sf::SocketTCP, Client>::const_iterator
i,
e = m_players.end();
for (PlayerId p = 1; 0 == player.pid; ++p)
{
for (i = m_players.begin(); ; ++i)
{
if (e == i)
{
player.pid = p;
break;
}
if (p == i->second.pid)
break;
}
}
// TODO: this is crappy
// try to automatically assign new user a pad
{
bool is_mapped[4] = {false,false,false,false};
for ( unsigned int m = 0; m<4; ++m)
{
for (i = m_players.begin(); i!=e; ++i)
{
if (i->second.pad_map[m] >= 0)
is_mapped[(unsigned)i->second.pad_map[m]] = true;
}
}
for ( unsigned int m = 0; m<4; ++m)
if (false == is_mapped[m])
{
player.pad_map[0] = m;
break;
}
}
// ENTER
m_crit.send.Enter();
// send join message to already connected clients
sf::Packet spac;
spac << (MessageId)NP_MSG_PLAYER_JOIN;
spac << player.pid << player.name << player.revision;
SendToClients(spac);
// send new client success message with their id
spac.Clear();
spac << (MessageId)0;
spac << player.pid;
socket.Send(spac);
// send new client the selected game
spac.Clear();
spac << (MessageId)NP_MSG_CHANGE_GAME;
spac << m_selected_game;
socket.Send(spac);
// sync values with new client
for (i = m_players.begin(); i!=e; ++i)
{
spac.Clear();
spac << (MessageId)NP_MSG_PLAYER_JOIN;
spac << i->second.pid << i->second.name << i->second.revision;
socket.Send(spac);
}
// LEAVE
m_crit.send.Leave();
// add client to the player list
m_crit.players.Enter(); // lock players
m_players[socket] = player;
m_crit.send.Enter(); // lock send
UpdatePadMapping(); // sync pad mappings with everyone
m_crit.send.Leave();
m_crit.players.Leave();
// add client to selector/ used for receiving
m_selector.Add(socket);
UpdateGUI();
return 0;
}
// called from ---NETPLAY--- thread
unsigned int NetPlayServer::OnDisconnect(sf::SocketTCP& socket)
{
if (m_is_running)
{
PanicAlertT("Client disconnect while game is running!! NetPlay is disabled. You must manually stop the game.");
CritLocker game_lock(m_crit.game); // lock game state
m_is_running = false;
NetPlay_Disable();
sf::Packet spac;
spac << (MessageId)NP_MSG_DISABLE_GAME;
// this thread doesnt need players lock
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
}
sf::Packet spac;
spac << (MessageId)NP_MSG_PLAYER_LEAVE;
spac << m_players[socket].pid;
m_selector.Remove(socket);
CritLocker player_lock(m_crit.players); // lock players
m_players.erase(m_players.find(socket));
// alert other players of disconnect
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
UpdateGUI();
return 0;
}
// called from ---GUI--- thread
bool NetPlayServer::GetPadMapping(const int pid, int map[])
{
CritLocker player_lock(m_crit.players); // lock players
std::map<sf::SocketTCP, Client>::const_iterator
i = m_players.begin(),
e = m_players.end();
for (; i!=e; ++i)
if (pid == i->second.pid)
break;
// player not found
if (i == e)
return false;
// get pad mapping
for (unsigned int m = 0; m<4; ++m)
map[m] = i->second.pad_map[m];
return true;
}
// called from ---GUI--- thread
bool NetPlayServer::SetPadMapping(const int pid, const int map[])
{
CritLocker game_lock(m_crit.game); // lock game
if (m_is_running)
return false;
CritLocker player_lock(m_crit.players); // lock players
std::map<sf::SocketTCP, Client>::iterator
i = m_players.begin(),
e = m_players.end();
for (; i!=e; ++i)
if (pid == i->second.pid)
break;
// player not found
if (i == e)
return false;
Client& player = i->second;
// set pad mapping
for (unsigned int m = 0; m<4; ++m)
{
player.pad_map[m] = (PadMapping)map[m];
// remove duplicate mappings
for (i = m_players.begin(); i!=e; ++i)
for (unsigned int p = 0; p<4; ++p)
if (p != m || i->second.pid != pid)
if (player.pad_map[m] == i->second.pad_map[p])
i->second.pad_map[p] = -1;
}
CritLocker send_lock(m_crit.send); // lock send
UpdatePadMapping(); // sync pad mappings with everyone
UpdateGUI();
return true;
}
// called from ---NETPLAY--- thread
void NetPlayServer::UpdatePadMapping()
{
std::map<sf::SocketTCP, Client>::const_iterator
i = m_players.begin(),
e = m_players.end();
for (; i!=e; ++i)
{
sf::Packet spac;
spac << (MessageId)NP_MSG_PAD_MAPPING;
spac << i->second.pid;
for (unsigned int pm = 0; pm<4; ++pm)
spac << i->second.pad_map[pm];
SendToClients(spac);
}
}
// called from ---GUI--- thread and ---NETPLAY--- thread
u64 NetPlayServer::CalculateMinimumBufferTime()
{
CritLocker player_lock(m_crit.players);
std::map<sf::SocketTCP, Client>::const_iterator
i = m_players.begin(),
e = m_players.end();
std::priority_queue<unsigned int> pings;
for ( ;i!=e; ++i)
pings.push(i->second.ping/2);
unsigned int required_ms = pings.top();
// if there is more than 1 client, buffersize must be >= (2 highest ping times combined)
if (pings.size() > 1)
{
pings.pop();
required_ms += pings.top();
}
return required_ms;
}
// called from ---GUI--- thread and ---NETPLAY--- thread
void NetPlayServer::AdjustPadBufferSize(unsigned int size)
{
CritLocker game_lock(m_crit.game); // lock game state
m_target_buffer_size = size;
// tell clients to change buffer size
sf::Packet spac;
spac << (MessageId)NP_MSG_PAD_BUFFER;
spac << (u32)m_target_buffer_size;
CritLocker player_lock(m_crit.players); // lock players
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
}
// called from ---NETPLAY--- thread
unsigned int NetPlayServer::OnData(sf::Packet& packet, sf::SocketTCP& socket)
{
MessageId mid;
packet >> mid;
// 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
Client& player = m_players[socket];
switch (mid)
{
case NP_MSG_CHAT_MESSAGE :
{
std::string msg;
packet >> msg;
// send msg to other clients
sf::Packet spac;
spac << (MessageId)NP_MSG_CHAT_MESSAGE;
spac << player.pid;
spac << msg;
m_crit.send.Enter(); // lock send
SendToClients(spac, player.pid);
m_crit.send.Leave();
// add to gui
std::ostringstream ss;
ss << player.name << '[' << (char)(player.pid+'0') << "]: " << msg;
AppendChatGUI(ss.str());
}
break;
case NP_MSG_PAD_DATA :
{
// if this is pad data from the last game still being received, ignore it
if (player.current_game != m_current_game)
break;
PadMapping map = 0;
NetPad np;
packet >> map >> np.nHi >> np.nLo;
// check if client's pad indeed maps in game
if (map >= 0 && map < 4)
map = player.pad_map[(unsigned)map];
else
map = -1;
// if not, they are hacking, so disconnect them
// this could happen right after a pad map change, but that isn't implimented yet
if (map < 0)
return 1;
// add to pad buffer
m_pad_buffer[(unsigned)map].Push(np);
// relay to clients
sf::Packet spac;
spac << (MessageId)NP_MSG_PAD_DATA;
spac << map; // in game mapping
spac << np.nHi << np.nLo;
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac, player.pid);
}
break;
case NP_MSG_PONG :
{
const u32 ping = m_ping_timer.GetTimeElapsed();
u32 ping_key = 0;
packet >> ping_key;
if (m_ping_key == ping_key)
{
//PanicAlertT("good pong");
player.ping = ping;
}
UpdateGUI();
}
break;
case NP_MSG_START_GAME :
{
packet >> player.current_game;
}
break;
default :
PanicAlertT("Unknown message with id:%d received from player:%d Kicking player!", mid, player.pid);
// unknown message, kick the client
return 1;
break;
}
return 0;
}
// called from ---GUI--- thread
void NetPlayServer::GetPlayerList(std::string& list, std::vector<int>& pid_list)
{
CritLocker player_lock(m_crit.players); // lock players
std::ostringstream ss;
std::map<sf::SocketTCP, Client>::const_iterator
i = m_players.begin(),
e = m_players.end();
for ( ; i!=e; ++i)
{
ss << i->second.ToString() << " " << i->second.ping << "ms\n";
pid_list.push_back(i->second.pid);
}
list = ss.str();
}
// called from ---GUI--- thread / and ---NETPLAY--- thread
void NetPlayServer::SendChatMessage(const std::string& msg)
{
sf::Packet spac;
spac << (MessageId)NP_MSG_CHAT_MESSAGE;
spac << (PlayerId)0; // server id always 0
spac << msg;
CritLocker player_lock(m_crit.players); // lock players
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
}
// called from ---GUI--- thread
bool NetPlayServer::ChangeGame(const std::string &game)
{
CritLocker game_lock(m_crit.game); // lock game state
m_selected_game = game;
// send changed game to clients
sf::Packet spac;
spac << (MessageId)NP_MSG_CHANGE_GAME;
spac << game;
CritLocker player_lock(m_crit.players); // lock players
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
return true;
}
// called from ---CPU--- thread
void NetPlayServer::SendPadState(const PadMapping local_nb, const NetPad& np)
{
// send to server
sf::Packet spac;
spac << (MessageId)NP_MSG_PAD_DATA;
spac << m_local_player->pad_map[local_nb]; // in-game pad num
spac << np.nHi << np.nLo;
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
}
// called from ---GUI--- thread
bool NetPlayServer::StartGame(const std::string &path)
{
CritLocker game_lock(m_crit.game); // lock game state
if (false == NetPlay::StartGame(path))
return false;
// TODO: i dont like this here
m_current_game = Common::Timer::GetTimeMs();
// no change, just update with clients
AdjustPadBufferSize(m_target_buffer_size);
// tell clients to start game
sf::Packet spac;
spac << (MessageId)NP_MSG_START_GAME;
spac << m_current_game;
CritLocker player_lock(m_crit.players); // lock players
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
return true;
}
// called from ---GUI--- thread
bool NetPlayServer::StopGame()
{
if (false == NetPlay::StopGame())
return false;
// tell clients to stop game
sf::Packet spac;
spac << (MessageId)NP_MSG_STOP_GAME;
CritLocker player_lock(m_crit.players); // lock players
CritLocker send_lock(m_crit.send); // lock send
SendToClients(spac);
return true;
}
// called from multiple threads
void NetPlayServer::SendToClients(sf::Packet& packet, const PlayerId skip_pid)
{
std::map<sf::SocketTCP, Client>::iterator
i = m_players.begin(),
e = m_players.end();
for ( ; i!=e; ++i)
if (i->second.pid && (i->second.pid != skip_pid))
i->second.socket.Send(packet);
}

View File

@ -20,6 +20,7 @@
#include "NetPlay.h"
#include "NetWindow.h"
#include "Frame.h"
#include <sstream>
@ -27,11 +28,13 @@
#define NETPLAY_TITLEBAR "Dolphin NetPlay"
DEFINE_EVENT_TYPE(wxEVT_THREAD)
BEGIN_EVENT_TABLE(NetPlayDiag, wxFrame)
EVT_COMMAND(wxID_ANY, wxEVT_THREAD, NetPlayDiag::OnThread)
END_EVENT_TABLE()
static NetPlay* netplay_ptr = NULL;
extern CFrame* main_frame;
NetPlaySetupDiag::NetPlaySetupDiag(wxWindow* const parent, const CGameListCtrl* const game_list)
: wxFrame(parent, wxID_ANY, wxT(NETPLAY_TITLEBAR), wxDefaultPosition, wxDefaultSize)
@ -218,7 +221,7 @@ void NetPlaySetupDiag::OnJoin(wxCommandEvent&)
unsigned long port = 0;
m_connect_port_text->GetValue().ToULong(&port);
::netplay_ptr = new NetPlayClient(std::string(m_connect_ip_text->GetValue().mb_str())
, (u16)port, std::string(m_nickname_text->GetValue().mb_str()), npd);
, (u16)port, npd, std::string(m_nickname_text->GetValue().mb_str()));
if (::netplay_ptr->is_connected)
{
//NetPlayServerDiag* const npsd =
@ -381,6 +384,50 @@ void NetPlayDiag::OnStop(wxCommandEvent&)
::netplay_ptr->StopGame();
}
void NetPlayDiag::BootGame(const std::string& filename)
{
main_frame->BootGame(filename);
}
void NetPlayDiag::StopGame()
{
main_frame->DoStop();
}
// NetPlayUI methods called from ---NETPLAY--- thread
void NetPlayDiag::Update()
{
wxCommandEvent evt(wxEVT_THREAD, 1);
GetEventHandler()->AddPendingEvent(evt);
}
void NetPlayDiag::AppendChat(const std::string& msg)
{
chat_msgs.Push(msg);
// silly
Update();
}
void NetPlayDiag::OnMsgChangeGame(const std::string& filename)
{
wxCommandEvent evt(wxEVT_THREAD, NP_GUI_EVT_CHANGE_GAME);
// TODO: using a wxString in AddPendingEvent from another thread is unsafe i guess?
evt.SetString(wxString(filename.c_str(), *wxConvCurrent));
GetEventHandler()->AddPendingEvent(evt);
}
void NetPlayDiag::OnMsgStartGame()
{
wxCommandEvent evt(wxEVT_THREAD, NP_GUI_EVT_START_GAME);
GetEventHandler()->AddPendingEvent(evt);
}
void NetPlayDiag::OnMsgStopGame()
{
wxCommandEvent evt(wxEVT_THREAD, NP_GUI_EVT_STOP_GAME);
GetEventHandler()->AddPendingEvent(evt);
}
void NetPlayDiag::OnPadBuffHelp(wxCommandEvent&)
{
const u64 time = ((NetPlayServer*)::netplay_ptr)->CalculateMinimumBufferTime();

View File

@ -35,6 +35,8 @@
#include "FifoQueue.h"
#include "NetPlay.h"
enum
{
NP_GUI_EVT_CHANGE_GAME = 45,
@ -62,7 +64,7 @@ private:
const CGameListCtrl* const m_game_list;
};
class NetPlayDiag : public wxFrame
class NetPlayDiag : public wxFrame, public NetPlayUI
{
public:
NetPlayDiag(wxWindow* const parent, const CGameListCtrl* const game_list
@ -74,6 +76,17 @@ public:
void OnStart(wxCommandEvent& event);
void OnStop(wxCommandEvent& event);
// implementation of NetPlayUI methods
void BootGame(const std::string& filename);
void StopGame();
void Update();
void AppendChat(const std::string& msg);
void OnMsgChangeGame(const std::string& filename);
void OnMsgStartGame();
void OnMsgStopGame();
private:
DECLARE_EVENT_TABLE()

View File

@ -33,9 +33,6 @@ else:
'MemcardManager.cpp',
'MemoryCards/GCMemcard.cpp',
'MemoryCards/WiiSaveCrypted.cpp',
'NetPlay.cpp',
'NetPlayClient.cpp',
'NetPlayServer.cpp',
'NetWindow.cpp',
'UDPConfigDiag.cpp',
'WiimoteConfigDiag.cpp',