| 114 | |
| 115 | |
| 116 | Future<Nothing> PollSocketImpl::connect( |
| 117 | const Address& address) |
| 118 | { |
| 119 | Try<Nothing, SocketError> connect = network::connect(get(), address); |
| 120 | if (connect.isError()) { |
| 121 | if (net::is_inprogress_error(connect.error().code)) { |
| 122 | // Need to hold a copy of `this` so that the underlying socket |
| 123 | // doesn't end up getting reused before we return from the call |
| 124 | // to `io::poll` and end up connecting incorrectly. |
| 125 | auto self = shared(this); |
| 126 | |
| 127 | return io::poll(get(), io::WRITE) |
| 128 | .then([self, address]() -> Future<Nothing> { |
| 129 | // Now check that a successful connection was made. |
| 130 | int opt; |
| 131 | socklen_t optlen = sizeof(opt); |
| 132 | |
| 133 | // NOTE: We cast to `char*` here because the function |
| 134 | // prototypes on Windows use `char*` instead of `void*`. |
| 135 | if (::getsockopt( |
| 136 | self->get(), |
| 137 | SOL_SOCKET, |
| 138 | SO_ERROR, |
| 139 | reinterpret_cast<char*>(&opt), |
| 140 | &optlen) < 0) { |
| 141 | return Failure(SocketError( |
| 142 | "Failed to get status of connect to " + stringify(address))); |
| 143 | } |
| 144 | |
| 145 | if (opt != 0) { |
| 146 | return Failure(SocketError( |
| 147 | opt, |
| 148 | "Failed to connect to " + |
| 149 | stringify(address))); |
| 150 | } |
| 151 | |
| 152 | return Nothing(); |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | return Failure(connect.error()); |
| 157 | } |
| 158 | |
| 159 | return Nothing(); |
| 160 | } |
| 161 | |
| 162 | #ifdef USE_SSL_SOCKET |
| 163 | Future<Nothing> PollSocketImpl::connect( |