Implemented primitive server "heartbeat" logic, which sends server info from server to client without being connected.
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Network/UDPClient.h"
|
||||
#include "Network/UDPServer.h" //LOL
|
||||
#include "Network/TCPClient.h"
|
||||
#include "Network/SnapshotDefinitions.h"
|
||||
#include "Core/World.h"
|
||||
@@ -82,6 +83,7 @@ public:
|
||||
void parseTCPConnect(Packet& packet);
|
||||
void parsePlayerConnected(Packet& packet);
|
||||
void parsePing();
|
||||
void parseHeartbeat(Packet& packet);
|
||||
void parseKick();
|
||||
void parsePlayersSpawned(Packet& packet);
|
||||
void parseEntityDeletion(Packet& packet);
|
||||
@@ -112,6 +114,7 @@ public:
|
||||
void parsePlayerDamage(Packet& packet);
|
||||
private:
|
||||
UDPClient m_Unreliable;
|
||||
UDPServer m_Heartbeat;
|
||||
TCPClient m_Reliable;
|
||||
};
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ enum class MessageType
|
||||
EntityDeleted,
|
||||
ComponentDeleted,
|
||||
PlayerTransform,
|
||||
Heartbeat,
|
||||
Invalid
|
||||
};
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "Network/TCPServer.h"
|
||||
#include "Network/UDPServer.h"
|
||||
#include "Network/UDPClient.h" //LOL
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Core/World.h"
|
||||
@@ -32,6 +33,7 @@ private:
|
||||
// Network channels
|
||||
TCPServer m_Reliable;
|
||||
UDPServer m_Unreliable;
|
||||
UDPClient m_Heartbeat;
|
||||
// dont forget to set these in the childrens receive logic
|
||||
boost::asio::ip::address m_Address;
|
||||
int m_Port = 27666;
|
||||
@@ -44,11 +46,13 @@ private:
|
||||
// time for previouse message
|
||||
std::clock_t previousePingMessage = std::clock();
|
||||
std::clock_t previousSnapshotMessage = std::clock();
|
||||
std::clock_t previousHeartbeat = std::clock();
|
||||
std::clock_t timOutTimer = std::clock();
|
||||
|
||||
// How often we send messages (milliseconds)
|
||||
float pingIntervalMs;
|
||||
float snapshotInterval;
|
||||
float heartbeatInterval = 5000;
|
||||
int checkTimeOutInterval = 100;
|
||||
int m_NextPlayerID = 0;
|
||||
std::vector<Events::InputCommand> m_InputCommandsToBroadcast;
|
||||
@@ -67,6 +71,7 @@ private:
|
||||
void addChildrenToPacket(Packet& packet, EntityID entityID);
|
||||
void addInputCommandsToPacket(Packet& packet);
|
||||
void sendPing();
|
||||
void sendHeartBeat();
|
||||
void checkForTimeOuts();
|
||||
void disconnect(PlayerID playerID);
|
||||
void parseMessageType(Packet& packet);
|
||||
|
||||
@@ -8,6 +8,7 @@ class UDPServer : public NetworkServer
|
||||
{
|
||||
public:
|
||||
UDPServer();
|
||||
UDPServer(int port);
|
||||
~UDPServer();
|
||||
void AcceptNewConnections(int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers);
|
||||
void Receive(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
|
||||
@@ -3,6 +3,7 @@ using namespace boost::asio::ip;
|
||||
|
||||
Client::Client(World* world, EventBroker* eventBroker)
|
||||
: Network(world, eventBroker)
|
||||
, m_Heartbeat(13)
|
||||
{
|
||||
// Asumes root node is EntityID_Invalid
|
||||
insertIntoServerClientMaps(EntityID_Invalid, EntityID_Invalid);
|
||||
@@ -65,7 +66,15 @@ void Client::Update()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
while (m_Heartbeat.IsSocketAvailable()) {
|
||||
Packet packet(MessageType::Invalid);
|
||||
PlayerDefinition localArea;
|
||||
localArea.Endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address().from_string("127.0.0.1"), 13);
|
||||
m_Heartbeat.Receive(packet, localArea);
|
||||
if(packet.GetMessageType() == MessageType::Heartbeat) {
|
||||
parseHeartbeat(packet);
|
||||
}
|
||||
}
|
||||
if (m_IsConnected) {
|
||||
// Don't send 1 input in 1 packet, bunch em up.
|
||||
if (m_SendInputIntervalMs < (1000 * (std::clock() - m_TimeSinceSentInputs) / (double)CLOCKS_PER_SEC)) {
|
||||
@@ -119,6 +128,9 @@ void Client::parseMessageType(Packet& packet)
|
||||
case MessageType::ComponentDeleted:
|
||||
parseComponentDeletion(packet);
|
||||
break;
|
||||
case MessageType::Heartbeat:
|
||||
parseHeartbeat(packet);
|
||||
break;
|
||||
case MessageType::OnPlayerDamage:
|
||||
parsePlayerDamage(packet);
|
||||
break;
|
||||
@@ -173,6 +185,18 @@ void Client::parsePing()
|
||||
m_Reliable.Send(packet);
|
||||
}
|
||||
|
||||
|
||||
void Client::parseHeartbeat(Packet& packet)
|
||||
{
|
||||
// Pop size, message type, and ID
|
||||
packet.ReadPrimitive<int>();
|
||||
packet.ReadPrimitive<int>();
|
||||
packet.ReadPrimitive<int>();
|
||||
std::string serverName = packet.ReadString();
|
||||
int playersConnected = packet.ReadPrimitive<int>();
|
||||
LOG_INFO("Serverlist\nName\tPlayers\n%s\t%i\n", serverName.c_str(), playersConnected);
|
||||
}
|
||||
|
||||
void Client::parseKick()
|
||||
{
|
||||
LOG_WARNING("You have been kicked from the server.");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Network/Server.h"
|
||||
|
||||
Server::Server(World* world, EventBroker* eventBroker, int port)
|
||||
Server::Server(World* world, EventBroker* eventBroker, int port)
|
||||
: Network(world, eventBroker)
|
||||
{
|
||||
ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
@@ -13,12 +13,13 @@ Server::Server(World* world, EventBroker* eventBroker, int port)
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EComponentDeleted, &Server::OnComponentDeleted);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &Server::OnPlayerDamage);
|
||||
|
||||
// Bind
|
||||
// BindWW
|
||||
if (port == 0) {
|
||||
port = config->Get<float>("Networking.Port", 27666);
|
||||
}
|
||||
m_Port = port;
|
||||
LOG_INFO("Server initialized and bound to port %i", port);
|
||||
m_Heartbeat.Connect("Server", "127.0.0.1", 13);
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
@@ -58,6 +59,7 @@ void Server::Update()
|
||||
parseMessageType(packet);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if players have disconnected
|
||||
for (int i = 0; i < m_PlayersToDisconnect.size(); i++) {
|
||||
disconnect(m_PlayersToDisconnect.at(i));
|
||||
@@ -75,6 +77,11 @@ void Server::Update()
|
||||
sendPing();
|
||||
previousePingMessage = currentTime;
|
||||
}
|
||||
// Server heartbeat (display server list on clients)
|
||||
if (heartbeatInterval < (1000 * (currentTime - previousHeartbeat) / (double)CLOCKS_PER_SEC)) {
|
||||
sendHeartBeat();
|
||||
previousHeartbeat = currentTime;
|
||||
}
|
||||
// Time out logic
|
||||
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
checkForTimeOuts();
|
||||
@@ -230,6 +237,15 @@ void Server::sendPing()
|
||||
reliableBroadcast(packet);
|
||||
}
|
||||
|
||||
|
||||
void Server::sendHeartBeat()
|
||||
{
|
||||
Packet packet(MessageType::Heartbeat);
|
||||
packet.WriteString("This is a servername"); // server name
|
||||
packet.WritePrimitive<int>(m_ConnectedPlayers.size());
|
||||
m_Heartbeat.Send(packet);
|
||||
}
|
||||
|
||||
void Server::checkForTimeOuts()
|
||||
{
|
||||
double startPing = 1000 * m_StartPingTime
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "Network/TCPServer.h"
|
||||
using namespace boost::asio::ip;
|
||||
|
||||
TCPServer::TCPServer()
|
||||
TCPServer::TCPServer()
|
||||
{
|
||||
acceptor = std::unique_ptr<tcp::acceptor>(new tcp::acceptor(m_IOService, tcp::endpoint(tcp::v4(), 27666)));
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@ UDPServer::UDPServer()
|
||||
m_Socket = std::unique_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(m_IOService, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 27666)));
|
||||
}
|
||||
|
||||
UDPServer::UDPServer(int port)
|
||||
{
|
||||
m_Socket = std::unique_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(m_IOService, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
|
||||
}
|
||||
|
||||
UDPServer::~UDPServer()
|
||||
{ }
|
||||
|
||||
@@ -31,6 +36,8 @@ void UDPServer::Send(Packet & packet)
|
||||
0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void UDPServer::Receive(Packet & packet, PlayerDefinition & playerDefinition)
|
||||
{
|
||||
int bytesRead = readBuffer(m_ReadBuffer);
|
||||
|
||||
Reference in New Issue
Block a user