| 1 |
// SPDX-License-Identifier: BSD-2-Clause |
| 2 |
// Copyright CM4all GmbH |
| 3 |
// author: Max Kellermann <[email protected]> |
| 4 |
|
| 5 |
#include "ConnectSocket.hxx" |
| 6 |
#include "net/UniqueSocketDescriptor.hxx" |
| 7 |
#include "net/SocketAddress.hxx" |
| 8 |
#include "net/AddressInfo.hxx" |
| 9 |
#include "net/SocketError.hxx" |
| 10 |
|
| 11 |
#include <cassert> |
| 12 |
#include <stdexcept> |
| 13 |
|
| 14 |
void |
| 15 |
ConnectSocketHandler::OnSocketConnectTimeout() noexcept |
| 16 |
{ |
| 17 |
/* default implementation falls back to OnSocketConnectError() */ |
| 18 |
OnSocketConnectError(std::make_exception_ptr(std::runtime_error("Connect timeout"))); |
| 19 |
} |
| 20 |
|
| 21 |
ConnectSocket::ConnectSocket(EventLoop &_event_loop, |
| 22 |
ConnectSocketHandler &_handler) noexcept |
| 23 |
:handler(_handler), |
| 24 |
event(_event_loop, BIND_THIS_METHOD(OnEvent)), |
| 25 |
timeout_event(_event_loop, BIND_THIS_METHOD(OnTimeout)) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
ConnectSocket::~ConnectSocket() noexcept |
| 30 |
{ |
| 31 |
event.Close(); |
| 32 |
} |
| 33 |
|
| 34 |
void |
| 35 |
ConnectSocket::Cancel() noexcept |
| 36 |
{ |
| 37 |
assert(IsPending()); |
| 38 |
|
| 39 |
timeout_event.Cancel(); |
| 40 |
event.Close(); |
| 41 |
} |
| 42 |
|
| 43 |
static UniqueSocketDescriptor |
| 44 |
Connect(const SocketAddress address) |
| 45 |
{ |
| 46 |
UniqueSocketDescriptor fd; |
| 47 |
if (!fd.CreateNonBlock(address.GetFamily(), SOCK_STREAM, 0)) |
| 48 |
throw MakeSocketError("Failed to create socket"); |
| 49 |
|
| 50 |
if (!fd.Connect(address)) { |
| 51 |
const auto e = GetSocketError(); |
| 52 |
if (!IsSocketErrorConnectWouldBlock(e)) |
| 53 |
throw MakeSocketError(e, "Failed to connect"); |
| 54 |
} |
| 55 |
|
| 56 |
return fd; |
| 57 |
} |
| 58 |
|
| 59 |
bool |
| 60 |
ConnectSocket::Connect(const SocketAddress address, |
| 61 |
Event::Duration timeout) noexcept |
| 62 |
{ |
| 63 |
assert(!event.IsDefined()); |
| 64 |
|
| 65 |
try { |
| 66 |
WaitConnected(::Connect(address), timeout); |
| 67 |
return true; |
| 68 |
} catch (...) { |
| 69 |
handler.OnSocketConnectError(std::current_exception()); |
| 70 |
return false; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static UniqueSocketDescriptor |
| 75 |
Connect(const AddressInfo &address) |
| 76 |
{ |
| 77 |
UniqueSocketDescriptor fd; |
| 78 |
if (!fd.CreateNonBlock(address.GetFamily(), address.GetType(), |
| 79 |
address.GetProtocol())) |
| 80 |
throw MakeSocketError("Failed to create socket"); |
| 81 |
|
| 82 |
if (!fd.Connect(address)) { |
| 83 |
const auto e = GetSocketError(); |
| 84 |
if (!IsSocketErrorConnectWouldBlock(e)) |
| 85 |
throw MakeSocketError(e, "Failed to connect"); |
| 86 |
} |
| 87 |
|
| 88 |
return fd; |
| 89 |
} |
| 90 |
|
| 91 |
bool |
| 92 |
ConnectSocket::Connect(const AddressInfo &address, |
| 93 |
Event::Duration timeout) noexcept |
| 94 |
{ |
| 95 |
assert(!event.IsDefined()); |
| 96 |
|
| 97 |
try { |
| 98 |
WaitConnected(::Connect(address), timeout); |
| 99 |
return true; |
| 100 |
} catch (...) { |
| 101 |
handler.OnSocketConnectError(std::current_exception()); |
| 102 |
return false; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
ConnectSocket::WaitConnected(UniqueSocketDescriptor _fd, |
| 108 |
Event::Duration timeout) noexcept |
| 109 |
{ |
| 110 |
assert(!event.IsDefined()); |
| 111 |
|
| 112 |
event.Open(_fd.Release()); |
| 113 |
event.ScheduleWrite(); |
| 114 |
|
| 115 |
if (timeout >= Event::Duration{}) |
| 116 |
timeout_event.Schedule(timeout); |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
ConnectSocket::OnEvent(unsigned events) noexcept |
| 121 |
{ |
| 122 |
timeout_event.Cancel(); |
| 123 |
|
| 124 |
if (SocketEvent::ERROR == 0 || events & SocketEvent::ERROR) { |
| 125 |
int s_err = event.GetSocket().GetError(); |
| 126 |
if (s_err != 0) { |
| 127 |
event.Close(); |
| 128 |
handler.OnSocketConnectError(std::make_exception_ptr(MakeSocketError(s_err, "Failed to connect"))); |
| 129 |
return; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
handler.OnSocketConnectSuccess(UniqueSocketDescriptor{AdoptTag{}, event.ReleaseSocket()}); |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
ConnectSocket::OnTimeout() noexcept |
| 138 |
{ |
| 139 |
event.Close(); |
| 140 |
|
| 141 |
handler.OnSocketConnectTimeout(); |
| 142 |
} |