2016-02-19 17:57:06 -05:00
|
|
|
#define NETWORK_MAX_PACKETS_PER_SECOND 100'000
|
|
|
|
|
2015-12-24 10:55:38 -05:00
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
class Network : public Component
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Address
|
|
|
|
{
|
|
|
|
public:
|
2016-02-15 14:28:01 -05:00
|
|
|
Address() { this->SetType(Game::netadrtype_t::NA_BAD); };
|
2015-12-24 10:55:38 -05:00
|
|
|
Address(std::string addrString);
|
|
|
|
Address(Game::netadr_t addr) : address(addr) {}
|
|
|
|
Address(Game::netadr_t* addr) : Address(*addr) {}
|
2016-01-24 15:08:14 -05:00
|
|
|
Address(const Address& obj) : address(obj.address) {};
|
2016-02-12 10:29:48 -05:00
|
|
|
Address(const Proto::Network::Address& addr) { this->Deserialize(addr); };
|
2015-12-24 10:55:38 -05:00
|
|
|
bool operator!=(const Address &obj) { return !(*this == obj); };
|
|
|
|
bool operator==(const Address &obj);
|
|
|
|
|
|
|
|
void SetPort(unsigned short port);
|
|
|
|
unsigned short GetPort();
|
2015-12-27 22:02:30 -05:00
|
|
|
|
|
|
|
void SetIP(DWORD ip);
|
2016-01-03 18:00:07 -05:00
|
|
|
void SetIP(Game::netIP_t ip);
|
|
|
|
Game::netIP_t GetIP();
|
2015-12-27 22:02:30 -05:00
|
|
|
|
2016-01-03 13:28:47 -05:00
|
|
|
void SetType(Game::netadrtype_t type);
|
|
|
|
Game::netadrtype_t GetType();
|
|
|
|
|
2015-12-24 10:55:38 -05:00
|
|
|
Game::netadr_t* Get();
|
|
|
|
const char* GetString();
|
|
|
|
|
2016-01-03 18:00:07 -05:00
|
|
|
bool IsLocal();
|
|
|
|
bool IsSelf();
|
2016-02-15 14:28:01 -05:00
|
|
|
bool IsValid();
|
2015-12-24 10:55:38 -05:00
|
|
|
|
2016-02-12 09:06:06 -05:00
|
|
|
void Serialize(Proto::Network::Address* protoAddress);
|
|
|
|
void Deserialize(const Proto::Network::Address& protoAddress);
|
|
|
|
|
2015-12-24 10:55:38 -05:00
|
|
|
private:
|
|
|
|
Game::netadr_t address;
|
|
|
|
};
|
|
|
|
|
2016-02-11 18:01:13 -05:00
|
|
|
typedef void(Callback)(Address address, std::string data);
|
2016-02-12 16:23:41 -05:00
|
|
|
typedef void(CallbackRaw)();
|
2015-12-24 10:55:38 -05:00
|
|
|
|
|
|
|
Network();
|
|
|
|
~Network();
|
|
|
|
const char* GetName() { return "Network"; };
|
|
|
|
|
2016-02-11 18:01:13 -05:00
|
|
|
static void Handle(std::string packet, Callback* callback);
|
2016-02-12 16:23:41 -05:00
|
|
|
static void OnStart(CallbackRaw* callback);
|
2016-01-03 09:14:44 -05:00
|
|
|
|
2016-02-10 11:18:45 -05:00
|
|
|
// Send quake-styled binary data
|
2015-12-27 22:02:30 -05:00
|
|
|
static void Send(Address target, std::string data);
|
2015-12-24 10:55:38 -05:00
|
|
|
static void Send(Game::netsrc_t type, Address target, std::string data);
|
|
|
|
|
2016-02-10 11:18:45 -05:00
|
|
|
// Allows sending raw data without quake header
|
2016-01-03 09:14:44 -05:00
|
|
|
static void SendRaw(Address target, std::string data);
|
|
|
|
static void SendRaw(Game::netsrc_t type, Address target, std::string data);
|
|
|
|
|
2016-02-10 11:18:45 -05:00
|
|
|
// Send quake-style command using binary data
|
|
|
|
static void SendCommand(Address target, std::string command, std::string data = "");
|
|
|
|
static void SendCommand(Game::netsrc_t type, Address target, std::string command, std::string data = "");
|
|
|
|
|
2016-01-03 13:28:47 -05:00
|
|
|
static void Broadcast(unsigned short port, std::string data);
|
|
|
|
static void BroadcastRange(unsigned int min, unsigned int max, std::string data);
|
|
|
|
static void BroadcastAll(std::string data);
|
|
|
|
|
2015-12-24 10:55:38 -05:00
|
|
|
private:
|
2016-01-09 09:30:13 -05:00
|
|
|
static SOCKET TcpSocket;
|
2015-12-24 10:55:38 -05:00
|
|
|
static std::string SelectedPacket;
|
2016-02-12 16:23:41 -05:00
|
|
|
static wink::signal<wink::slot<CallbackRaw>> StartupSignal;
|
2016-02-11 18:01:13 -05:00
|
|
|
static std::map<std::string, wink::slot<Callback>> PacketHandlers;
|
2016-02-12 16:23:41 -05:00
|
|
|
|
2015-12-24 10:55:38 -05:00
|
|
|
static int PacketInterceptionHandler(const char* packet);
|
2016-02-10 14:11:34 -05:00
|
|
|
static void DeployPacket(Game::netadr_t* from, Game::msg_t* msg);
|
2015-12-24 10:55:38 -05:00
|
|
|
static void DeployPacketStub();
|
2016-02-12 16:23:41 -05:00
|
|
|
|
|
|
|
static void NetworkStart();
|
|
|
|
static void NetworkStartStub();
|
2015-12-24 10:55:38 -05:00
|
|
|
};
|
|
|
|
}
|