Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Release 0.4.0 (pending)
==========================

- Added tcp support for streaming via remote:prot=tcp
- Support for bulk register read/write interface APIs

Release 0.3.1 (2016-09-01)
Expand Down
7 changes: 6 additions & 1 deletion client/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
******************************************************************/

SoapyRemoteDevice::SoapyRemoteDevice(const std::string &url, const SoapySDR::Kwargs &args):
_logAcceptor(nullptr)
_logAcceptor(nullptr),
_defaultStreamProt("udp")
{
//try to connect to the remote server
int ret = _sock.connect(url);
Expand All @@ -37,6 +38,10 @@ SoapyRemoteDevice::SoapyRemoteDevice(const std::string &url, const SoapySDR::Kwa
packer & args;
packer();
SoapyRPCUnpacker unpacker(_sock);

//default stream protocol specified in device args
const auto protIt = args.find("prot");
if (protIt != args.end()) _defaultStreamProt = protIt->second;
}

SoapyRemoteDevice::~SoapyRemoteDevice(void)
Expand Down
1 change: 1 addition & 0 deletions client/SoapyClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,5 @@ class SoapyRemoteDevice : public SoapySDR::Device
SoapyRPCSocket _sock;
SoapyLogAcceptor *_logAcceptor;
std::mutex _mutex;
std::string _defaultStreamProt;
};
110 changes: 79 additions & 31 deletions client/Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ SoapySDR::Stream *SoapyRemoteDevice::setupStream(
const int direction,
const std::string &localFormat,
const std::vector<size_t> &channels_,
const SoapySDR::Kwargs &args)
const SoapySDR::Kwargs &args_)
{
//default to channel 0 when not specified
//the channels vector cannot be empty
//its used for stream endpoint allocation
auto channels = channels_;
if (channels.empty()) channels.push_back(0);
SoapySDR::Kwargs args(args_); //read/write copy

//use the remote device's native stream format and scale factor when the conversion is supported
double nativeScaleFactor = 0.0;
Expand All @@ -158,13 +159,27 @@ SoapySDR::Stream *SoapyRemoteDevice::setupStream(
const auto scaleFactorIt = args.find(SOAPY_REMOTE_KWARG_SCALAR);
if (scaleFactorIt != args.end()) scaleFactor = std::stod(scaleFactorIt->second);

size_t mtu = SOAPY_REMOTE_DEFAULT_ENDPOINT_MTU;
//determine reliable stream mode with tcp or datagram mode
std::string prot = _defaultStreamProt;
const auto protIt = args.find(SOAPY_REMOTE_KWARG_PROT);
if (protIt != args.end()) prot = protIt->second;
const bool datagramMode = (prot == "udp");
if (prot == "udp") {}
else if (prot == "tcp") {}
else throw std::runtime_error(
"SoapyRemote::setupStream() protcol not supported;"
"expected 'udp' or 'tcp', but got '"+prot+"'");
args[SOAPY_REMOTE_KWARG_PROT] = prot;

size_t mtu = datagramMode?SOAPY_REMOTE_DEFAULT_ENDPOINT_MTU:SOAPY_REMOTE_SOCKET_BUFFMAX;
const auto mtuIt = args.find(SOAPY_REMOTE_KWARG_MTU);
if (mtuIt != args.end()) mtu = size_t(std::stod(mtuIt->second));
args[SOAPY_REMOTE_KWARG_MTU] = std::to_string(mtu);

size_t window = SOAPY_REMOTE_DEFAULT_ENDPOINT_WINDOW;
const auto windowIt = args.find(SOAPY_REMOTE_KWARG_WINDOW);
if (windowIt != args.end()) window = size_t(std::stod(windowIt->second));
args[SOAPY_REMOTE_KWARG_WINDOW] = std::to_string(window);

SoapySDR::logf(SOAPY_SDR_INFO, "SoapyRemote::setup%sStream(remoteFormat=%s, localFormat=%s, scaleFactor=%g, mtu=%d, window=%d)",
(direction == SOAPY_SDR_RX)?"Rx":"Tx", remoteFormat.c_str(), localFormat.c_str(), scaleFactor, int(mtu), int(window));
Expand Down Expand Up @@ -192,28 +207,33 @@ SoapySDR::Stream *SoapyRemoteDevice::setupStream(
const auto localNode = SoapyURL(_sock.getsockname()).getNode();
const auto remoteNode = SoapyURL(_sock.getpeername()).getNode();

//bind the stream socket to an automatic port
const auto bindURL = SoapyURL("udp", localNode, "0").toString();
int ret = data->streamSock.bind(bindURL);
if (ret != 0)
//bind the receiver side of the sockets in datagram mode
std::string clientBindPort, statusBindPort;
if (datagramMode)
{
const std::string errorMsg = data->streamSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+bindURL+") -- bind FAIL: " + errorMsg);
}
SoapySDR::logf(SOAPY_SDR_INFO, "Client side stream bound to %s", data->streamSock.getsockname().c_str());
const auto clientBindPort = SoapyURL(data->streamSock.getsockname()).getService();
//bind the stream socket to an automatic port
const auto bindURL = SoapyURL("udp", localNode, "0").toString();
int ret = data->streamSock.bind(bindURL);
if (ret != 0)
{
const std::string errorMsg = data->streamSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+bindURL+") -- bind FAIL: " + errorMsg);
}
SoapySDR::logf(SOAPY_SDR_INFO, "Client side stream bound to %s", data->streamSock.getsockname().c_str());
clientBindPort = SoapyURL(data->streamSock.getsockname()).getService();

//bind the status socket to an automatic port
ret = data->statusSock.bind(bindURL);
if (ret != 0)
{
const std::string errorMsg = data->statusSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+bindURL+") -- bind FAIL: " + errorMsg);
//bind the status socket to an automatic port
ret = data->statusSock.bind(bindURL);
if (ret != 0)
{
const std::string errorMsg = data->statusSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+bindURL+") -- bind FAIL: " + errorMsg);
}
SoapySDR::logf(SOAPY_SDR_INFO, "Client side status bound to %s", data->statusSock.getsockname().c_str());
statusBindPort = SoapyURL(data->statusSock.getsockname()).getService();
}
SoapySDR::logf(SOAPY_SDR_INFO, "Client side status bound to %s", data->statusSock.getsockname().c_str());
const auto statusBindPort = SoapyURL(data->statusSock.getsockname()).getService();

