Commit c26e446c authored by Nicholas Milef's avatar Nicholas Milef Committed by Nicholas Milef

ENH: added broadcast ability to allow for multiple clients

parent 0792fbed
Pipeline #144707 passed with stage
......@@ -93,7 +93,7 @@ NetworkClient::receiveNextPacket()
m_socket->async_receive_from(asio::buffer(
&receivePacket.get()[0],
sizeof(Network::Packet)),
m_endpoint,
m_receiveEndpoint,
receiveCallback);
}
......@@ -107,13 +107,15 @@ NetworkClient::initModule()
new asio::ip::udp::resolver(m_service));
m_query = std::unique_ptr<asio::ip::udp::resolver::query>(
new asio::ip::udp::resolver::query(asio::ip::udp::v4(), m_ipAddress, "daytime"));
m_endpoint = *m_resolver->resolve(*m_query.get());
m_receiveEndpoint = *m_resolver->resolve(*m_query.get());
m_sendEndpoint = asio::ip::udp::endpoint(asio::ip::udp::v4(), 13);
m_work = std::unique_ptr<asio::io_service::work>(
new asio::io_service::work(m_service));
// Establish connection
m_socket->open(asio::ip::udp::v4());
m_socket->connect(m_endpoint);
m_socket->bind(m_receiveEndpoint);
m_socket->connect(m_sendEndpoint);
// Receive first message
auto receivePacket = std::make_shared<std::array<char, sizeof(Network::Packet)>>();
......@@ -126,7 +128,7 @@ NetworkClient::initModule()
m_socket->async_receive_from(
asio::buffer((void*)&receivePacket.get()[0],
sizeof(Network::Packet)),
m_endpoint,
m_receiveEndpoint,
receiveCallback);
}
......@@ -196,7 +198,7 @@ NetworkClient::sendPacket(
std::placeholders::_2,
this,
sendPacket);
m_socket->async_send_to(asio::buffer((void*)&sendPacket.get()[0], sizeof(Network::Packet)), m_endpoint, sendCallback);
m_socket->async_send_to(asio::buffer((void*)&sendPacket.get()[0], sizeof(Network::Packet)), m_sendEndpoint, sendCallback);
m_framePackets++;
}
......@@ -208,11 +210,14 @@ NetworkClient::readPingPacket(const Network::Packet& packet)
unsigned long long dt = this->getNodeTime();
m_latency = dt - packet.m_time;
m_serverTime = packet.m_time;
m_pingSent = false;
auto info = (Network::PingInfo*)packet.m_data;
auto createConnection = info->m_createConnection;
if (m_nodeID == -1)
if (m_nodeID == -1 && createConnection)
{
m_nodeID = packet.m_clientID;
LOG(INFO) << "Client ID: " << m_nodeID;
m_pingSent = false;
}
}
}
......
......@@ -108,6 +108,7 @@ NetworkNode::updateLocalEntity(const std::unique_ptr<Network::NetworkEntityInfo>
auto bufferedInfo = m_receiveBufferedEntityInfo[entity->getID()];
sceneObject->getVisualModel(0)->getGeometry()->setTranslation(bufferedInfo->m_translation);
sceneObject->getVisualModel(0)->getGeometry()->setRotation(bufferedInfo->m_orientation);
}
void
......@@ -116,13 +117,14 @@ NetworkNode::updateNetworkEntity(const std::unique_ptr<Network::NetworkEntityInf
auto entity = networkEntityInfo->m_entity;
auto sceneObject = std::dynamic_pointer_cast<SceneObject>(entity);
if (!sceneObject || networkEntityInfo->m_owner != m_nodeID)
if (!sceneObject || (networkEntityInfo->m_owner != m_nodeID && m_nodeID != 0))
{
return;
}
auto bufferedInfo = m_sendBufferedEntityInfo[entity->getID()];
bufferedInfo->m_translation = sceneObject->getVisualModel(0)->getGeometry()->getTranslation();
bufferedInfo->m_orientation = sceneObject->getVisualModel(0)->getGeometry()->getRotation();
}
const NetworkPacketHistory&
......@@ -154,7 +156,8 @@ NetworkNode::readRigidObjectPacket(const Network::Packet& packet)
// If owner is the current node, then ignore (likely outdated values)
if (rigidObjectInfo.m_owner == m_nodeID ||
rigidObjectInfo.m_ownershipTime < m_networkEntities[packet.m_entityID]->m_ownershipTime)
rigidObjectInfo.m_ownershipTime < m_networkEntities[packet.m_entityID]->m_ownershipTime ||
((m_nodeID == 0) && (packet.m_clientID == 0)))
{
return;
}
......@@ -183,6 +186,7 @@ NetworkNode::sendRigidObjectPacket(
dataPacket.m_entityID = entityID;
dataPacket.m_frameNumber = frameNumber;
dataPacket.m_packetNumber = 0;
dataPacket.m_clientID = m_nodeID;
dataPacket.m_time = this->getNodeTime();
Network::RigidObjectInfo rigidObjectInfo;
......
......@@ -149,7 +149,8 @@ protected:
std::unordered_map<EntityID, std::shared_ptr<Network::RigidSceneObjectInfo>> m_sendBufferedEntityInfo;
asio::io_service m_service;
asio::ip::udp::endpoint m_endpoint;
asio::ip::udp::endpoint m_receiveEndpoint;
asio::ip::udp::endpoint m_sendEndpoint;
std::unique_ptr<asio::ip::udp::socket> m_socket;
std::unique_ptr<asio::io_service::work> m_work;
std::unique_ptr<asio::ip::udp::resolver> m_resolver;
......
......@@ -73,7 +73,7 @@ NetworkServer::receiveNextPacket()
m_socket->async_receive_from(asio::buffer(
&receivePacket.get()[0],
sizeof(Network::Packet)),
m_endpoint,
m_receiveEndpoint,
receiveCallback);
}
......@@ -81,9 +81,13 @@ void
NetworkServer::initModule()
{
m_lastID = 0;
m_socket = std::unique_ptr<asio::ip::udp::socket>(new asio::ip::udp::socket(
m_service,
asio::ip::udp::endpoint(asio::ip::udp::v4(), 13)));
m_socket = std::unique_ptr<asio::ip::udp::socket>(new asio::ip::udp::socket(m_service));
m_socket->open(asio::ip::udp::v4());
m_receiveEndpoint = asio::ip::udp::endpoint(asio::ip::udp::v4(), 13);
m_sendEndpoint = asio::ip::udp::endpoint(asio::ip::address_v4::broadcast(), 13);
m_socket->set_option(asio::socket_base::reuse_address(true));
m_socket->set_option(asio::socket_base::broadcast(true));
m_socket->bind(m_receiveEndpoint);
auto receivePacket = std::make_shared<std::array<char, sizeof(Network::Packet)>>();
auto receiveCallback = std::bind(
......@@ -95,7 +99,7 @@ NetworkServer::initModule()
m_socket->async_receive_from(
asio::buffer((void*)&receivePacket.get()[0],
sizeof(Network::Packet)),
m_endpoint,
m_receiveEndpoint,
receiveCallback);
}
......@@ -119,7 +123,11 @@ NetworkServer::runModule()
packet.m_authenticate = Network::AuthenticationByte::Ping;
packet.m_clientID = connection.second->m_clientID;
packet.m_time = m_serverTime;
memcpy(packet.m_data, (void*)&m_currentTime, sizeof(unsigned long long));
Network::PingInfo pingInfo;
pingInfo.m_createConnection = connection.second->m_createConnection;
memcpy(packet.m_data, (void*)&pingInfo, sizeof(Network::PingInfo));
memcpy(&((sendPacket.get())[0]), &packet, sizeof(Network::Packet));
this->sendPacket(packet, sendPacket);
......@@ -176,7 +184,7 @@ NetworkServer::sendPacket(
std::placeholders::_2,
this,
sendPacket);
m_socket->async_send_to(asio::buffer((void*)&sendPacket.get()[0], sizeof(Network::Packet)), m_endpoint, sendCallback);
m_socket->async_send_to(asio::buffer((void*)&sendPacket.get()[0], sizeof(Network::Packet)), m_sendEndpoint, sendCallback);
m_framePackets++;
}
......@@ -193,12 +201,13 @@ NetworkServer::addClientConnection()
m_clientConnections[clientID] = std::unique_ptr<ConnectionInstance>(new ConnectionInstance());
m_clientConnections[clientID]->m_clientID = clientID;
m_clientConnections[clientID]->m_pingReceived = true;
m_clientConnections[clientID]->m_createConnection = true;
LOG(INFO) << "Adding client " << clientID;
return m_clientConnections[clientID]->m_clientID;
}
const ConnectionInstance&
ConnectionInstance&
NetworkServer::getClientConnection(const Network::NodeID clientID)
{
return *m_clientConnections[clientID].get();
......@@ -212,7 +221,17 @@ NetworkServer::readPingPacket(const Network::Packet& packet)
auto clientID = packet.m_clientID;
if (clientID == -1)
{
clientID = this->addClientConnection();
if (m_clientConnections.find(m_lastID) != m_clientConnections.end() &&
!m_clientConnections[m_lastID]->m_createConnection &&
(m_lastID == 0))
{
clientID = this->addClientConnection();
}
clientID = m_lastID;
}
else
{
m_clientConnections[clientID]->m_createConnection = false;
}
this->sendPacket(packet, sendPacket);
......
......@@ -44,6 +44,7 @@ struct ConnectionInstance
{
Network::NodeID m_clientID;
bool m_pingReceived = false;
bool m_createConnection = false;
};
///
......@@ -118,7 +119,7 @@ public:
/// \param clientID ID of client
/// \returns ConnectionIndex with clientID
///
const ConnectionInstance& getClientConnection(const Network::NodeID clientID);
ConnectionInstance& getClientConnection(const Network::NodeID clientID);
///
/// \brief Read ping packet
......
......@@ -59,6 +59,11 @@ public:
bool isOutdated(unsigned long frameNumber) const;
};
struct PingInfo
{
bool m_createConnection = false;
};
struct RigidObjectInfo
{
NodeID m_owner;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment