mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2024-11-13 22:12:51 +01:00
New custom network lib + half finished rigctl server
This commit is contained in:
parent
218844ed47
commit
def6036b30
@ -36,6 +36,7 @@ option(OPT_BUILD_WEATHER_SAT_DECODER "Build the HRPT decoder module (no dependen
|
|||||||
option(OPT_BUILD_DISCORD_PRESENCE "Build the Discord Rich Presence module" ON)
|
option(OPT_BUILD_DISCORD_PRESENCE "Build the Discord Rich Presence module" ON)
|
||||||
option(OPT_BUILD_FREQUENCY_MANAGER "Build the Frequency Manager module" ON)
|
option(OPT_BUILD_FREQUENCY_MANAGER "Build the Frequency Manager module" ON)
|
||||||
option(OPT_BUILD_RECORDER "Audio and baseband recorder" ON)
|
option(OPT_BUILD_RECORDER "Audio and baseband recorder" ON)
|
||||||
|
option(OPT_BUILD_RIGCTL_SERVER "Rigctl backend for controlling SDR++ with software like gpredict" ON)
|
||||||
|
|
||||||
# Compiler arguments for each platform
|
# Compiler arguments for each platform
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
@ -145,6 +146,10 @@ if (OPT_BUILD_RECORDER)
|
|||||||
add_subdirectory("recorder")
|
add_subdirectory("recorder")
|
||||||
endif (OPT_BUILD_RECORDER)
|
endif (OPT_BUILD_RECORDER)
|
||||||
|
|
||||||
|
if (OPT_BUILD_RIGCTL_SERVER)
|
||||||
|
add_subdirectory("rigctl_server")
|
||||||
|
endif (OPT_BUILD_RIGCTL_SERVER)
|
||||||
|
|
||||||
add_executable(sdrpp "src/main.cpp" "win32/resources.rc")
|
add_executable(sdrpp "src/main.cpp" "win32/resources.rc")
|
||||||
target_link_libraries(sdrpp PRIVATE sdrpp_core)
|
target_link_libraries(sdrpp PRIVATE sdrpp_core)
|
||||||
|
|
||||||
|
@ -121,5 +121,5 @@ Please follow this guide to properly format the JSON files for custom color maps
|
|||||||
# Best Practices
|
# Best Practices
|
||||||
|
|
||||||
* All additions and/or bug fixes to the core must not add additional dependencies.
|
* All additions and/or bug fixes to the core must not add additional dependencies.
|
||||||
|
|
||||||
* Use VSCode for development, VS seems to cause issues.
|
* Use VSCode for development, VS seems to cause issues.
|
||||||
|
* DO NOT use libboost for any code meant for this repository
|
@ -48,6 +48,9 @@ if (MSVC)
|
|||||||
find_package(FFTW3f CONFIG REQUIRED)
|
find_package(FFTW3f CONFIG REQUIRED)
|
||||||
target_link_libraries(sdrpp_core PUBLIC FFTW3::fftw3f)
|
target_link_libraries(sdrpp_core PUBLIC FFTW3::fftw3f)
|
||||||
|
|
||||||
|
# WinSock2
|
||||||
|
target_link_libraries(sdrpp_core PUBLIC wsock32 ws2_32)
|
||||||
|
|
||||||
else()
|
else()
|
||||||
find_package(PkgConfig)
|
find_package(PkgConfig)
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
|
@ -1,2 +1,381 @@
|
|||||||
|
#pragma once
|
||||||
#include <utils/networking.h>
|
#include <utils/networking.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
namespace net {
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
extern bool winsock_init = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ConnClass::ConnClass(Socket sock) {
|
||||||
|
_sock = sock;
|
||||||
|
connectionOpen = true;
|
||||||
|
readWorkerThread = std::thread(&ConnClass::readWorker, this);
|
||||||
|
writeWorkerThread = std::thread(&ConnClass::writeWorker, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
ConnClass::~ConnClass() {
|
||||||
|
ConnClass::close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnClass::close() {
|
||||||
|
std::lock_guard lck(closeMtx);
|
||||||
|
// Set stopWorkers to true
|
||||||
|
{
|
||||||
|
std::lock_guard lck1(readQueueMtx);
|
||||||
|
std::lock_guard lck2(writeQueueMtx);
|
||||||
|
stopWorkers = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify the workers of the change
|
||||||
|
readQueueCnd.notify_all();
|
||||||
|
writeQueueCnd.notify_all();
|
||||||
|
|
||||||
|
if (connectionOpen) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
closesocket(_sock);
|
||||||
|
#else
|
||||||
|
::close(_sock);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for the theads to terminate
|
||||||
|
if (readWorkerThread.joinable()) { readWorkerThread.join(); }
|
||||||
|
if (writeWorkerThread.joinable()) { writeWorkerThread.join(); }
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard lck(connectionOpenMtx);
|
||||||
|
connectionOpen = false;
|
||||||
|
}
|
||||||
|
connectionOpenCnd.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConnClass::isOpen() {
|
||||||
|
return connectionOpen;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnClass::waitForEnd() {
|
||||||
|
std::unique_lock lck(readQueueMtx);
|
||||||
|
connectionOpenCnd.wait(lck, [this](){ return !connectionOpen; });
|
||||||
|
}
|
||||||
|
|
||||||
|
int ConnClass::read(int count, uint8_t* buf) {
|
||||||
|
assert(connectionOpen);
|
||||||
|
std::lock_guard lck(readMtx);
|
||||||
|
#ifdef _WIN32
|
||||||
|
int ret = recv(_sock, (char*)buf, count, 0);
|
||||||
|
#else
|
||||||
|
int ret = ::read(_sock, buf, count);
|
||||||
|
#endif
|
||||||
|
if (ret <= 0) {
|
||||||
|
{
|
||||||
|
std::lock_guard lck(connectionOpenMtx);
|
||||||
|
connectionOpen = false;
|
||||||
|
}
|
||||||
|
connectionOpenCnd.notify_all();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConnClass::write(int count, uint8_t* buf) {
|
||||||
|
assert(connectionOpen);
|
||||||
|
std::lock_guard lck(writeMtx);
|
||||||
|
#ifdef _WIN32
|
||||||
|
int ret = send(_sock, (char*)buf, count, 0);
|
||||||
|
#else
|
||||||
|
int ret = ::write(_sock, buf, count);
|
||||||
|
#endif
|
||||||
|
if (ret <= 0) {
|
||||||
|
{
|
||||||
|
std::lock_guard lck(connectionOpenMtx);
|
||||||
|
connectionOpen = false;
|
||||||
|
}
|
||||||
|
connectionOpenCnd.notify_all();
|
||||||
|
}
|
||||||
|
return (ret > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnClass::readAsync(int count, uint8_t* buf, void (*handler)(int count, uint8_t* buf, void* ctx), void* ctx) {
|
||||||
|
assert(connectionOpen);
|
||||||
|
// Create entry
|
||||||
|
ConnReadEntry entry;
|
||||||
|
entry.count = count;
|
||||||
|
entry.buf = buf;
|
||||||
|
entry.handler = handler;
|
||||||
|
entry.ctx = ctx;
|
||||||
|
|
||||||
|
// Add entry to queue
|
||||||
|
{
|
||||||
|
std::lock_guard lck(readQueueMtx);
|
||||||
|
readQueue.push_back(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify read worker
|
||||||
|
readQueueCnd.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnClass::writeAsync(int count, uint8_t* buf) {
|
||||||
|
assert(connectionOpen);
|
||||||
|
// Create entry
|
||||||
|
ConnWriteEntry entry;
|
||||||
|
entry.count = count;
|
||||||
|
entry.buf = buf;
|
||||||
|
|
||||||
|
// Add entry to queue
|
||||||
|
{
|
||||||
|
std::lock_guard lck(writeQueueMtx);
|
||||||
|
writeQueue.push_back(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify write worker
|
||||||
|
writeQueueCnd.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnClass::readWorker() {
|
||||||
|
while (true) {
|
||||||
|
// Wait for wakeup and exit if it's for terminating the thread
|
||||||
|
std::unique_lock lck(readQueueMtx);
|
||||||
|
readQueueCnd.wait(lck, [this](){ return (readQueue.size() > 0 || stopWorkers); });
|
||||||
|
if (stopWorkers || !connectionOpen) { return; }
|
||||||
|
|
||||||
|
// Pop first element off the list
|
||||||
|
ConnReadEntry entry = readQueue[0];
|
||||||
|
readQueue.erase(readQueue.begin());
|
||||||
|
lck.unlock();
|
||||||
|
|
||||||
|
// Read from socket and send data to the handler
|
||||||
|
int ret = read(entry.count, entry.buf);
|
||||||
|
if (ret <= 0) {
|
||||||
|
{
|
||||||
|
std::lock_guard lck(connectionOpenMtx);
|
||||||
|
connectionOpen = false;
|
||||||
|
}
|
||||||
|
connectionOpenCnd.notify_all();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
entry.handler(ret, entry.buf, entry.ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnClass::writeWorker() {
|
||||||
|
while (true) {
|
||||||
|
// Wait for wakeup and exit if it's for terminating the thread
|
||||||
|
std::unique_lock lck(writeQueueMtx);
|
||||||
|
writeQueueCnd.wait(lck, [this](){ return (writeQueue.size() > 0 || stopWorkers); });
|
||||||
|
if (stopWorkers || !connectionOpen) { return; }
|
||||||
|
|
||||||
|
// Pop first element off the list
|
||||||
|
ConnWriteEntry entry = writeQueue[0];
|
||||||
|
writeQueue.erase(writeQueue.begin());
|
||||||
|
lck.unlock();
|
||||||
|
|
||||||
|
// Write to socket
|
||||||
|
if (!write(entry.count, entry.buf)) {
|
||||||
|
{
|
||||||
|
std::lock_guard lck(connectionOpenMtx);
|
||||||
|
connectionOpen = false;
|
||||||
|
}
|
||||||
|
connectionOpenCnd.notify_all();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ListenerClass::ListenerClass(Socket listenSock) {
|
||||||
|
sock = listenSock;
|
||||||
|
listening = true;
|
||||||
|
acceptWorkerThread = std::thread(&ListenerClass::worker, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
ListenerClass::~ListenerClass() {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
Conn ListenerClass::accept() {
|
||||||
|
assert(listening);
|
||||||
|
std::lock_guard lck(acceptMtx);
|
||||||
|
Socket _sock;
|
||||||
|
|
||||||
|
// Accept socket
|
||||||
|
_sock = ::accept(sock, NULL, NULL);
|
||||||
|
if (_sock < 0) {
|
||||||
|
listening = false;
|
||||||
|
throw std::runtime_error("Could not bind socket");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Conn(new ConnClass(_sock));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListenerClass::acceptAsync(void (*handler)(Conn conn, void* ctx), void* ctx) {
|
||||||
|
assert(listening);
|
||||||
|
// Create entry
|
||||||
|
ListenerAcceptEntry entry;
|
||||||
|
entry.handler = handler;
|
||||||
|
entry.ctx = ctx;
|
||||||
|
|
||||||
|
// Add entry to queue
|
||||||
|
{
|
||||||
|
std::lock_guard lck(acceptQueueMtx);
|
||||||
|
acceptQueue.push_back(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify write worker
|
||||||
|
acceptQueueCnd.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListenerClass::close() {
|
||||||
|
{
|
||||||
|
std::lock_guard lck(acceptQueueMtx);
|
||||||
|
stopWorker = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listening) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
closesocket(sock);
|
||||||
|
#else
|
||||||
|
::close(sock);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
acceptQueueCnd.notify_all();
|
||||||
|
if (acceptWorkerThread.joinable()) { acceptWorkerThread.join(); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
listening = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ListenerClass::isListening() {
|
||||||
|
return listening;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListenerClass::worker() {
|
||||||
|
while (true) {
|
||||||
|
// Wait for wakeup and exit if it's for terminating the thread
|
||||||
|
std::unique_lock lck(acceptQueueMtx);
|
||||||
|
acceptQueueCnd.wait(lck, [this](){ return (acceptQueue.size() > 0 || stopWorker); });
|
||||||
|
if (stopWorker || !listening) { return; }
|
||||||
|
|
||||||
|
// Pop first element off the list
|
||||||
|
ListenerAcceptEntry entry = acceptQueue[0];
|
||||||
|
acceptQueue.erase(acceptQueue.begin());
|
||||||
|
lck.unlock();
|
||||||
|
|
||||||
|
// Read from socket and send data to the handler
|
||||||
|
try {
|
||||||
|
Conn client = accept();
|
||||||
|
if (!client) {
|
||||||
|
listening = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
entry.handler(std::move(client), entry.ctx);
|
||||||
|
}
|
||||||
|
catch (std::exception e) {
|
||||||
|
listening = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Conn connect(Protocol proto, std::string host, uint16_t port) {
|
||||||
|
Socket sock;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Initilize WinSock2
|
||||||
|
if (!winsock_init) {
|
||||||
|
WSADATA wsa;
|
||||||
|
if (WSAStartup(MAKEWORD(2,2),&wsa)) {
|
||||||
|
throw std::runtime_error("Could not initialize WinSock2");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
winsock_init = true;
|
||||||
|
}
|
||||||
|
assert(winsock_init);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Create a socket
|
||||||
|
sock = socket(AF_INET, SOCK_STREAM, (proto == PROTO_TCP) ? IPPROTO_TCP : IPPROTO_UDP);
|
||||||
|
if (sock < 0) {
|
||||||
|
throw std::runtime_error("Could not create socket");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get address from hostname/ip
|
||||||
|
hostent* remoteHost = gethostbyname(host.c_str());
|
||||||
|
if (remoteHost == NULL || remoteHost->h_addr_list[0] == NULL) {
|
||||||
|
throw std::runtime_error("Could get address from host");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
uint32_t* naddr = (uint32_t*)remoteHost->h_addr_list[0];
|
||||||
|
|
||||||
|
// Create host address
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
addr.sin_addr.s_addr = *naddr;
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
// Connect to host
|
||||||
|
if (::connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||||
|
throw std::runtime_error("Could not connect to host");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Conn(new ConnClass(sock));
|
||||||
|
}
|
||||||
|
|
||||||
|
Listener listen(Protocol proto, std::string host, uint16_t port) {
|
||||||
|
Socket listenSock;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Initilize WinSock2
|
||||||
|
if (!winsock_init) {
|
||||||
|
WSADATA wsa;
|
||||||
|
if (WSAStartup(MAKEWORD(2,2),&wsa)) {
|
||||||
|
throw std::runtime_error("Could not initialize WinSock2");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
winsock_init = true;
|
||||||
|
}
|
||||||
|
assert(winsock_init);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Create a socket
|
||||||
|
listenSock = socket(AF_INET, SOCK_STREAM, (proto == PROTO_TCP) ? IPPROTO_TCP : IPPROTO_UDP);
|
||||||
|
if (listenSock < 0) {
|
||||||
|
throw std::runtime_error("Could not create socket");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get address from hostname/ip
|
||||||
|
hostent* remoteHost = gethostbyname(host.c_str());
|
||||||
|
if (remoteHost == NULL || remoteHost->h_addr_list[0] == NULL) {
|
||||||
|
throw std::runtime_error("Could get address from host");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
uint32_t* naddr = (uint32_t*)remoteHost->h_addr_list[0];
|
||||||
|
|
||||||
|
// Create host address
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
addr.sin_addr.s_addr = *naddr;
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
// Bind socket
|
||||||
|
if (bind(listenSock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||||
|
throw std::runtime_error("Could not bind socket");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen
|
||||||
|
if (::listen(listenSock, SOMAXCONN) != 0) {
|
||||||
|
throw std::runtime_error("Could not listen");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Listener(new ListenerClass(listenSock));
|
||||||
|
}
|
||||||
|
}
|
@ -1,66 +1,128 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <thread>
|
|
||||||
#include <chrono>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
struct TCPAsyncRead {
|
#ifdef _WIN32
|
||||||
int timeout;
|
#include <WinSock2.h>
|
||||||
|
#include <WS2tcpip.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace net {
|
||||||
|
#ifdef _WIN32
|
||||||
|
typedef SOCKET Socket;
|
||||||
|
#else
|
||||||
|
typedef int Socket;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum Protocol {
|
||||||
|
PROTO_TCP,
|
||||||
|
PROTO_UDP
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ConnReadEntry {
|
||||||
int count;
|
int count;
|
||||||
void (*handler)(int count, char* data, void* ctx);
|
uint8_t* buf;
|
||||||
|
void (*handler)(int count, uint8_t* buf, void* ctx);
|
||||||
void* ctx;
|
void* ctx;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TCPAsyncWrite {
|
struct ConnWriteEntry {
|
||||||
int timeoutMs;
|
|
||||||
int count;
|
int count;
|
||||||
char* data;
|
uint8_t* buf;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
class ConnClass {
|
||||||
NET_ERR_OK = 0,
|
|
||||||
NET_ERR_TIMEOUT = -1,
|
|
||||||
NET_ERR_SYSTEM = -2
|
|
||||||
};
|
|
||||||
|
|
||||||
class TCPClient {
|
|
||||||
public:
|
public:
|
||||||
TCPClient();
|
ConnClass(Socket sock);
|
||||||
~TCPClient();
|
~ConnClass();
|
||||||
|
|
||||||
bool connect(std::string host, int port);
|
|
||||||
bool disconnect();
|
|
||||||
|
|
||||||
int enableAsync();
|
|
||||||
int disableAsync();
|
|
||||||
|
|
||||||
bool isAsync();
|
|
||||||
|
|
||||||
int read(char* data, int count, int timeout = -1);
|
|
||||||
int write(char* data, int count, int timeout = -1);
|
|
||||||
|
|
||||||
int asyncRead(int count, void (*handler)(int count, char* data, void* ctx), int timeout = -1);
|
|
||||||
int asyncWrite(char* data, int count, int timeout = -1);
|
|
||||||
|
|
||||||
|
void close();
|
||||||
bool isOpen();
|
bool isOpen();
|
||||||
|
void waitForEnd();
|
||||||
|
|
||||||
int close();
|
int read(int count, uint8_t* buf);
|
||||||
|
bool write(int count, uint8_t* buf);
|
||||||
|
void readAsync(int count, uint8_t* buf, void (*handler)(int count, uint8_t* buf, void* ctx), void* ctx);
|
||||||
|
void writeAsync(int count, uint8_t* buf);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void readWorker();
|
void readWorker();
|
||||||
void writeWorker();
|
void writeWorker();
|
||||||
|
|
||||||
|
bool stopWorkers = false;
|
||||||
|
bool connectionOpen = false;
|
||||||
|
|
||||||
|
std::mutex readMtx;
|
||||||
|
std::mutex writeMtx;
|
||||||
std::mutex readQueueMtx;
|
std::mutex readQueueMtx;
|
||||||
std::vector<TCPAsyncRead> readQueue;
|
|
||||||
|
|
||||||
std::mutex writeQueueMtx;
|
std::mutex writeQueueMtx;
|
||||||
std::vector<TCPAsyncWrite> writeQueue;
|
std::mutex connectionOpenMtx;
|
||||||
|
std::mutex closeMtx;
|
||||||
|
std::condition_variable readQueueCnd;
|
||||||
|
std::condition_variable writeQueueCnd;
|
||||||
|
std::condition_variable connectionOpenCnd;
|
||||||
|
std::vector<ConnReadEntry> readQueue;
|
||||||
|
std::vector<ConnWriteEntry> writeQueue;
|
||||||
std::thread readWorkerThread;
|
std::thread readWorkerThread;
|
||||||
std::thread writeWorkerThread;
|
std::thread writeWorkerThread;
|
||||||
|
|
||||||
bool open = false;
|
Socket _sock;
|
||||||
bool async = false;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef std::unique_ptr<ConnClass> Conn;
|
||||||
|
|
||||||
|
struct ListenerAcceptEntry {
|
||||||
|
void (*handler)(Conn conn, void* ctx);
|
||||||
|
void* ctx;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ListenerClass {
|
||||||
|
public:
|
||||||
|
ListenerClass(Socket listenSock);
|
||||||
|
~ListenerClass();
|
||||||
|
|
||||||
|
Conn accept();
|
||||||
|
void acceptAsync(void (*handler)(Conn conn, void* ctx), void* ctx);
|
||||||
|
|
||||||
|
void close();
|
||||||
|
bool isListening();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void worker();
|
||||||
|
|
||||||
|
bool listening = false;
|
||||||
|
bool stopWorker = false;
|
||||||
|
|
||||||
|
std::mutex acceptMtx;
|
||||||
|
std::mutex acceptQueueMtx;
|
||||||
|
std::condition_variable acceptQueueCnd;
|
||||||
|
std::vector<ListenerAcceptEntry> acceptQueue;
|
||||||
|
std::thread acceptWorkerThread;
|
||||||
|
|
||||||
|
Socket sock;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::unique_ptr<ListenerClass> Listener;
|
||||||
|
|
||||||
|
Conn connect(Protocol proto, std::string host, uint16_t port);
|
||||||
|
Listener listen(Protocol proto, std::string host, uint16_t port);
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
extern bool winsock_init;
|
||||||
|
#endif
|
||||||
|
}
|
@ -4,15 +4,17 @@ project(demo)
|
|||||||
if (MSVC)
|
if (MSVC)
|
||||||
set(CMAKE_CXX_FLAGS "-O2 /std:c++17 /EHsc")
|
set(CMAKE_CXX_FLAGS "-O2 /std:c++17 /EHsc")
|
||||||
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||||
set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -Wno-unused-command-line-argument -undefined dynamic_lookup -fPIC")
|
set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -Wno-unused-command-line-argument -undefined dynamic_lookup")
|
||||||
else ()
|
else ()
|
||||||
set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -fPIC")
|
set(CMAKE_CXX_FLAGS "-O3 -std=c++17")
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
file(GLOB SRC "src/*.cpp")
|
file(GLOB SRC "src/*.cpp")
|
||||||
|
|
||||||
include_directories("src/")
|
include_directories("src/")
|
||||||
|
|
||||||
add_library(demo SHARED ${SRC})
|
add_library(demo SHARED ${SRC})
|
||||||
|
target_link_libraries(demo PRIVATE sdrpp_core)
|
||||||
set_target_properties(demo PROPERTIES PREFIX "")
|
set_target_properties(demo PROPERTIES PREFIX "")
|
||||||
|
|
||||||
# Install directives
|
# Install directives
|
||||||
|
@ -18,7 +18,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
~DemoModule() {
|
~DemoModule() {
|
||||||
|
gui::menu.removeEntry(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void enable() {
|
void enable() {
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
#include <options.h>
|
#include <options.h>
|
||||||
#include <gui/widgets/folder_select.h>
|
#include <gui/widgets/folder_select.h>
|
||||||
|
#include <recorder_interface.h>
|
||||||
|
#include <core.h>
|
||||||
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
||||||
|
|
||||||
SDRPP_MOD_INFO {
|
SDRPP_MOD_INFO {
|
||||||
@ -55,7 +57,7 @@ public:
|
|||||||
|
|
||||||
// Create config if it doesn't exist
|
// Create config if it doesn't exist
|
||||||
if (!config.conf.contains(name)) {
|
if (!config.conf.contains(name)) {
|
||||||
config.conf[name]["mode"] = 1;
|
config.conf[name]["mode"] = RECORDER_MODE_AUDIO;
|
||||||
config.conf[name]["recPath"] = "%ROOT%/recordings";
|
config.conf[name]["recPath"] = "%ROOT%/recordings";
|
||||||
config.conf[name]["audioStream"] = "Radio";
|
config.conf[name]["audioStream"] = "Radio";
|
||||||
created = true;
|
created = true;
|
||||||
@ -85,12 +87,15 @@ public:
|
|||||||
refreshStreams();
|
refreshStreams();
|
||||||
|
|
||||||
gui::menu.registerEntry(name, menuHandler, this);
|
gui::menu.registerEntry(name, menuHandler, this);
|
||||||
|
core::modComManager.registerInterface("recorder", name, moduleInterfaceHandler, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~RecorderModule() {
|
~RecorderModule() {
|
||||||
|
core::modComManager.unregisterInterface(name);
|
||||||
|
|
||||||
// Stop recording
|
// Stop recording
|
||||||
if (recording) {
|
if (recording) {
|
||||||
if (recMode == 1) {
|
if (recMode == RECORDER_MODE_AUDIO) {
|
||||||
audioSplit.unbindStream(&audioHandlerStream);
|
audioSplit.unbindStream(&audioHandlerStream);
|
||||||
audioHandler.stop();
|
audioHandler.stop();
|
||||||
audioWriter->close();
|
audioWriter->close();
|
||||||
@ -171,15 +176,15 @@ private:
|
|||||||
if (_this->recording) { style::beginDisabled(); }
|
if (_this->recording) { style::beginDisabled(); }
|
||||||
ImGui::BeginGroup();
|
ImGui::BeginGroup();
|
||||||
ImGui::Columns(2, CONCAT("AirspyGainModeColumns##_", _this->name), false);
|
ImGui::Columns(2, CONCAT("AirspyGainModeColumns##_", _this->name), false);
|
||||||
if (ImGui::RadioButton(CONCAT("Baseband##_recmode_", _this->name), _this->recMode == 0)) {
|
if (ImGui::RadioButton(CONCAT("Baseband##_recmode_", _this->name), _this->recMode == RECORDER_MODE_BASEBAND)) {
|
||||||
_this->recMode = 0;
|
_this->recMode = RECORDER_MODE_BASEBAND;
|
||||||
config.acquire();
|
config.acquire();
|
||||||
config.conf[_this->name]["mode"] = _this->recMode;
|
config.conf[_this->name]["mode"] = _this->recMode;
|
||||||
config.release(true);
|
config.release(true);
|
||||||
}
|
}
|
||||||
ImGui::NextColumn();
|
ImGui::NextColumn();
|
||||||
if (ImGui::RadioButton(CONCAT("Audio##_recmode_", _this->name), _this->recMode == 1)) {
|
if (ImGui::RadioButton(CONCAT("Audio##_recmode_", _this->name), _this->recMode == RECORDER_MODE_AUDIO)) {
|
||||||
_this->recMode = 1;
|
_this->recMode = RECORDER_MODE_AUDIO;
|
||||||
config.acquire();
|
config.acquire();
|
||||||
config.conf[_this->name]["mode"] = _this->recMode;
|
config.conf[_this->name]["mode"] = _this->recMode;
|
||||||
config.release(true);
|
config.release(true);
|
||||||
@ -198,7 +203,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mode specific menu
|
// Mode specific menu
|
||||||
if (_this->recMode == 1) {
|
if (_this->recMode == RECORDER_MODE_AUDIO) {
|
||||||
_this->audioMenu(menuColumnWidth);
|
_this->audioMenu(menuColumnWidth);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -210,29 +215,15 @@ private:
|
|||||||
if (!folderSelect.pathIsValid()) { style::beginDisabled(); }
|
if (!folderSelect.pathIsValid()) { style::beginDisabled(); }
|
||||||
if (!recording) {
|
if (!recording) {
|
||||||
if (ImGui::Button(CONCAT("Record##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
if (ImGui::Button(CONCAT("Record##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
||||||
samplesWritten = 0;
|
std::lock_guard lck(recMtx);
|
||||||
std::string expandedPath = expandString(folderSelect.path + genFileName("/baseband_", false));
|
startRecording();
|
||||||
sampleRate = sigpath::signalPath.getSampleRate();
|
|
||||||
basebandWriter = new WavWriter(expandedPath, 16, 2, sigpath::signalPath.getSampleRate());
|
|
||||||
if (basebandWriter->isOpen()) {
|
|
||||||
basebandHandler.start();
|
|
||||||
sigpath::signalPath.bindIQStream(&basebandStream);
|
|
||||||
recording = true;
|
|
||||||
spdlog::info("Recording to '{0}'", expandedPath);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
spdlog::error("Could not create '{0}'", expandedPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_Text), "Idle --:--:--");
|
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_Text), "Idle --:--:--");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (ImGui::Button(CONCAT("Stop##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
if (ImGui::Button(CONCAT("Stop##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
||||||
recording = false;
|
std::lock_guard lck(recMtx);
|
||||||
sigpath::signalPath.unbindIQStream(&basebandStream);
|
stopRecording();
|
||||||
basebandHandler.stop();
|
|
||||||
basebandWriter->close();
|
|
||||||
delete basebandWriter;
|
|
||||||
}
|
}
|
||||||
uint64_t seconds = samplesWritten / (uint64_t)sampleRate;
|
uint64_t seconds = samplesWritten / (uint64_t)sampleRate;
|
||||||
time_t diff = seconds;
|
time_t diff = seconds;
|
||||||
@ -279,29 +270,15 @@ private:
|
|||||||
if (!folderSelect.pathIsValid() || selectedStreamName == "") { style::beginDisabled(); }
|
if (!folderSelect.pathIsValid() || selectedStreamName == "") { style::beginDisabled(); }
|
||||||
if (!recording) {
|
if (!recording) {
|
||||||
if (ImGui::Button(CONCAT("Record##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
if (ImGui::Button(CONCAT("Record##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
||||||
samplesWritten = 0;
|
std::lock_guard lck(recMtx);
|
||||||
std::string expandedPath = expandString(folderSelect.path + genFileName("/audio_", true, selectedStreamName));
|
startRecording();
|
||||||
sampleRate = sigpath::sinkManager.getStreamSampleRate(selectedStreamName);
|
|
||||||
audioWriter = new WavWriter(expandedPath, 16, 2, sigpath::sinkManager.getStreamSampleRate(selectedStreamName));
|
|
||||||
if (audioWriter->isOpen()) {
|
|
||||||
recording = true;
|
|
||||||
audioHandler.start();
|
|
||||||
audioSplit.bindStream(&audioHandlerStream);
|
|
||||||
spdlog::info("Recording to '{0}'", expandedPath);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
spdlog::error("Could not create '{0}'", expandedPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_Text), "Idle --:--:--");
|
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_Text), "Idle --:--:--");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (ImGui::Button(CONCAT("Stop##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
if (ImGui::Button(CONCAT("Stop##_recorder_rec_", name), ImVec2(menuColumnWidth, 0))) {
|
||||||
recording = false;
|
std::lock_guard lck(recMtx);
|
||||||
audioSplit.unbindStream(&audioHandlerStream);
|
stopRecording();
|
||||||
audioHandler.stop();
|
|
||||||
audioWriter->close();
|
|
||||||
delete audioWriter;
|
|
||||||
}
|
}
|
||||||
uint64_t seconds = samplesWritten / (uint64_t)sampleRate;
|
uint64_t seconds = samplesWritten / (uint64_t)sampleRate;
|
||||||
time_t diff = seconds;
|
time_t diff = seconds;
|
||||||
@ -325,6 +302,76 @@ private:
|
|||||||
_this->samplesWritten += count;
|
_this->samplesWritten += count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void moduleInterfaceHandler(int code, void* in, void* out, void* ctx) {
|
||||||
|
RecorderModule* _this = (RecorderModule*)ctx;
|
||||||
|
std::lock_guard lck(_this->recMtx);
|
||||||
|
if (code == RECORDER_IFACE_CMD_GET_MODE) {
|
||||||
|
int* _out = (int*)out;
|
||||||
|
*_out = _this->recMode;
|
||||||
|
}
|
||||||
|
else if (code == RECORDER_IFACE_CMD_SET_MODE) {
|
||||||
|
if (_this->recording) { return; }
|
||||||
|
int* _in = (int*)in;
|
||||||
|
_this->recMode = std::clamp<int>(*_in, 0, 1);
|
||||||
|
}
|
||||||
|
else if (code == RECORDER_IFACE_CMD_START) {
|
||||||
|
if (!_this->recording) { _this->startRecording(); }
|
||||||
|
}
|
||||||
|
else if (code == RECORDER_IFACE_CMD_STOP) {
|
||||||
|
if (_this->recording) { _this->stopRecording(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void startRecording() {
|
||||||
|
if (recMode == RECORDER_MODE_BASEBAND) {
|
||||||
|
samplesWritten = 0;
|
||||||
|
std::string expandedPath = expandString(folderSelect.path + genFileName("/baseband_", false));
|
||||||
|
sampleRate = sigpath::signalPath.getSampleRate();
|
||||||
|
basebandWriter = new WavWriter(expandedPath, 16, 2, sigpath::signalPath.getSampleRate());
|
||||||
|
if (basebandWriter->isOpen()) {
|
||||||
|
basebandHandler.start();
|
||||||
|
sigpath::signalPath.bindIQStream(&basebandStream);
|
||||||
|
recording = true;
|
||||||
|
spdlog::info("Recording to '{0}'", expandedPath);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
spdlog::error("Could not create '{0}'", expandedPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (recMode == RECORDER_MODE_AUDIO) {
|
||||||
|
samplesWritten = 0;
|
||||||
|
std::string expandedPath = expandString(folderSelect.path + genFileName("/audio_", true, selectedStreamName));
|
||||||
|
sampleRate = sigpath::sinkManager.getStreamSampleRate(selectedStreamName);
|
||||||
|
audioWriter = new WavWriter(expandedPath, 16, 2, sigpath::sinkManager.getStreamSampleRate(selectedStreamName));
|
||||||
|
if (audioWriter->isOpen()) {
|
||||||
|
recording = true;
|
||||||
|
audioHandler.start();
|
||||||
|
audioSplit.bindStream(&audioHandlerStream);
|
||||||
|
spdlog::info("Recording to '{0}'", expandedPath);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
spdlog::error("Could not create '{0}'", expandedPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopRecording() {
|
||||||
|
if (recMode == 0) {
|
||||||
|
recording = false;
|
||||||
|
sigpath::signalPath.unbindIQStream(&basebandStream);
|
||||||
|
basebandHandler.stop();
|
||||||
|
basebandWriter->close();
|
||||||
|
delete basebandWriter;
|
||||||
|
}
|
||||||
|
else if (recMode == 1) {
|
||||||
|
recording = false;
|
||||||
|
audioSplit.unbindStream(&audioHandlerStream);
|
||||||
|
audioHandler.stop();
|
||||||
|
audioWriter->close();
|
||||||
|
delete audioWriter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
bool enabled = true;
|
bool enabled = true;
|
||||||
@ -341,6 +388,8 @@ private:
|
|||||||
|
|
||||||
dsp::stream<dsp::stereo_t> dummyStream;
|
dsp::stream<dsp::stereo_t> dummyStream;
|
||||||
|
|
||||||
|
std::mutex recMtx;
|
||||||
|
|
||||||
FolderSelect folderSelect;
|
FolderSelect folderSelect;
|
||||||
|
|
||||||
// Audio path
|
// Audio path
|
||||||
|
Loading…
Reference in New Issue
Block a user