src/Socket.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 20 Aug 2022 00:20:03 +0200
branchv_0
changeset 27 e6e5780339bd
parent 18 e16fa75135ad
child 31 17cea3a6d33f
permissions -rw-r--r--
introduce IncomingMessage and OutgoingMessage types

/**
 * 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 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) {
	}

};

class OutgoingMessage : public Message {
public:

	OutgoingMessage(const std::string& data) : Message(data) {
	}

	std::string remoteHost;
	std::string remotePort;
	pid_t remotePID;
	uid_t remoteUID;
	gid_t remoteGID;
};

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);
};


}
}
}