2015-02-02 09:56:53 +00:00
|
|
|
// This file is public domain, in case it's useful to anyone. -comex
|
|
|
|
|
|
|
|
#pragma once
|
2017-09-09 19:52:35 +00:00
|
|
|
|
|
|
|
#include <cstddef>
|
2015-02-02 09:56:53 +00:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
2017-09-09 19:52:35 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <enet/enet.h>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2015-02-02 09:56:53 +00:00
|
|
|
#include "Common/Thread.h"
|
|
|
|
#include "Common/TraversalProto.h"
|
|
|
|
|
|
|
|
class TraversalClientClient
|
|
|
|
{
|
|
|
|
public:
|
2017-07-30 19:56:12 +00:00
|
|
|
virtual ~TraversalClientClient() = default;
|
2016-06-24 08:43:46 +00:00
|
|
|
virtual void OnTraversalStateChanged() = 0;
|
|
|
|
virtual void OnConnectReady(ENetAddress addr) = 0;
|
|
|
|
virtual void OnConnectFailed(u8 reason) = 0;
|
2015-02-02 09:56:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TraversalClient
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
enum State
|
|
|
|
{
|
|
|
|
Connecting,
|
|
|
|
Connected,
|
|
|
|
Failure
|
|
|
|
};
|
2017-08-08 22:29:47 +00:00
|
|
|
enum class FailureReason
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
BadHost = 0x300,
|
|
|
|
VersionTooOld,
|
|
|
|
ServerForgotAboutUs,
|
|
|
|
SocketSendError,
|
|
|
|
ResendTimeout,
|
|
|
|
};
|
|
|
|
TraversalClient(ENetHost* netHost, const std::string& server, const u16 port);
|
|
|
|
~TraversalClient();
|
2018-04-16 20:02:21 +00:00
|
|
|
|
|
|
|
TraversalHostId GetHostID() const;
|
|
|
|
State GetState() const;
|
|
|
|
FailureReason GetFailureReason() const;
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void Reset();
|
|
|
|
void ConnectToClient(const std::string& host);
|
|
|
|
void ReconnectToServer();
|
|
|
|
void Update();
|
|
|
|
// called from NetHost
|
|
|
|
bool TestPacket(u8* data, size_t size, ENetAddress* from);
|
|
|
|
void HandleResends();
|
2015-02-02 09:56:53 +00:00
|
|
|
|
2018-04-16 20:24:09 +00:00
|
|
|
TraversalClientClient* m_Client = nullptr;
|
2015-02-02 09:56:53 +00:00
|
|
|
|
|
|
|
private:
|
2016-06-24 08:43:46 +00:00
|
|
|
struct OutgoingTraversalPacketInfo
|
|
|
|
{
|
|
|
|
TraversalPacket packet;
|
|
|
|
int tries;
|
|
|
|
enet_uint32 sendTime;
|
|
|
|
};
|
|
|
|
void HandleServerPacket(TraversalPacket* packet);
|
|
|
|
void ResendPacket(OutgoingTraversalPacketInfo* info);
|
|
|
|
TraversalRequestId SendTraversalPacket(const TraversalPacket& packet);
|
|
|
|
void OnFailure(FailureReason reason);
|
|
|
|
void HandlePing();
|
|
|
|
static int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event);
|
2018-04-16 20:02:21 +00:00
|
|
|
|
|
|
|
ENetHost* m_NetHost;
|
2018-04-16 20:24:09 +00:00
|
|
|
TraversalHostId m_HostId{};
|
|
|
|
State m_State{};
|
|
|
|
FailureReason m_FailureReason{};
|
|
|
|
TraversalRequestId m_ConnectRequestId = 0;
|
|
|
|
bool m_PendingConnect = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
std::list<OutgoingTraversalPacketInfo> m_OutgoingTraversalPackets;
|
2018-04-16 20:24:09 +00:00
|
|
|
ENetAddress m_ServerAddress{};
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string m_Server;
|
|
|
|
u16 m_port;
|
2018-04-16 20:24:09 +00:00
|
|
|
enet_uint32 m_PingTime = 0;
|
2015-02-02 09:56:53 +00: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 11:08:37 +00:00
|
|
|
bool EnsureTraversalClient(const std::string& server, u16 server_port, u16 listen_port = 0);
|
2015-02-02 09:56:53 +00:00
|
|
|
void ReleaseTraversalClient();
|