2010-05-01 19:10:35 +00:00
|
|
|
#ifndef _NETPLAY_H
|
|
|
|
#define _NETPLAY_H
|
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
#include "CommonTypes.h"
|
|
|
|
//#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include "Thread.h"
|
2010-05-05 04:31:21 +00:00
|
|
|
#include "Timer.h"
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
// hax, i hope something like this isn't needed on non-windows
|
|
|
|
#define _WINSOCK2API_
|
|
|
|
#include <SFML/Network.hpp>
|
|
|
|
|
2010-06-13 09:26:00 +00:00
|
|
|
#include "GCPadStatus.h"
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
//#include <wx/wx.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
2010-05-05 04:31:21 +00:00
|
|
|
#include <sstream>
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
class NetPlayDiag;
|
|
|
|
|
|
|
|
class NetPad
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NetPad();
|
|
|
|
NetPad(const SPADStatus* const);
|
|
|
|
|
|
|
|
u32 nHi;
|
|
|
|
u32 nLo;
|
|
|
|
};
|
|
|
|
|
2010-06-06 03:52:11 +00:00
|
|
|
struct Rpt : public std::vector<u8>
|
2010-05-05 04:31:21 +00:00
|
|
|
{
|
2010-06-06 03:52:11 +00:00
|
|
|
u16 channel;
|
2010-05-05 04:31:21 +00:00
|
|
|
};
|
|
|
|
|
2010-06-06 03:52:11 +00:00
|
|
|
typedef std::vector<Rpt> NetWiimote;
|
|
|
|
|
2010-05-05 04:31:21 +00:00
|
|
|
#define NETPLAY_VERSION "Dolphin NetPlay 2.2"
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
// messages
|
|
|
|
#define NP_MSG_PLAYER_JOIN 0x10
|
|
|
|
#define NP_MSG_PLAYER_LEAVE 0x11
|
|
|
|
|
|
|
|
#define NP_MSG_CHAT_MESSAGE 0x30
|
|
|
|
|
|
|
|
#define NP_MSG_PAD_DATA 0x60
|
|
|
|
#define NP_MSG_PAD_MAPPING 0x61
|
|
|
|
#define NP_MSG_PAD_BUFFER 0x62
|
|
|
|
|
2010-05-05 04:31:21 +00:00
|
|
|
#define NP_MSG_WIIMOTE_DATA 0x70
|
|
|
|
#define NP_MSG_WIIMOTE_MAPPING 0x71 // just using pad mapping for now
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
#define NP_MSG_START_GAME 0xA0
|
|
|
|
#define NP_MSG_CHANGE_GAME 0xA1
|
2010-05-02 23:01:50 +00:00
|
|
|
#define NP_MSG_STOP_GAME 0xA2
|
2010-05-05 04:31:21 +00:00
|
|
|
#define NP_MSG_DISABLE_GAME 0xA3
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
#define NP_MSG_READY 0xD0
|
|
|
|
#define NP_MSG_NOT_READY 0xD1
|
|
|
|
|
|
|
|
#define NP_MSG_PING 0xE0
|
|
|
|
#define NP_MSG_PONG 0xE1
|
|
|
|
// end messages
|
|
|
|
|
|
|
|
typedef u8 MessageId;
|
|
|
|
typedef u8 PlayerId;
|
|
|
|
typedef s8 PadMapping;
|
2010-05-05 04:31:21 +00:00
|
|
|
typedef u32 FrameNum;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
CON_ERR_SERVER_FULL = 1,
|
|
|
|
CON_ERR_GAME_RUNNING,
|
|
|
|
CON_ERR_VERSION_MISMATCH
|
|
|
|
};
|
|
|
|
|
|
|
|
THREAD_RETURN NetPlayThreadFunc(void* arg);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
class NetPlay
|
|
|
|
{
|
|
|
|
public:
|
2010-05-05 04:31:21 +00:00
|
|
|
NetPlay();
|
2010-05-01 19:10:35 +00:00
|
|
|
virtual ~NetPlay();
|
|
|
|
virtual void Entry() = 0;
|
|
|
|
|
|
|
|
bool is_connected;
|
|
|
|
|
|
|
|
// Send and receive pads values
|
2010-05-05 04:31:21 +00:00
|
|
|
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);
|
2010-05-02 23:01:50 +00:00
|
|
|
virtual bool ChangeGame(const std::string& game) = 0;
|
2010-06-06 03:52:11 +00:00
|
|
|
virtual void GetPlayerList(std::string& list, std::vector<int>& pid_list) = 0;
|
2010-05-01 19:10:35 +00:00
|
|
|
virtual void SendChatMessage(const std::string& msg) = 0;
|
2010-05-02 23:01:50 +00:00
|
|
|
|
|
|
|
virtual bool StartGame(const std::string &path);
|
|
|
|
virtual bool StopGame();
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2010-05-05 04:31:21 +00:00
|
|
|
//void PushPadStates(unsigned int count);
|
|
|
|
|
|
|
|
u8 GetPadNum(u8 numPAD);
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
protected:
|
|
|
|
//NetPlay(Common::ThreadFunc entry, void* arg) : m_thread(entry, arg) {}
|
2010-05-05 04:31:21 +00:00
|
|
|
//void GetBufferedPad(const u8 pad_nb, NetPad* const netvalues);
|
|
|
|
void ClearBuffers();
|
2010-05-01 19:10:35 +00:00
|
|
|
void UpdateGUI();
|
2010-05-02 23:01:50 +00:00
|
|
|
void AppendChatGUI(const std::string& msg);
|
2010-05-05 04:31:21 +00:00
|
|
|
virtual void SendPadState(const PadMapping local_nb, const NetPad& np) = 0;
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2010-05-05 04:31:21 +00:00
|
|
|
Common::CriticalSection game;
|
|
|
|
// lock order
|
|
|
|
Common::CriticalSection players, buffer, send;
|
2010-05-01 19:10:35 +00:00
|
|
|
} m_crit;
|
|
|
|
|
2010-05-02 23:01:50 +00:00
|
|
|
class Player
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Player();
|
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
PlayerId pid;
|
|
|
|
std::string name;
|
|
|
|
PadMapping pad_map[4];
|
|
|
|
std::string revision;
|
|
|
|
};
|
|
|
|
|
2010-06-06 03:52:11 +00:00
|
|
|
std::queue<NetPad> m_pad_buffer[4];
|
|
|
|
std::queue<NetWiimote> m_wiimote_buffer[4];
|
2010-05-05 04:31:21 +00:00
|
|
|
|
2010-06-06 03:52:11 +00:00
|
|
|
NetWiimote m_wiimote_input[4];
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
NetPlayDiag* m_dialog;
|
|
|
|
sf::SocketTCP m_socket;
|
|
|
|
Common::Thread* m_thread;
|
|
|
|
sf::Selector<sf::SocketTCP> m_selector;
|
|
|
|
|
|
|
|
std::string m_selected_game;
|
2010-05-05 04:31:21 +00:00
|
|
|
volatile bool m_is_running;
|
2010-05-01 19:10:35 +00:00
|
|
|
volatile bool m_do_loop;
|
|
|
|
|
2010-05-05 04:31:21 +00:00
|
|
|
unsigned int m_target_buffer_size;
|
|
|
|
|
|
|
|
Player* m_local_player;
|
|
|
|
|
|
|
|
u32 m_on_game;
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-05-05 04:31:21 +00:00
|
|
|
void NetPlay_Enable(NetPlay* const np);
|
|
|
|
void NetPlay_Disable();
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
class NetPlayServer : public NetPlay
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Entry();
|
|
|
|
|
|
|
|
NetPlayServer(const u16 port, const std::string& name, NetPlayDiag* const npd = NULL, const std::string& game = "");
|
|
|
|
~NetPlayServer();
|
|
|
|
|
2010-06-06 03:52:11 +00:00
|
|
|
void GetPlayerList(std::string& list, std::vector<int>& pid_list);
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
// Send and receive pads values
|
2010-05-05 04:31:21 +00:00
|
|
|
//bool GetNetPads(const u8 pad_nb, const SPADStatus* const, NetPad* const netvalues);
|
2010-05-02 23:01:50 +00:00
|
|
|
bool ChangeGame(const std::string& game);
|
2010-05-01 19:10:35 +00:00
|
|
|
void SendChatMessage(const std::string& msg);
|
2010-05-02 23:01:50 +00:00
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
bool StartGame(const std::string &path);
|
2010-05-02 23:01:50 +00:00
|
|
|
bool StopGame();
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2010-06-06 03:52:11 +00:00
|
|
|
bool GetPadMapping(const int pid, int map[]);
|
|
|
|
bool SetPadMapping(const int pid, const int map[]);
|
|
|
|
|
2010-05-05 04:31:21 +00:00
|
|
|
u64 CalculateMinimumBufferTime();
|
|
|
|
void AdjustPadBufferSize(unsigned int size);
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
private:
|
2010-05-02 23:01:50 +00:00
|
|
|
class Client : public Player
|
2010-05-01 19:10:35 +00:00
|
|
|
{
|
|
|
|
public:
|
2010-05-05 04:31:21 +00:00
|
|
|
Client() : ping(0), on_game(0) {}
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
sf::SocketTCP socket;
|
2010-05-05 04:31:21 +00:00
|
|
|
u64 ping;
|
|
|
|
u32 on_game;
|
2010-05-01 19:10:35 +00:00
|
|
|
};
|
|
|
|
|
2010-05-05 04:31:21 +00:00
|
|
|
void SendPadState(const PadMapping local_nb, const NetPad& np);
|
2010-05-02 04:24:49 +00:00
|
|
|
void SendToClients(sf::Packet& packet, const PlayerId skip_pid = 0);
|
2010-05-01 19:10:35 +00:00
|
|
|
unsigned int OnConnect(sf::SocketTCP& socket);
|
|
|
|
unsigned int OnDisconnect(sf::SocketTCP& socket);
|
|
|
|
unsigned int OnData(sf::Packet& packet, sf::SocketTCP& socket);
|
2010-06-06 03:52:11 +00:00
|
|
|
void UpdatePadMapping();
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2010-05-02 23:01:50 +00:00
|
|
|
std::map<sf::SocketTCP, Client> m_players;
|
2010-05-05 04:31:21 +00:00
|
|
|
|
|
|
|
Common::Timer m_ping_timer;
|
|
|
|
u32 m_ping_key;
|
|
|
|
bool m_update_pings;
|
2010-05-01 19:10:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class NetPlayClient : public NetPlay
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Entry();
|
|
|
|
|
|
|
|
NetPlayClient(const std::string& address, const u16 port, const std::string& name, NetPlayDiag* const npd = NULL);
|
|
|
|
~NetPlayClient();
|
|
|
|
|
2010-06-06 03:52:11 +00:00
|
|
|
void GetPlayerList(std::string& list, std::vector<int>& pid_list);
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
// Send and receive pads values
|
2010-05-05 04:31:21 +00:00
|
|
|
//bool GetNetPads(const u8 pad_nb, const SPADStatus* const, NetPad* const netvalues);
|
|
|
|
bool StartGame(const std::string &path);
|
2010-05-02 23:01:50 +00:00
|
|
|
bool ChangeGame(const std::string& game);
|
2010-05-01 19:10:35 +00:00
|
|
|
void SendChatMessage(const std::string& msg);
|
|
|
|
|
|
|
|
private:
|
2010-05-05 04:31:21 +00:00
|
|
|
void SendPadState(const PadMapping local_nb, const NetPad& np);
|
2010-05-01 19:10:35 +00:00
|
|
|
unsigned int OnData(sf::Packet& packet);
|
|
|
|
|
|
|
|
PlayerId m_pid;
|
2010-05-02 04:24:49 +00:00
|
|
|
std::map<PlayerId, Player> m_players;
|
2010-05-01 19:10:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|