mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 16:55:07 +02:00
Added ping detection between agents
This commit is contained in:
parent
6f9d223454
commit
a48d9ecd95
3 changed files with 69 additions and 9 deletions
|
@ -9,6 +9,7 @@
|
|||
#include <iostream>
|
||||
#include "Agent.h"
|
||||
#include "Head.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
// Structure to hold references to other agents that are nearby
|
||||
|
@ -17,6 +18,8 @@ const int MAX_AGENTS = 100;
|
|||
struct AgentList {
|
||||
char address[255];
|
||||
unsigned short port;
|
||||
timeval pingStarted;
|
||||
int pingMsecs;
|
||||
Head head;
|
||||
} agents[MAX_AGENTS];
|
||||
|
||||
|
@ -84,7 +87,7 @@ int add_agent(char * address, unsigned short port) {
|
|||
//std::cout << "Checking for " << IP->c_str() << " ";
|
||||
for (int i = 0; i < num_agents; i++) {
|
||||
if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) {
|
||||
std::cout << "Found agent!\n";
|
||||
//std::cout << "Found agent!\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +107,7 @@ int add_agent(char * address, unsigned short port) {
|
|||
//
|
||||
// Broadcast data to all the other agents you are aware of, returns 1 if success
|
||||
//
|
||||
int broadcast_to_agents(UDPSocket *handle, char * data, int length) {
|
||||
int broadcastToAgents(UDPSocket *handle, char * data, int length) {
|
||||
int sent_bytes;
|
||||
//printf("broadcasting to %d agents\n", num_agents);
|
||||
for (int i = 0; i < num_agents; i++) {
|
||||
|
@ -125,3 +128,27 @@ int broadcast_to_agents(UDPSocket *handle, char * data, int length) {
|
|||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Ping other agents to see how fast we are running
|
||||
void pingAgents(UDPSocket *handle) {
|
||||
char payload[] = "P";
|
||||
for (int i = 0; i < num_agents; i++) {
|
||||
gettimeofday(&agents[i].pingStarted, NULL);
|
||||
handle->send(agents[i].address, agents[i].port, payload, 1);
|
||||
printf("\nSent Ping at %d usecs\n", agents[i].pingStarted.tv_usec);
|
||||
}
|
||||
}
|
||||
|
||||
// On receiving a ping reply, save that with the agent record
|
||||
void setAgentPing(char * address, unsigned short port) {
|
||||
for (int i = 0; i < num_agents; i++) {
|
||||
if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) {
|
||||
timeval pingReceived;
|
||||
gettimeofday(&pingReceived, NULL);
|
||||
float pingMsecs = diffclock(&agents[i].pingStarted, &pingReceived);
|
||||
printf("Received ping at %d usecs, Agent ping = %3.1f\n", pingReceived.tv_usec, pingMsecs);
|
||||
agents[i].pingMsecs = pingMsecs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/time.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include "UDPSocket.h"
|
||||
|
@ -20,7 +21,9 @@
|
|||
|
||||
int update_agents(char * data, int length);
|
||||
int add_agent(char * address, unsigned short port);
|
||||
int broadcast_to_agents(UDPSocket * handle, char * data, int length);
|
||||
int broadcastToAgents(UDPSocket * handle, char * data, int length);
|
||||
void pingAgents(UDPSocket *handle);
|
||||
void setAgentPing(char * address, unsigned short port);
|
||||
void update_agent(char * address, unsigned short port, char * data, int length);
|
||||
void render_agents();
|
||||
|
||||
|
|
|
@ -100,7 +100,7 @@ Cloud cloud(0, // Particles
|
|||
false // Wrap
|
||||
);
|
||||
|
||||
VoxelSystem voxels(0, box);
|
||||
VoxelSystem voxels(1000, box);
|
||||
|
||||
Lattice lattice(160,100);
|
||||
Finger myFinger(WIDTH, HEIGHT);
|
||||
|
@ -230,6 +230,25 @@ void Timer(int extra)
|
|||
int packet_size = strlen(output);
|
||||
agentSocket.send(DOMAINSERVER_IP, DOMAINSERVER_PORT, output, packet_size);
|
||||
|
||||
// Ping the agents we can see
|
||||
pingAgents(&agentSocket);
|
||||
|
||||
if (0) {
|
||||
// Massive send packet speed test
|
||||
timeval starttest, endtest;
|
||||
gettimeofday(&starttest, NULL);
|
||||
char junk[1000];
|
||||
junk[0] = 'J';
|
||||
for (int i = 0; i < 3000; i++)
|
||||
{
|
||||
agentSocket.send("192.168.1.38", AGENT_UDP_PORT, junk, 1000);
|
||||
}
|
||||
gettimeofday(&endtest, NULL);
|
||||
float sendTime = diffclock(&starttest, &endtest);
|
||||
printf("packet test = %4.1f\n", sendTime);
|
||||
}
|
||||
|
||||
|
||||
// Send a message to ourselves
|
||||
//char test[]="T";
|
||||
//agentSocket.send("127.0.0.1", AGENT_UDP_PORT, test, 1);
|
||||
|
@ -520,7 +539,7 @@ void update_pos(float frametime)
|
|||
const int MAX_BROADCAST_STRING = 200;
|
||||
char broadcast_string[MAX_BROADCAST_STRING];
|
||||
int broadcast_bytes = myHead.getBroadcastData(broadcast_string);
|
||||
broadcast_to_agents(&agentSocket, broadcast_string, broadcast_bytes);
|
||||
broadcastToAgents(&agentSocket, broadcast_string, broadcast_bytes);
|
||||
|
||||
//printf("-> %s\n", broadcast_string);
|
||||
//dummyHead.recvBroadcastData(broadcast_string, broadcast_bytes);
|
||||
|
@ -773,14 +792,25 @@ void read_network()
|
|||
{
|
||||
sockaddr_in senderAddress;
|
||||
int bytes_recvd;
|
||||
agentSocket.receive(&senderAddress, (void *)incoming_packet, &bytes_recvd);
|
||||
|
||||
if (bytes_recvd > 0)
|
||||
while (agentSocket.receive(&senderAddress, (void *)incoming_packet, &bytes_recvd))
|
||||
{
|
||||
packetcount++;
|
||||
bytescount += bytes_recvd;
|
||||
// If packet is a Mouse data packet, copy it over
|
||||
if (incoming_packet[0] == 'M') {
|
||||
if (incoming_packet[0] == 'P') {
|
||||
//
|
||||
// Got Ping, reply immediately!
|
||||
//
|
||||
printf("Replying to ping!\n");
|
||||
char reply[] = "R";
|
||||
agentSocket.send(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port), reply, 1);
|
||||
} else if (incoming_packet[0] == 'R') {
|
||||
//
|
||||
// Got Reply, record as appropriate
|
||||
//
|
||||
setAgentPing(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port));
|
||||
|
||||
} else if (incoming_packet[0] == 'M') {
|
||||
//
|
||||
// mouse location packet
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue