Skip to content

Commit

Permalink
more progress on the network source
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreRouma committed Feb 13, 2024
1 parent 95052c3 commit edc08dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ option(OPT_BUILD_FILE_SOURCE "Wav file source" ON)
option(OPT_BUILD_HACKRF_SOURCE "Build HackRF Source Module (Dependencies: libhackrf)" ON)
option(OPT_BUILD_HERMES_SOURCE "Build Hermes Source Module (no dependencies required)" ON)
option(OPT_BUILD_LIMESDR_SOURCE "Build LimeSDR Source Module (Dependencies: liblimesuite)" OFF)
option(OPT_BUILD_NETWORK_SOURCE "Build Network Source Module (no dependencies required)" on)
option(OPT_BUILD_NETWORK_SOURCE "Build Network Source Module (no dependencies required)" ON)
option(OPT_BUILD_PERSEUS_SOURCE "Build Perseus Source Module (Dependencies: libperseus-sdr)" OFF)
option(OPT_BUILD_PLUTOSDR_SOURCE "Build PlutoSDR Source Module (Dependencies: libiio, libad9361)" ON)
option(OPT_BUILD_RFSPACE_SOURCE "Build RFspace Source Module (no dependencies required)" ON)
Expand Down
48 changes: 39 additions & 9 deletions source_modules/network_source/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class NetworkSourceModule : public ModuleManager::Instance {
}

// Define protocols
protocols.define("TCP (Server)", PROTOCOL_TCP_SERVER);
// protocols.define("TCP (Server)", PROTOCOL_TCP_SERVER);
protocols.define("TCP (Client)", PROTOCOL_TCP_CLIENT);
protocols.define("UDP", PROTOCOL_UDP);

Expand Down Expand Up @@ -164,7 +164,31 @@ class NetworkSourceModule : public ModuleManager::Instance {
NetworkSourceModule* _this = (NetworkSourceModule*)ctx;
if (_this->running) { return; }

// TODO
// Depends on protocol
try {
if (_this->proto == PROTOCOL_TCP_SERVER) {
// Create TCP listener
// TODO

// Start listen worker
// TODO
}
else if (_this->proto == PROTOCOL_TCP_CLIENT) {
// Connect to TCP server
_this->sock = net::connect(_this->hostname, _this->port);
}
else if (_this->proto == PROTOCOL_UDP) {
// Open UDP socket
_this->sock = net::openudp("0.0.0.0", _this->port, _this->hostname, _this->port, true);
}
}
catch (const std::exception& e) {
flog::error("Could not start Network Source: {}", e.what());
return;
}

// Start receive worker
_this->workerThread = std::thread(&NetworkSourceModule::worker, _this);

_this->running = true;
flog::info("NetworkSourceModule '{0}': Start!", _this->name);
Expand All @@ -174,8 +198,17 @@ class NetworkSourceModule : public ModuleManager::Instance {
NetworkSourceModule* _this = (NetworkSourceModule*)ctx;
if (!_this->running) { return; }

// Stop listen worker
// TODO

// Close connection
if (_this->sock) { _this->sock->close(); }

// Stop worker thread
_this->stream.stopWriter();
if (_this->workerThread.joinable()) { _this->workerThread.join(); }
_this->stream.clearWriteStop();

_this->running = false;
flog::info("NetworkSourceModule '{0}': Stop!", _this->name);
}
Expand Down Expand Up @@ -254,12 +287,8 @@ class NetworkSourceModule : public ModuleManager::Instance {

while (true) {
// Read samples from socket
int bytes;
{
std::lock_guard lck(sockMtx);
bytes = sock->recv(buffer, frameSize, true);
if (bytes <= 0) { break; }
}
int bytes = sock->recv(buffer, frameSize, true);
if (bytes <= 0) { break; }

// Convert to CF32 (note: problem if partial sample)
int count = bytes / sampleSize;
Expand Down Expand Up @@ -297,7 +326,7 @@ class NetworkSourceModule : public ModuleManager::Instance {

int samplerate = 1000000;
int srId;
Protocol proto = PROTOCOL_TCP_SERVER;
Protocol proto = PROTOCOL_UDP;
int protoId;
SampleType sampType = SAMPLE_TYPE_INT16;
int sampTypeId;
Expand All @@ -308,6 +337,7 @@ class NetworkSourceModule : public ModuleManager::Instance {
OptionList<std::string, Protocol> protocols;
OptionList<std::string, SampleType> sampleTypes;

std::thread workerThread;
std::thread listenWorkerThread;

std::mutex sockMtx;
Expand Down

0 comments on commit edc08dd

Please sign in to comment.