Skip to content

Commit

Permalink
mice optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor committed May 15, 2024
1 parent 649b723 commit 57dff23
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/core/networking/VRNetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class VRNetworkClient : public std::enable_shared_from_this<VRNetworkClient> {

virtual void onMessage( function<string(string)> f ) {};
virtual void connect(string host, int port) {};
virtual bool isConnected(string host, int port) { return false; };
virtual void send(const string& message, string guard = "", bool verbose = false) {};

string getName();
Expand Down
6 changes: 4 additions & 2 deletions src/core/networking/mqtt/VRMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,16 @@ void VRMQTTClient::disconnect() {
if (s) s->queueJob(f, 0, 60); // delay in N frames
}

bool VRMQTTClient::isConnected(string host, int port) { return bool("mqtt://"+host+":"+toString(port) == uri); }

void VRMQTTClient::connect(string host, int port) { // connect("broker.hivemq.com", 1883)
//cout << "VRMQTTClient::connect " << this << endl;
if (data->connecting) return;
if (data->mqttConnected) return;
data->connecting = true;

string address = "mqtt://"+host+":"+toString(port);
data->s_url = address;
uri = "mqtt://"+host+":"+toString(port);
data->s_url = uri;

mg_mqtt_opts opts = data->basicOpts();
opts.clean = true;
Expand Down
1 change: 1 addition & 0 deletions src/core/networking/mqtt/VRMQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class VRMQTTClient : public VRNetworkClient {

void disconnect();
void connect(string host, int port) override;
bool isConnected(string host, int port) override;
void onMessage( function<string(string)> f ) override;

bool connected();
Expand Down
10 changes: 8 additions & 2 deletions src/core/networking/tcp/VRTCPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ class TCPClient {
}

void runService() { // needed because windows complains..
io_service.run();
try {
io_service.run();
} catch(...) {
cout << "Error in TCPClient::runService!" << endl;
runService();
}
}

public:
Expand All @@ -171,7 +176,7 @@ class TCPClient {
boost::system::error_code _error_code;
socket->shutdown(tcp::socket::shutdown_both, _error_code);
} catch(...) {
;
cout << "Error in TCPClient::close!" << endl;
}

cout << "join service thread" << endl;
Expand Down Expand Up @@ -377,6 +382,7 @@ VRTCPClientPtr VRTCPClient::create(string name) {
}

void VRTCPClient::connect(string host, int port) { client->connect(host, port); uri = host+":"+toString(port); }
bool VRTCPClient::isConnected(string host, int port) { return bool(host+":"+toString(port) == uri); }
void VRTCPClient::connect(string host) { client->connect(host); uri = host; }
void VRTCPClient::setGuard(string guard) { client->setGuard(guard); }
void VRTCPClient::send(const string& message, string guard, bool verbose) { client->send(message, guard, verbose); }
Expand Down
1 change: 1 addition & 0 deletions src/core/networking/tcp/VRTCPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class VRTCPClient : public VRNetworkClient {
void setGuard( string g );

void connect(string host, int port) override;
bool isConnected(string host, int port) override;
void connect(string uri);
void connectToPeer(int localPort, string remoteIP, int remotePort);
void send(const string& message, string guard = "", bool verbose = false) override;
Expand Down
1 change: 1 addition & 0 deletions src/core/networking/udp/VRUDPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,5 @@ VRUDPClientPtr VRUDPClient::create(string name) {

void VRUDPClient::onMessage( function<string(string)> f ) { client->onMessage(f); }
void VRUDPClient::connect(string host, int port) { client->connect(host, port); uri = host+":"+toString(port); }
bool VRUDPClient::isConnected(string host, int port) { return bool(host+":"+toString(port) == uri); }
void VRUDPClient::send(const string& message, string guard, bool verbose) { client->send(message+guard, verbose); }
1 change: 1 addition & 0 deletions src/core/networking/udp/VRUDPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class VRUDPClient : public VRNetworkClient {
void onMessage( function<string(string)> f ) override;

void connect(string host, int port) override;
bool isConnected(string host, int port) override;
void send(const string& message, string guard = "", bool verbose = false) override;
};

Expand Down
14 changes: 10 additions & 4 deletions src/core/scene/sound/VRMicrophone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ using duration = std::chrono::duration<T, std::milli>;
}*/

void VRMicrophone::startRecordingThread() {
if (recording) return;

auto recordCb = [&]() {
recording = true;
cout << "VRMicrophone::startRecordingThread" << endl;

while (doStream) {
auto frame = fetchDevicePacket();
Expand All @@ -188,8 +191,11 @@ void VRMicrophone::startRecordingThread() {
}

void VRMicrophone::startStreamingThread(string method) {
if (streaming) return;

auto streamCb = [&](string method) {
streaming = true;
cout << "VRMicrophone::startStreamingThread" << endl;

while (doStream) {
bool enoughInitialQueuedFrames = bool(queuedFrames >= queueSize);
Expand Down Expand Up @@ -232,8 +238,8 @@ void VRMicrophone::startStreamingOver(VRNetworkClientPtr client, string method)
doStream = true;
recordingSound->addOutStreamClient(client, method);

if (!recording) startRecordingThread();
if (!streaming) startStreamingThread(method);
startRecordingThread();
startStreamingThread(method);
}

void VRMicrophone::startStreaming(string address, int port, string method) {
Expand All @@ -242,8 +248,8 @@ void VRMicrophone::startStreaming(string address, int port, string method) {
doStream = true;
recordingSound->setupOutStream(address, port, method);

if (!recording) startRecordingThread();
if (!streaming) startStreamingThread(method);
startRecordingThread();
startStreamingThread(method);
}

bool VRMicrophone::isStreaming() { return doStream; }
Expand Down
1 change: 1 addition & 0 deletions src/core/scene/sound/VRSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ bool VRSound::addOutStreamClient(VRNetworkClientPtr client, string method) {
}

bool VRSound::setupOutStream(string url, int port, string method) { // TODO: make a udpClients map instead of vector
for (auto c : udpClients) if (c->isConnected(url, port)) return true;
auto cli = VRUDPClient::create("sound-out");
cli->connect(url, port);
return addOutStreamClient(cli, method);
Expand Down

0 comments on commit 57dff23

Please sign in to comment.