24 #include "TPSocket.hpp"
39 bool TPSocket::_wsaInitialized =
false;
55 int oldflags = fcntl(this->
_fd, F_GETFL, 0);
60 oldflags |= O_NONBLOCK;
62 oldflags &= ~O_NONBLOCK;
63 return(fcntl(this->
_fd, F_SETFL, oldflags) > -1);
65 u_long arg = (u_long)block;
66 ioctlsocket(this->
_fd, FIONBIO, &arg);
89 TPSocket::InitializeWSA();
102 const char* status = NULL;
103 struct sockaddr addr;
104 int addrlen =
sizeof(addr);
106 getsockname(this->
_fd,
107 (
struct sockaddr*)&addr, (socklen_t*)&addrlen);
108 struct sockaddr_in *addr_ptr = (
struct sockaddr_in*)&addr;
110 this->
local.
port = ntohs(addr_ptr->sin_port);
111 status = inet_ntop(AF_INET,
112 &(addr_ptr->sin_addr.s_addr), this->local.address,
114 return(status != NULL);
122 const char* status = NULL;
123 struct sockaddr addr;
124 int addrlen =
sizeof(addr);
126 getpeername(this->
_fd,
127 (
struct sockaddr*)&addr, (socklen_t*)&addrlen);
128 struct sockaddr_in *addr_ptr = (
struct sockaddr_in*)&addr;
130 this->
remote.
port = ntohs(addr_ptr->sin_port);
131 status = inet_ntop(AF_INET, &(addr_ptr->sin_addr.s_addr),
132 this->remote.address, (socklen_t)addrlen);
133 return(status != NULL);
140 socklen_t optlen =
sizeof(this->
_bsizemax);
142 if(getsockopt(this->
_fd, SOL_SOCKET, SO_SNDBUF, &this->
_bsizemax,
151 switch(this->_type) {
153 this->
_results.ai_socktype = SOCK_STREAM;
156 this->
_results.ai_socktype = SOCK_DGRAM;
162 this->
_results.ai_flags = AI_PASSIVE;
163 this->
_results.ai_family = AF_UNSPEC;
170 return(close(this->
_fd) == 0);
179 if(getaddrinfo(ip.c_str(), port.c_str(), &(this->
_results), &ai) != 0)
184 this->
_fd = socket(this->
_info->ai_family,
185 this->_info->ai_socktype,
186 this->_info->ai_protocol);
192 retopt = setsockopt(this->
_fd,
193 SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof(
int));
198 bndret = bind(this->
_fd, this->
_info->ai_addr, this->_info->ai_addrlen);
213 return(listen(this->
_fd, this->
_mc) == 0);
217 if(endpoint->_type != this->_type)
221 unsigned int addrlen =
sizeof(endpoint->
_address);
228 (socklen_t*)&addrlen);
232 return endpoint->
_fd;
239 if(getaddrinfo(ip.c_str(), port.c_str(), &(this->
_results), &ai) != 0)
243 this->
_fd = socket(this->
_info->ai_family,
244 this->_info->ai_socktype, this->_info->ai_protocol);
249 conopt = connect(this->
_fd, this->
_info->ai_addr,
250 this->_info->ai_addrlen);
268 switch(this->_type) {
271 bytes = send(this->
_fd, message.c_str(), message.size(),
274 bytes = send(this->
_fd, message.c_str(), message.size(), 0);
278 return sendto(this->
_fd, message.c_str(), message.size(), 0,
279 this->
_info->ai_addr, this->
_info->ai_addrlen);
287 switch(this->_type) {
299 (
struct sockaddr *)&this->
_endpoint, (socklen_t*)&addr_len);
302 (
struct sockaddr *)&this->
_endpoint, (socklen_t*)&addr_len);
308 message->assign((
const char*)this->
_buffer, (
size_t)bytes);
314 bool TPSocket::InitializeWSA(
void) {
315 if(TPSocket::_wsaInitialized ==
true)
319 TPSocket::_wsaInitialized = (WSAStartup(MAKEWORD(2,2), &wsaData) != 0);
320 return TPSocket::_wsaInitialized;
326 empty.push_back(
'\0');
327 return(this->
Send(empty) > (ssize_t)0);