src/Socket.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Jul 2022 23:25:23 +0200
branchv_0
changeset 9 de48b9278211
parent 7 e6f005f3edfe
child 10 6a6b93507856
permissions -rw-r--r--
TCP and UDP client sockets - first version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
924e354948df 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
924e354948df 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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
924e354948df 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,
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
924e354948df 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
924e354948df 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/>.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <string>
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    18
#include <cstring>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    19
#include <unistd.h>
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <stdexcept>
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
#include <arpa/inet.h>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    22
#include <sys/types.h>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    23
#include <sys/socket.h>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
#include <netinet/in.h>
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    25
#include <vector>
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    26
#include <memory>
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    27
#include <regex>
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    28
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    29
#include "Socket.h"
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace relpipe {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace out {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace socket {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    35
using namespace relpipe::out::socket::options;
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    36
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    37
static const std::string findOption(SocketOptions options, std::string name, bool required = false, const std::string defaultValue = "") {
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    38
	for (auto o : options) if (o.name == name) return o.value;
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    39
	if (required) throw std::invalid_argument("Option " + name + " is required but was not found");
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    40
	else return defaultValue;
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    41
}
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    42
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
class FD {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
private:
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
	int fd;
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
public:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    48
	FD(int fd) : fd(fd) {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    49
	};
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    51
	virtual ~FD() {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    52
		close(fd);
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    53
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    54
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
	int getFD() {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
		return fd;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
};
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    60
static void check(int result, std::string message) {
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    61
	if (result == 0); // OK
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    62
	else throw std::logic_error("Got error result: " + std::to_string(result) + " - " + message);
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    63
}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    64
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    65
class UDPClientSocket : public Socket {
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    66
private:
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    67
	struct sockaddr_in remoteAddress;
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    68
	int protocol = IPPROTO_UDP;
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    69
	int type = SOCK_DGRAM;
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    70
public:
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    71
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    72
	static std::shared_ptr<Socket> open(const SocketOptions& options) {
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    73
		std::shared_ptr<UDPClientSocket> s = std::make_shared<UDPClientSocket>();
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    74
		memset((char *) &s->remoteAddress, 0, sizeof (s->remoteAddress));
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    75
		s->remoteAddress.sin_family = AF_INET;
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    76
		s->remoteAddress.sin_addr.s_addr = inet_addr(findOption(options, OPTION_HOST, true).c_str()); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    77
		s->remoteAddress.sin_port = htons(std::stoi(findOption(options, OPTION_PORT, true)));
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    78
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    79
		auto protocol = findOption(options, OPTION_PROTOCOL);
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    80
		if (protocol == PROTOCOL_SCTP) {
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    81
			s->protocol = IPPROTO_SCTP;
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    82
			s->type = SOCK_SEQPACKET;
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    83
		}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    84
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    85
		return s;
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    86
	}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    87
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    88
	void send(const std::string& message) override {
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    89
		FD s(::socket(AF_INET, type, protocol));
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    90
		sendto(s.getFD(), message.c_str(), message.size(), 0, (sockaddr*) & remoteAddress, sizeof (remoteAddress));
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    91
	}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    92
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    93
	const std::string receive() override {
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    94
		// TODO: TCP receive()
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    95
		return "TODO: receive() a message";
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    96
	}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    97
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    98
};
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    99
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   100
class TCPClientSocket : public Socket {
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   101
private:
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   102
	struct sockaddr_in remoteAddress;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   103
public:
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   104
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   105
	static std::shared_ptr<Socket> open(const SocketOptions& options) {
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   106
		std::shared_ptr<TCPClientSocket> s = std::make_shared<TCPClientSocket>();
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   107
		memset((char *) &s->remoteAddress, 0, sizeof (s->remoteAddress));
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   108
		s->remoteAddress.sin_family = AF_INET;
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   109
		s->remoteAddress.sin_addr.s_addr = inet_addr(findOption(options, OPTION_HOST, true).c_str()); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   110
		s->remoteAddress.sin_port = htons(std::stoi(findOption(options, OPTION_PORT, true)));
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   111
		return s;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   112
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   113
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   114
	void send(const std::string& message) override {
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   115
		FD s(::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   116
		check(::connect(s.getFD(), (sockaddr*) & remoteAddress, sizeof (remoteAddress)), "connect socket");
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   117
		ssize_t written = ::write(s.getFD(), message.c_str(), message.size());
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   118
		if (written != message.size()) throw std::logic_error("writing to the socket failed");
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   119
	}
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   121
	const std::string receive() override {
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   122
		// TODO: TCP receive()
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   123
		return "TODO: receive() a message";
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   126
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   129
template<const char* protocol, const char* role, const char* mode, typename SocketClass>
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   130
class TemplateSocketFactory : public SocketFactory {
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   131
public:
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   132
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   133
	bool canHandle(const SocketOptions& options) override {
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   134
		return findOption(options, OPTION_PROTOCOL) == protocol
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   135
				&& findOption(options, OPTION_ROLE) == role
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   136
				&& findOption(options, OPTION_MODE) == mode;
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   137
	}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   138
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   139
	std::shared_ptr<Socket> open(const SocketOptions& options) override {
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   140
		return SocketClass::open(options);
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   141
	}
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   142
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   143
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   144
static std::vector<std::shared_ptr<SocketFactory>> factories
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   145
{
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   146
	std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_CLIENT, MODE_STREAM, TCPClientSocket >> (),
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   147
	std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_SERVER, MODE_STREAM, TCPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   148
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (),
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   149
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   150
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   151
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   152
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   153
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   154
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   155
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   156
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_SERVER, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   157
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   158
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   159
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   160
std::shared_ptr<SocketFactory> SocketFactory::find(const SocketOptions& options) {
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   161
	for (auto f : factories) if (f->canHandle(options)) return f;
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   162
	throw std::logic_error("Unable to find a SocketFactory"); // TODO: add relevant options?
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   163
}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   164
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   165
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
}