13 #ifndef __PROCESS_SOCKET_HPP__
14 #define __PROCESS_SOCKET_HPP__
17 #include <sys/socket.h>
55 class SocketImpl :
public std::enable_shared_from_this<SocketImpl>
220 template <
typename T>
223 std::shared_ptr<T> pointer =
224 std::dynamic_pointer_cast<T>(CHECK_NOTNULL(t)->shared_from_this());
240 template <
typename AddressType>
245 std::is_convertible<AddressType, network::Address>::value,
246 "Requires type convertible to `network::Address`");
313 return impl == that.impl;
323 return convert<AddressType>(impl->address());
328 return convert<AddressType>(impl->peer());
338 return convert<AddressType>(impl->bind(address));
343 return impl->listen(backlog);
351 std::shared_ptr<SocketImpl>
self = impl->shared_from_this();
353 return impl->accept()
355 .then([
self](
const std::shared_ptr<SocketImpl>& accepted) {
362 return impl->connect(address);
367 return impl->recv(data, size);
372 return impl->send(data, size);
377 return impl->sendfile(fd, offset, size);
382 return impl->recv(
size);
387 return impl->send(data);
405 case Shutdown::READ_WRITE:
return SHUT_RDWR;
410 return impl->shutdown(how);
423 template <
typename T>
426 explicit Socket(std::shared_ptr<SocketImpl>&& that) : impl(std::move(that)) {}
428 explicit Socket(
const std::shared_ptr<SocketImpl>& that) : impl(that) {}
430 std::shared_ptr<SocketImpl> impl;
446 #endif // __WINDOWS__
508 #endif // __WINDOWS__
514 #endif // __PROCESS_SOCKET_HPP__
network::internal::Socket< network::Address > Socket
Definition: socket.hpp:436
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:375
Future< std::string > recv(const Option< ssize_t > &size=None())
Definition: socket.hpp:380
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:35
Definition: option.hpp:28
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:100
int_fd release()
Releases ownership of the file descriptor.
Definition: socket.hpp:210
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:365
virtual Try< Nothing > shutdown(int how)
Shuts down the socket.
Definition: socket.hpp:190
int_fd s
Definition: socket.hpp:229
static std::shared_ptr< T > shared(T *t)
Returns a std::shared_ptr<T> from this implementation.
Definition: socket.hpp:221
Kind
Available kinds of implementations.
Definition: socket.hpp:64
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:257
virtual Future< size_t > send(const char *data, size_t size)=0
Definition: address.hpp:277
static Kind DEFAULT_KIND()
Returns the default Kind of implementation.
Definition: errorbase.hpp:49
constexpr int SHUT_RDWR
Definition: windows.hpp:199
Try< Nothing > listen(int backlog)
Definition: socket.hpp:341
Try< Address > bind(const Address &address)
Assigns the specified address to the socket.
friend class Socket
Definition: socket.hpp:424
virtual Future< Nothing > connect(const Address &address)=0
Future< size_t > send(const char *data, size_t size) const
Definition: socket.hpp:370
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:44
SocketImpl(int_fd _s)
Definition: socket.hpp:202
Try< Nothing > close(int fd)
Definition: close.hpp:24
const short WRITE
A possible event while polling.
Definition: io.hpp:39
Implementation interface for a Socket.
Definition: socket.hpp:55
static Try error(const E &e)
Definition: try.hpp:42
#define UNREACHABLE()
Definition: unreachable.hpp:22
SocketImpl::Kind kind() const
Returns the kind representing the underlying implementation of the Socket instance.
Definition: socket.hpp:306
Shutdown
Definition: socket.hpp:390
Try< AddressType > bind(const AddressType &address)
Definition: socket.hpp:336
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
bool isError() const
Definition: try.hpp:71
bool operator==(const Socket &that) const
Definition: socket.hpp:311
Try< Nothing > shutdown(Shutdown shutdown=Shutdown::READ)
Definition: socket.hpp:399
Future< Nothing > send(const std::string &data)
Definition: socket.hpp:385
constexpr int SHUT_RD
Definition: windows.hpp:197
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
Future< Socket > accept()
Definition: socket.hpp:346
int int_fd
Definition: int_fd.hpp:35
virtual ~SocketImpl()
Definition: socket.hpp:105
An abstraction around a socket (file descriptor).
Definition: socket.hpp:241
Family
Definition: address.hpp:286
Try< AddressType > address() const
Definition: socket.hpp:321
constexpr int SHUT_WR
Definition: windows.hpp:198
const T & get() const
Definition: try.hpp:73
Try< AddressType > peer() const
Definition: socket.hpp:326
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:57
Future< Nothing > connect(const AddressType &address)
Definition: socket.hpp:360
virtual Future< size_t > recv(char *data, size_t size)=0