-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update C++ example implementation (#491)
### Public-Facing Changes Update C++ example implementation ### Description - Allows choosing between boost asio and standalone asio (fixes #483) - Adds support for `asset` capability - Some refactoring --------- Co-authored-by: Jacob Bandes-Storch <[email protected]>
- Loading branch information
Showing
21 changed files
with
964 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
cpp/foxglove-websocket/include/foxglove/websocket/base64.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace foxglove { | ||
|
||
std::string base64Encode(const std::string_view& input); | ||
|
||
std::vector<unsigned char> base64Decode(const std::string& input); | ||
|
||
} // namespace foxglove |
81 changes: 81 additions & 0 deletions
81
cpp/foxglove-websocket/include/foxglove/websocket/callback_queue.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#pragma once | ||
|
||
#include <atomic> | ||
#include <condition_variable> | ||
#include <deque> | ||
#include <functional> | ||
#include <mutex> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include "websocket_logging.hpp" | ||
|
||
namespace foxglove { | ||
|
||
class CallbackQueue { | ||
public: | ||
CallbackQueue(LogCallback logCallback, size_t numThreads = 1) | ||
: _logCallback(logCallback) | ||
, _quit(false) { | ||
for (size_t i = 0; i < numThreads; ++i) { | ||
_workerThreads.push_back(std::thread(&CallbackQueue::doWork, this)); | ||
} | ||
} | ||
|
||
~CallbackQueue() { | ||
stop(); | ||
} | ||
|
||
void stop() { | ||
_quit = true; | ||
_cv.notify_all(); | ||
for (auto& thread : _workerThreads) { | ||
thread.join(); | ||
} | ||
} | ||
|
||
void addCallback(std::function<void(void)> cb) { | ||
if (_quit) { | ||
return; | ||
} | ||
std::unique_lock<std::mutex> lock(_mutex); | ||
_callbackQueue.push_back(cb); | ||
_cv.notify_one(); | ||
} | ||
|
||
private: | ||
void doWork() { | ||
while (!_quit) { | ||
std::unique_lock<std::mutex> lock(_mutex); | ||
_cv.wait(lock, [this] { | ||
return (_quit || !_callbackQueue.empty()); | ||
}); | ||
if (_quit) { | ||
break; | ||
} else if (!_callbackQueue.empty()) { | ||
std::function<void(void)> cb = _callbackQueue.front(); | ||
_callbackQueue.pop_front(); | ||
lock.unlock(); | ||
try { | ||
cb(); | ||
} catch (const std::exception& ex) { | ||
// Should never get here if we catch all exceptions in the callbacks. | ||
const std::string msg = | ||
std::string("Caught unhandled exception in calback_queue") + ex.what(); | ||
_logCallback(WebSocketLogLevel::Error, msg.c_str()); | ||
} catch (...) { | ||
_logCallback(WebSocketLogLevel::Error, "Caught unhandled exception in calback_queue"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
LogCallback _logCallback; | ||
std::atomic<bool> _quit; | ||
std::mutex _mutex; | ||
std::condition_variable _cv; | ||
std::deque<std::function<void(void)>> _callbackQueue; | ||
std::vector<std::thread> _workerThreads; | ||
}; | ||
|
||
} // namespace foxglove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.