-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_socket.cpp
More file actions
123 lines (96 loc) · 2.66 KB
/
tcp_socket.cpp
File metadata and controls
123 lines (96 loc) · 2.66 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "tcp_socket.h"
#include "util.h"
#include "exception.h"
#include <string>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <poll.h>
#include <iostream>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
TCPSocket::TCPSocket(bool verbose_in, bool debug_in) : IPSocket(verbose_in, debug_in)
{
if(debug)
std::cerr << "TCPSocket called" << std::endl;
}
TCPSocket::~TCPSocket()
{
if(debug)
std::cerr << "~TCPSocket called" << std::endl;
}
void TCPSocket::__connect(int timeout)
{
struct addrinfo hints;
struct addrinfo *res = nullptr;
struct pollfd pfd;
struct sockaddr_in6 saddr;
if(debug)
std::cerr << "TCPSocket::__connect called" << std::endl;
try
{
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV;
if(getaddrinfo(host.c_str(), service.c_str(), &hints, &res))
{
if(res)
freeaddrinfo(res);
throw("unknown host");
}
if(!res || !res->ai_addr)
throw("resolve failed");
saddr = *(struct sockaddr_in6 *)res->ai_addr;
freeaddrinfo(res);
if((socket_fd = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)
throw("socket failed");
pfd.fd = socket_fd;
pfd.events = POLLOUT;
pfd.revents = 0;
if((::connect(socket_fd, (const struct sockaddr *)&saddr, sizeof(saddr))) && (errno != EINPROGRESS))
throw("connect failed");
if(poll(&pfd, 1, timeout) != 1)
throw("connect timeout");
if(pfd.revents & (POLLERR | POLLHUP))
throw("connect poll error");
if(!(pfd.revents & POLLOUT))
throw("connect poll unfinished");
}
catch(const char *e)
{
throw(hard_exception(std::string("TCPSocket connect: ") + e + ", connecting to " + host));
}
}
void TCPSocket::__disconnect()
{
if(debug)
std::cerr << "TCPSocket::__disconnect called" << std::endl;
if(socket_fd >= 0)
close(socket_fd);
socket_fd = -1;
}
void TCPSocket::__send(const std::string &data) const
{
if(debug)
std::cerr << "TCPSocket::__send called: " << data.length() << std::endl;
if(::send(socket_fd, data.data(), data.length(), 0) <= 0)
throw(transient_exception(boost::format("TCPSocket::send failed: %s") % strerror(errno)));
}
void TCPSocket::__receive(std::string &data) const
{
int length;
if(debug)
std::cerr << "TCPSocket::__receive called" << std::endl;
if(ioctl(socket_fd, FIONREAD, &length))
{
std::cerr << "tcp: fionread error\n";
length = 65536;
}
data.resize(length);
if((length = ::recv(socket_fd, data.data(), data.size(), 0)) <= 0)
throw(hard_exception(boost::format("TCPSocket::receive: receive error: %d") % length));
data.resize(length);
if(debug)
std::cerr << boost::format("received %d bytes by tcp\n") % length;
}