src/Socket.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 21 Aug 2022 00:16:50 +0200
branchv_0
changeset 3 2b57c8683ffe
parent 2 2665ab0bcf44
permissions -rw-r--r--
synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <string>
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    20
#include <vector>
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
#include <cstring>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    22
#include <unistd.h>
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <stdexcept>
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
#include <arpa/inet.h>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
#include <sys/types.h>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
#include <sys/socket.h>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    27
#include <netinet/in.h>
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace relpipe {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace in {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace socket {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    33
namespace options {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    34
static const char OPTION_PROTOCOL[] = "protocol";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    35
static const char OPTION_ROLE[] = "role";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    36
static const char OPTION_MODE[] = "mode";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    37
static const char OPTION_HOST[] = "host";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    38
static const char OPTION_PORT[] = "port";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    39
static const char OPTION_PATH[] = "path";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    40
static const char OPTION_DELAY[] = "delay";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    41
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    42
static const char PROTOCOL_TCP[] = "tcp";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    43
static const char PROTOCOL_UDP[] = "udp";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    44
static const char PROTOCOL_UDS[] = "uds";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    45
static const char PROTOCOL_SCTP[] = "sctp";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    46
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    47
static const char ROLE_CLIENT[] = "client";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    48
static const char ROLE_SERVER[] = "server";
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    50
static const char MODE_STREAM[] = "stream";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
static const char MODE_DATAGRAM[] = "datagram";
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    52
}
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    53
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    54
class Message {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
public:
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    56
	std::string data;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    57
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    58
	Message() {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    59
	}
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
	Message(const std::string& data) : data(data) {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    62
	}
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
	virtual ~Message() = default;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
};
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    68
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    69
class IncomingMessage : public Message {
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
public:
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    72
	IncomingMessage(const std::string& data) : Message(data) {
2
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    73
	}
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    74
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    75
	std::string remoteHost;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    76
	in_port_t remotePort = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    77
	pid_t remotePID = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    78
	uid_t remoteUID = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    79
	gid_t remoteGID = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    80
};
2
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    82
class OutgoingMessage : public Message {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    83
public:
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    85
	OutgoingMessage(const std::string& data) : Message(data) {
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
};
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    90
class Socket {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    91
public:
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    92
	virtual ~Socket() = default;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    93
	virtual void send(const OutgoingMessage& message) = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    94
	virtual const IncomingMessage receive() = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    95
};
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    96
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    97
class SocketOption {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    98
public:
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    99
	const std::string name;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   100
	const std::string value;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   101
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   102
	SocketOption(const std::string name, const std::string value) : name(name), value(value) {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   103
	}
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   104
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   105
	virtual ~SocketOption() = default;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   106
};
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   107
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   108
using SocketOptions = std::vector<SocketOption>;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   109
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   110
class SocketFactory {
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   111
public:
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   112
	virtual ~SocketFactory() = default;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   113
	virtual bool canHandle(const SocketOptions& options) = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   114
	virtual std::shared_ptr<Socket> open(const SocketOptions& options) = 0;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   115
	static std::shared_ptr<SocketFactory> find(const SocketOptions& options);
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   116
};
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   117
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   118
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
}