1#ifndef LIBFILEZILLA_SOCKET_HEADER
2#define LIBFILEZILLA_SOCKET_HEADER
63 return (
static_cast<std::underlying_type_t<socket_event_flag>
>(lhs) &
static_cast<std::underlying_type_t<socket_event_flag>
>(rhs)) != 0;
67 return static_cast<socket_event_flag>(
static_cast<std::underlying_type_t<socket_event_flag>
>(lhs) |
static_cast<std::underlying_type_t<socket_event_flag>
>(rhs));
84class FZ_PUBLIC_SYMBOL socket_event_source
87 virtual ~socket_event_source() =
default;
93 socket_event_source*
root()
const {
103 socket_event_source*
const root_{};
107struct socket_event_type;
134struct hostaddress_event_type{};
166class FZ_PUBLIC_SYMBOL socket_base
186 std::string
local_ip(
bool strip_zone_index =
false)
const;
195 static std::string address_to_string(sockaddr
const* addr,
int addr_len,
bool with_port =
true,
bool strip_zone_index =
false);
196 static std::string address_to_string(
char const* buf,
int buf_len);
203 bool bind(std::string
const& address);
206 typedef intptr_t socket_t;
208 typedef int socket_t;
214 friend class socket_thread;
217 virtual ~socket_base() =
default;
225 socket_thread* socket_thread_{};
231 unsigned int port_{};
235 int buffer_sizes_[2];
250class FZ_PUBLIC_SYMBOL socket_descriptor final
253 socket_descriptor() =
default;
254 ~socket_descriptor();
255 explicit socket_descriptor(socket_base::socket_t fd) noexcept : fd_(fd) {}
257 socket_descriptor(socket_descriptor
const&) =
delete;
258 socket_descriptor& operator=(socket_descriptor
const&) =
delete;
260 socket_descriptor(socket_descriptor && rhs)
noexcept { std::swap(fd_, rhs.fd_); }
261 socket_descriptor& operator=(socket_descriptor && rhs)
noexcept {
262 std::swap(fd_, rhs.fd_);
266 socket_base::socket_t detach() {
267 socket_base::socket_t ret = fd_;
272 explicit operator bool()
const {
return fd_ != -1; }
279 std::string
peer_ip(
bool strip_zone_index =
false)
const;
289 socket_base::socket_t fd_{-1};
299class FZ_PUBLIC_SYMBOL listen_socket final :
public socket_base,
public socket_event_source
301 friend class socket_base;
302 friend class socket_thread;
305 virtual ~listen_socket();
307 listen_socket(listen_socket
const&) =
delete;
308 listen_socket& operator=(listen_socket
const&) =
delete;
319 int listen(address_type family,
int port = 0);
374class FZ_PUBLIC_SYMBOL socket_interface :
public socket_event_source
377 socket_interface(socket_interface
const&) =
delete;
378 socket_interface& operator=(socket_interface
const&) =
delete;
380 virtual int read(
void*
buffer,
unsigned int size,
int&
error) = 0;
381 virtual int write(
void const*
buffer,
unsigned int size,
int&
error) = 0;
383 template<
typename T, std::enable_if_t<std::is_
signed_v<T>,
int> = 0>
391 return read(
buffer,
static_cast<unsigned int>(size),
error);
393 template<
typename T, std::enable_if_t<std::is_
unsigned_v<T> && (sizeof(T) > sizeof(
unsigned int)),
int> = 0>
396 if (size > std::numeric_limits<unsigned int>::max()) {
397 size = std::numeric_limits<unsigned int>::max();
399 return read(
buffer,
static_cast<unsigned int>(size),
error);
402 template<
typename T, std::enable_if_t<std::is_
signed_v<T>,
int> = 0>
410 return write(
buffer,
static_cast<std::make_unsigned_t<T>
>(size),
error);
412 template<
typename T, std::enable_if_t<std::is_
unsigned_v<T> && (sizeof(T) > sizeof(
unsigned int)),
int> = 0>
415 if (size > std::numeric_limits<unsigned int>::max()) {
416 size = std::numeric_limits<unsigned int>::max();
418 return write(
buffer,
static_cast<unsigned int>(size),
error);
424 virtual int peer_port(
int&
error)
const = 0;
426 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown) = 0;
446 socket_interface() =
delete;
448 explicit socket_interface(socket_event_source *
root)
449 : socket_event_source(
root)
461class FZ_PUBLIC_SYMBOL socket final :
public socket_base,
public socket_interface
463 friend class socket_thread;
468 socket(socket
const&) =
delete;
469 socket& operator=(socket
const&) =
delete;
474 bool is_connected()
const {
492 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown)
override;
533 std::string
peer_ip(
bool strip_zone_index =
false)
const;
578 int flags()
const {
return flags_; }
595 socket_t get_descriptor();
635 socket_state state_{};
653class FZ_PUBLIC_SYMBOL socket_layer :
public socket_interface
656 explicit socket_layer(
event_handler* handler, socket_interface& next_layer,
bool event_passthrough);
657 virtual ~socket_layer();
659 socket_layer(socket_layer
const&) =
delete;
660 socket_layer& operator=(socket_layer
const&) =
delete;
680 socket_interface&
next() {
return next_layer_; }
705 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown)
override {
706 return next_layer_.connect(host, port, family);
710 return next_layer_.shutdown();
714 return next_layer_.get_state();
739 socket_interface& next_layer_;
740 bool event_passthrough_{};
762class FZ_PRIVATE_SYMBOL winsock_initializer final
765 winsock_initializer();
766 ~winsock_initializer();
773#define EISCONN WSAEISCONN
776#define EINPROGRESS WSAEINPROGRESS
779#define EAFNOSUPPORT WSAEAFNOSUPPORT
782#define EADDRINUSE WSAEADDRINUSE
785#define ENOBUFS WSAENOBUFS
787#ifndef EPROTONOSUPPORT
788#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
791#define EALREADY WSAEALREADY
794#define ECONNREFUSED WSAECONNREFUSED
797#define ENOTSOCK WSAENOTSOCK
800#define ETIMEDOUT WSAETIMEDOUT
803#define ENETUNREACH WSAENETUNREACH
806#define EHOSTUNREACH WSAEHOSTUNREACH
809#define ENOTCONN WSAENOTCONN
812#define ENETRESET WSAENETRESET
815#define EOPNOTSUPP WSAEOPNOTSUPP
818#define ESHUTDOWN WSAESHUTDOWN
821#define EMSGSIZE WSAEMSGSIZE
824#define ECONNABORTED WSAECONNABORTED
827#define ECONNRESET WSAECONNRESET
830#define EHOSTDOWN WSAEHOSTDOWN
832#ifndef ESOCKTNOSUPPORT
833#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:28
The duration class represents a time interval in milliseconds.
Definition time.hpp:291
Simple handler for asynchronous event processing.
Definition event_handler.hpp:55
Simple Listen socket.
Definition socket.hpp:300
socket_descriptor fast_accept(int &error)
Like accept, but only returns a socket descriptor.
int listen(address_type family, int port=0)
Starts listening.
std::unique_ptr< socket > accept(int &error, fz::event_handler *handler=nullptr)
Accepts incoming connection. If no socket is returned, error contains the reason.
A simple scoped lock.
Definition mutex.hpp:98
This is the recommended event class.
Definition event.hpp:68
Common base clase for fz::socket and fz::listen_socket.
Definition socket.hpp:167
std::string local_ip(bool strip_zone_index=false) const
Returns local address of a connected socket.
int set_buffer_sizes(int size_receive, int size_send)
Sets socket buffer sizes.
address_type address_family() const
If connected, either ipv4, ipv6 or unix, unknown otherwise.
bool bind(std::string const &address)
Bind socket to the specific local IP.
int local_port(int &error) const
Returns local port of a connected socket.
Lightweight holder for socket descriptors.
Definition socket.hpp:251
std::string peer_ip(bool strip_zone_index=false) const
Returns remote address of a connected socket.
int peer_port(int &error) const
Returns remote port of a connected socket.
All classes sending socket events should derive from this.
Definition socket.hpp:85
socket_event_source * root() const
Gets the root source.
Definition socket.hpp:93
virtual int shutdown()=0
Signals peers that we want to close the connections.
virtual int shutdown_read()=0
void set_event_passthrough(socket_event_flag retrigger_block=socket_event_flag{})
virtual native_string peer_host() const override
Definition socket.hpp:670
void forward_hostaddress_event(socket_event_source *source, std::string const &address)
virtual int shutdown() override
Signals peers that we want to close the connections.
Definition socket.hpp:709
void forward_socket_event(socket_event_source *source, socket_event_flag t, int error)
virtual void set_event_handler(event_handler *handler, fz::socket_event_flag retrigger_block=fz::socket_event_flag{}) override
The handler for any events generated (or forwarded) by this layer.
virtual int peer_port(int &error) const override
Definition socket.hpp:677
virtual int shutdown_read() override
Check that all layers further down also have reached EOF.
socket_interface & next()
The next layer further down. Usually another layer or the actual socket.
Definition socket.hpp:680
IPv6 capable, non-blocking socket class.
Definition socket.hpp:462
virtual native_string peer_host() const override
Returns the hostname passed to connect()
void set_flags(int flags, bool enable)
Enables or disabled the passed flags.
virtual int shutdown() override
Signals peers that we want to close the connections.
virtual int connect(native_string const &host, unsigned int port, address_type family=address_type::unknown) override
Starts connecting to the given host, given as name, IPv4 or IPv6 address.
virtual void set_event_handler(event_handler *pEvtHandler, fz::socket_event_flag retrigger_block=fz::socket_event_flag{}) override
Changes the associated event handler.
std::string peer_ip(bool strip_zone_index=false) const
Returns remote address of a connected socket.
void set_flags(int flags)
Sets the entire mask of enabled flag, disabling all others.
@ flag_keepalive
flag_keepalive enables TCP keepalive.
Definition socket.hpp:575
@ flag_nodelay
flag_nodelay disables Nagle's algorithm
Definition socket.hpp:572
virtual int write(void const *buffer, unsigned int size, int &error) override
Write data to socket.
virtual int peer_port(int &error) const override
Returns remote port of a connected socket.
virtual int read(void *buffer, unsigned int size, int &error) override
Read data from socket.
int ideal_send_buffer_size()
void set_keepalive_interval(duration const &d)
virtual int shutdown_read() override
Definition socket.hpp:593
A dumb thread-pool for asynchronous tasks.
Definition thread_pool.hpp:64
Declares the event_handler class.
Various functions to deal with IP address strings.
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
fz::socket_event_flag change_socket_event_handler(event_handler *old_handler, event_handler *new_handler, socket_event_source const *const source, fz::socket_event_flag remove)
Changes all pending socket events from source.
listen_socket_state
Definition socket.hpp:241
@ listening
Only in listening state you can get a connection event.
Definition socket.hpp:246
@ error
Operationf failed.
Definition aio.hpp:199
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition string.hpp:69
simple_event< hostaddress_event_type, socket_event_source *, std::string > hostaddress_event
Definition socket.hpp:139
socket_state
State transitions are monotonically increasing.
Definition socket.hpp:343
@ connected
Socket is in its normal working state. You can get send and receive events.
Definition socket.hpp:353
@ shut_down
Write side has finished shutting down. Receive still working normally.
Definition socket.hpp:360
@ failed
Socket has failed. Further events disabled.
Definition socket.hpp:366
@ closed
Socket has been closed. Further events disabled.
Definition socket.hpp:363
@ shutting_down
Definition socket.hpp:357
@ connecting
Definition socket.hpp:350
std::string socket_error_string(int error)
Gets a symbolic name for socket errors.
simple_event< socket_event_type, socket_event_source *, socket_event_flag, int > socket_event
Definition socket.hpp:131
void remove_socket_events(event_handler *handler, socket_event_source const *const source)
Remove all pending socket events from source sent to handler.
native_string socket_error_description(int error)
Gets a human-readable, translated description of the error.
@ read
Data has become available.
Definition process.hpp:28
@ write
data can be written.
Definition process.hpp:31
socket_event_flag
The type of a socket event.
Definition socket.hpp:35
@ connection
Definition socket.hpp:47
@ connection_next
Definition socket.hpp:41