42 ERROR, EMPTY, DATA, HMAC, NONCE, IV,
43 FINAL, MESSAGE, ACK, REFUSED, REEXCHANGE,
47 #define PACKET_SIZE 1024
58 char data[PACKET_SIZE] = {0};
73 switch (poll(&fd, 1, timeout * 1000)) {
74 case -1:
case 0:
return -1;
75 default:
return send(
connection,
reinterpret_cast<const void*
>(&p),
sizeof(p), 0);
93 switch (poll(&fd, 1, timeout * 1000)) {
94 case -1:
case 0: p.m = network::meta::ERROR;
break;
95 default: recv(
connection,
reinterpret_cast<void*
>(&p),
sizeof(p), 0);
break;
109 template <
typename T>
inline int send_value(
const T& value,
const network::meta& type = DATA,
const size_t& timeout=5) {
113 std::stringstream in; in << value;
115 if (str.length() > PACKET_SIZE) {
116 throw std::runtime_error(
"Value exceeds packet size!");
118 strncpy(&p.data[0], str.c_str(), PACKET_SIZE);
131 template <
typename T>
inline const T
recv_value(
const size_t& timeout=5) {
133 if (p.m = network::meta::ERROR)
throw std::runtime_error(
"Failed to read from socket!");
136 auto str = std::string(&p.data[0], PACKET_SIZE);
138 std::istringstream (str) >> ret;
153 inline int send_string(
const std::string& message,
const network::meta& type = DATA,
const size_t& timeout=5) {
161 p.data[0] = message[0];
165 for (x = 1; x < message.length(); ++x) {
166 if (x % PACKET_SIZE == 0) {
170 p.data[x % PACKET_SIZE] = message[x];
176 while (x % PACKET_SIZE != 0) p.data[x++ % PACKET_SIZE] = 0;
199 if (p.m == ERROR)
throw std::runtime_error(
"Failure recieving packet!");
202 ret.append(p.data, PACKET_SIZE);
203 if (p.m == FINAL)
break;
207 return std::string(ret.c_str(), length);
224 sock = socket(AF_INET, SOCK_STREAM, 0);
225 if (
sock == -1)
return;
228 sockaddr_in serverAddress = {
229 .sin_family = AF_INET,
230 .sin_port = htons(port),
231 .sin_addr = {.s_addr = INADDR_ANY}
236 struct timeval timeout;
239 setsockopt(
sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof(timeout));
241 if (bind(
sock, (
struct sockaddr*)&serverAddress,
sizeof(serverAddress)) == -1) {
249 if (listen(
sock, 1) == -1) {
256 sockaddr_in clientAddress;
257 socklen_t clientSize =
sizeof(clientAddress);
258 connection = accept(
sock, (
struct sockaddr *)&clientAddress, &clientSize);
272 void get_server(
const size_t& port,
const char address[] =
"127.0.0.1") {
282 sockaddr_in serverAddress = {
283 .sin_family = AF_INET,
284 .sin_port = htons(port),
285 .sin_addr = {.s_addr = inet_addr(address)}
288 if (connect(
connection, (
struct sockaddr *)&serverAddress,
sizeof(serverAddress)) == -1) {
The namespace for communication along a socket.
Definition network.h:13
int send_packet(const packet &p, const size_t &timeout=5)
Send a packet.
Definition network.h:68
int sock
The FD sock for when the program is in Listen mode.
Definition network.h:20
std::string recv_string(const size_t &timeout=5)
Receive a string.
Definition network.h:188
int send_value(const T &value, const network::meta &type=DATA, const size_t &timeout=5)
Send a value.
Definition network.h:109
int connection
the FD sock that peers communicate over.
Definition network.h:25
int send_string(const std::string &message, const network::meta &type=DATA, const size_t &timeout=5)
Send a string of any size.
Definition network.h:153
const T recv_value(const size_t &timeout=5)
Receive a value.
Definition network.h:131
packet recv_packet(const size_t &timeout=5)
Receive a packet.
Definition network.h:87
void get_server(const size_t &port, const char address[]="127.0.0.1")
Try and connect to the server.
Definition network.h:272
void get_client(const int &port)
Listen on the socket for a client to initiate a connection.
Definition network.h:220