src/Socket.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 07 Aug 2022 10:45:05 +0200
branchv_0
changeset 2 2665ab0bcf44
parent 1 d93ea7346b66
child 3 2b57c8683ffe
permissions -rw-r--r--
reuse the UDP socket
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>
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    20
#include <cstring>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
#include <unistd.h>
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <stdexcept>
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    23
#include <arpa/inet.h>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
#include <sys/types.h>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
#include <sys/socket.h>
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
#include <netinet/in.h>
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace socket {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class Socket {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	const static size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
2
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    35
	int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
public:
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
2
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    39
	Socket() {
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    40
		s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
		struct sockaddr_in a;
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
		memset((char *) &a, 0, sizeof (a));
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
		a.sin_family = AF_INET;
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
		a.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
		a.sin_port = htons(1234);
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
		::bind(s, (sockaddr*) & a, sizeof (a));
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
2
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
		// int soBufferSize = 1024 * 1024;
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    50
		// setsockopt(s, SOL_SOCKET, SO_RCVBUF, &soBufferSize, sizeof (soBufferSize));
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    51
		// soBufferSize = 0;
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
		// socklen_t soBufferSizeLength = sizeof (soBufferSize);
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    53
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    54
		// std::cerr << "soBufferSize=" << soBufferSize << std::endl;
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
		// getsockopt(s, SOL_SOCKET, SO_RCVBUF, &soBufferSize, &soBufferSizeLength);
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
		// std::cerr << "soBufferSize=" << soBufferSize << " length=" << soBufferSizeLength << std::endl;
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
	}
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    58
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
	virtual ~Socket() {
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    60
		close(s);
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    61
	}
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    62
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    63
	std::string receive() {
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    64
		char buffer[MSG_SIZE + 1];
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    65
		memset(buffer, 0, MSG_SIZE + 1);
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    66
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
		struct sockaddr_in remoteAddress;
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
		memset((char *) &remoteAddress, 0, sizeof (remoteAddress));
2
2665ab0bcf44 reuse the UDP socket
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    69
		socklen_t remoteAddressSize = sizeof (remoteAddress);
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
		ssize_t msgSize = recvfrom(s, buffer, sizeof (buffer), 0, (sockaddr*) & remoteAddress, &remoteAddressSize);
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		if (msgSize > sizeof (buffer))throw std::logic_error("Invalid Socket message size.");
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		else if (msgSize >= 0) return std::string(buffer, msgSize);
1
d93ea7346b66 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
		else throw std::logic_error("Unable to receive Socket message the socket; error: " + std::string(strerror(errno)));
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
};
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}