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