Can now Identify Packet Loss. Enumerated messages that loops around 0-1000.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
#define BOARDSIZE 16
|
||||
#define MAXCONNECTIONS 8
|
||||
#define INPUTSIZE 128
|
||||
#define PACKETMODULUS 1000 // How many packets before the number resets
|
||||
#define PACKETMODULUS 1000 // How many packets to send before the number resets
|
||||
|
||||
typedef boost::shared_ptr<boost::asio::ip::udp::socket> socket_ptr;
|
||||
typedef boost::shared_ptr<std::string> string_ptr;
|
||||
|
||||
@@ -6,7 +6,7 @@ using namespace boost::asio::ip;
|
||||
Client::Client() : m_Socket(m_IOService)
|
||||
{
|
||||
// Set up network stream
|
||||
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13);
|
||||
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.2"), 13);
|
||||
m_NextSnapshot.inputForward = "";
|
||||
m_NextSnapshot.inputRight = "";
|
||||
}
|
||||
@@ -133,14 +133,15 @@ void Client::ParseMessageType(char* data, size_t length)
|
||||
|
||||
void Client::ParseConnect(char* data, size_t len)
|
||||
{
|
||||
memcpy(&m_PlayerID, data, sizeof(int));
|
||||
std::cout << "I am player: " << m_PlayerID << std::endl;
|
||||
memcpy(&m_PacketID, data, sizeof(int));
|
||||
m_PreviousPacketID = m_PacketID;
|
||||
std::cout << m_PacketID << ": I am player: " << m_PlayerID << std::endl;
|
||||
}
|
||||
|
||||
void Client::ParsePing()
|
||||
{
|
||||
m_DurationOfPingTime = 1000 * (std::clock() - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
|
||||
std::cout << "response time with ctime(ms): " << m_DurationOfPingTime << std::endl;
|
||||
std::cout << m_PacketID << ": response time with ctime(ms): " << m_DurationOfPingTime << std::endl;
|
||||
}
|
||||
|
||||
void Client::ParseServerPing()
|
||||
@@ -169,7 +170,7 @@ void Client::ParseEventMessage(char* data, size_t length)
|
||||
m_PlayerDefinitions[Id].Name = command.erase(0, 7);
|
||||
}
|
||||
else {
|
||||
std::cout << "Event message: " << std::string(data) << std::endl;
|
||||
std::cout << m_PacketID << ": Event message: " << std::string(data) << std::endl;
|
||||
}
|
||||
|
||||
MoveMessageHead(data, length, std::string(data).size() + 1);
|
||||
@@ -177,6 +178,7 @@ void Client::ParseEventMessage(char* data, size_t length)
|
||||
|
||||
void Client::ParseSnapshot(char* data, size_t length)
|
||||
{
|
||||
std::cout << m_PacketID << ": Parsing incoming snapshot." << std::endl;
|
||||
std::string tempName;
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
// We're checking for empty name for now. This might not be the best way,
|
||||
|
||||
@@ -53,6 +53,8 @@ void Server::ReadFromClients()
|
||||
// m_ThreadIsRunning might be unnecessary but the
|
||||
// program crashed if it executed m_Socket.available()
|
||||
// when closing the program.
|
||||
|
||||
// If available message -> Socket.available() = true
|
||||
if (m_ThreadIsRunning && m_Socket.available()) {
|
||||
try {
|
||||
bytesRead = Receive(readBuf, INPUTSIZE);
|
||||
@@ -60,30 +62,29 @@ void Server::ReadFromClients()
|
||||
} catch (const std::exception& err) {
|
||||
// To not spam "socket closed messages"
|
||||
//if (std::string(err.what()).find("forcefully closed") != std::string::npos) {
|
||||
std::cout << "Read from client crashed: " << err.what();
|
||||
std::cout << m_PacketID << ": Read from client crashed: " << err.what();
|
||||
//}
|
||||
}
|
||||
|
||||
std::clock_t currentTime = std::clock();
|
||||
// int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC);
|
||||
// Send snapshot
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
SendSnapshot();
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
|
||||
// Send pings each
|
||||
if (intervallMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
SendPing();
|
||||
previousePingMessage = currentTime;
|
||||
}
|
||||
|
||||
// Time out logic
|
||||
if (timeToCheckTimeOutTime < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
CheckForTimeOuts();
|
||||
timOutTimer = currentTime;
|
||||
}
|
||||
}
|
||||
std::clock_t currentTime = std::clock();
|
||||
// int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC);
|
||||
// Send snapshot
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
SendSnapshot();
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
|
||||
// Send pings each
|
||||
if (intervallMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
SendPing();
|
||||
previousePingMessage = currentTime;
|
||||
}
|
||||
|
||||
// Time out logic
|
||||
if (timeToCheckTimeOutTime < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
CheckForTimeOuts();
|
||||
timOutTimer = currentTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +103,7 @@ void Server::InputLoop()
|
||||
Broadcast(inputMessage);
|
||||
|
||||
} catch (const std::exception& err) {
|
||||
std::cout << "Read from WriteLoop crashed: " << err.what();
|
||||
std::cout << m_PacketID << ": Read from WriteLoop crashed: " << err.what();
|
||||
}
|
||||
}
|
||||
if (inputMessage.find("exit") != std::string::npos)
|
||||
@@ -163,7 +164,7 @@ int Server::CreateMessage(MessageType type, std::string message, char * data)
|
||||
memcpy(data + offset, &type, sizeof(int));
|
||||
offset += sizeof(int);
|
||||
// Packet ID
|
||||
m_PacketID = m_PacketCounter % 10;
|
||||
m_PacketID = m_PacketCounter % PACKETMODULUS;
|
||||
memcpy(data + offset, &m_PacketID, sizeof(int));
|
||||
offset += sizeof(int);
|
||||
// Message, add one extra byte for null terminator
|
||||
@@ -182,7 +183,7 @@ void Server::MoveMessageHead(char *& data, size_t & length, size_t stepSize)
|
||||
|
||||
void Server::Broadcast(std::string message)
|
||||
{
|
||||
std::cout << "Broadcast: " << message << std::endl;
|
||||
std::cout << m_PacketID << ": Broadcast: " << message << std::endl;
|
||||
char* data = new char[128];
|
||||
int offset = CreateMessage(MessageType::Event, message, data);
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
@@ -238,7 +239,7 @@ void Server::SendPing()
|
||||
// Prints connected players ping
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address())
|
||||
std::cout << "Player " << i << "'s ping: " << 1000 * (m_StopTimes[i] - m_StartPingTime)
|
||||
std::cout << m_PacketID << ": Player " << i << "'s ping: " << 1000 * (m_StopTimes[i] - m_StartPingTime)
|
||||
/ static_cast<double>(CLOCKS_PER_SEC) << std::endl;
|
||||
}
|
||||
|
||||
@@ -278,9 +279,10 @@ int Server::CreateHeader(MessageType type, char * data)
|
||||
int offset = 0;
|
||||
memcpy(data, &messageType, sizeof(int));
|
||||
offset += sizeof(int);
|
||||
m_PacketID = m_PacketCounter % 10;
|
||||
m_PacketID = m_PacketCounter % PACKETMODULUS;
|
||||
memcpy(data + offset, &m_PacketID, sizeof(int));
|
||||
offset += sizeof(int);
|
||||
m_PacketCounter++;
|
||||
|
||||
return offset;
|
||||
}
|
||||
@@ -358,7 +360,7 @@ void Server::ParseConnect(char * data, size_t length)
|
||||
m_PlayerDefinitions[i].Name = std::string(data);
|
||||
m_StopTimes[i] = std::clock();
|
||||
|
||||
std::cout << "Player \"" << m_PlayerDefinitions[i].Name << "\" connected on IP: " <<
|
||||
std::cout << m_PacketID << ": Player \"" << m_PlayerDefinitions[i].Name << "\" connected on IP: " <<
|
||||
m_PlayerDefinitions[i].Endpoint.address().to_string() << std::endl;
|
||||
|
||||
int offset = 0;
|
||||
@@ -379,7 +381,7 @@ void Server::ParseConnect(char * data, size_t length)
|
||||
0);
|
||||
|
||||
// Send notification that a player has connected
|
||||
std::string str = "Player " + m_PlayerDefinitions[i].Name + " connected on: "
|
||||
std::string str = m_PacketID + "Player " + m_PlayerDefinitions[i].Name + " connected on: "
|
||||
+ m_PlayerDefinitions[i].Endpoint.address().to_string();
|
||||
Broadcast(str);
|
||||
// +1 is the null terminator
|
||||
@@ -392,7 +394,7 @@ void Server::ParseConnect(char * data, size_t length)
|
||||
|
||||
void Server::ParseDisconnect()
|
||||
{
|
||||
std::cout << "Parsing disconnect. \n";
|
||||
std::cout << m_PacketID << ":Parsing disconnect. \n";
|
||||
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||
@@ -407,7 +409,7 @@ void Server::ParseClientPing()
|
||||
char* testMesssage = new char[128];
|
||||
int testOffset = CreateMessage(MessageType::ClientPing, "Ping recieved", testMesssage);
|
||||
|
||||
std::cout << "Parsing ping." << std::endl;
|
||||
std::cout << m_PacketID << ":Parsing ping." << std::endl;
|
||||
// Return ping
|
||||
m_Socket.send_to(
|
||||
boost::asio::buffer(
|
||||
|
||||
Reference in New Issue
Block a user