WIP Clients are working properly, servers are not.
This commit is contained in:
@@ -17,8 +17,7 @@ Client::Client(ConfigFile* config)
|
||||
}
|
||||
|
||||
Client::~Client()
|
||||
{
|
||||
}
|
||||
{ }
|
||||
|
||||
void Client::Start(World* world, EventBroker* eventBroker)
|
||||
{
|
||||
@@ -35,17 +34,23 @@ void Client::Start(World* world, EventBroker* eventBroker)
|
||||
void Client::Update()
|
||||
{
|
||||
m_EventBroker->Process<Client>();
|
||||
readFromServer();
|
||||
while (m_UDPClient.IsSocketAvailable()) {
|
||||
// Packet will get real data in receive
|
||||
Packet packet(MessageType::Invalid);
|
||||
m_UDPClient.Receive(packet);
|
||||
parseMessageType(packet);
|
||||
}
|
||||
|
||||
if (m_IsConnected) {
|
||||
hasServerTimedOut();
|
||||
// Don't sent 1 input in 1 packet, bunch em up.
|
||||
// Don't send 1 input in 1 packet, bunch em up.
|
||||
if (m_SendInputIntervalMs < (1000 * (std::clock() - m_TimeSinceSentInputs) / (double)CLOCKS_PER_SEC)) {
|
||||
sendInputCommands();
|
||||
m_TimeSinceSentInputs = std::clock();
|
||||
}
|
||||
sendLocalPlayerTransform();
|
||||
}
|
||||
Network::Update();
|
||||
//Network::Update();
|
||||
}
|
||||
|
||||
void Client::parseMessageType(Packet& packet)
|
||||
@@ -59,7 +64,7 @@ void Client::parseMessageType(Packet& packet)
|
||||
// Read packet ID
|
||||
m_PreviousPacketID = m_PacketID; // Set previous packet id
|
||||
m_PacketID = packet.ReadPrimitive<int>(); //Read new packet id
|
||||
identifyPacketLoss();
|
||||
//identifyPacketLoss();
|
||||
|
||||
switch (static_cast<MessageType>(messageType)) {
|
||||
case MessageType::Connect:
|
||||
@@ -118,7 +123,7 @@ void Client::parsePing()
|
||||
|
||||
Packet packet(MessageType::Ping, m_SendPacketID);
|
||||
packet.WriteString("Ping recieved");
|
||||
send(packet);
|
||||
m_UDPClient.Send(packet);
|
||||
}
|
||||
|
||||
void Client::parseKick()
|
||||
@@ -250,14 +255,14 @@ void Client::disconnect()
|
||||
m_PreviousPacketID = 0;
|
||||
m_PacketID = 0;
|
||||
Packet packet(MessageType::Disconnect, m_SendPacketID);
|
||||
send(packet);
|
||||
m_UDPClient.Send(packet);
|
||||
}
|
||||
|
||||
bool Client::OnInputCommand(const Events::InputCommand & e)
|
||||
{
|
||||
if (e.Command == "ConnectToServer") { // Connect for now
|
||||
if (e.Value > 0) {
|
||||
connect();
|
||||
m_UDPClient.Connect(m_PlayerName, address, port);
|
||||
}
|
||||
//LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
return true;
|
||||
@@ -280,7 +285,7 @@ bool Client::OnInputCommand(const Events::InputCommand & e)
|
||||
m_SaveDataTimer = std::clock();
|
||||
}
|
||||
} else {
|
||||
if (m_IsConnected) {
|
||||
if (m_IsConnected) {
|
||||
m_InputCommandBuffer.push_back(e);
|
||||
}
|
||||
//LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
@@ -294,7 +299,7 @@ bool Client::OnPlayerDamage(const Events::PlayerDamage & e)
|
||||
Packet packet(MessageType::OnPlayerDamage, m_SendPacketID);
|
||||
packet.WritePrimitive(e.Damage);
|
||||
packet.WritePrimitive(m_ClientIDToServerID.at(e.Player.ID));
|
||||
send(packet);
|
||||
m_UDPClient.Send(packet);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -322,7 +327,7 @@ void Client::sendLocalPlayerTransform()
|
||||
packet.WritePrimitive(orientation.x);
|
||||
packet.WritePrimitive(orientation.y);
|
||||
packet.WritePrimitive(orientation.z);
|
||||
send(packet);
|
||||
m_UDPClient.Send(packet);
|
||||
}
|
||||
|
||||
void Client::identifyPacketLoss()
|
||||
@@ -365,7 +370,7 @@ void Client::sendInputCommands()
|
||||
packet.WriteString(m_InputCommandBuffer[i].Command);
|
||||
packet.WritePrimitive(m_InputCommandBuffer[i].Value);
|
||||
}
|
||||
send(packet);
|
||||
m_UDPClient.Send(packet);
|
||||
m_InputCommandBuffer.clear();
|
||||
}
|
||||
}
|
||||
@@ -373,7 +378,7 @@ void Client::sendInputCommands()
|
||||
void Client::becomePlayer()
|
||||
{
|
||||
Packet packet = Packet(MessageType::BecomePlayer, m_SendPacketID);
|
||||
send(packet);
|
||||
m_UDPClient.Send(packet);
|
||||
}
|
||||
|
||||
bool Client::clientServerMapsHasEntity(EntityID clientEntityID)
|
||||
|
||||
@@ -5,6 +5,21 @@ void Network::Update()
|
||||
updateNetworkData();
|
||||
}
|
||||
|
||||
void Network::logSentData(int bytesSent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Network::logReceivedData(int bytesReceived)
|
||||
{
|
||||
// Network Debug data
|
||||
if (isReadingData) {
|
||||
m_NetworkData.TotalDataReceived += bytesReceived;
|
||||
m_NetworkData.DataReceivedThisInterval += bytesReceived;
|
||||
m_NetworkData.AmountOfMessagesReceived++;
|
||||
}
|
||||
}
|
||||
|
||||
void Network::saveToFile()
|
||||
{
|
||||
std::ofstream outfile;
|
||||
|
||||
@@ -37,6 +37,7 @@ void Packet::Init(MessageType type, unsigned int & packetID)
|
||||
// allocate memory for size of packet(only used in tcp)
|
||||
Packet::WritePrimitive<int>(0);
|
||||
// Add message type
|
||||
m_MessageType = type;
|
||||
int messageType = static_cast<int>(type);
|
||||
Packet::WritePrimitive<int>(messageType);
|
||||
Packet::WritePrimitive<int>(packetID);
|
||||
@@ -58,9 +59,12 @@ void Packet::WriteString(const std::string& str)
|
||||
|
||||
void Packet::WriteData(char * data, int sizeOfData)
|
||||
{
|
||||
|
||||
if (m_Offset + sizeOfData > m_MaxPacketSize) {
|
||||
//LOG_WARNING("Packet::WriteData(): Data size in packet exceeded maximum packet size. New size is %i bytes\n", m_MaxPacketSize*2);
|
||||
resizeData();
|
||||
while (m_Offset + sizeOfData > m_MaxPacketSize) {
|
||||
resizeData();
|
||||
}
|
||||
}
|
||||
memcpy(m_Data + m_Offset, data, sizeOfData);
|
||||
m_Offset += sizeOfData;
|
||||
@@ -78,19 +82,34 @@ std::string Packet::ReadString()
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
void Packet::ReconstructFromData(char * data, int sizeOfData)
|
||||
{
|
||||
if (sizeOfData > m_MaxPacketSize) {
|
||||
// Delete our data
|
||||
delete[] m_Data;
|
||||
// Set new max size
|
||||
m_MaxPacketSize = sizeOfData;
|
||||
m_Data = new char[m_MaxPacketSize];
|
||||
// while we resized the old data container.
|
||||
}
|
||||
memcpy(m_Data, data, sizeOfData);
|
||||
m_Offset = sizeOfData;
|
||||
|
||||
}
|
||||
|
||||
void Packet::UpdateSize()
|
||||
{
|
||||
{
|
||||
memcpy(m_Data, &m_Offset, sizeof(int));
|
||||
}
|
||||
|
||||
char * Packet::ReadData(int SizeOfData)
|
||||
char * Packet::ReadData(int sizeOfData)
|
||||
{
|
||||
if (m_Offset < m_ReturnDataOffset + SizeOfData) {
|
||||
if (m_Offset < m_ReturnDataOffset + sizeOfData) {
|
||||
//LOG_WARNING("packet ReadData(): Oh no! You are trying to remove things outside my memory kingdom");
|
||||
return nullptr;
|
||||
}
|
||||
unsigned int oldReturnDataOffset = m_ReturnDataOffset;
|
||||
m_ReturnDataOffset += SizeOfData;
|
||||
m_ReturnDataOffset += sizeOfData;
|
||||
return (m_Data + oldReturnDataOffset);
|
||||
}
|
||||
|
||||
@@ -103,20 +122,24 @@ void Packet::ChangePacketID(unsigned int & packetID)
|
||||
|
||||
void Packet::resizeData()
|
||||
{
|
||||
resizeData(m_MaxPacketSize * 2);
|
||||
}
|
||||
|
||||
void Packet::resizeData(int size)
|
||||
{
|
||||
// Allocate memory to store our data in
|
||||
char* holdData = new char[m_MaxPacketSize];
|
||||
// Copy our data to the newly allocated memory
|
||||
memcpy(holdData, m_Data, m_Offset);
|
||||
// Increase max packet size
|
||||
m_MaxPacketSize = m_MaxPacketSize * 2;
|
||||
m_MaxPacketSize = size;
|
||||
// Delete our data
|
||||
delete m_Data;
|
||||
// Allocate twice the memory we had before
|
||||
delete[] m_Data;
|
||||
// Allocate memory
|
||||
m_Data = new char[m_MaxPacketSize];
|
||||
// Copy our data to new location
|
||||
memcpy(m_Data, holdData, m_Offset);
|
||||
// Delete the memory allocated to hold our data
|
||||
// while we resized the old data container.
|
||||
delete holdData;
|
||||
delete[] holdData;
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ Server::Server()
|
||||
pingIntervalMs = config->Get<float>("Networking.PingIntervalMs", 1000);
|
||||
}
|
||||
Server::~Server()
|
||||
{
|
||||
}
|
||||
{ }
|
||||
void Server::Start(World* world, EventBroker* eventBroker)
|
||||
{
|
||||
m_World = world;
|
||||
@@ -25,11 +24,27 @@ void Server::Start(World* world, EventBroker* eventBroker)
|
||||
void Server::Update()
|
||||
{
|
||||
readFromClients();
|
||||
|
||||
std::clock_t currentTime = std::clock();
|
||||
// Send snapshot
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendSnapshot();
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
// Send pings each
|
||||
if (pingIntervalMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendPing();
|
||||
previousePingMessage = currentTime;
|
||||
}
|
||||
// Time out logic
|
||||
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
checkForTimeOuts();
|
||||
timOutTimer = currentTime;
|
||||
}
|
||||
m_EventBroker->Process<Server>();
|
||||
if (isReadingData) {
|
||||
Network::Update();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Server::parseMessageType(Packet& packet)
|
||||
|
||||
@@ -2,46 +2,55 @@
|
||||
|
||||
using namespace boost::asio::ip;
|
||||
|
||||
TCPClient::TCPClient(ConfigFile * config) : Client(config)
|
||||
TCPClient::TCPClient()
|
||||
{
|
||||
m_Endpoint = tcp::endpoint(boost::asio::ip::address::from_string(address), port);
|
||||
m_Socket = std::unique_ptr<tcp::socket>(new tcp::socket(m_IOService, m_Endpoint));
|
||||
tcp::no_delay option(true);
|
||||
m_Socket->set_option(option);
|
||||
}
|
||||
|
||||
TCPClient::~TCPClient()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TCPClient::connect()
|
||||
void TCPClient::Connect(std::string playerName, std::string address, int port)
|
||||
{
|
||||
if (m_Socket) {
|
||||
return;
|
||||
}
|
||||
if (!m_IsConnected) {
|
||||
boost::system::error_code error = boost::asio::error::host_not_found;
|
||||
m_Endpoint = tcp::endpoint(boost::asio::ip::address::from_string(address), port);
|
||||
m_Socket = std::unique_ptr<tcp::socket>(new tcp::socket(m_IOService, m_Endpoint));
|
||||
tcp::no_delay option(true);
|
||||
m_Socket->set_option(option);
|
||||
m_Socket->close();
|
||||
m_Socket->connect(m_Endpoint, error);
|
||||
LOG_INFO(error.message().c_str());
|
||||
if (!error) {
|
||||
m_IsConnected = true;
|
||||
Packet packet(MessageType::Connect, m_SendPacketID);
|
||||
packet.WriteString(m_PlayerName);
|
||||
m_StartPingTime = std::clock();
|
||||
send(packet);
|
||||
packet.WriteString(playerName);
|
||||
Send(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TCPClient::readFromServer()
|
||||
void TCPClient::Disconnect()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TCPClient::Receive(Packet& packet)
|
||||
{
|
||||
while (m_Socket->available()) {
|
||||
bytesRead = receive(readBuffer);
|
||||
Packet packet(readBuffer, bytesRead);
|
||||
parseMessageType(packet);
|
||||
int bytesRead = readBuffer(m_ReadBuffer);
|
||||
if (bytesRead > 0) {
|
||||
packet.ReconstructFromData(m_ReadBuffer, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
int TCPClient::receive(char * data)
|
||||
{
|
||||
int TCPClient::readBuffer(char* data)
|
||||
{
|
||||
if (!m_Socket) {
|
||||
return 0;
|
||||
}
|
||||
boost::system::error_code error;
|
||||
// Read size of packet
|
||||
int bytesReceived = m_Socket->read_some(boost
|
||||
@@ -54,29 +63,26 @@ int TCPClient::receive(char * data)
|
||||
bytesReceived += m_Socket->read_some(boost
|
||||
::asio::buffer((void*)(data + bytesReceived), sizeOfPacket - bytesReceived),
|
||||
error);
|
||||
// Network Debug data
|
||||
if (isReadingData) {
|
||||
m_NetworkData.TotalDataReceived += bytesReceived;
|
||||
m_NetworkData.DataReceivedThisInterval += bytesReceived;
|
||||
m_NetworkData.AmountOfMessagesReceived++;
|
||||
}
|
||||
if (error) {
|
||||
//LOG_ERROR("receive: %s", error.message().c_str());
|
||||
}
|
||||
return bytesReceived;
|
||||
}
|
||||
|
||||
void TCPClient::send(Packet & packet)
|
||||
void TCPClient::Send(Packet & packet)
|
||||
{
|
||||
packet.UpdateSize();
|
||||
boost::system::error_code error;
|
||||
m_Socket->send(boost::asio::buffer(
|
||||
packet.Data(),
|
||||
packet.Size()), 0, error);
|
||||
// Network Debug data
|
||||
if (isReadingData) {
|
||||
m_NetworkData.TotalDataSent += packet.Size();
|
||||
m_NetworkData.DataSentThisInterval += packet.Size();
|
||||
m_NetworkData.AmountOfMessagesSent++;
|
||||
}
|
||||
//Network::logSentData(packet.Size());
|
||||
}
|
||||
|
||||
bool TCPClient::IsSocketAvailable()
|
||||
{
|
||||
if (!m_Socket) {
|
||||
return false;
|
||||
}
|
||||
return m_Socket->available();
|
||||
}
|
||||
@@ -7,8 +7,7 @@ TCPServer::TCPServer()
|
||||
}
|
||||
|
||||
TCPServer::~TCPServer()
|
||||
{
|
||||
}
|
||||
{ }
|
||||
|
||||
void TCPServer::readFromClients()
|
||||
{
|
||||
@@ -30,25 +29,6 @@ void TCPServer::readFromClients()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::clock_t currentTime = std::clock();
|
||||
// Send snapshot
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendSnapshot();
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
|
||||
// Send pings each
|
||||
if (pingIntervalMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendPing();
|
||||
previousePingMessage = currentTime;
|
||||
}
|
||||
|
||||
// Time out logic
|
||||
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
checkForTimeOuts();
|
||||
timOutTimer = currentTime;
|
||||
}
|
||||
}
|
||||
|
||||
void TCPServer::acceptNewConnections()
|
||||
@@ -80,7 +60,7 @@ void TCPServer::parseConnect(Packet & packet)
|
||||
LOG_INFO("Parsing connections");
|
||||
// Check if player is already connected
|
||||
PlayerID playerID = GetPlayerIDFromEndpoint();
|
||||
if(playerID = -1){
|
||||
if (playerID = -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,65 +2,69 @@
|
||||
|
||||
using namespace boost::asio::ip;
|
||||
|
||||
UDPClient::UDPClient(ConfigFile * config) : Client(config), m_Socket(m_IOService)
|
||||
{
|
||||
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string(address), port);
|
||||
m_Socket.connect(m_ReceiverEndpoint);
|
||||
UDPClient::UDPClient()
|
||||
{
|
||||
}
|
||||
|
||||
UDPClient::~UDPClient()
|
||||
{
|
||||
{
|
||||
}
|
||||
|
||||
void UDPClient::readFromServer()
|
||||
void UDPClient::Connect(std::string playerName, std::string address, int port)
|
||||
{
|
||||
while (m_Socket.available()) {
|
||||
bytesRead = receive(readBuffer);
|
||||
if (bytesRead > 0) {
|
||||
Packet packet(readBuffer, bytesRead);
|
||||
parseMessageType(packet);
|
||||
}
|
||||
if (m_Socket) {
|
||||
return;
|
||||
}
|
||||
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string(address), port);
|
||||
m_Socket = boost::shared_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(m_IOService));
|
||||
m_Socket->connect(m_ReceiverEndpoint);
|
||||
|
||||
Packet packet(MessageType::Connect, m_SendPacketID);
|
||||
packet.WriteString(playerName);
|
||||
Send(packet);
|
||||
}
|
||||
|
||||
void UDPClient::Disconnect()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void UDPClient::Receive(Packet& packet)
|
||||
{
|
||||
int bytesRead = readBuffer(m_ReadBuffer);
|
||||
if (bytesRead > 0) {
|
||||
packet.ReconstructFromData(m_ReadBuffer, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
int UDPClient::receive(char* data)
|
||||
int UDPClient::readBuffer(char* data)
|
||||
{
|
||||
if (!m_Socket) {
|
||||
return 0;
|
||||
}
|
||||
boost::system::error_code error;
|
||||
|
||||
int bytesReceived = m_Socket.receive_from(boost
|
||||
int bytesReceived = m_Socket->receive_from(boost
|
||||
::asio::buffer((void*)data, BUFFERSIZE),
|
||||
m_ReceiverEndpoint,
|
||||
0, error);
|
||||
// Network Debug data
|
||||
if (isReadingData) {
|
||||
m_NetworkData.TotalDataReceived += bytesReceived;
|
||||
m_NetworkData.DataReceivedThisInterval += bytesReceived;
|
||||
m_NetworkData.AmountOfMessagesReceived++;
|
||||
}
|
||||
if (error) {
|
||||
//LOG_ERROR("receive: %s", error.message().c_str());
|
||||
}
|
||||
return bytesReceived;
|
||||
}
|
||||
|
||||
void UDPClient::send(Packet& packet)
|
||||
void UDPClient::Send(Packet& packet)
|
||||
{
|
||||
m_Socket.send_to(boost::asio::buffer(
|
||||
m_Socket->send_to(boost::asio::buffer(
|
||||
packet.Data(),
|
||||
packet.Size()),
|
||||
m_ReceiverEndpoint, 0);
|
||||
// Network Debug data
|
||||
if (isReadingData) {
|
||||
m_NetworkData.TotalDataSent += packet.Size();
|
||||
m_NetworkData.DataSentThisInterval += packet.Size();
|
||||
m_NetworkData.AmountOfMessagesSent++;
|
||||
}
|
||||
}
|
||||
|
||||
void UDPClient::connect()
|
||||
bool UDPClient::IsSocketAvailable()
|
||||
{
|
||||
Packet packet(MessageType::Connect, m_SendPacketID);
|
||||
packet.WriteString(m_PlayerName);
|
||||
m_StartPingTime = std::clock();
|
||||
send(packet);
|
||||
if (!m_Socket) {
|
||||
return false;
|
||||
}
|
||||
return m_Socket->available();
|
||||
}
|
||||
@@ -6,8 +6,7 @@ UDPServer::UDPServer()
|
||||
}
|
||||
|
||||
UDPServer::~UDPServer()
|
||||
{
|
||||
}
|
||||
{ }
|
||||
|
||||
void UDPServer::readFromClients()
|
||||
{
|
||||
@@ -22,24 +21,6 @@ void UDPServer::readFromClients()
|
||||
//LOG_ERROR("%i: Read from client crashed %s", m_PacketID, err.what());
|
||||
}
|
||||
}
|
||||
std::clock_t currentTime = std::clock();
|
||||
// Send snapshot
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendSnapshot();
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
|
||||
// Send pings each
|
||||
if (pingIntervalMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
sendPing();
|
||||
previousePingMessage = currentTime;
|
||||
}
|
||||
|
||||
// Time out logic
|
||||
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
checkForTimeOuts();
|
||||
timOutTimer = currentTime;
|
||||
}
|
||||
}
|
||||
|
||||
void UDPServer::parseConnect(Packet& packet)
|
||||
|
||||
Reference in New Issue
Block a user