UDP: add delay option
The other side might be too slow to receive all datagrams and might loose some.
We might slow down – add a delay between the messages.
The delay is specified in microseconds: e.g. --connection-option delay 800
However better solution is to increase the buffer size on the receiver side.
--- a/bash-completion.sh Sat Aug 06 12:12:46 2022 +0200
+++ b/bash-completion.sh Sat Aug 06 15:23:40 2022 +0200
@@ -36,6 +36,7 @@
"host"
"port"
"path"
+ "delay"
)
PROTOCOLS=(
@@ -55,6 +56,13 @@
"datagram"
)
+ DELAYS=(
+ "100"
+ "200"
+ "400"
+ "800"
+ )
+
if [[ "$w1" == "--relation" && "x$w0" == "x" ]]; then COMPREPLY=("''")
elif [[ "$w1" == "--connection-string" ]]; then COMPREPLY=($(compgen -W "${CONNECTION_STRINGS[*]}" -- "$w0"))
elif [[ "$w1" == "--connection-option" ]]; then COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
@@ -62,6 +70,7 @@
elif [[ "$w2" == "--connection-option" && "$w1" == "role" ]]; then COMPREPLY=($(compgen -W "${ROLES[*]}" -- "$w0"))
elif [[ "$w2" == "--connection-option" && "$w1" == "mode" ]]; then COMPREPLY=($(compgen -W "${MODES[*]}" -- "$w0"))
elif [[ "$w2" == "--connection-option" && "$w1" == "path" ]]; then COMPREPLY=($(compgen -f -- "$w0"))
+ elif [[ "$w2" == "--connection-option" && "$w1" == "delay" ]]; then COMPREPLY=($(compgen -W "${DELAYS[*]}" -- "$w0"))
else
OPTIONS=(
"--relation"
--- a/src/Socket.cpp Sat Aug 06 12:12:46 2022 +0200
+++ b/src/Socket.cpp Sat Aug 06 15:23:40 2022 +0200
@@ -124,7 +124,7 @@
};
-template<class SocketClass> static std::shared_ptr<Socket> openClientSocket(const SocketOptions& options, int socketType, int protocol) {
+template<class SocketClass> static std::shared_ptr<SocketClass> openClientSocket(const SocketOptions& options, int socketType, int protocol) {
AddressInfos remoteAddresses = AddressInfos::getAddressInfos(
findOption(options, OPTION_HOST, true),
findOption(options, OPTION_PORT, true),
@@ -137,6 +137,7 @@
class UDPClientSocket : public Socket {
private:
AddressInfos::AddressInfo remoteAddress;
+ useconds_t delay = 0;
public:
@@ -145,13 +146,16 @@
static std::shared_ptr<Socket> open(const SocketOptions& options) {
bool sctp = findOption(options, OPTION_PROTOCOL) == PROTOCOL_SCTP;
- return openClientSocket<UDPClientSocket>(options, sctp ? SOCK_SEQPACKET : SOCK_DGRAM, sctp ? IPPROTO_SCTP : IPPROTO_UDP);
+ auto socket = openClientSocket<UDPClientSocket>(options, sctp ? SOCK_SEQPACKET : SOCK_DGRAM, sctp ? IPPROTO_SCTP : IPPROTO_UDP);
+ socket->delay = std::stol(findOption(options, OPTION_DELAY, false, "0"));
+ return socket;
}
void send(const std::string& message) override {
auto ai = remoteAddress.ai;
FD s(::socket(AF_INET, ai->ai_socktype, ai->ai_protocol));
sendto(s.getFD(), message.c_str(), message.size(), 0, ai->ai_addr, ai->ai_addrlen);
+ if (delay) usleep(delay);
}
const std::string receive() override {
--- a/src/Socket.h Sat Aug 06 12:12:46 2022 +0200
+++ b/src/Socket.h Sat Aug 06 15:23:40 2022 +0200
@@ -37,6 +37,7 @@
static const char OPTION_HOST[] = "host";
static const char OPTION_PORT[] = "port";
static const char OPTION_PATH[] = "path";
+static const char OPTION_DELAY[] = "delay";
static const char PROTOCOL_TCP[] = "tcp";
static const char PROTOCOL_UDP[] = "udp";