| 256 | */ |
| 257 | template <typename AddressType> |
| 258 | class Socket |
| 259 | { |
| 260 | public: |
| 261 | static_assert( |
| 262 | std::is_convertible<AddressType, network::Address>::value, |
| 263 | "Requires type convertible to `network::Address`"); |
| 264 | |
| 265 | /** |
| 266 | * Returns an instance of a `Socket` using the specified kind of |
| 267 | * implementation. |
| 268 | * |
| 269 | * @param s Optional. The file descriptor to wrap with the `Socket`. |
| 270 | * @param kind Optional. The desired `Socket` implementation. |
| 271 | * |
| 272 | * @return An instance of a `Socket`. |
| 273 | */ |
| 274 | static Try<Socket> create( |
| 275 | int_fd s, |
| 276 | SocketImpl::Kind kind = SocketImpl::DEFAULT_KIND()) |
| 277 | { |
| 278 | Try<std::shared_ptr<SocketImpl>> impl = SocketImpl::create(s, kind); |
| 279 | if (impl.isError()) { |
| 280 | return Error(impl.error()); |
| 281 | } |
| 282 | return Socket(impl.get()); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Returns an instance of a `Socket` using `AddressType` to determine |
| 287 | * the address family to use. An optional implementation kind can be |
| 288 | * specified. The NONBLOCK and CLOEXEC options will be set on the |
| 289 | * underlying file descriptor for the socket. |
| 290 | * |
| 291 | * @param kind Optional. The desired `Socket` implementation. |
| 292 | * |
| 293 | * @return An instance of a `Socket`. |
| 294 | */ |
| 295 | // TODO(josephw): MESOS-5729: Consider making the CLOEXEC option |
| 296 | // configurable by the caller of the interface. |
| 297 | static Try<Socket> create(SocketImpl::Kind kind = SocketImpl::DEFAULT_KIND()); |
| 298 | |
| 299 | /** |
| 300 | * Returns an instance of a `Socket` using the specified |
| 301 | * `Address::Family` to determine the address family to use. An |
| 302 | * optional implementation kind can be specified. The NONBLOCK and |
| 303 | * CLOEXEC options will be set on the underlying file descriptor for |
| 304 | * the socket. |
| 305 | * |
| 306 | * NOTE: this is only defined for `AddressType` of |
| 307 | * `network::Address`, all others are explicitly deleted. |
| 308 | * |
| 309 | * @param kind Optional. The desired `Socket` implementation. |
| 310 | * |
| 311 | * @return An instance of a `Socket`. |
| 312 | */ |
| 313 | static Try<Socket> create( |
| 314 | Address::Family family, |
| 315 | SocketImpl::Kind kind = SocketImpl::DEFAULT_KIND()); |