13 #ifndef __PROCESS_SOCKET_HPP__ 14 #define __PROCESS_SOCKET_HPP__ 17 #include <sys/socket.h> 58 class SocketImpl :
public std::enable_shared_from_this<SocketImpl>
157 #ifdef USE_SSL_SOCKET 160 const openssl::TLSClientConfig& config) = 0;
215 #if defined(USE_SSL_SOCKET) && !defined(USE_LIBEVENT) 219 #endif // USE_SSL_SOCKET && !USE_LIBEVENT 237 template <
typename T>
240 std::shared_ptr<T> pointer =
241 std::dynamic_pointer_cast<T>(CHECK_NOTNULL(t)->shared_from_this());
257 template <
typename AddressType>
262 std::is_convertible<AddressType, network::Address>::value,
263 "Requires type convertible to `network::Address`");
330 return impl == that.impl;
340 return convert<AddressType>(impl->address());
345 return convert<AddressType>(impl->peer());
355 return convert<AddressType>(impl->bind(address));
360 return impl->listen(backlog);
368 std::shared_ptr<SocketImpl>
self = impl->shared_from_this();
370 return impl->accept()
372 .then([
self](
const std::shared_ptr<SocketImpl>& accepted) {
381 return impl->connect(address);
384 #ifdef USE_SSL_SOCKET 389 const openssl::TLSClientConfig& config)
391 return impl->connect(address, config);
397 return impl->recv(data, size);
402 return impl->send(data, size);
407 return impl->sendfile(fd, offset, size);
412 return impl->recv(
size);
417 return impl->send(data);
435 case Shutdown::READ_WRITE:
return SHUT_RDWR;
440 return impl->shutdown(how);
453 template <
typename T>
456 explicit Socket(std::shared_ptr<SocketImpl>&& that) : impl(
std::move(that)) {}
458 explicit Socket(
const std::shared_ptr<SocketImpl>& that) : impl(that) {}
460 std::shared_ptr<SocketImpl> impl;
476 #endif // __WINDOWS__ 538 #endif // __WINDOWS__ 544 #endif // __PROCESS_SOCKET_HPP__ network::internal::Socket< network::Address > Socket
Definition: socket.hpp:466
const short READ
A possible event while polling.
Definition: io.hpp:34
Future< size_t > sendfile(int_fd fd, off_t offset, size_t size) const
Definition: socket.hpp:405
Future< std::string > recv(const Option< ssize_t > &size=None())
Definition: socket.hpp:410
Definition: openssl_socket.hpp:27
virtual Kind kind() const =0
Definition: nothing.hpp:16
virtual Future< std::shared_ptr< SocketImpl > > accept()=0
Returns an implementation corresponding to the next pending connection for the listening socket...
Definition: errorbase.hpp:36
Definition: option.hpp:29
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
int_fd release()
Releases ownership of the file descriptor.
Definition: socket.hpp:227
T & get()&
Definition: try.hpp:80
virtual Future< size_t > sendfile(int_fd fd, off_t offset, size_t size)=0
Try< Address > peer() const
Returns the peer's Address for the accepted or connected socket.
Future< size_t > recv(char *data, size_t size) const
Definition: socket.hpp:395
int_fd s
Definition: socket.hpp:246
static std::shared_ptr< T > shared(T *t)
Returns a std::shared_ptr<T> from this implementation.
Definition: socket.hpp:238
Kind
Available kinds of implementations.
Definition: socket.hpp:67
static Try< Socket > create(int_fd s, SocketImpl::Kind kind=SocketImpl::DEFAULT_KIND())
Returns an instance of a Socket using the specified kind of implementation.
Definition: socket.hpp:274
virtual Future< size_t > send(const char *data, size_t size)=0
Definition: type_utils.hpp:619
Definition: address.hpp:324
static Kind DEFAULT_KIND()
Returns the default Kind of implementation.
constexpr int SHUT_RDWR
Definition: windows.hpp:193
virtual Try< Nothing, SocketError > shutdown(int how)
Shuts down the socket.
Definition: socket.hpp:201
Try< Nothing > listen(int backlog)
Definition: socket.hpp:358
Try< Address > bind(const Address &address)
Assigns the specified address to the socket.
virtual Future< Nothing > connect(const Address &address)=0
Future< size_t > send(const char *data, size_t size) const
Definition: socket.hpp:400
ErrnoError SocketError
Definition: error.hpp:33
Try< Nothing, SocketError > shutdown(Shutdown shutdown=Shutdown::READ)
Definition: socket.hpp:429
virtual Try< Nothing > listen(int backlog)=0
Try< Address > address() const
Returns the Address with the assigned ip and assigned port.
#define CHECK_SOME(expression)
Definition: check.hpp:50
SocketImpl(int_fd _s)
Definition: socket.hpp:213
Try< Nothing > close(int fd)
Definition: close.hpp:24
const short WRITE
A possible event while polling.
Definition: io.hpp:40
Implementation interface for a Socket.
Definition: socket.hpp:58
static Try error(const E &e)
Definition: try.hpp:43
#define UNREACHABLE()
Definition: unreachable.hpp:22
SocketImpl::Kind kind() const
Returns the kind representing the underlying implementation of the Socket instance.
Definition: socket.hpp:323
Shutdown
Definition: socket.hpp:420
Try< AddressType > bind(const AddressType &address)
Definition: socket.hpp:353
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:78
bool operator==(const Socket &that) const
Definition: socket.hpp:328
Definition: executor.hpp:48
Future< Nothing > send(const std::string &data)
Definition: socket.hpp:415
constexpr int SHUT_RD
Definition: windows.hpp:191
Future< Socket > accept()
Definition: socket.hpp:363
int int_fd
Definition: int_fd.hpp:35
virtual ~SocketImpl()
Definition: socket.hpp:108
An abstraction around a socket (file descriptor).
Definition: socket.hpp:258
Family
Definition: address.hpp:333
Try< AddressType > address() const
Definition: socket.hpp:338
constexpr int SHUT_WR
Definition: windows.hpp:192
Try< AddressType > peer() const
Definition: socket.hpp:343
static Try< std::shared_ptr< SocketImpl > > create(int_fd s, Kind kind=DEFAULT_KIND())
Returns an instance of a SocketImpl using the specified kind of implementation.
Definition: future.hpp:58
Future< Nothing > connect(const AddressType &address)
Definition: socket.hpp:379
virtual Future< size_t > recv(char *data, size_t size)=0