//setup the remote end of the stream
std::lock_guard<std::mutex> lock(_mutex);
Expand All @@ -227,25 +247,53 @@ SoapySDR::Stream *SoapyRemoteDevice::setupStream(
packer & statusBindPort;
packer();

SoapyRPCUnpacker unpacker(_sock);
//for tcp mode: get the binding port here and connect to it
std::string serverBindPort;
if (not datagramMode)
{
SoapyRPCUnpacker unpackerTcp(_sock);
unpackerTcp & serverBindPort;
const auto connectURL = SoapyURL(prot, remoteNode, serverBindPort).toString();
int ret = data->streamSock.connect(connectURL);
if (ret != 0)
{
const std::string errorMsg = data->streamSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+connectURL+") -- connect FAIL: " + errorMsg);
}
ret = data->statusSock.connect(connectURL);
if (ret != 0)
{
const std::string errorMsg = data->statusSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+connectURL+") -- connect FAIL: " + errorMsg);
}
}

//and wait for the response with binding port and stream id
SoapyRPCUnpacker unpacker(_sock);
unpacker & data->streamId;
unpacker & serverBindPort;

//connect the stream socket to the specified port
const auto connectURL = SoapyURL("udp", remoteNode, serverBindPort).toString();
ret = data->streamSock.connect(connectURL);
if (ret != 0)
//connect the sending end of the stream socket
if (datagramMode)
{
const std::string errorMsg = data->streamSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+connectURL+") -- connect FAIL: " + errorMsg);
//connect the stream socket to the specified port
const auto connectURL = SoapyURL(prot, remoteNode, serverBindPort).toString();
int ret = data->streamSock.connect(connectURL);
if (ret != 0)
{
const std::string errorMsg = data->streamSock.lastErrorMsg();
delete data;
throw std::runtime_error("SoapyRemote::setupStream("+connectURL+") -- connect FAIL: " + errorMsg);
}
SoapySDR::logf(SOAPY_SDR_INFO, "Client side stream connected to %s", data->streamSock.getpeername().c_str());
}
SoapySDR::logf(SOAPY_SDR_INFO, "Client side stream connected to %s", data->streamSock.getpeername().c_str());

//create endpoint
data->endpoint = new SoapyStreamEndpoint(data->streamSock, data->statusSock,
direction == SOAPY_SDR_RX, channels.size(), SoapySDR::formatToSize(remoteFormat), mtu, window);
datagramMode, direction == SOAPY_SDR_RX, channels.size(),
SoapySDR::formatToSize(remoteFormat), mtu, window);

return (SoapySDR::Stream *)data;
}
Expand Down
3 changes: 3 additions & 0 deletions common/SoapyRemoteDefs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
//! Stream args key to set the buffer MTU bytes for network transfers
#define SOAPY_REMOTE_KWARG_MTU (SOAPY_REMOTE_KWARG_PREFIX "mtu")

//! Stream args key to select the stream's protocol (tcp or udp)
#define SOAPY_REMOTE_KWARG_PROT (SOAPY_REMOTE_KWARG_PREFIX "prot")

