src/Socket.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Aug 2022 00:54:03 +0200
branchv_0
changeset 15 290c58261df9
parent 14 86feef4b6ee6
child 16 63bb2c2038c7
permissions -rw-r--r--
template openClientSocket()
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>
10
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    28
#include <iostream>
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
#include <netdb.h>
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    30
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    31
#include "Socket.h"
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace relpipe {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace out {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace socket {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    37
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
    38
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    39
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
    40
	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
    41
	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
    42
	else return defaultValue;
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    43
}
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    44
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
class FD {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
private:
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    47
	int fd;
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
public:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
	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
    51
	};
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    52
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    53
	virtual ~FD() {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    54
		close(fd);
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
	int getFD() {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
		return fd;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
};
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    62
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
    63
	if (result == 0); // OK
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    64
	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
    65
}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    66
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    67
class AddressInfos {
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    68
private:
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    69
	std::shared_ptr<addrinfo> addrInfo;
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    70
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    71
	AddressInfos(addrinfo* addrInfo) : addrInfo(std::shared_ptr<addrinfo>(addrInfo, freeaddrinfo)) {
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    72
	}
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    73
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    74
public:
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    75
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    76
	virtual ~AddressInfos() {
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    77
	}
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    78
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    79
	class AddressInfo {
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    80
	private:
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    81
		std::shared_ptr<addrinfo> parent; // to avoid premature deletion of the whole structure
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    82
	public:
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    83
		const addrinfo * const ai;
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    84
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    85
		AddressInfo(const addrinfo * const ai, std::shared_ptr<addrinfo> parent) : ai(ai), parent(parent) {
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    86
		}
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    87
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    88
		const std::string toString() const {
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    89
			char buffer[INET6_ADDRSTRLEN] = {0};
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    90
			if (ai->ai_family == AF_INET) return inet_ntop(ai->ai_family, &((sockaddr_in const *) ai->ai_addr)->sin_addr, buffer, sizeof (buffer)); // TODO: check 0 result
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    91
			else if (ai->ai_family == AF_INET6) return inet_ntop(ai->ai_family, &((sockaddr_in6 const *) ai->ai_addr)->sin6_addr, buffer, sizeof (buffer)); // TODO: check 0 result
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    92
			else return "unknown address family: " + std::to_string(ai->ai_family);
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    93
		}
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    94
	};
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    95
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    96
	static AddressInfos getAddressInfos(const std::string& host, const std::string& port, int socketType = SOCK_STREAM, int protocol = IPPROTO_TCP) {
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    97
		struct addrinfo query;
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    98
		memset(&query, sizeof (query), 0);
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    99
		query.ai_family = AF_UNSPEC;
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   100
		query.ai_socktype = socketType;
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   101
		query.ai_protocol = protocol;
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   102
		query.ai_flags = AI_ALL;
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   103
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   104
		struct addrinfo* addrInfo;
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   105
		check(getaddrinfo(host.c_str(), port.c_str(), &query, &addrInfo), "getaddrinfo");
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   106
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   107
		return AddressInfos(addrInfo);
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   108
	}
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   109
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   110
	const std::size_t size() const {
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   111
		std::size_t size = 0;
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   112
		for (addrinfo* ai = addrInfo.get(); ai; ai = ai->ai_next) size++;
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   113
		return size;
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   114
	}
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   115
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   116
	const AddressInfo operator[](std::size_t index) const {
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   117
		for (addrinfo* ai = addrInfo.get(); ai; index--) {
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   118
			if (index == 0) return AddressInfo(ai, addrInfo);
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   119
			else ai = ai->ai_next;
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   120
		}
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   121
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   122
		throw std::out_of_range("invalid index for AddressInfo: " + std::to_string(index));
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   123
	}
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   124
12
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   125
};
fcc5ed0fab9f AddressInfos [] = addrinfo&
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   126
15
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   127
template<class SocketClass> static std::shared_ptr<Socket> openClientSocket(const SocketOptions& options, int socketType, int protocol) {
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   128
	AddressInfos remoteAddresses = AddressInfos::getAddressInfos(
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   129
			findOption(options, OPTION_HOST, true),
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   130
			findOption(options, OPTION_PORT, true),
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   131
			socketType,
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   132
			protocol);
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   133
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   134
	return std::shared_ptr<SocketClass>(new SocketClass(remoteAddresses));
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   135
}
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   136
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   137
class UDPClientSocket : public Socket {
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   138
private:
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   139
	AddressInfos remoteAddresses;
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   140
15
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   141
public:
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   142
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   143
	UDPClientSocket(AddressInfos remoteAddresses) : remoteAddresses(remoteAddresses) {
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   144
	}
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
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
	static std::shared_ptr<Socket> open(const SocketOptions& options) {
15
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   147
		bool sctp = findOption(options, OPTION_PROTOCOL) == PROTOCOL_SCTP;
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   148
		return openClientSocket<UDPClientSocket>(options, sctp ? SOCK_SEQPACKET : SOCK_DGRAM, sctp ? IPPROTO_SCTP : IPPROTO_UDP);
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   149
	}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   150
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   151
	void send(const std::string& message) override {
13
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   152
		auto ai = remoteAddresses[0].ai;
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   153
		FD s(::socket(AF_INET, ai->ai_socktype, ai->ai_protocol));
c29b13a659a7 AddressInfos [] = AddressInfo; UDP
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   154
		sendto(s.getFD(), message.c_str(), message.size(), 0, ai->ai_addr, ai->ai_addrlen);
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   155
	}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   156
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   157
	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
   158
		// TODO: TCP receive()
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   159
		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
   160
	}
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   161
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   162
};
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   163
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   164
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
   165
private:
14
86feef4b6ee6 AddressInfos [] = AddressInfo; TCP
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   166
	AddressInfos remoteAddresses;
86feef4b6ee6 AddressInfos [] = AddressInfo; TCP
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   167
15
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   168
public:
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   169
14
86feef4b6ee6 AddressInfos [] = AddressInfo; TCP
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   170
	TCPClientSocket(AddressInfos remoteAddresses) : remoteAddresses(remoteAddresses) {
86feef4b6ee6 AddressInfos [] = AddressInfo; TCP
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   171
	}
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   172
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   173
	static std::shared_ptr<Socket> open(const SocketOptions& options) {
15
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   174
		return openClientSocket<TCPClientSocket>(options, SOCK_STREAM, IPPROTO_TCP);
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   175
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   176
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   177
	void send(const std::string& message) override {
14
86feef4b6ee6 AddressInfos [] = AddressInfo; TCP
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   178
		auto ai = remoteAddresses[0].ai;
86feef4b6ee6 AddressInfos [] = AddressInfo; TCP
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   179
		FD s(::socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol));
86feef4b6ee6 AddressInfos [] = AddressInfo; TCP
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   180
		check(::connect(s.getFD(), ai->ai_addr, ai->ai_addrlen), "connect socket");
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   181
		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
   182
		if (written != message.size()) throw std::logic_error("writing to the socket failed");
15
290c58261df9 template openClientSocket()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   183
		// TODO: partial writes, repeat
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   184
	}
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   185
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   186
	const std::string receive() override {
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   187
		// TODO: TCP receive()
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   188
		return "TODO: receive() a message";
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   190
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   191
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   193
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   194
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
   195
class TemplateSocketFactory : public SocketFactory {
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   196
public:
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   197
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   198
	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
   199
		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
   200
				&& 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
   201
				&& findOption(options, OPTION_MODE) == mode;
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   202
	}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   203
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   204
	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
   205
		return SocketClass::open(options);
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   206
	}
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   207
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   208
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   209
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
   210
{
9
de48b9278211 TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   211
	std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_CLIENT, MODE_STREAM, TCPClientSocket >> (),
10
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   212
			std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_SERVER, MODE_STREAM, TCPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   213
			std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (),
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   214
			std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   215
			std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   216
			std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   217
			std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   218
			std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   219
			std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   220
			std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   221
			std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_SERVER, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class
6a6b93507856 use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   222
			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
   223
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   224
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   225
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
   226
	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
   227
	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
   228
}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   229
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   230
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   231
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   232
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   233
}