mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-19 01:39:16 +02:00
fix UDPSocket pointer to pointer, use in domain
This commit is contained in:
parent
b52c24ee47
commit
1e4213b676
2 changed files with 24 additions and 75 deletions
|
@ -27,13 +27,11 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/time.h>
|
||||
#include "UDPSocket.h"
|
||||
|
||||
|
||||
const int LISTENING_UDP_PORT = 40102;
|
||||
const int DOMAIN_LISTEN_PORT = 40102;
|
||||
const char DESTINATION_IP[] = "127.0.0.1";
|
||||
sockaddr_in address, dest_address;
|
||||
socklen_t destLength = sizeof( dest_address );
|
||||
|
||||
|
||||
const int MAX_PACKET_SIZE = 1500;
|
||||
char packet_data[MAX_PACKET_SIZE];
|
||||
|
@ -52,9 +50,10 @@ struct AgentList {
|
|||
} agents[MAX_AGENTS];
|
||||
|
||||
int num_agents = 0;
|
||||
|
||||
int lastActiveCount = 0;
|
||||
|
||||
UDPSocket domainSocket = UDPSocket(DOMAIN_LISTEN_PORT);
|
||||
|
||||
double diffclock(timeval clock1,timeval clock2)
|
||||
{
|
||||
double diffms = (clock2.tv_sec - clock1.tv_sec) * 1000.0;
|
||||
|
@ -62,43 +61,6 @@ double diffclock(timeval clock1,timeval clock2)
|
|||
return diffms;
|
||||
}
|
||||
|
||||
int network_init()
|
||||
{
|
||||
// Create socket
|
||||
int handle = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
|
||||
|
||||
if ( handle <= 0 )
|
||||
{
|
||||
printf( "failed to create socket\n" );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Bind socket to port
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
address.sin_port = htons( (unsigned short) LISTENING_UDP_PORT );
|
||||
|
||||
if ( bind( handle, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 )
|
||||
{
|
||||
printf( "failed to bind socket\n" );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set socket as non-blocking
|
||||
int nonBlocking = 1;
|
||||
if ( fcntl( handle, F_SETFL, O_NONBLOCK, nonBlocking ) == -1 )
|
||||
{
|
||||
printf( "failed to set non-blocking socket\n" );
|
||||
return false;
|
||||
}
|
||||
|
||||
dest_address.sin_family = AF_INET;
|
||||
dest_address.sin_addr.s_addr = inet_addr(DESTINATION_IP);
|
||||
dest_address.sin_port = htons( (unsigned short) LISTENING_UDP_PORT );
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
int addAgent(uint32_t ip, in_port_t port, char agentType, float x, float y, float z) {
|
||||
// Search for agent in list and add if needed
|
||||
int i = 0;
|
||||
|
@ -136,9 +98,9 @@ void update_agent_list(timeval now) {
|
|||
}
|
||||
}
|
||||
|
||||
void send_agent_list(int handle, sockaddr_in * dest_address) {
|
||||
void send_agent_list(sockaddr_in *agentAddrPointer) {
|
||||
int i, length = 0;
|
||||
ssize_t sent_bytes;
|
||||
ssize_t sentBytes;
|
||||
char buffer[MAX_PACKET_SIZE];
|
||||
char * address;
|
||||
char portstring[10];
|
||||
|
@ -163,10 +125,11 @@ void send_agent_list(int handle, sockaddr_in * dest_address) {
|
|||
numSent++;
|
||||
}
|
||||
}
|
||||
|
||||
if (length > 1) {
|
||||
sent_bytes = (ssize_t) sendto( handle, (const char*)buffer, length,
|
||||
0, (sockaddr *) dest_address, sizeof(sockaddr_in) );
|
||||
if (sent_bytes < length)
|
||||
sentBytes = domainSocket.send(agentAddrPointer, buffer, length);
|
||||
|
||||
if (sentBytes < length)
|
||||
std::cout << "Error sending agent list!\n";
|
||||
else if (numSent != lastActiveCount) {
|
||||
std::cout << numSent << " Active Agents\n";
|
||||
|
@ -178,43 +141,30 @@ void send_agent_list(int handle, sockaddr_in * dest_address) {
|
|||
|
||||
int main(int argc, const char * argv[])
|
||||
{
|
||||
ssize_t received_bytes = 0;
|
||||
//int sent_bytes = 0;
|
||||
//int packet_size = 0;
|
||||
timeval time, last_time;
|
||||
ssize_t receivedBytes = 0;
|
||||
timeval time, last_time;
|
||||
sockaddr_in agentAddress;
|
||||
|
||||
int handle = network_init();
|
||||
if (!handle) {
|
||||
std::cout << "Failed to create network.\n";
|
||||
return 0;
|
||||
} else {
|
||||
std::cout << "DomainServer started, listening on port " << LISTENING_UDP_PORT << "\n";
|
||||
}
|
||||
gettimeofday(&last_time, NULL);
|
||||
|
||||
while (1) {
|
||||
received_bytes = recvfrom(handle, (char*)packet_data, MAX_PACKET_SIZE,
|
||||
0, (sockaddr*)&dest_address, &destLength );
|
||||
if (received_bytes > 0) {
|
||||
//std::cout << "Packet from: " << inet_ntoa(dest_address.sin_addr)
|
||||
//<< " " << packet_data << "\n";
|
||||
while (true) {
|
||||
if (domainSocket.receive(&agentAddress, packet_data, &receivedBytes)) {
|
||||
float x,y,z;
|
||||
char agentType;
|
||||
sscanf(packet_data, "%c %f,%f,%f", &agentType, &x, &y, &z);
|
||||
if (addAgent(dest_address.sin_addr.s_addr, ntohs(dest_address.sin_port), agentType, x, y, z)) {
|
||||
if (addAgent(agentAddress.sin_addr.s_addr, ntohs(agentAddress.sin_port), agentType, x, y, z)) {
|
||||
std::cout << "Added Agent, type " << agentType << " from " <<
|
||||
inet_ntoa(dest_address.sin_addr) << ":" << ntohs(dest_address.sin_port) << "\n";
|
||||
inet_ntoa(agentAddress.sin_addr) << ":" << ntohs(agentAddress.sin_port) << "\n";
|
||||
}
|
||||
// Reply with packet listing nearby active agents
|
||||
send_agent_list(handle, &dest_address);
|
||||
send_agent_list(&agentAddress);
|
||||
}
|
||||
|
||||
gettimeofday(&time, NULL);
|
||||
if (diffclock(last_time, time) > LOGOFF_CHECK_INTERVAL) {
|
||||
gettimeofday(&last_time, NULL);
|
||||
update_agent_list(last_time);
|
||||
}
|
||||
|
||||
usleep(10000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <cstdio>
|
||||
#include <errno.h>
|
||||
|
||||
sockaddr_in destSockaddr, senderAddress;
|
||||
|
||||
|
@ -23,11 +24,7 @@ UDPSocket::UDPSocket(int listeningPort) {
|
|||
return;
|
||||
}
|
||||
|
||||
// instantiate the re-usable dest_sockaddr with a dummy IP and port
|
||||
sockaddr_in dest_sockaddr;
|
||||
dest_sockaddr.sin_family = AF_INET;
|
||||
dest_sockaddr.sin_addr.s_addr = inet_addr("1.0.0.0");
|
||||
dest_sockaddr.sin_port = htons((uint16_t) 1);
|
||||
destSockaddr.sin_family = AF_INET;
|
||||
|
||||
// bind the socket to the passed listeningPort
|
||||
sockaddr_in bind_address;
|
||||
|
@ -66,10 +63,12 @@ bool UDPSocket::receive(sockaddr_in *recvAddress, void *receivedData, ssize_t *r
|
|||
|
||||
int UDPSocket::send(sockaddr_in *destAddress, const void *data, size_t byteLength) {
|
||||
// send data via UDP
|
||||
|
||||
int sent_bytes = sendto(handle, (const char*)data, byteLength,
|
||||
0, (sockaddr *)&destSockaddr, sizeof(sockaddr_in));
|
||||
0, (sockaddr *) destAddress, sizeof(sockaddr_in));
|
||||
|
||||
if (sent_bytes != byteLength) {
|
||||
std::cout << strerror(errno) << "\n";
|
||||
printf("Failed to send packet: return value = %d\n", sent_bytes);
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue