danicoin/src/Platform/Linux/System/TcpConnector.cpp

181 lines
6.2 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin 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 3 of the License, or
// (at your option) any later version.
//
// Bytecoin 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 Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#include "TcpConnector.h"
#include <cassert>
2015-05-27 12:08:46 +00:00
#include <stdexcept>
#include <fcntl.h>
2015-05-27 12:08:46 +00:00
#include <netdb.h>
#include <unistd.h>
#include <sys/epoll.h>
2015-05-27 12:08:46 +00:00
#include <System/InterruptedException.h>
#include <System/Ipv4Address.h>
#include "Dispatcher.h"
#include "TcpConnection.h"
2015-05-27 12:08:46 +00:00
namespace System {
namespace {
2015-07-30 15:22:07 +00:00
struct TcpConnectorContextExt : public OperationContext {
int connection;
};
}
TcpConnector::TcpConnector() : dispatcher(nullptr) {
}
2015-07-30 15:22:07 +00:00
TcpConnector::TcpConnector(Dispatcher& dispatcher) : dispatcher(&dispatcher), context(nullptr) {
}
TcpConnector::TcpConnector(TcpConnector&& other) : dispatcher(other.dispatcher) {
if (other.dispatcher != nullptr) {
2015-05-27 12:08:46 +00:00
assert(other.context == nullptr);
context = nullptr;
other.dispatcher = nullptr;
}
}
TcpConnector::~TcpConnector() {
}
TcpConnector& TcpConnector::operator=(TcpConnector&& other) {
dispatcher = other.dispatcher;
if (other.dispatcher != nullptr) {
2015-05-27 12:08:46 +00:00
assert(other.context == nullptr);
context = nullptr;
other.dispatcher = nullptr;
}
return *this;
}
2015-05-27 12:08:46 +00:00
TcpConnection TcpConnector::connect(const Ipv4Address& address, uint16_t port) {
assert(dispatcher != nullptr);
assert(context == nullptr);
2015-07-30 15:22:07 +00:00
if (dispatcher->interrupted()) {
throw InterruptedException();
}
2015-05-27 12:08:46 +00:00
std::string message;
int connection = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connection == -1) {
message = "socket() failed, errno=" + std::to_string(errno);
} else {
2015-05-27 12:08:46 +00:00
sockaddr_in bindAddress;
bindAddress.sin_family = AF_INET;
bindAddress.sin_port = 0;
bindAddress.sin_addr.s_addr = INADDR_ANY;
if (bind(connection, reinterpret_cast<sockaddr*>(&bindAddress), sizeof bindAddress) != 0) {
message = "bind failed, errno=" + std::to_string(errno);
} else {
2015-05-27 12:08:46 +00:00
int flags = fcntl(connection, F_GETFL, 0);
if (flags == -1 || fcntl(connection, F_SETFL, flags | O_NONBLOCK) == -1) {
message = "fcntl() failed errno=" + std::to_string(errno);
} else {
2015-05-27 12:08:46 +00:00
sockaddr_in addressData;
addressData.sin_family = AF_INET;
addressData.sin_port = htons(port);
addressData.sin_addr.s_addr = htonl(address.getValue());
int result = ::connect(connection, reinterpret_cast<sockaddr *>(&addressData), sizeof addressData);
if (result == -1) {
if (errno == EINPROGRESS) {
2015-07-30 15:22:07 +00:00
ContextPair contextPair;
2015-05-27 12:08:46 +00:00
TcpConnectorContextExt connectorContext;
connectorContext.interrupted = false;
connectorContext.context = dispatcher->getCurrentContext();
connectorContext.connection = connection;
contextPair.readContext = nullptr;
contextPair.writeContext = &connectorContext;
epoll_event connectEvent;
connectEvent.events = EPOLLOUT | EPOLLRDHUP | EPOLLERR | EPOLLONESHOT;
connectEvent.data.ptr = &contextPair;
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_ADD, connection, &connectEvent) == -1) {
message = "epoll_ctl() failed, errno=" + std::to_string(errno);
} else {
context = &connectorContext;
2015-07-30 15:22:07 +00:00
dispatcher->getCurrentContext()->interruptProcedure = [&] {
TcpConnectorContextExt* connectorContext1 = static_cast<TcpConnectorContextExt*>(context);
if (!connectorContext1->interrupted) {
if (close(connectorContext1->connection) == -1) {
throw std::runtime_error("TcpListener::stop, close failed, errno=" + std::to_string(errno));
}
connectorContext1->interrupted = true;
dispatcher->pushContext(connectorContext1->context);
}
};
2015-05-27 12:08:46 +00:00
dispatcher->dispatch();
2015-07-30 15:22:07 +00:00
dispatcher->getCurrentContext()->interruptProcedure = nullptr;
2015-05-27 12:08:46 +00:00
assert(dispatcher != nullptr);
assert(connectorContext.context == dispatcher->getCurrentContext());
assert(contextPair.readContext == nullptr);
assert(context == &connectorContext);
context = nullptr;
connectorContext.context = nullptr;
if (connectorContext.interrupted) {
throw InterruptedException();
}
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_DEL, connection, NULL) == -1) {
message = "epoll_ctl() failed, errno=" + std::to_string(errno);
} else {
2015-05-27 12:08:46 +00:00
if((connectorContext.events & (EPOLLERR | EPOLLHUP)) != 0) {
int result = close(connection);
assert(result != -1);
2015-05-27 12:08:46 +00:00
throw std::runtime_error("TcpConnector::connect, connection failed");
}
2015-05-27 12:08:46 +00:00
int retval = -1;
socklen_t retValLen = sizeof(retval);
int s = getsockopt(connection, SOL_SOCKET, SO_ERROR, &retval, &retValLen);
if (s == -1) {
message = "getsockopt() failed, errno=" + std::to_string(errno);
} else {
2015-05-27 12:08:46 +00:00
if (retval != 0) {
message = "connect failed; getsockopt retval =" + std::to_string(errno);
} else {
2015-05-27 12:08:46 +00:00
return TcpConnection(*dispatcher, connection);
}
}
}
}
}
2015-05-27 12:08:46 +00:00
} else {
return TcpConnection(*dispatcher, connection);
}
}
}
2015-05-27 12:08:46 +00:00
int result = close(connection);
assert(result != -1);
}
2015-05-27 12:08:46 +00:00
throw std::runtime_error("TcpConnector::connect, "+message);
}
}