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));
103 socket_event_source*
const root_{};
107 struct socket_event_type;
134 struct hostaddress_event_type{};
176 int set_buffer_sizes(
int size_receive,
int size_send);
179 address_type address_family()
const;
186 std::string local_ip(
bool strip_zone_index =
false)
const;
193 int local_port(
int&
error)
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;
225 socket_thread* socket_thread_{};
231 unsigned int port_{};
235 int buffer_sizes_[2];
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;
286 int peer_port(
int&
error)
const;
289 socket_base::socket_t fd_{-1};
302 friend class socket_thread;
319 int listen(address_type family,
int port = 0);
322 std::unique_ptr<socket> accept(
int& error,
fz::event_handler * handler =
nullptr);
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>
384 int read(
void* buffer, T size,
int& error)
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>
394 int read(
void* buffer, T size,
int& error)
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>
403 int write(
void const* buffer, T size,
int& error)
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>
413 int write(
void const* buffer, T size,
int& error)
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;
440 virtual int shutdown() = 0;
443 virtual int shutdown_read() = 0;
463 friend class socket_thread;
474 bool is_connected()
const {
492 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown)
override;
509 virtual int read(
void *
buffer,
unsigned int size,
int& error)
override;
526 virtual int write(
void const* buffer,
unsigned int size,
int& error)
override;
533 std::string peer_ip(
bool strip_zone_index =
false)
const;
543 virtual int peer_port(
int& error)
const override;
551 int ideal_send_buffer_size();
553 virtual int shutdown()
override;
575 flag_keepalive = 0x02,
578 flag_oobinline = 0x04
581 int flags()
const {
return flags_; }
584 void set_flags(
int flags,
bool enable);
587 void set_flags(
int flags);
594 void set_keepalive_interval(duration
const& d);
598 socket_t get_descriptor();
616 int send_fd(
fz::buffer & buf,
int fd,
int & error);
627 int read_fd(
fz::buffer & buf,
int &fd,
int & error);
680 virtual int peer_port(
int&
error)
const override {
return next_layer_.peer_port(error); }
706 virtual int shutdown_read()
override;
708 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown)
override {
709 return next_layer_.connect(host, port, family);
713 return next_layer_.shutdown();
717 return next_layer_.get_state();
733 void forward_hostaddress_event(socket_event_source* source, std::string
const& address);
741 event_handler* event_handler_{};
742 socket_interface& next_layer_;
743 bool event_passthrough_{};
778 class FZ_PRIVATE_SYMBOL winsock_initializer final
781 winsock_initializer();
782 ~winsock_initializer();
789 #define EISCONN WSAEISCONN
792 #define EINPROGRESS WSAEINPROGRESS
795 #define EAFNOSUPPORT WSAEAFNOSUPPORT
798 #define EADDRINUSE WSAEADDRINUSE
801 #define ENOBUFS WSAENOBUFS
803 #ifndef EPROTONOSUPPORT
804 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
807 #define EALREADY WSAEALREADY
810 #define ECONNREFUSED WSAECONNREFUSED
813 #define ENOTSOCK WSAENOTSOCK
816 #define ETIMEDOUT WSAETIMEDOUT
819 #define ENETUNREACH WSAENETUNREACH
822 #define EHOSTUNREACH WSAEHOSTUNREACH
825 #define ENOTCONN WSAENOTCONN
828 #define ENETRESET WSAENETRESET
831 #define EOPNOTSUPP WSAEOPNOTSUPP
834 #define ESHUTDOWN WSAESHUTDOWN
837 #define EMSGSIZE WSAEMSGSIZE
840 #define ECONNABORTED WSAECONNABORTED
843 #define ECONNRESET WSAECONNRESET
846 #define EHOSTDOWN WSAEHOSTDOWN
848 #ifndef ESOCKTNOSUPPORT
849 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
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.
Data has become available.
std::string socket_error_string(int error)
Gets a symbolic name for socket errors.
A simple scoped lock.
Definition: mutex.hpp:116
Interface for sockets.
Definition: socket.hpp:374
Lightweight holder for socket descriptors.
Definition: socket.hpp:250
simple_event< socket_event_type, socket_event_source *, socket_event_flag, int > socket_event
Definition: socket.hpp:107
virtual int shutdown_read() override
Definition: socket.hpp:596
Definition: event_handler.hpp:60
socket_interface & next()
The next layer further down. Usually another layer or the actual socket.
Definition: socket.hpp:683
Declares the event_handler class.
Socket has failed. Further events disabled.
Common base clase for fz::socket and fz::listen_socket.
Definition: socket.hpp:166
Simple Listen socket.
Definition: socket.hpp:299
Various functions to deal with IP address strings.
Socket has been closed. Further events disabled.
socket_event_source * root() const
Gets the root source.
Definition: socket.hpp:93
This is the recommended event class.
Definition: event.hpp:67
virtual int peer_port(int &error) const override
Definition: socket.hpp:680
simple_event< hostaddress_event_type, socket_event_source *, std::string > hostaddress_event
Definition: socket.hpp:139
IPv6 capable, non-blocking socket class.
Definition: socket.hpp:461
virtual int shutdown() override
Signals peers that we want to close the connections.
Definition: socket.hpp:712
void remove_socket_events(event_handler *handler, socket_event_source const *const source)
Remove all pending socket events from source sent to handler.
A base class for socket layers.
Definition: socket.hpp:656
virtual native_string peer_host() const override
Definition: socket.hpp:673
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
socket_state
State transitions are monotonically increasing.
Definition: socket.hpp:342
Only in listening state you can get a connection event.
The namespace used by libfilezilla.
Definition: apply.hpp:17
std::optional< std::pair< std::unique_ptr< fz::socket >, std::unique_ptr< fz::socket > > > create_tcp_socketpair(fz::thread_pool &pool)
Creates a pair of connected TCP sockets.
listen_socket_state
Definition: socket.hpp:240
native_string socket_error_description(int error)
Gets a human-readable, translated description of the error.
Socket is in its normal working state. You can get send and receive events.
How the socket is initially.
All classes sending socket events should derive from this.
Definition: socket.hpp:84
The duration class represents a time interval in milliseconds.
Definition: time.hpp:290
Sets some global macros and further includes string.hpp.
How the socket is initially.
Write side has finished shutting down. Receive still working normally.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:26
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:63
socket_event_flag
The type of a socket event.
Definition: socket.hpp:34