author | František Kučera <franta-hg@frantovo.cz> |
Sun, 07 Aug 2022 10:45:05 +0200 | |
branch | v_0 |
changeset 2 | 2665ab0bcf44 |
parent 1 | d93ea7346b66 |
child 3 | 2b57c8683ffe |
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> |
|
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 | 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 | 27 |
|
28 |
namespace relpipe { |
|
29 |
namespace in { |
|
30 |
namespace socket { |
|
31 |
||
32 |
class Socket { |
|
33 |
private: |
|
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 | 36 |
|
37 |
public: |
|
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 | 73 |
|
74 |
if (msgSize > sizeof (buffer))throw std::logic_error("Invalid Socket message size."); |
|
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 | 77 |
} |
78 |
||
79 |
}; |
|
80 |
||
81 |
} |
|
82 |
} |
|
83 |
} |