src/Socket.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Aug 2022 15:23:40 +0200
branchv_0
changeset 18 e16fa75135ad
parent 7 e6f005f3edfe
child 27 e6e5780339bd
permissions -rw-r--r--
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.

/**
 * Relational pipes
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <string>
#include <vector>
#include <cstring>
#include <unistd.h>
#include <stdexcept>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

namespace relpipe {
namespace out {
namespace socket {

namespace options {
static const char OPTION_PROTOCOL[] = "protocol";
static const char OPTION_ROLE[] = "role";
static const char OPTION_MODE[] = "mode";
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";
static const char PROTOCOL_UDS[] = "uds";
static const char PROTOCOL_SCTP[] = "sctp";

static const char ROLE_CLIENT[] = "client";
static const char ROLE_SERVER[] = "server";

static const char MODE_STREAM[] = "stream";
static const char MODE_DATAGRAM[] = "datagram";
}

class Socket {
public:
	virtual ~Socket() = default;
	virtual void send(const std::string& message) = 0;
	virtual const std::string receive() = 0;
};

class SocketOption {
public:
	const std::string name;
	const std::string value;

	SocketOption(const std::string name, const std::string value) : name(name), value(value) {
	}

	virtual ~SocketOption() = default;
};

using SocketOptions = std::vector<SocketOption>;

class SocketFactory {
public:
	virtual ~SocketFactory() = default;
	virtual bool canHandle(const SocketOptions& options) = 0;
	virtual std::shared_ptr<Socket> open(const SocketOptions& options) = 0;
	static std::shared_ptr<SocketFactory> find(const SocketOptions& options);
};


}
}
}