-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
96 lines (82 loc) · 2.18 KB
/
Client.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "Client.h"
#include <cstdio>
#include <unistd.h>
#include <poll.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <error.h>
#include <cerrno>
#include <cstdlib>
#include <iostream>
namespace rd {
bool active_socket::init() {
fd = socket(AF_INET, SOCK_STREAM, 0);
return fd != -1;
}
bool active_socket::connect(const char *addr, uint16_t port) {
struct sockaddr_in client = {
.sin_family = AF_INET,
.sin_port = htons(port),
};
client.sin_addr.s_addr = inet_addr(addr);
int i = ::connect(fd, (struct sockaddr *) &client, sizeof(client));
return i == 0;
}
void active_socket::close() {
::close(fd);
}
std::atomic<int> Client::id_generator{0};
void Client::run(const char *addr, uint16_t port, int input_fd, int output_fd) {
fprintf(stderr, "Client #%d is running: %s\\%d\n", id, addr, port);
if (!sock.init()) {
error(EXIT_FAILURE, errno, "Creating socket failed, client #%d", id);
}
if (!sock.connect(addr, port)) {
error(EXIT_FAILURE, errno, "Connecting socket failed, client #%d", id);
}
fprintf(stderr, "Client #%d connected\n", id);
struct pollfd fds[2];
auto const &socket_pfd = fds[0] = {
.fd = sock.fd,
.events = POLLIN
};
auto const &stdin_pfd = fds[1] = {
.fd = input_fd,
.events = POLLIN
};
nfds_t timeout = 1;
while (true) {
try_poll(fds, 2, timeout);
if (socket_pfd.revents & POLLHUP) {
fprintf(stderr, "Client %d closed", id);
break;
}
if (socket_pfd.revents & POLLIN) {
int len = try_read(socket_pfd.fd, socket_buffer, SOCKET_BUFFER_SIZE);
if (len == 0) {
continue;
}
write(output_fd, socket_buffer, len); //trace message
}
if (stdin_pfd.revents & POLLIN) {
int len = try_read(stdin_pfd.fd, stdin_buffer, STDIN_BUFFER_SIZE);
if (len == 0) {
continue;
}
int k = write(socket_pfd.fd, stdin_buffer, len);
if (len == k) {
fprintf(stderr, "Client #%d passed throught %d bytes\n", id, len);
} else {
error(EXIT_FAILURE, errno, "Passing through failed, client #%d", id);
}
}
if (stdin_pfd.revents != 0) {
pause();
}
}
}
void Client::close() {
sock.close();
}
Client::Client() : id(++id_generator) {}
}