From ea09add145a3e4e745c99b11e7fe86f382c31ec5 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 9 Mar 2018 15:32:21 -0500 Subject: [PATCH] Bridge networking --- src/Core/AudioInterface.cpp | 5 ++--- src/bridge.cpp | 37 ++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Core/AudioInterface.cpp b/src/Core/AudioInterface.cpp index 3e90d21c..8e6350bd 100644 --- a/src/Core/AudioInterface.cpp +++ b/src/Core/AudioInterface.cpp @@ -32,9 +32,6 @@ struct AudioInterfaceIO : AudioIO { // Audio thread consumes, engine thread produces DoubleRingBuffer, (1<<15)> outputBuffer; - AudioInterfaceIO() { - } - ~AudioInterfaceIO() { // Close stream here before destructing AudioInterfaceIO, so the mutexes are still valid when waiting to close. setDevice(-1, 0); @@ -69,6 +66,7 @@ struct AudioInterfaceIO : AudioIO { else { // Timed out, fill output with zeros memset(output, 0, frames * numOutputs * sizeof(float)); + debug("Audio Interface IO underflow"); } } @@ -199,6 +197,7 @@ void AudioInterface::step() { } else { // Give up on pushing output + debug("Audio Interface underflow"); } } } diff --git a/src/bridge.cpp b/src/bridge.cpp index ca15756d..2c220cf1 100644 --- a/src/bridge.cpp +++ b/src/bridge.cpp @@ -138,8 +138,7 @@ struct BridgeClientConnection { } } 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 output[audioBufferLength]; memset(output, 0, sizeof(output)); @@ -185,12 +184,12 @@ struct BridgeClientConnection { 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)) return; if (!audioListeners[channel]) return; - audioListeners[channel]->processStream(input, output, length); + audioListeners[channel]->processStream(input, output, frames); } }; @@ -198,6 +197,7 @@ struct BridgeClientConnection { static void clientRun(int client) { int err; + BridgeClientConnection connection; // // Get client address // struct sockaddr_in addr; @@ -219,7 +219,12 @@ static void clientRun(int client) { setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &flag, sizeof(int)); #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) { uint8_t buffer[RECV_BUFFER_SIZE]; @@ -234,11 +239,12 @@ static void clientRun(int client) { connection.recv(buffer, received); } - info("Bridge client closed"); err = close(client); (void) err; + info("Bridge client closed"); } + static void serverRun() { int err; @@ -265,6 +271,10 @@ static void serverRun() { hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; err = getaddrinfo(NULL, "5000", &hints, &result); + if (err) { + warn("Could not get Bridge server address"); + return; + } defer({ freeaddrinfo(result); }); @@ -272,13 +282,9 @@ static void serverRun() { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); 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); #endif - if (err) { - warn("Could not get Bridge server address"); - return; - } // Open socket #ifdef ARCH_WIN @@ -317,18 +323,15 @@ static void serverRun() { // Make server non-blocking #ifdef ARCH_WIN - { - unsigned long mode = 1; - ioctlsocket(server, FIONBIO, &mode); - } + unsigned long blockingMode = 1; + ioctlsocket(server, FIONBIO, &blockingMode); #else err = fcntl(server, F_SETFL, fcntl(server, F_GETFL, 0) | O_NONBLOCK); #endif + // Accept clients serverQuit = false; while (!serverQuit) { - // Accept client socket - int client = accept(server, NULL, NULL); if (client < 0) { // Wait a bit before attempting to accept another client