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 BOARDSIZE 16
|
||||||
#define MAXCONNECTIONS 8
|
#define MAXCONNECTIONS 8
|
||||||
#define INPUTSIZE 128
|
#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<boost::asio::ip::udp::socket> socket_ptr;
|
||||||
typedef boost::shared_ptr<std::string> string_ptr;
|
typedef boost::shared_ptr<std::string> string_ptr;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ using namespace boost::asio::ip;
|
|||||||
Client::Client() : m_Socket(m_IOService)
|
Client::Client() : m_Socket(m_IOService)
|
||||||
{
|
{
|
||||||
// Set up network stream
|
// 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.inputForward = "";
|
||||||
m_NextSnapshot.inputRight = "";
|
m_NextSnapshot.inputRight = "";
|
||||||
}
|
}
|
||||||
@@ -133,14 +133,15 @@ void Client::ParseMessageType(char* data, size_t length)
|
|||||||
|
|
||||||
void Client::ParseConnect(char* data, size_t len)
|
void Client::ParseConnect(char* data, size_t len)
|
||||||
{
|
{
|
||||||
memcpy(&m_PlayerID, data, sizeof(int));
|
memcpy(&m_PacketID, data, sizeof(int));
|
||||||
std::cout << "I am player: " << m_PlayerID << std::endl;
|
m_PreviousPacketID = m_PacketID;
|
||||||
|
std::cout << m_PacketID << ": I am player: " << m_PlayerID << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::ParsePing()
|
void Client::ParsePing()
|
||||||
{
|
{
|
||||||
m_DurationOfPingTime = 1000 * (std::clock() - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
|
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()
|
void Client::ParseServerPing()
|
||||||
@@ -169,7 +170,7 @@ void Client::ParseEventMessage(char* data, size_t length)
|
|||||||
m_PlayerDefinitions[Id].Name = command.erase(0, 7);
|
m_PlayerDefinitions[Id].Name = command.erase(0, 7);
|
||||||
}
|
}
|
||||||
else {
|
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);
|
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)
|
void Client::ParseSnapshot(char* data, size_t length)
|
||||||
{
|
{
|
||||||
|
std::cout << m_PacketID << ": Parsing incoming snapshot." << std::endl;
|
||||||
std::string tempName;
|
std::string tempName;
|
||||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
// We're checking for empty name for now. This might not be the best way,
|
// 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
|
// m_ThreadIsRunning might be unnecessary but the
|
||||||
// program crashed if it executed m_Socket.available()
|
// program crashed if it executed m_Socket.available()
|
||||||
// when closing the program.
|
// when closing the program.
|
||||||
|
|
||||||
|
// If available message -> Socket.available() = true
|
||||||
if (m_ThreadIsRunning && m_Socket.available()) {
|
if (m_ThreadIsRunning && m_Socket.available()) {
|
||||||
try {
|
try {
|
||||||
bytesRead = Receive(readBuf, INPUTSIZE);
|
bytesRead = Receive(readBuf, INPUTSIZE);
|
||||||
@@ -60,10 +62,10 @@ void Server::ReadFromClients()
|
|||||||
} catch (const std::exception& err) {
|
} catch (const std::exception& err) {
|
||||||
// To not spam "socket closed messages"
|
// To not spam "socket closed messages"
|
||||||
//if (std::string(err.what()).find("forcefully closed") != std::string::npos) {
|
//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();
|
std::clock_t currentTime = std::clock();
|
||||||
// int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC);
|
// int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC);
|
||||||
// Send snapshot
|
// Send snapshot
|
||||||
@@ -84,7 +86,6 @@ void Server::ReadFromClients()
|
|||||||
timOutTimer = currentTime;
|
timOutTimer = currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::InputLoop()
|
void Server::InputLoop()
|
||||||
@@ -102,7 +103,7 @@ void Server::InputLoop()
|
|||||||
Broadcast(inputMessage);
|
Broadcast(inputMessage);
|
||||||
|
|
||||||
} catch (const std::exception& err) {
|
} 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)
|
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));
|
memcpy(data + offset, &type, sizeof(int));
|
||||||
offset += sizeof(int);
|
offset += sizeof(int);
|
||||||
// Packet ID
|
// Packet ID
|
||||||
m_PacketID = m_PacketCounter % 10;
|
m_PacketID = m_PacketCounter % PACKETMODULUS;
|
||||||
memcpy(data + offset, &m_PacketID, sizeof(int));
|
memcpy(data + offset, &m_PacketID, sizeof(int));
|
||||||
offset += sizeof(int);
|
offset += sizeof(int);
|
||||||
// Message, add one extra byte for null terminator
|
// 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)
|
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];
|
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++) {
|
||||||
@@ -238,7 +239,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_PlayerDefinitions[i].Endpoint.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 << m_PacketID << ": 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,9 +279,10 @@ int Server::CreateHeader(MessageType type, char * data)
|
|||||||
int offset = 0;
|
int offset = 0;
|
||||||
memcpy(data, &messageType, sizeof(int));
|
memcpy(data, &messageType, sizeof(int));
|
||||||
offset += sizeof(int);
|
offset += sizeof(int);
|
||||||
m_PacketID = m_PacketCounter % 10;
|
m_PacketID = m_PacketCounter % PACKETMODULUS;
|
||||||
memcpy(data + offset, &m_PacketID, sizeof(int));
|
memcpy(data + offset, &m_PacketID, sizeof(int));
|
||||||
offset += sizeof(int);
|
offset += sizeof(int);
|
||||||
|
m_PacketCounter++;
|
||||||
|
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
@@ -358,7 +360,7 @@ void Server::ParseConnect(char * data, size_t length)
|
|||||||
m_PlayerDefinitions[i].Name = std::string(data);
|
m_PlayerDefinitions[i].Name = std::string(data);
|
||||||
m_StopTimes[i] = std::clock();
|
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;
|
m_PlayerDefinitions[i].Endpoint.address().to_string() << std::endl;
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
@@ -379,7 +381,7 @@ void Server::ParseConnect(char * data, size_t length)
|
|||||||
0);
|
0);
|
||||||
|
|
||||||
// Send notification that a player has connected
|
// 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();
|
+ m_PlayerDefinitions[i].Endpoint.address().to_string();
|
||||||
Broadcast(str);
|
Broadcast(str);
|
||||||
// +1 is the null terminator
|
// +1 is the null terminator
|
||||||
@@ -392,7 +394,7 @@ void Server::ParseConnect(char * data, size_t length)
|
|||||||
|
|
||||||
void Server::ParseDisconnect()
|
void Server::ParseDisconnect()
|
||||||
{
|
{
|
||||||
std::cout << "Parsing disconnect. \n";
|
std::cout << m_PacketID << ":Parsing disconnect. \n";
|
||||||
|
|
||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) {
|
||||||
@@ -407,7 +409,7 @@ void Server::ParseClientPing()
|
|||||||
char* testMesssage = new char[128];
|
char* testMesssage = new char[128];
|
||||||
int testOffset = CreateMessage(MessageType::ClientPing, "Ping recieved", testMesssage);
|
int testOffset = CreateMessage(MessageType::ClientPing, "Ping recieved", testMesssage);
|
||||||
|
|
||||||
std::cout << "Parsing ping." << std::endl;
|
std::cout << m_PacketID << ":Parsing ping." << std::endl;
|
||||||
// Return ping
|
// Return ping
|
||||||
m_Socket.send_to(
|
m_Socket.send_to(
|
||||||
boost::asio::buffer(
|
boost::asio::buffer(
|
||||||
|
|||||||
Reference in New Issue
Block a user