Added support for multiplayer.
Reactored code, clumped together variables in PlayerDefinition.h. Integrated server with entity system.
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef PlayerDefinition_h__
|
||||||
|
#define PlayerDefinition_h__
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct PlayerDefinition {
|
||||||
|
unsigned int EntityID;
|
||||||
|
std::string Name;
|
||||||
|
boost::asio::ip::udp::endpoint Endpoint;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,27 +8,31 @@
|
|||||||
#include <glm/common.hpp>
|
#include <glm/common.hpp>
|
||||||
#include "NetworkDefines.h"
|
#include "NetworkDefines.h"
|
||||||
#include "MessageType.h"
|
#include "MessageType.h"
|
||||||
|
#include "Core/World.h"
|
||||||
|
#include "Network/PlayerDefinition.h"
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Server();
|
Server();
|
||||||
~Server();
|
~Server();
|
||||||
void Start();
|
void Start(World* m_world);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// udp stuff
|
// udp stuff
|
||||||
boost::asio::ip::udp::endpoint m_ReceiverEndpoint;
|
boost::asio::ip::udp::endpoint m_ReceiverEndpoint;
|
||||||
boost::asio::io_service m_IOService;
|
boost::asio::io_service m_IOService;
|
||||||
boost::asio::ip::udp::socket m_Socket;
|
boost::asio::ip::udp::socket m_Socket;
|
||||||
boost::asio::ip::udp::endpoint m_Connections[MAXCONNECTIONS];
|
// boost::asio::ip::udp::endpoint m_Connections[MAXCONNECTIONS];
|
||||||
|
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
||||||
//Timers
|
//Timers
|
||||||
std::clock_t m_StartPingTime;
|
std::clock_t m_StartPingTime;
|
||||||
std::clock_t m_StopTimes[8];
|
std::clock_t m_StopTimes[8];
|
||||||
// Game logic
|
// Game logic
|
||||||
std::string m_PlayerNames[MAXCONNECTIONS];
|
// std::string m_PlayerNames[MAXCONNECTIONS];
|
||||||
char m_GameBoard[BOARDSIZE][BOARDSIZE];
|
char m_GameBoard[BOARDSIZE][BOARDSIZE];
|
||||||
glm::vec2 m_PlayerPositions[8];
|
glm::vec2 m_PlayerPositions[8];
|
||||||
|
World* m_World;
|
||||||
|
|
||||||
// Threaded
|
// Threaded
|
||||||
void DisplayLoop();
|
void DisplayLoop();
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ Server::~Server()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Server::Start()
|
void Server::Start(World* world)
|
||||||
{
|
{
|
||||||
|
m_World = world;
|
||||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
m_StopTimes[i] = std::clock();
|
m_StopTimes[i] = std::clock();
|
||||||
m_PlayerPositions[i].x = -1;
|
m_PlayerPositions[i].x = -1;
|
||||||
@@ -178,10 +179,10 @@ void Server::Broadcast(std::string message)
|
|||||||
char* data = new char[128];
|
char* data = new char[128];
|
||||||
int offset = CreateMessage(MessageType::Event, message, data);
|
int offset = CreateMessage(MessageType::Event, message, data);
|
||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() != boost::asio::ip::address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||||
m_Socket.send_to(
|
m_Socket.send_to(
|
||||||
boost::asio::buffer(data, offset),
|
boost::asio::buffer(data, offset),
|
||||||
m_Connections[i],
|
m_PlayerDefinitions[i].Endpoint,
|
||||||
0);
|
0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,10 +192,10 @@ void Server::Broadcast(std::string message)
|
|||||||
void Server::Broadcast(char * data, size_t length)
|
void Server::Broadcast(char * data, size_t length)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MAXCONNECTIONS; ++i) {
|
for (int i = 0; i < MAXCONNECTIONS; ++i) {
|
||||||
if (m_Connections[i].address() != boost::asio::ip::address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||||
m_Socket.send_to(
|
m_Socket.send_to(
|
||||||
boost::asio::buffer(data, length),
|
boost::asio::buffer(data, length),
|
||||||
m_Connections[i],
|
m_PlayerDefinitions[i].Endpoint,
|
||||||
0);
|
0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -210,8 +211,8 @@ void Server::SendSnapshot()
|
|||||||
memcpy(data + offset, &m_PlayerPositions[i].y, sizeof(float));
|
memcpy(data + offset, &m_PlayerPositions[i].y, sizeof(float));
|
||||||
offset += sizeof(float);
|
offset += sizeof(float);
|
||||||
// +1 for null terminator
|
// +1 for null terminator
|
||||||
memcpy(data + offset, m_PlayerNames[i].data(), m_PlayerNames[i].size() + 1);
|
memcpy(data + offset, m_PlayerDefinitions[i].Name.data(), m_PlayerDefinitions[i].Name.size() + 1);
|
||||||
offset += (m_PlayerNames[i].size() + 1) * sizeof(char);
|
offset += (m_PlayerDefinitions[i].Name.size() + 1) * sizeof(char);
|
||||||
}
|
}
|
||||||
Broadcast(data, offset);
|
Broadcast(data, offset);
|
||||||
delete[] data;
|
delete[] data;
|
||||||
@@ -221,7 +222,7 @@ void Server::SendPing()
|
|||||||
{
|
{
|
||||||
// Prints connected players ping
|
// Prints connected players ping
|
||||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() != boost::asio::ip::address())
|
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address())
|
||||||
std::cout << "Player " << i << "'s ping: " << 1000 * (m_StopTimes[i] - m_StartPingTime)
|
std::cout << "Player " << i << "'s ping: " << 1000 * (m_StopTimes[i] - m_StartPingTime)
|
||||||
/ static_cast<double>(CLOCKS_PER_SEC) << std::endl;
|
/ static_cast<double>(CLOCKS_PER_SEC) << std::endl;
|
||||||
}
|
}
|
||||||
@@ -245,14 +246,13 @@ void Server::CheckForTimeOuts()
|
|||||||
/ static_cast<double>(CLOCKS_PER_SEC);
|
/ static_cast<double>(CLOCKS_PER_SEC);
|
||||||
|
|
||||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() != boost::asio::ip::address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||||
int tempStopPing = 1000 * m_StopTimes[i]
|
int tempStopPing = 1000 * m_StopTimes[i]
|
||||||
/ static_cast<double>(CLOCKS_PER_SEC);
|
/ static_cast<double>(CLOCKS_PER_SEC);
|
||||||
if (tempStartPing > tempStopPing + timeOutTimeMs) {
|
if (tempStartPing > tempStopPing + timeOutTimeMs) {
|
||||||
std::cout << "player " << i << " timed out!" << std::endl;
|
std::cout << "player " << i << " timed out!" << std::endl;
|
||||||
Disconnect(i);
|
Disconnect(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -271,7 +271,9 @@ void Server::Disconnect(int i)
|
|||||||
{
|
{
|
||||||
Broadcast("A player disconnected");
|
Broadcast("A player disconnected");
|
||||||
std::cout << "Player " << i << " disconnected/Timed out" << std::endl;
|
std::cout << "Player " << i << " disconnected/Timed out" << std::endl;
|
||||||
m_Connections[i] = boost::asio::ip::udp::endpoint();
|
m_PlayerDefinitions[i].Endpoint = boost::asio::ip::udp::endpoint();
|
||||||
|
m_PlayerDefinitions[i].EntityID = -1;
|
||||||
|
m_PlayerDefinitions[i].Name = "Name not Set";
|
||||||
// Reset disconnected players position
|
// Reset disconnected players position
|
||||||
m_PlayerPositions[i].x = -1;
|
m_PlayerPositions[i].x = -1;
|
||||||
m_PlayerPositions[i].y = -1;
|
m_PlayerPositions[i].y = -1;
|
||||||
@@ -281,23 +283,37 @@ void Server::ParseEvent(char * data, size_t length)
|
|||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < MAXCONNECTIONS; i++) {
|
for (i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() == m_ReceiverEndpoint.address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// If no player matches the ip return.
|
||||||
|
if (i >= 8)
|
||||||
|
return;
|
||||||
|
|
||||||
if ("+Forward" == std::string(data))
|
unsigned int entityId = m_PlayerDefinitions[i].EntityID;
|
||||||
if (m_PlayerPositions[i].y > 0)
|
if ("+Forward" == std::string(data)) {
|
||||||
m_PlayerPositions[i].y--;
|
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||||
if ("-Forward" == std::string(data))
|
temp.z -= 0.5f;
|
||||||
if (m_PlayerPositions[i].y < BOARDSIZE - 1)
|
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||||
m_PlayerPositions[i].y++;
|
}
|
||||||
if ("+Right" == std::string(data))
|
if ("-Forward" == std::string(data)) {
|
||||||
if (m_PlayerPositions[i].x < BOARDSIZE - 1)
|
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||||
m_PlayerPositions[i].x++;
|
temp.z += 0.5f;
|
||||||
if ("-Right" == std::string(data))
|
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||||
if (m_PlayerPositions[i].x > 0)
|
}
|
||||||
m_PlayerPositions[i].x--;
|
|
||||||
|
if ("+Right" == std::string(data)) {
|
||||||
|
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||||
|
temp.x += 0.5f;
|
||||||
|
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ("-Right" == std::string(data)) {
|
||||||
|
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||||
|
temp.x -= 0.5f;
|
||||||
|
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::ParseConnect(char * data, size_t length)
|
void Server::ParseConnect(char * data, size_t length)
|
||||||
@@ -305,37 +321,47 @@ void Server::ParseConnect(char * data, size_t length)
|
|||||||
std::cout << "Parsing connection." << std::endl;
|
std::cout << "Parsing connection." << std::endl;
|
||||||
|
|
||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() == m_ReceiverEndpoint.address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() == boost::asio::ip::address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() == boost::asio::ip::address()) {
|
||||||
m_Connections[i] = m_ReceiverEndpoint;
|
|
||||||
|
|
||||||
m_PlayerNames[i] = std::string(data);
|
|
||||||
m_PlayerPositions[i].x = 0;
|
m_PlayerDefinitions[i].EntityID = m_World->CreateEntity();
|
||||||
m_PlayerPositions[i].y = 0;
|
ComponentWrapper transform = m_World->AttachComponent(m_PlayerDefinitions[i].EntityID, "Transform");
|
||||||
|
transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
||||||
|
ComponentWrapper model = m_World->AttachComponent(m_PlayerDefinitions[i].EntityID, "Model");
|
||||||
|
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||||
|
|
||||||
|
m_PlayerDefinitions[i].Endpoint = m_ReceiverEndpoint;
|
||||||
|
m_PlayerDefinitions[i].Name = std::string(data);
|
||||||
m_StopTimes[i] = std::clock();
|
m_StopTimes[i] = std::clock();
|
||||||
std::cout << "Player \"" << m_PlayerNames[i] << "\" connected on IP: " << m_Connections[i].address().to_string() << std::endl;
|
|
||||||
|
std::cout << "Player \"" << m_PlayerDefinitions[i].Name << "\" connected on IP: " <<
|
||||||
|
m_PlayerDefinitions[i].Endpoint.address().to_string() << std::endl;
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
char* temp = new char[sizeof(int) * 2];
|
char* temp = new char[sizeof(int) * 2];
|
||||||
int msgType = 0;
|
int messagType = 0;
|
||||||
memcpy(temp, &msgType, sizeof(int));
|
|
||||||
|
memcpy(temp, &messagType, sizeof(int));
|
||||||
offset += sizeof(int);
|
offset += sizeof(int);
|
||||||
memcpy(temp + offset, &i, sizeof(int));
|
memcpy(temp + offset, &i, sizeof(int));
|
||||||
|
|
||||||
m_Socket.send_to(
|
m_Socket.send_to(
|
||||||
boost::asio::buffer(temp, sizeof(int) * 2),
|
boost::asio::buffer(temp, sizeof(int) * 2),
|
||||||
m_Connections[i],
|
m_PlayerDefinitions[i].Endpoint,
|
||||||
0);
|
0);
|
||||||
|
|
||||||
// Send notification that a player has connected
|
// Send notification that a player has connected
|
||||||
std::string str = "Player " + m_PlayerNames[i] + " connected on: " + m_ReceiverEndpoint.address().to_string();
|
std::string str = "Player " + m_PlayerDefinitions[i].Name + " connected on: "
|
||||||
|
+ m_PlayerDefinitions[i].Endpoint.address().to_string();
|
||||||
Broadcast(str);
|
Broadcast(str);
|
||||||
// +1 is the null terminator
|
// +1 is the null terminator
|
||||||
MoveMessageHead(data, length, m_PlayerNames[i].size() + 1);
|
MoveMessageHead(data, length, m_PlayerDefinitions[i].Name.size() + 1);
|
||||||
delete[] temp;
|
delete[] temp;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -347,7 +373,7 @@ void Server::ParseDisconnect()
|
|||||||
std::cout << "Parsing disconnect. \n";
|
std::cout << "Parsing disconnect. \n";
|
||||||
|
|
||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() == m_ReceiverEndpoint.address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||||
Disconnect(i);
|
Disconnect(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -373,7 +399,7 @@ void Server::ParseClientPing()
|
|||||||
void Server::ParseServerPing()
|
void Server::ParseServerPing()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() == m_ReceiverEndpoint.address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||||
m_StopTimes[i] = std::clock();
|
m_StopTimes[i] = std::clock();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -385,10 +411,10 @@ void Server::ParseSnapshot(char * data, size_t length)
|
|||||||
// Does no logic. Returns snapshot if client request one
|
// Does no logic. Returns snapshot if client request one
|
||||||
// The snapshot is not a real snapshot tho...
|
// The snapshot is not a real snapshot tho...
|
||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_Connections[i].address() != boost::asio::ip::address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||||
m_Socket.send_to(
|
m_Socket.send_to(
|
||||||
boost::asio::buffer("I'm sending a snapshot to you guys!"),
|
boost::asio::buffer("I'm sending a snapshot to you guys!"),
|
||||||
m_Connections[i],
|
m_PlayerDefinitions[i].Endpoint,
|
||||||
0);
|
0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -81,6 +81,6 @@ void Game::NetworkFunction()
|
|||||||
m_Client.Start();
|
m_Client.Start();
|
||||||
}
|
}
|
||||||
if (inputMessage == "s" || inputMessage == "S") {
|
if (inputMessage == "s" || inputMessage == "S") {
|
||||||
m_Server.Start();
|
m_Server.Start(m_World);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user