UDPNetworkUtility accepts addresses by value, encapsulated ClientData
I started encapsulating ClientData, and I added the internals for the heartbeat ssytem. However, when I took a look inside UDPNetworkUtility, I realized that I didn't have to pass the IPaddresses by reference anymore. Therefore, I've changed it to accept the addresses by value, and I'm committing that change right away before I finish the heartbeat system. This engine is really shaping up, I think.
This commit is contained in:
@@ -315,7 +315,7 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) {
|
||||
void InWorld::HandlePing(ServerPacket* const argPacket) {
|
||||
ServerPacket newPacket;
|
||||
newPacket.type = SerialPacketType::PONG;
|
||||
network.SendTo(&argPacket->srcAddress, &newPacket);
|
||||
network.SendTo(argPacket->srcAddress, &newPacket);
|
||||
}
|
||||
|
||||
void InWorld::HandlePong(ServerPacket* const argPacket) {
|
||||
|
||||
@@ -217,7 +217,7 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
|
||||
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
|
||||
clientIndex = argPacket->clientIndex;
|
||||
accountIndex = argPacket->accountIndex;
|
||||
network.Bind(&argPacket->srcAddress, Channels::SERVER);
|
||||
network.Bind(argPacket->srcAddress, Channels::SERVER);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
|
||||
//send this player's character info
|
||||
@@ -251,6 +251,6 @@ void LobbyMenu::SendJoinRequest() {
|
||||
strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
|
||||
|
||||
//join the selected server
|
||||
network.SendTo(&selection->address, &packet);
|
||||
network.SendTo(selection->address, &packet);
|
||||
selection = nullptr;
|
||||
}
|
||||
|
||||
@@ -55,11 +55,11 @@ int UDPNetworkUtility::Bind(const char* ip, int port, int channel) {
|
||||
throw(std::runtime_error("Failed to resolve a host"));
|
||||
}
|
||||
|
||||
return Bind(&add, channel);
|
||||
return Bind(add, channel);
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Bind(IPaddress* add, int channel) {
|
||||
int ret = SDLNet_UDP_Bind(socket, channel, add);
|
||||
int UDPNetworkUtility::Bind(IPaddress add, int channel) {
|
||||
int ret = SDLNet_UDP_Bind(socket, channel, &add);
|
||||
|
||||
if (ret < 0) {
|
||||
throw(std::runtime_error("Failed to bind to a channel"));
|
||||
@@ -82,17 +82,17 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, void* data, int len) {
|
||||
throw(std::runtime_error("Failed to resolve a host"));
|
||||
}
|
||||
|
||||
SendTo(&add, data, len);
|
||||
SendTo(add, data, len);
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::SendTo(IPaddress* add, void* data, int len) {
|
||||
int UDPNetworkUtility::SendTo(IPaddress add, void* data, int len) {
|
||||
if (len > packet->maxlen) {
|
||||
throw(std::runtime_error("The buffer is to large for the UDPpacket"));
|
||||
}
|
||||
memset(packet->data, 0, packet->maxlen);
|
||||
memcpy(packet->data, data, len);
|
||||
packet->len = len;
|
||||
packet->address = *add;
|
||||
packet->address = add;
|
||||
|
||||
int ret = SDLNet_UDP_Send(socket, -1, packet);
|
||||
|
||||
@@ -162,14 +162,14 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacketBase* serial
|
||||
throw(std::runtime_error("Failed to resolve a host"));
|
||||
}
|
||||
|
||||
SendTo(&add, serialPacket);
|
||||
SendTo(add, serialPacket);
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacketBase* serialPacket) {
|
||||
int UDPNetworkUtility::SendTo(IPaddress add, SerialPacketBase* serialPacket) {
|
||||
memset(packet->data, 0, packet->maxlen);
|
||||
serializePacket(packet->data, serialPacket);
|
||||
packet->len = PACKET_BUFFER_SIZE;
|
||||
packet->address = *add;
|
||||
packet->address = add;
|
||||
|
||||
int ret = SDLNet_UDP_Send(socket, -1, packet);
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public:
|
||||
|
||||
//bind to a channel
|
||||
int Bind(const char* ip, int port, int channel = -1);
|
||||
int Bind(IPaddress* add, int channel = -1);
|
||||
int Bind(IPaddress add, int channel = -1);
|
||||
void Unbind(int channel);
|
||||
|
||||
IPaddress* GetIPAddress(int channel) {
|
||||
@@ -45,14 +45,14 @@ public:
|
||||
|
||||
//send a buffer
|
||||
int SendTo(const char* ip, int port, void* data, int len);
|
||||
int SendTo(IPaddress* add, void* data, int len);
|
||||
int SendTo(IPaddress add, void* data, int len);
|
||||
int SendTo(int channel, void* data, int len);
|
||||
int SendToAllChannels(void* data, int len);
|
||||
int Receive();
|
||||
|
||||
//send a SerialPacketBase
|
||||
int SendTo(const char* ip, int port, SerialPacketBase* serialPacket);
|
||||
int SendTo(IPaddress* add, SerialPacketBase* serialPacket);
|
||||
int SendTo(IPaddress add, SerialPacketBase* serialPacket);
|
||||
int SendTo(int channel, SerialPacketBase* serialPacket);
|
||||
int SendToAllChannels(SerialPacketBase* serialPacket);
|
||||
int Receive(SerialPacketBase* serialPacket);
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "client_data.hpp"
|
||||
|
||||
int ClientData::IncrementAttempts() {
|
||||
lastBeat = Clock::now();
|
||||
return attemptedBeats++;
|
||||
}
|
||||
|
||||
int ClientData::ResetAttempts() {
|
||||
lastBeat = Clock::now();
|
||||
return attemptedBeats = 0;
|
||||
}
|
||||
+24
-2
@@ -24,9 +24,31 @@
|
||||
|
||||
#include "SDL/SDL_net.h"
|
||||
|
||||
struct ClientData {
|
||||
#include <chrono>
|
||||
|
||||
//TODO: ClientManager?
|
||||
class ClientData {
|
||||
public:
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
|
||||
ClientData() = default;
|
||||
ClientData(IPaddress add): address(add) {}
|
||||
~ClientData() = default;
|
||||
|
||||
IPaddress SetAddress(IPaddress add) { return address = add; }
|
||||
IPaddress GetAddress() { return address; }
|
||||
|
||||
Clock::time_point GetLastBeat() { return lastBeat; }
|
||||
|
||||
int GetAttempts() { return attemptedBeats; }
|
||||
int IncrementAttempts();
|
||||
int ResetAttempts();
|
||||
|
||||
private:
|
||||
IPaddress address = {0,0};
|
||||
//TODO: ping system?
|
||||
|
||||
Clock::time_point lastBeat = Clock::now();
|
||||
int attemptedBeats = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
void ServerApplication::HandlePing(ServerPacket* const argPacket) {
|
||||
ServerPacket newPacket;
|
||||
newPacket.type = SerialPacketType::PONG;
|
||||
network.SendTo(&argPacket->srcAddress, &newPacket);
|
||||
network.SendTo(argPacket->srcAddress, &newPacket);
|
||||
}
|
||||
|
||||
void ServerApplication::HandlePong(ServerPacket* const argPacket) {
|
||||
@@ -46,13 +46,13 @@ void ServerApplication::HandleBroadcastRequest(ServerPacket* const argPacket) {
|
||||
newPacket.playerCount = characterMgr.GetContainer()->size();
|
||||
newPacket.version = NETWORK_VERSION;
|
||||
|
||||
network.SendTo(&argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
||||
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
||||
}
|
||||
|
||||
void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) {
|
||||
//create the new client
|
||||
ClientData newClient;
|
||||
newClient.address = argPacket->srcAddress;
|
||||
newClient.SetAddress(argPacket->srcAddress);
|
||||
|
||||
//load the user account
|
||||
//TODO: handle passwords
|
||||
@@ -69,7 +69,7 @@ void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) {
|
||||
newPacket.clientIndex = clientIndex;
|
||||
newPacket.accountIndex = accountIndex;
|
||||
|
||||
network.SendTo(&newClient.address, static_cast<SerialPacket*>(&newPacket));
|
||||
network.SendTo(newClient.GetAddress(), static_cast<SerialPacket*>(&newPacket));
|
||||
|
||||
//finished this routine
|
||||
clientMap[clientIndex++] = newClient;
|
||||
@@ -90,7 +90,7 @@ void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) {
|
||||
|
||||
//forward to the specified client
|
||||
network.SendTo(
|
||||
&clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex() ].address,
|
||||
clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex() ].GetAddress(),
|
||||
static_cast<SerialPacket*>(argPacket)
|
||||
);
|
||||
|
||||
@@ -148,7 +148,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
|
||||
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
|
||||
|
||||
//send the content
|
||||
network.SendTo(&argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
||||
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
@@ -247,7 +247,7 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) {
|
||||
for (auto& it : *characterMgr.GetContainer()) {
|
||||
newPacket.characterIndex = it.first;
|
||||
CopyCharacterToPacket(&newPacket, it.first);
|
||||
network.SendTo(&client.address, static_cast<SerialPacket*>(&newPacket));
|
||||
network.SendTo(client.GetAddress(), static_cast<SerialPacket*>(&newPacket));
|
||||
}
|
||||
|
||||
//TODO: more in HandleSynchronize()
|
||||
@@ -261,7 +261,7 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) {
|
||||
|
||||
void ServerApplication::PumpPacket(SerialPacket* const argPacket) {
|
||||
for (auto& it : clientMap) {
|
||||
network.SendTo(&it.second.address, argPacket);
|
||||
network.SendTo(it.second.GetAddress(), argPacket);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user