Browse Source

Bridge networking

tags/v0.6.0
Andrew Belt 7 years ago
parent
commit
ea09add145
2 changed files with 22 additions and 20 deletions
  1. +2
    -3
      src/Core/AudioInterface.cpp
  2. +20
    -17
      src/bridge.cpp

+ 2
- 3
src/Core/AudioInterface.cpp View File

@@ -32,9 +32,6 @@ struct AudioInterfaceIO : AudioIO {
// Audio thread consumes, engine thread produces // Audio thread consumes, engine thread produces
DoubleRingBuffer<Frame<OUTPUTS>, (1<<15)> outputBuffer; DoubleRingBuffer<Frame<OUTPUTS>, (1<<15)> outputBuffer;


AudioInterfaceIO() {
}

~AudioInterfaceIO() { ~AudioInterfaceIO() {
// Close stream here before destructing AudioInterfaceIO, so the mutexes are still valid when waiting to close. // Close stream here before destructing AudioInterfaceIO, so the mutexes are still valid when waiting to close.
setDevice(-1, 0); setDevice(-1, 0);
@@ -69,6 +66,7 @@ struct AudioInterfaceIO : AudioIO {
else { else {
// Timed out, fill output with zeros // Timed out, fill output with zeros
memset(output, 0, frames * numOutputs * sizeof(float)); memset(output, 0, frames * numOutputs * sizeof(float));
debug("Audio Interface IO underflow");
} }
} }


@@ -199,6 +197,7 @@ void AudioInterface::step() {
} }
else { else {
// Give up on pushing output // Give up on pushing output
debug("Audio Interface underflow");
} }
} }
} }


+ 20
- 17
src/bridge.cpp View File

@@ -138,8 +138,7 @@ struct BridgeClientConnection {
} }
} }
else { else {
if (recvQueue.size() >= (size_t) audioBufferLength) {
debug("Received %d audio samples", audioBufferLength);
if (recvQueue.size() >= (size_t) (sizeof(float) * audioBufferLength)) {
float input[audioBufferLength]; float input[audioBufferLength];
float output[audioBufferLength]; float output[audioBufferLength];
memset(output, 0, sizeof(output)); memset(output, 0, sizeof(output));
@@ -185,12 +184,12 @@ struct BridgeClientConnection {
while (step()) {} while (step()) {}
} }


void processStream(const float *input, float *output, int length) {
void processStream(const float *input, float *output, int frames) {
if (!(0 <= channel && channel < BRIDGE_CHANNELS)) if (!(0 <= channel && channel < BRIDGE_CHANNELS))
return; return;
if (!audioListeners[channel]) if (!audioListeners[channel])
return; return;
audioListeners[channel]->processStream(input, output, length);
audioListeners[channel]->processStream(input, output, frames);
} }
}; };


@@ -198,6 +197,7 @@ struct BridgeClientConnection {


static void clientRun(int client) { static void clientRun(int client) {
int err; int err;
BridgeClientConnection connection;


// // Get client address // // Get client address
// struct sockaddr_in addr; // struct sockaddr_in addr;
@@ -219,7 +219,12 @@ static void clientRun(int client) {
setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &flag, sizeof(int)); setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &flag, sizeof(int));
#endif #endif


BridgeClientConnection connection;
#ifdef ARCH_WIN
unsigned long blockingMode = 1;
ioctlsocket(client, FIONBIO, &blockingMode);
#else
err = fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) & ~O_NONBLOCK);
#endif


while (!connection.closeRequested) { while (!connection.closeRequested) {
uint8_t buffer[RECV_BUFFER_SIZE]; uint8_t buffer[RECV_BUFFER_SIZE];
@@ -234,11 +239,12 @@ static void clientRun(int client) {
connection.recv(buffer, received); connection.recv(buffer, received);
} }


info("Bridge client closed");
err = close(client); err = close(client);
(void) err; (void) err;
info("Bridge client closed");
} }



static void serverRun() { static void serverRun() {
int err; int err;


@@ -265,6 +271,10 @@ static void serverRun() {
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
err = getaddrinfo(NULL, "5000", &hints, &result); err = getaddrinfo(NULL, "5000", &hints, &result);
if (err) {
warn("Could not get Bridge server address");
return;
}
defer({ defer({
freeaddrinfo(result); freeaddrinfo(result);
}); });
@@ -272,13 +282,9 @@ static void serverRun() {
struct sockaddr_in addr; struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
err = inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
addr.sin_port = htons(5000); addr.sin_port = htons(5000);
#endif #endif
if (err) {
warn("Could not get Bridge server address");
return;
}


// Open socket // Open socket
#ifdef ARCH_WIN #ifdef ARCH_WIN
@@ -317,18 +323,15 @@ static void serverRun() {


// Make server non-blocking // Make server non-blocking
#ifdef ARCH_WIN #ifdef ARCH_WIN
{
unsigned long mode = 1;
ioctlsocket(server, FIONBIO, &mode);
}
unsigned long blockingMode = 1;
ioctlsocket(server, FIONBIO, &blockingMode);
#else #else
err = fcntl(server, F_SETFL, fcntl(server, F_GETFL, 0) | O_NONBLOCK); err = fcntl(server, F_SETFL, fcntl(server, F_GETFL, 0) | O_NONBLOCK);
#endif #endif


// Accept clients
serverQuit = false; serverQuit = false;
while (!serverQuit) { while (!serverQuit) {
// Accept client socket

int client = accept(server, NULL, NULL); int client = accept(server, NULL, NULL);
if (client < 0) { if (client < 0) {
// Wait a bit before attempting to accept another client // Wait a bit before attempting to accept another client


Loading…
Cancel
Save