-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_socket.cpp
More file actions
98 lines (77 loc) · 1.98 KB
/
ip_socket.cpp
File metadata and controls
98 lines (77 loc) · 1.98 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
#include "generic_socket.h"
#include "ip_socket.h"
#include "exception.h"
#include "packet.h"
#include <unistd.h>
#include <poll.h>
#include <iostream>
#include <string>
IPSocket::IPSocket(bool verbose_in, bool debug_in) : GenericSocket(verbose_in, debug_in)
{
if(debug)
std::cerr << "IPSocket called" << std::endl;
}
IPSocket::~IPSocket()
{
if(debug)
std::cerr << "~IPSocket called" << std::endl;
}
void IPSocket::_connect(int timeout)
{
if(debug)
std::cerr << "IPSocket::_connect called" << std::endl;
this->__connect(timeout);
}
void IPSocket::_disconnect()
{
if(debug)
std::cerr << "IPSocket::_disconnect called" << std::endl;
this->__disconnect();
}
void IPSocket::_send(const std::string &data, int timeout) const
{
struct pollfd pfd = { .fd = socket_fd, .events = POLLOUT | POLLERR | POLLHUP, .revents = 0 };
if(debug)
std::cerr << "IPSocket::_send called: " << data.length() << std::endl;
try
{
if(data.length() == 0)
throw("empty_buffer");
if(pfd.revents & (POLLERR | POLLHUP))
throw("poll error");
if(poll(&pfd, 1, timeout) != 1)
throw(" poll timeout");
}
catch(const char *e)
{
throw(hard_exception(boost::format("IPSocket::send: %s (%s)") % e % strerror(errno)));
}
this->__send(data);
}
void IPSocket::_receive(std::string &data, int timeout) const
{
std::string segment;
struct pollfd pfd = { .fd = socket_fd, .events = POLLIN | POLLERR | POLLHUP, .revents = 0 };
if(debug)
std::cerr << "IPSocket::_receive called" << std::endl;
try
{
for(;;)
{
if(pfd.revents & (POLLERR | POLLHUP))
throw("receive poll error");
if(poll(&pfd, 1, timeout) != 1)
throw(transient_exception("IPSocket::receive: poll timeout"));
this->__receive(segment);
data.append(segment);
if(!Packet::valid(data)) // FIXME telnet unpacketised
throw("IPSocket::invalid packet");
if(Packet::complete(data))
return;
}
}
catch(const char *e)
{
throw(hard_exception(boost::format("IPSocket::receive: %s (%s)") % e % strerror(errno)));
}
}