mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 19:59:28 +02:00
remove incorrect threaded agent processing, fix agent replacement
This commit is contained in:
parent
ff83335123
commit
5196a1ba1b
3 changed files with 21 additions and 37 deletions
|
@ -61,25 +61,29 @@ double usecTimestamp(timeval *time, double addedUsecs = 0) {
|
||||||
return (time->tv_sec * 1000000.0) + time->tv_usec + addedUsecs;
|
return (time->tv_sec * 1000000.0) + time->tv_usec + addedUsecs;
|
||||||
}
|
}
|
||||||
|
|
||||||
int addAgent(sockaddr_in agentAddress, void *audioData) {
|
int addAgent(sockaddr_in *newAddress, void *audioData) {
|
||||||
// Search for agent in list and add if needed
|
// Search for agent in list and add if needed
|
||||||
int is_new = 0;
|
int is_new = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
for (i = 0; i < numAgents; i++) {
|
for (i = 0; i < numAgents; i++) {
|
||||||
if (strcmp(inet_ntoa(agentAddress.sin_addr), agents[i].address) == 0
|
if (strcmp(inet_ntoa(newAddress->sin_addr), agents[i].address) == 0
|
||||||
&& ntohs(agentAddress.sin_port) == agents[i].port) {
|
&& ntohs(newAddress->sin_port) == agents[i].port) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((i == numAgents) || (agents[i].active == false)) {
|
if ((i == numAgents) || (agents[i].active == false)) {
|
||||||
is_new = 1;
|
is_new = 1;
|
||||||
|
|
||||||
|
agents[i].address = new char();
|
||||||
|
strcpy(agents[i].address, inet_ntoa(newAddress->sin_addr));
|
||||||
|
|
||||||
agents[i].bufferTransmitted = false;
|
agents[i].bufferTransmitted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
agents[i].address = inet_ntoa(agentAddress.sin_addr);
|
|
||||||
agents[i].port = ntohs(agentAddress.sin_port);
|
agents[i].port = ntohs(newAddress->sin_port);
|
||||||
agents[i].active = true;
|
agents[i].active = true;
|
||||||
gettimeofday(&agents[i].time, NULL);
|
gettimeofday(&agents[i].time, NULL);
|
||||||
|
|
||||||
|
@ -218,26 +222,6 @@ void *sendBuffer(void *args)
|
||||||
pthread_exit(0);
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct processArgStruct {
|
|
||||||
int16_t *packetData;
|
|
||||||
sockaddr_in agentAddress;
|
|
||||||
};
|
|
||||||
|
|
||||||
void *processClientPacket(void *args)
|
|
||||||
{
|
|
||||||
struct processArgStruct *processArgs = (struct processArgStruct *) args;
|
|
||||||
|
|
||||||
sockaddr_in agentAddress = processArgs->agentAddress;
|
|
||||||
|
|
||||||
if (addAgent(agentAddress, processArgs->packetData)) {
|
|
||||||
std::cout << "Added agent: " <<
|
|
||||||
inet_ntoa(agentAddress.sin_addr) << " on " <<
|
|
||||||
ntohs(agentAddress.sin_port) << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, const char * argv[])
|
int main(int argc, const char * argv[])
|
||||||
{
|
{
|
||||||
timeval lastAgentUpdate;
|
timeval lastAgentUpdate;
|
||||||
|
@ -262,13 +246,12 @@ int main(int argc, const char * argv[])
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if(audioSocket.receive(&agentAddress, packetData, &receivedBytes)) {
|
if(audioSocket.receive(&agentAddress, packetData, &receivedBytes)) {
|
||||||
struct processArgStruct args;
|
|
||||||
args.packetData = packetData;
|
|
||||||
args.agentAddress = agentAddress;
|
|
||||||
|
|
||||||
pthread_t clientProcessThread;
|
if (addAgent(&agentAddress, packetData)) {
|
||||||
pthread_create(&clientProcessThread, NULL, processClientPacket, (void *)&args);
|
std::cout << "Added agent: " <<
|
||||||
pthread_join(clientProcessThread, NULL);
|
inet_ntoa(agentAddress.sin_addr) << " on " <<
|
||||||
|
ntohs(agentAddress.sin_port) << "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
sockaddr_in destSockaddr, senderAddress;
|
sockaddr_in destSockaddr, senderAddress;
|
||||||
socklen_t addLength = sizeof(senderAddress);
|
|
||||||
|
|
||||||
UDPSocket::UDPSocket(int listeningPort) {
|
UDPSocket::UDPSocket(int listeningPort) {
|
||||||
// create the socket
|
// create the socket
|
||||||
|
@ -55,10 +54,12 @@ bool UDPSocket::receive(void *receivedData, int *receivedBytes) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive data on this socket with the address of the sender
|
// Receive data on this socket with the address of the sender
|
||||||
bool UDPSocket::receive(sockaddr_in *senderAddress, void *receivedData, int *receivedBytes) {
|
bool UDPSocket::receive(sockaddr_in *recvAddress, void *receivedData, int *receivedBytes) {
|
||||||
|
|
||||||
|
socklen_t addressSize = sizeof(&recvAddress);
|
||||||
|
|
||||||
*receivedBytes = recvfrom(handle, receivedData, MAX_BUFFER_LENGTH_BYTES,
|
*receivedBytes = recvfrom(handle, receivedData, MAX_BUFFER_LENGTH_BYTES,
|
||||||
0, (sockaddr *)senderAddress, &addLength);
|
0, (sockaddr *) recvAddress, &addressSize);
|
||||||
|
|
||||||
return (*receivedBytes > 0);
|
return (*receivedBytes > 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ class UDPSocket {
|
||||||
~UDPSocket();
|
~UDPSocket();
|
||||||
int send(char *destAddress, int destPort, const void *data, int byteLength);
|
int send(char *destAddress, int destPort, const void *data, int byteLength);
|
||||||
bool receive(void *receivedData, int *receivedBytes);
|
bool receive(void *receivedData, int *receivedBytes);
|
||||||
bool receive(sockaddr_in *senderAddress, void *receivedData, int *receivedBytes);
|
bool receive(sockaddr_in *recvAddress, void *receivedData, int *receivedBytes);
|
||||||
private:
|
private:
|
||||||
int handle;
|
int handle;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue