synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
/**
* 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 in {
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 Message {
public:
std::string data;
Message() {
}
Message(const std::string& data) : data(data) {
}
virtual ~Message() = default;
};
class IncomingMessage : public Message {
public:
IncomingMessage(const std::string& data) : Message(data) {
}
std::string remoteHost;
in_port_t remotePort = 0;
pid_t remotePID = 0;
uid_t remoteUID = 0;
gid_t remoteGID = 0;
};
class OutgoingMessage : public Message {
public:
OutgoingMessage(const std::string& data) : Message(data) {
}
};
class Socket {
public:
virtual ~Socket() = default;
virtual void send(const OutgoingMessage& message) = 0;
virtual const IncomingMessage 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);
};
}
}
}