2021-07-04 20:31:23 -06:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2015-02-02 02:56:53 -07:00
|
|
|
|
|
|
|
#pragma once
|
2017-09-09 13:52:35 -06:00
|
|
|
|
|
|
|
#include <cstddef>
|
2015-02-02 02:56:53 -07:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
2017-09-09 13:52:35 -06:00
|
|
|
#include <string>
|
2021-01-19 12:26:04 -07:00
|
|
|
#include <string_view>
|
2017-09-09 13:52:35 -06:00
|
|
|
|
|
|
|
#include <enet/enet.h>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2015-02-02 02:56:53 -07:00
|
|
|
#include "Common/Thread.h"
|
|
|
|
#include "Common/TraversalProto.h"
|
|
|
|
|
|
|
|
class TraversalClientClient
|
|
|
|
{
|
|
|
|
public:
|
2017-07-30 13:56:12 -06:00
|
|
|
virtual ~TraversalClientClient() = default;
|
2015-02-02 02:56:53 -07:00
|
|
|
virtual void OnTraversalStateChanged() = 0;
|
|
|
|
virtual void OnConnectReady(ENetAddress addr) = 0;
|
2021-01-19 12:05:16 -07:00
|
|
|
virtual void OnConnectFailed(TraversalConnectFailedReason reason) = 0;
|
2015-02-02 02:56:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class TraversalClient
|
|
|
|
{
|
|
|
|
public:
|
2021-01-19 12:00:01 -07:00
|
|
|
enum class State
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
Connecting,
|
|
|
|
Connected,
|
2021-01-19 12:00:01 -07:00
|
|
|
Failure,
|
2015-02-02 02:56:53 -07:00
|
|
|
};
|
2017-08-08 16:29:47 -06:00
|
|
|
enum class FailureReason
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
BadHost = 0x300,
|
|
|
|
VersionTooOld,
|
|
|
|
ServerForgotAboutUs,
|
|
|
|
SocketSendError,
|
|
|
|
ResendTimeout,
|
|
|
|
};
|
2015-02-14 20:51:08 -07:00
|
|
|
TraversalClient(ENetHost* netHost, const std::string& server, const u16 port);
|
2015-02-02 02:56:53 -07:00
|
|
|
~TraversalClient();
|
2018-04-16 14:02:21 -06:00
|
|
|
|
|
|
|
TraversalHostId GetHostID() const;
|
|
|
|
State GetState() const;
|
|
|
|
FailureReason GetFailureReason() const;
|
|
|
|
|
2021-01-19 12:00:01 -07:00
|
|
|
bool HasFailed() const { return m_State == State::Failure; }
|
|
|
|
bool IsConnecting() const { return m_State == State::Connecting; }
|
|
|
|
bool IsConnected() const { return m_State == State::Connected; }
|
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
void Reset();
|
2021-01-19 12:26:04 -07:00
|
|
|
void ConnectToClient(std::string_view host);
|
2015-02-02 02:56:53 -07:00
|
|
|
void ReconnectToServer();
|
|
|
|
void Update();
|
|
|
|
void HandleResends();
|
|
|
|
|
2018-04-16 14:24:09 -06:00
|
|
|
TraversalClientClient* m_Client = nullptr;
|
2015-02-02 02:56:53 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct OutgoingTraversalPacketInfo
|
|
|
|
{
|
|
|
|
TraversalPacket packet;
|
|
|
|
int tries;
|
2018-04-16 14:28:48 -06:00
|
|
|
u32 sendTime;
|
2015-02-02 02:56:53 -07:00
|
|
|
};
|
|
|
|
void HandleServerPacket(TraversalPacket* packet);
|
2018-04-16 14:25:34 -06:00
|
|
|
// called from NetHost
|
|
|
|
bool TestPacket(u8* data, size_t size, ENetAddress* from);
|
2015-02-02 02:56:53 -07:00
|
|
|
void ResendPacket(OutgoingTraversalPacketInfo* info);
|
|
|
|
TraversalRequestId SendTraversalPacket(const TraversalPacket& packet);
|
2015-02-16 23:53:50 -07:00
|
|
|
void OnFailure(FailureReason reason);
|
2015-02-02 02:56:53 -07:00
|
|
|
void HandlePing();
|
|
|
|
static int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event);
|
2018-04-16 14:02:21 -06:00
|
|
|
|
|
|
|
ENetHost* m_NetHost;
|
2018-04-16 14:24:09 -06:00
|
|
|
TraversalHostId m_HostId{};
|
|
|
|
State m_State{};
|
|
|
|
FailureReason m_FailureReason{};
|
|
|
|
TraversalRequestId m_ConnectRequestId = 0;
|
|
|
|
bool m_PendingConnect = false;
|
2015-02-02 02:56:53 -07:00
|
|
|
std::list<OutgoingTraversalPacketInfo> m_OutgoingTraversalPackets;
|
2018-04-16 14:24:09 -06:00
|
|
|
ENetAddress m_ServerAddress{};
|
2015-02-02 02:56:53 -07:00
|
|
|
std::string m_Server;
|
2015-02-14 20:51:08 -07:00
|
|
|
u16 m_port;
|
2018-04-16 14:28:48 -06:00
|
|
|
u32 m_PingTime = 0;
|
2015-02-02 02:56:53 -07:00
|
|
|
};
|
|
|
|
extern std::unique_ptr<TraversalClient> g_TraversalClient;
|
|
|
|
// the NetHost connected to the TraversalClient.
|
|
|
|
extern std::unique_ptr<ENetHost> g_MainNetHost;
|
|
|
|
// Create g_TraversalClient and g_MainNetHost if necessary.
|
2015-09-20 05:08:37 -06:00
|
|
|
bool EnsureTraversalClient(const std::string& server, u16 server_port, u16 listen_port = 0);
|
2015-02-02 02:56:53 -07:00
|
|
|
void ReleaseTraversalClient();
|