/*!
* Default stream transfer size (under network MTU).
* Larger transfer sizes may not be supported in hardware
Expand Down
44 changes: 35 additions & 9 deletions common/SoapyStreamEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ struct StreamDatagramHeader
SoapyStreamEndpoint::SoapyStreamEndpoint(
SoapyRPCSocket &streamSock,
SoapyRPCSocket &statusSock,
const bool datagramMode,
const bool isRecv,
const size_t numChans,
const size_t elemSize,
const size_t mtu,
const size_t window):
_streamSock(streamSock),
_statusSock(statusSock),
_datagramMode(datagramMode),
_xferSize(mtu-PROTO_HEADER_SIZE),
_numChans(numChans),
_elemSize(elemSize),
Expand Down Expand Up @@ -168,6 +170,8 @@ bool SoapyStreamEndpoint::waitRecv(const long timeoutUs)

int SoapyStreamEndpoint::acquireRecv(size_t &handle, const void **buffs, int &flags, long long &timeNs)
{
int ret = 0;

//no available handles, the user is hoarding them...
if (_numHandlesAcquired == _buffData.size())
{
Expand All @@ -181,23 +185,38 @@ int SoapyStreamEndpoint::acquireRecv(size_t &handle, const void **buffs, int &fl

//receive into the buffer
assert(not _streamSock.null());
int ret = _streamSock.recv(data.buff.data(), data.buff.size());
if (_datagramMode) ret = _streamSock.recv(data.buff.data(), data.buff.size());
else ret = _streamSock.recv(data.buff.data(), HEADER_SIZE, MSG_WAITALL);
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "StreamEndpoint::acquireRecv(), FAILED %s", _streamSock.lastErrorMsg());
return SOAPY_SDR_STREAM_ERROR;
}
size_t bytesRecvd = size_t(ret);
_receiveInitial = true;

//check the header
auto header = (const StreamDatagramHeader*)data.buff.data();
size_t bytes = ntohl(header->bytes);
if (bytes > size_t(ret))

if (_datagramMode and bytes > bytesRecvd)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "StreamEndpoint::acquireRecv(%d bytes), FAILED %d\n"
"This MTU setting may be unachievable. Check network configuration.", int(bytes), ret);
return SOAPY_SDR_STREAM_ERROR;
}

else while (bytesRecvd < bytes)
{
ret = _streamSock.recv(data.buff.data()+bytesRecvd, std::min<size_t>(SOAPY_REMOTE_SOCKET_BUFFMAX, bytes-bytesRecvd));
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "StreamEndpoint::acquireRecv(), FAILED %s", _streamSock.lastErrorMsg());
return SOAPY_SDR_STREAM_ERROR;
}
bytesRecvd += size_t(ret);
}

const int numElemsOrErr = int(ntohl(header->elems));

//dropped or out of order packets
Expand Down Expand Up @@ -307,14 +326,21 @@ void SoapyStreamEndpoint::releaseSend(const size_t handle, const int numElemsOrE

//send from the buffer
assert(not _streamSock.null());
int ret = _streamSock.send(data.buff.data(), bytes);
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "StreamEndpoint::releaseSend(), FAILED %s", _streamSock.lastErrorMsg());
}
else if (size_t(ret) != bytes)
size_t bytesSent = 0;
while (bytesSent < bytes)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "StreamEndpoint::releaseSend(%d bytes), FAILED %d", int(bytes), ret);
int ret = _streamSock.send(data.buff.data()+bytesSent, std::min<size_t>(SOAPY_REMOTE_SOCKET_BUFFMAX, bytes-bytesSent));
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "StreamEndpoint::releaseSend(), FAILED %s", _streamSock.lastErrorMsg());
break;
}
bytesSent += size_t(ret);
if (not _datagramMode) continue;
if (bytesSent != bytes)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "StreamEndpoint::releaseSend(%d bytes), FAILED %d", int(bytes), ret);
}
}

//actually release in order of handle index
Expand Down
4 changes: 3 additions & 1 deletion common/SoapyStreamEndpoint.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2015 Josh Blum
// Copyright (c) 2015-2016 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#pragma once
Expand All @@ -19,6 +19,7 @@ class SOAPY_REMOTE_API SoapyStreamEndpoint
SoapyStreamEndpoint(
SoapyRPCSocket &streamSock,
SoapyRPCSocket &statusSock,
const bool datagramMode,
const bool isRecv,
const size_t numChans,
const size_t elemSize,
Expand Down Expand Up @@ -125,6 +126,7 @@ class SOAPY_REMOTE_API SoapyStreamEndpoint
private:
SoapyRPCSocket &_streamSock;
SoapyRPCSocket &_statusSock;
const bool _datagramMode;
const size_t _xferSize;
const size_t _numChans;
const size_t _elemSize;
Expand Down
Loading