2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 22:43:11 -04:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-02-10 13:54:46 -05:00
|
|
|
#pragma once
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2016-01-24 23:26:38 -05:00
|
|
|
#include <array>
|
2015-05-21 19:52:26 -04:00
|
|
|
#include <atomic>
|
2010-05-01 19:10:35 +00:00
|
|
|
#include <map>
|
2016-01-24 22:10:38 -05:00
|
|
|
#include <memory>
|
2015-05-26 17:23:43 -04:00
|
|
|
#include <mutex>
|
2016-01-24 21:46:46 -05:00
|
|
|
#include <string>
|
2015-05-26 17:23:43 -04:00
|
|
|
#include <thread>
|
2016-01-24 21:46:46 -05:00
|
|
|
#include <vector>
|
2015-02-02 02:08:58 -08:00
|
|
|
#include <SFML/Network/Packet.hpp>
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/FifoQueue.h"
|
2015-02-02 01:56:53 -08:00
|
|
|
#include "Common/TraversalClient.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/NetPlayProto.h"
|
|
|
|
#include "InputCommon/GCPadStatus.h"
|
2015-02-02 02:08:58 -08:00
|
|
|
|
2010-11-16 01:55:29 +00:00
|
|
|
|
2011-02-08 15:36:15 +00:00
|
|
|
class NetPlayUI
|
|
|
|
{
|
|
|
|
public:
|
2014-09-11 13:00:40 -04:00
|
|
|
virtual ~NetPlayUI() {}
|
2011-02-18 23:52:14 +00:00
|
|
|
|
2011-02-08 15:36:15 +00:00
|
|
|
virtual void BootGame(const std::string& filename) = 0;
|
|
|
|
virtual void StopGame() = 0;
|
|
|
|
|
|
|
|
virtual void Update() = 0;
|
|
|
|
virtual void AppendChat(const std::string& msg) = 0;
|
|
|
|
|
|
|
|
virtual void OnMsgChangeGame(const std::string& filename) = 0;
|
|
|
|
virtual void OnMsgStartGame() = 0;
|
|
|
|
virtual void OnMsgStopGame() = 0;
|
2013-09-03 15:50:41 -04:00
|
|
|
virtual bool IsRecording() = 0;
|
2011-02-08 15:36:15 +00:00
|
|
|
};
|
|
|
|
|
2013-08-18 09:43:01 -04:00
|
|
|
class Player
|
|
|
|
{
|
2015-02-02 01:27:06 -08:00
|
|
|
public:
|
2014-02-16 15:30:18 -05:00
|
|
|
PlayerId pid;
|
|
|
|
std::string name;
|
|
|
|
std::string revision;
|
|
|
|
u32 ping;
|
2013-08-18 09:43:01 -04:00
|
|
|
};
|
|
|
|
|
2015-02-02 01:56:53 -08:00
|
|
|
class NetPlayClient : public TraversalClientClient
|
2010-05-01 19:10:35 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-08-05 04:56:30 -04:00
|
|
|
void ThreadFunc();
|
2016-01-24 22:10:38 -05:00
|
|
|
void SendAsync(std::unique_ptr<sf::Packet> packet);
|
2013-08-05 04:56:30 -04:00
|
|
|
|
2015-05-28 20:28:48 -04:00
|
|
|
NetPlayClient(const std::string& address, const u16 port, NetPlayUI* dialog, const std::string& name, bool traversal, const std::string& centralServer, u16 centralPort);
|
2013-08-05 04:56:30 -04:00
|
|
|
~NetPlayClient();
|
|
|
|
|
|
|
|
void GetPlayerList(std::string& list, std::vector<int>& pid_list);
|
2015-08-16 00:58:15 -04:00
|
|
|
std::vector<const Player*> GetPlayers();
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2016-01-24 21:52:02 -05:00
|
|
|
// Called from the GUI thread.
|
|
|
|
bool IsConnected() const { return m_is_connected; }
|
2013-08-05 04:56:30 -04:00
|
|
|
|
|
|
|
bool StartGame(const std::string &path);
|
|
|
|
bool StopGame();
|
2013-09-02 21:54:28 -04:00
|
|
|
void Stop();
|
2013-08-05 04:56:30 -04:00
|
|
|
bool ChangeGame(const std::string& game);
|
|
|
|
void SendChatMessage(const std::string& msg);
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
// Send and receive pads values
|
2013-08-19 13:45:02 -04:00
|
|
|
bool WiimoteUpdate(int _number, u8* data, const u8 size);
|
2014-10-16 02:36:39 -04:00
|
|
|
bool GetNetPads(const u8 pad_nb, GCPadStatus* pad_status);
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2015-02-02 01:56:53 -08:00
|
|
|
void OnTraversalStateChanged() override;
|
|
|
|
void OnConnectReady(ENetAddress addr) override;
|
|
|
|
void OnConnectFailed(u8 reason) override;
|
|
|
|
|
2016-05-08 15:29:01 +02:00
|
|
|
bool IsFirstInGamePad(u8 ingame_pad) const;
|
|
|
|
u8 NumLocalPads() const;
|
|
|
|
|
|
|
|
u8 InGamePadToLocalPad(u8 ingame_pad);
|
2013-09-09 03:09:45 -04:00
|
|
|
u8 LocalPadToInGamePad(u8 localPad);
|
2010-05-05 04:31:21 +00:00
|
|
|
|
2013-09-22 14:27:52 -04:00
|
|
|
u8 LocalWiimoteToInGameWiimote(u8 local_pad);
|
2010-05-05 04:31:21 +00:00
|
|
|
|
2015-03-08 06:50:47 -04:00
|
|
|
static void SendTimeBase();
|
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
protected:
|
2010-05-05 04:31:21 +00:00
|
|
|
void ClearBuffers();
|
2010-05-01 19:10:35 +00:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2011-03-05 06:11:26 +00:00
|
|
|
std::recursive_mutex game;
|
2010-05-05 04:31:21 +00:00
|
|
|
// lock order
|
2015-03-09 17:31:13 +01:00
|
|
|
std::recursive_mutex players;
|
2015-03-13 02:03:09 +01:00
|
|
|
std::recursive_mutex async_queue_write;
|
2010-05-01 19:10:35 +00:00
|
|
|
} m_crit;
|
|
|
|
|
2015-03-13 02:03:09 +01:00
|
|
|
Common::FifoQueue<std::unique_ptr<sf::Packet>, false> m_async_queue;
|
2015-03-09 17:31:13 +01:00
|
|
|
|
2016-01-24 23:26:38 -05:00
|
|
|
std::array<Common::FifoQueue<GCPadStatus>, 4> m_pad_buffer;
|
|
|
|
std::array<Common::FifoQueue<NetWiimote >, 4> m_wiimote_buffer;
|
2010-05-05 04:31:21 +00:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
NetPlayUI* m_dialog = nullptr;
|
2015-02-02 01:27:06 -08:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
ENetHost* m_client = nullptr;
|
|
|
|
ENetPeer* m_server = nullptr;
|
2015-02-02 01:27:06 -08:00
|
|
|
std::thread m_thread;
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2015-05-21 19:52:26 -04:00
|
|
|
std::string m_selected_game;
|
2016-01-24 22:06:57 -05:00
|
|
|
std::atomic<bool> m_is_running{false};
|
|
|
|
std::atomic<bool> m_do_loop{true};
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
unsigned int m_target_buffer_size = 20;
|
2010-05-05 04:31:21 +00:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
Player* m_local_player = nullptr;
|
2010-05-05 04:31:21 +00:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
u32 m_current_game = 0;
|
2010-05-01 19:10:35 +00:00
|
|
|
|
2015-08-16 00:08:09 -04:00
|
|
|
PadMappingArray m_pad_map;
|
|
|
|
PadMappingArray m_wiimote_map;
|
2013-08-18 09:43:01 -04:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
bool m_is_recording = false;
|
2013-09-03 15:50:41 -04:00
|
|
|
|
2010-05-01 19:10:35 +00:00
|
|
|
private:
|
2016-01-24 21:58:56 -05:00
|
|
|
enum class ConnectionState
|
|
|
|
{
|
|
|
|
WaitingForTraversalClientConnection,
|
|
|
|
WaitingForTraversalClientConnectReady,
|
|
|
|
Connecting,
|
|
|
|
WaitingForHelloResponse,
|
|
|
|
Connected,
|
|
|
|
Failure
|
|
|
|
};
|
|
|
|
|
2016-01-24 22:46:37 -05:00
|
|
|
bool LocalPlayerHasControllerMapped() const;
|
|
|
|
|
2016-01-24 22:10:38 -05:00
|
|
|
void SendStartGamePacket();
|
|
|
|
void SendStopGamePacket();
|
|
|
|
|
2013-08-23 20:24:45 -04:00
|
|
|
void UpdateDevices();
|
2014-10-16 02:36:39 -04:00
|
|
|
void SendPadState(const PadMapping in_game_pad, const GCPadStatus& np);
|
2013-09-22 14:27:52 -04:00
|
|
|
void SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw);
|
2010-05-01 19:10:35 +00:00
|
|
|
unsigned int OnData(sf::Packet& packet);
|
2015-02-02 01:27:06 -08:00
|
|
|
void Send(sf::Packet& packet);
|
|
|
|
void Disconnect();
|
|
|
|
bool Connect();
|
|
|
|
|
2016-01-24 21:52:02 -05:00
|
|
|
bool m_is_connected = false;
|
2016-01-24 21:58:56 -05:00
|
|
|
ConnectionState m_connection_state = ConnectionState::Failure;
|
2016-01-24 21:52:02 -05:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
PlayerId m_pid = 0;
|
2014-02-09 18:29:13 -05:00
|
|
|
std::map<PlayerId, Player> m_players;
|
2015-02-02 01:27:06 -08:00
|
|
|
std::string m_host_spec;
|
|
|
|
std::string m_player_name;
|
2016-01-24 22:06:57 -05:00
|
|
|
bool m_connecting = false;
|
|
|
|
TraversalClient* m_traversal_client = nullptr;
|
2015-06-06 01:20:51 -04:00
|
|
|
|
2016-01-24 22:06:57 -05:00
|
|
|
u32 m_timebase_frame = 0;
|
2010-05-01 19:10:35 +00:00
|
|
|
};
|
|
|
|
|
2013-08-05 04:56:30 -04:00
|
|
|
void NetPlay_Enable(NetPlayClient* const np);
|
|
|
|
void NetPlay_Disable();
|