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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* TCPConnector.cpp
* Copyright (C) 2012 Simon Newton
*/
#include <errno.h>
#ifdef _WIN32
#include <Ws2tcpip.h>
#ifndef ETIMEDOUT
#define ETIMEDOUT WSAETIMEDOUT
#endif // !ETIMEDOUT
#endif // _WIN32
#include "ola/Logging.h"
#include "ola/network/NetworkUtils.h"
#include "ola/network/TCPConnector.h"
#include "ola/stl/STLUtils.h"
namespace ola {
namespace network {
/*
* A TCP socket waiting to connect.
*/
class PendingTCPConnection: public ola::io::WriteFileDescriptor {
public:
PendingTCPConnection(TCPConnector *connector,
const IPV4Address &ip,
int fd,
TCPConnector::TCPConnectCallback *callback);
ola::io::DescriptorHandle WriteDescriptor() const { return m_handle; }
void PerformWrite();
void Close();
const IPV4Address ip_address;
TCPConnector::TCPConnectCallback *callback;
ola::thread::timeout_id timeout_id;
private:
TCPConnector *m_connector;
ola::io::DescriptorHandle m_handle;
};
PendingTCPConnection::PendingTCPConnection(
TCPConnector *connector,
const IPV4Address &ip,
int fd,
TCPConnector::TCPConnectCallback *callback)
: WriteFileDescriptor(),
ip_address(ip),
callback(callback),
timeout_id(ola::thread::INVALID_TIMEOUT),
m_connector(connector) {
#ifdef _WIN32
m_handle.m_handle.m_fd = fd;
m_handle.m_type = ola::io::SOCKET_DESCRIPTOR;
#else
m_handle = fd;
#endif // _WIN32
}
/*
* Close this connection
*/
void PendingTCPConnection::Close() {
#ifdef _WIN32
close(m_handle.m_handle.m_fd);
#else
close(m_handle);
#endif // _WIN32
}
/*
* Called when the socket becomes writeable
*/
void PendingTCPConnection::PerformWrite() {
m_connector->SocketWritable(this);
}
void DeleteConnection(PendingTCPConnection *connection) {
delete connection;
}
TCPConnector::TCPConnector(ola::io::SelectServerInterface *ss)
: m_ss(ss) {
}
TCPConnector::~TCPConnector() {
CancelAll();
}
TCPConnector::TCPConnectionID TCPConnector::Connect(
const IPV4SocketAddress &endpoint,
const ola::TimeInterval &timeout,
TCPConnectCallback *callback) {
struct sockaddr server_address;
if (!endpoint.ToSockAddr(&server_address, sizeof(server_address))) {
callback->Run(-1, 0);
return 0;
}
int sd = socket(endpoint.Family(), SOCK_STREAM, 0);
if (sd < 0) {
int error = errno;
OLA_WARN << "socket() failed, " << strerror(error);
callback->Run(-1, error);
return 0;
}
#ifdef _WIN32
ola::io::DescriptorHandle descriptor;
descriptor.m_handle.m_fd = sd;
descriptor.m_type = ola::io::SOCKET_DESCRIPTOR;
#else
ola::io::DescriptorHandle descriptor = sd;
#endif // _WIN32
ola::io::ConnectedDescriptor::SetNonBlocking(descriptor);
int r = connect(sd, &server_address, sizeof(server_address));
if (r) {
#ifdef _WIN32
if (WSAGetLastError() != WSAEWOULDBLOCK) {
#else
if (errno != EINPROGRESS) {
#endif // _WIN32
int error = errno;
OLA_WARN << "connect() to " << endpoint << " returned, "
<< strerror(error);
close(sd);
callback->Run(-1, error);
return 0;
}
} else {
// Connect returned immediately
// The callback takes ownership of the socket descriptor.
callback->Run(sd, 0);
// coverity[RESOURCE_LEAK]
return 0;
}
PendingTCPConnection *connection = new PendingTCPConnection(
this,
endpoint.Host(),
sd,
callback);
m_connections.insert(connection);
connection->timeout_id = m_ss->RegisterSingleTimeout(
timeout,
ola::NewSingleCallback(this, &TCPConnector::TimeoutEvent, connection));
m_ss->AddWriteDescriptor(connection);
return connection;
}
bool TCPConnector::Cancel(TCPConnectionID id) {
PendingTCPConnection *connection =
const_cast<PendingTCPConnection*>(
reinterpret_cast<const PendingTCPConnection*>(id));
ConnectionSet::iterator iter = m_connections.find(connection);
if (iter == m_connections.end())
return false;
if (connection->timeout_id != ola::thread::INVALID_TIMEOUT) {
m_ss->RemoveTimeout(connection->timeout_id);
connection->timeout_id = ola::thread::INVALID_TIMEOUT;
}
Timeout(iter);
m_connections.erase(iter);
return true;
}
void TCPConnector::CancelAll() {
ConnectionSet::iterator iter = m_connections.begin();
for (; iter != m_connections.end(); ++iter) {
PendingTCPConnection *connection = *iter;
if (connection->timeout_id != ola::thread::INVALID_TIMEOUT) {
m_ss->RemoveTimeout(connection->timeout_id);
connection->timeout_id = ola::thread::INVALID_TIMEOUT;
}
Timeout(iter);
}
m_connections.clear();
}
/*
* Called when a socket becomes writeable.
*/
void TCPConnector::SocketWritable(PendingTCPConnection *connection) {
// Cancel the timeout
m_ss->RemoveTimeout(connection->timeout_id);
connection->timeout_id = ola::thread::INVALID_TIMEOUT;
m_ss->RemoveWriteDescriptor(connection);
// fetch the error code
#ifdef _WIN32
int sd = connection->WriteDescriptor().m_handle.m_fd;
#else
int sd = connection->WriteDescriptor();
#endif // _WIN32
int error = 0;
socklen_t len;
len = sizeof(error);
#ifdef _WIN32
int r = getsockopt(sd, SOL_SOCKET, SO_ERROR,
reinterpret_cast<char*>(&error), &len);
#else
int r = getsockopt(sd, SOL_SOCKET, SO_ERROR, &error, &len);
#endif // _WIN32
if (r < 0) {
error = errno;
}
ConnectionSet::iterator iter = m_connections.find(connection);
if (iter != m_connections.end()) {
m_connections.erase(iter);
}
// we're already within the PendingTCPConnection's call stack here
// schedule the deletion to run later
m_ss->Execute(ola::NewSingleCallback(DeleteConnection, connection));
if (error) {
OLA_WARN << "connect() to " << connection->ip_address << " returned: "
<< strerror(error);
connection->Close();
connection->callback->Run(-1, error);
} else {
#ifdef _WIN32
connection->callback->Run(connection->WriteDescriptor().m_handle.m_fd, 0);
#else
connection->callback->Run(connection->WriteDescriptor(), 0);
#endif // _WIN32
}
}
/**
* Delete this pending connection
*/
void TCPConnector::FreePendingConnection(PendingTCPConnection *connection) {
delete connection;
}
void TCPConnector::Timeout(const ConnectionSet::iterator &iter) {
PendingTCPConnection *connection = *iter;
m_ss->RemoveWriteDescriptor(connection);
connection->Close();
TCPConnectCallback *callback = connection->callback;
delete connection;
callback->Run(-1, ETIMEDOUT);
}
/**
* Called when the connect() times out.
*/
void TCPConnector::TimeoutEvent(PendingTCPConnection *connection) {
ConnectionSet::iterator iter = m_connections.find(connection);
if (iter == m_connections.end()) {
OLA_FATAL <<
"Timeout triggered but couldn't find the connection this refers to";
}
connection->timeout_id = ola::thread::INVALID_TIMEOUT;
Timeout(iter);
m_connections.erase(iter);
}
} // namespace network
} // namespace ola
|