-
Notifications
You must be signed in to change notification settings - Fork 0
/
net-socket.cpp
143 lines (140 loc) · 3.68 KB
/
net-socket.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "net-socket.h"
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace rtypes;
using namespace minecraft_controller;
network_socket_address::network_socket_address()
: _addrInfo(NULL), _pWalker(NULL)
{
}
network_socket_address::network_socket_address(const char* address,const char* portNo)
: _addrInfo(NULL), _pWalker(NULL)
{
assign(address,portNo);
}
network_socket_address::~network_socket_address()
{
if (_addrInfo != NULL)
freeaddrinfo(reinterpret_cast<addrinfo*>(_addrInfo));
}
void network_socket_address::_assignAddress(const char* address,const char* service)
{
if (_addrInfo != NULL) {
freeaddrinfo(reinterpret_cast<addrinfo*>(_addrInfo));
_addrInfo = NULL;
}
addrinfo hints;
memset(&hints,0,sizeof(addrinfo));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// try to get address information
if (getaddrinfo(address,service,&hints,(addrinfo**)&_addrInfo) == 0)
_pWalker = _addrInfo;
}
bool network_socket_address::_getNextAddress(const void*& address,rtypes::size_type& sz) const
{
if (_addrInfo!=NULL && _pWalker!=NULL) {
addrinfo* tmp = reinterpret_cast<addrinfo*>(_pWalker);
_pWalker = tmp->ai_next;
if (_pWalker == NULL)
_pWalker = _addrInfo;
address = tmp->ai_addr;
sz = tmp->ai_addrlen;
return tmp->ai_next != NULL;
}
address = NULL;
sz = 0;
return false;
}
void network_socket_address::_prepareAddressString(char* buffer,rtypes::size_type length) const
{
// use the most current address pointed to by _pWalker
if (_pWalker != NULL) {
const addrinfo* pitem = reinterpret_cast<addrinfo*>(_pWalker);
getnameinfo(pitem->ai_addr,pitem->ai_addrlen,buffer,length,NULL,0,0);
}
else
buffer[0] = 0;
}
network_socket::network_socket()
{
_addrbuf = new sockaddr_in;
}
network_socket::~network_socket()
{
delete reinterpret_cast<sockaddr_in*>(_addrbuf);
}
void network_socket::_openSocket(int& fd)
{
fd = ::socket(AF_INET,SOCK_STREAM,0);
if (fd == -1)
throw network_socket_error();
}
void network_socket::_createClientSocket(socket*& socknew)
{
socknew = new network_socket;
}
void* network_socket::_getAddressBuffer(size_type& length)
{
length = sizeof(sockaddr_in);
return _addrbuf;
}
void network_socket::_addressBufferToString(str& s) const
{
size_type i;
s.resize(20);
inet_ntop(AF_INET,&reinterpret_cast<const sockaddr_in*>(_addrbuf)->sin_addr,&s[0],s.size());
i = 0;
while (i < s.size()) {
if (s[i] == 0)
break;
++i;
}
s.resize(i);
}
network_socket_stream::network_socket_stream()
{
}
network_socket_stream::network_socket_stream(network_socket& device)
: stream_device<network_socket>(device)
{
}
network_socket_stream::~network_socket_stream()
{
if ( !_bufOut.is_empty() )
_outDevice();
}
void network_socket_stream::_clearDevice()
{
// no implementation
}
bool network_socket_stream::_openDevice(const char*)
{
_device->open();
return _device->get_last_operation_status() == no_operation;
}
void network_socket_stream::_closeDevice()
{
_device->close();
}
bool network_socket_stream::_inDevice() const
{
char buffer[4096];
_device->read(buffer,4096);
if (_device->get_last_operation_status() == success_read) {
_bufIn.push_range(buffer,_device->get_last_byte_count());
return true;
}
return false;
}
void network_socket_stream::_outDevice()
{
// write the entire buffer
_device->write(&_bufOut.peek(),_bufOut.size());
_bufOut.clear();
}