author | František Kučera <franta-hg@frantovo.cz> |
Tue, 26 Jul 2022 23:22:24 +0200 | |
branch | v_0 |
changeset 1 | d93ea7346b66 |
parent 0 | e8f15f432efc |
child 2 | 2665ab0bcf44 |
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 |
|
35 |
||
36 |
public: |
|
37 |
||
38 |
virtual ~Socket() { |
|
39 |
} |
|
40 |
||
41 |
std::string receive() { |
|
42 |
char buffer[MSG_SIZE + 1]; |
|
43 |
memset(buffer, 0, MSG_SIZE + 1); |
|
1
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
|
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
|
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
struct sockaddr_in a; |
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
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
|
49 |
a.sin_family = AF_INET; |
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
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
|
51 |
a.sin_port = htons(1234); |
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
::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
|
53 |
|
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
struct sockaddr_in remoteAddress; |
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
memset((char *) &remoteAddress, 0, sizeof (remoteAddress)); |
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
socklen_t remoteAddressSize = sizeof(remoteAddress); |
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
|
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
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
|
59 |
|
d93ea7346b66
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
close(s); |
0 | 61 |
|
62 |
if (msgSize > sizeof (buffer))throw std::logic_error("Invalid Socket message size."); |
|
63 |
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
|
64 |
else throw std::logic_error("Unable to receive Socket message the socket; error: " + std::string(strerror(errno))); |
0 | 65 |
} |
66 |
||
67 |
}; |
|
68 |
||
69 |
} |
|
70 |
} |
|
71 |
} |