-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtcpWrapper.cpp
More file actions
73 lines (64 loc) · 1.88 KB
/
tcpWrapper.cpp
File metadata and controls
73 lines (64 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <netinet/in.h>
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include "tcpWrapper.h"
int tcp_socket() {
auto sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
throw std::runtime_error{"Could not open socket"};
}
const int enable = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
throw std::runtime_error{"Could not set SO_REUSEADDR"};
}
return sock;
}
void tcp_connect(int sock, sockaddr_in &addr) {
if (connect(sock, reinterpret_cast<sockaddr *>(&addr), sizeof addr) < 0) {
throw std::runtime_error{"error connect'ing"};
}
}
void tcp_write(int sock, void *buffer, std::size_t size) {
if (send(sock, buffer, size, 0) < 0) {
perror("send");
throw std::runtime_error{"error write'ing"};
}
}
void tcp_read(int sock, void *buffer, std::size_t size) {
if (recv(sock, buffer, size, 0) < 0) {
perror("recv");
throw std::runtime_error{"error read'ing"};
}
}
void tcp_bind(int sock, sockaddr_in &addr) {
if (bind(sock, reinterpret_cast<sockaddr *>(&addr), sizeof addr) < 0) {
throw std::runtime_error{"error bind'ing"};
}
}
int tcp_accept(int sock, sockaddr_in &inAddr) {
socklen_t inAddrLen = sizeof inAddr;
auto acced = accept(sock, reinterpret_cast<sockaddr *>(&inAddr), &inAddrLen);
if (acced < 0) {
perror("accept");
throw std::runtime_error{"error accept'ing"};
}
return acced;
}
void tcp_setBlocking(int sock) {
auto opts = fcntl(sock, F_GETFL);
opts &= ~O_NONBLOCK;
fcntl(sock, F_SETFL, opts);
}
void tcp_close(int sock) {
if (close(sock) < 0) {
perror("close");
throw std::runtime_error{"error close'ing"};
}
}
void tcp_listen(int sock) {
if (listen(sock, SOMAXCONN) < 0) {
perror("listen");
throw std::runtime_error{"error close'ing"};
}
}