mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 19:56:44 +02:00
stub out a thread to send back buffer mix
This commit is contained in:
parent
2fc8bf9fa5
commit
89bcc7a908
1 changed files with 43 additions and 23 deletions
60
socket.cpp
60
socket.cpp
|
@ -8,14 +8,13 @@
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
const int UDP_PORT = 55443;
|
const int UDP_PORT = 55443;
|
||||||
|
const int MAX_PACKET_SIZE = 1024;
|
||||||
|
const float SAMPLE_RATE = 22050.0;
|
||||||
|
const int SAMPLES_PER_PACKET = 512;
|
||||||
|
|
||||||
sockaddr_in address, dest_address;
|
sockaddr_in address, dest_address;
|
||||||
socklen_t destLength = sizeof( dest_address );
|
|
||||||
|
|
||||||
double diffclock(timeval clock1,timeval clock2)
|
double diffclock(timeval clock1,timeval clock2)
|
||||||
{
|
{
|
||||||
|
@ -24,7 +23,7 @@ double diffclock(timeval clock1,timeval clock2)
|
||||||
return diffms;
|
return diffms;
|
||||||
}
|
}
|
||||||
|
|
||||||
int network_init()
|
int create_socket()
|
||||||
{
|
{
|
||||||
// Create socket
|
// Create socket
|
||||||
int handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
int handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
@ -34,6 +33,13 @@ int network_init()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
int network_init()
|
||||||
|
{
|
||||||
|
int handle = create_socket();
|
||||||
|
|
||||||
// Bind socket to port
|
// Bind socket to port
|
||||||
address.sin_family = AF_INET;
|
address.sin_family = AF_INET;
|
||||||
address.sin_addr.s_addr = INADDR_ANY;
|
address.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
@ -44,18 +50,29 @@ int network_init()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set socket as non-blocking
|
return handle;
|
||||||
int nonBlocking = 1;
|
}
|
||||||
if (fcntl( handle, F_SETFL, O_NONBLOCK, nonBlocking ) == -1) {
|
|
||||||
printf( "failed to set non-blocking socket\n" );
|
void send_buffer_thread()
|
||||||
return false;
|
{
|
||||||
|
// create our send socket
|
||||||
|
int handle = create_socket();
|
||||||
|
|
||||||
|
if (!handle) {
|
||||||
|
std::cout << "Failed to create buffer send socket.\n";
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
std::cout << "Buffer send socket created.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
dest_address.sin_family = AF_INET;
|
while (1) {
|
||||||
dest_address.sin_addr.s_addr = inet_addr(DESTINATION_IP);
|
// sleep for the length of a packet of audio
|
||||||
dest_address.sin_port = htons( (unsigned short) UDP_PORT );
|
sleep((SAMPLES_PER_PACKET/SAMPLE_RATE) * pow(10, 6));
|
||||||
|
|
||||||
return handle;
|
// send out whatever we have in the buffer as mixed audio
|
||||||
|
// to our recent clients
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char * argv[])
|
int main(int argc, const char * argv[])
|
||||||
|
@ -66,30 +83,32 @@ int main(int argc, const char * argv[])
|
||||||
int handle = network_init();
|
int handle = network_init();
|
||||||
|
|
||||||
if (!handle) {
|
if (!handle) {
|
||||||
cout << "Failed to create network.\n";
|
std::cout << "Failed to create listening socket.\n";
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
cout << "Network Started. Waiting for packets.\n";
|
std::cout << "Network Started. Waiting for packets.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
gettimeofday(&last_time, NULL);
|
gettimeofday(&last_time, NULL);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
received_bytes = recvfrom(handle, (char*)packet_data, MAX_PACKET_SIZE,
|
received_bytes = recvfrom(handle, (char*)packet_data, MAX_PACKET_SIZE,
|
||||||
0, (sockaddr*)&dest_address, &destLength );
|
0, (sockaddr*)&dest_address, sizeof(dest_address));
|
||||||
if (received_bytes > 0) {
|
if (received_bytes > 0) {
|
||||||
//std::cout << "Packet from: " << inet_ntoa(dest_address.sin_addr)
|
// std::cout << "Packet from: " << inet_ntoa(dest_address.sin_addr)
|
||||||
//<< " " << packet_data << "\n";
|
// << " " << packet_data << "\n";
|
||||||
float x,y,z;
|
float x,y,z;
|
||||||
sscanf(packet_data, "%f,%f,%f", &x, &y, &z);
|
sscanf(packet_data, "%f,%f,%f", &x, &y, &z);
|
||||||
if (addAgent(dest_address.sin_addr.s_addr, x, y, z)) {
|
if (addAgent(dest_address.sin_addr.s_addr, x, y, z)) {
|
||||||
cout << "Added agent from IP: " <<
|
std::cout << "Added agent from IP: " <<
|
||||||
inet_ntoa(dest_address.sin_addr) << "\n";
|
inet_ntoa(dest_address.sin_addr) << "\n";
|
||||||
}
|
}
|
||||||
// Reply with packet listing nearby active agents
|
// Reply with packet listing nearby active agents
|
||||||
send_agent_list(handle, &dest_address);
|
send_agent_list(handle, &dest_address);
|
||||||
}
|
}
|
||||||
|
|
||||||
gettimeofday(&time, NULL);
|
gettimeofday(&time, NULL);
|
||||||
|
|
||||||
if (diffclock(last_time, time) > LOGOFF_CHECK_INTERVAL) {
|
if (diffclock(last_time, time) > LOGOFF_CHECK_INTERVAL) {
|
||||||
gettimeofday(&last_time, NULL);
|
gettimeofday(&last_time, NULL);
|
||||||
update_agent_list(last_time);
|
update_agent_list(last_time);
|
||||||
|
@ -97,6 +116,7 @@ int main(int argc, const char * argv[])
|
||||||
|
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue