1 #ifndef LIBFILEZILLA_SOCKET_HEADER
2 #define LIBFILEZILLA_SOCKET_HEADER
26 enum class socket_event_flag
64 socket_event_source*
const root_{};
68 struct socket_event_type;
88 struct hostaddress_event_type{};
129 int set_buffer_sizes(
int size_receive,
int size_send);
132 address_type address_family()
const;
139 std::string local_ip(
bool strip_zone_index =
false)
const;
146 int local_port(
int& error)
const;
148 static std::string address_to_string(sockaddr
const* addr,
int addr_len,
bool with_port =
true,
bool strip_zone_index =
false);
149 static std::string address_to_string(
char const* buf,
int buf_len);
156 bool bind(std::string
const& address);
159 typedef intptr_t socket_t;
161 typedef int socket_t;
165 friend class socket_thread;
182 socket_thread* socket_thread_{};
184 unsigned int port_{};
188 int buffer_sizes_[2];
216 std::swap(fd_, rhs.fd_);
220 socket_base::socket_t detach() {
221 socket_base::socket_t ret = fd_;
226 explicit operator bool()
const {
return fd_ != -1; }
229 socket_base::socket_t fd_{-1};
242 friend class socket_thread;
258 int listen(address_type family,
int port = 0);
261 std::unique_ptr<socket> accept(
int& error);
274 do_set_event_handler(pEvtHandler);
321 virtual int read(
void*
buffer,
unsigned int size,
int& error) = 0;
322 virtual int write(
void const* buffer,
unsigned int size,
int& error) = 0;
324 virtual void set_event_handler(
event_handler* pEvtHandler) = 0;
327 virtual int peer_port(
int& error)
const = 0;
329 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown) = 0;
339 virtual int shutdown() = 0;
341 virtual int shutdown_read() = 0;
361 friend class socket_thread;
372 bool is_connected()
const {
390 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown)
override;
407 virtual int read(
void *
buffer,
unsigned int size,
int& error)
override;
424 virtual int write(
void const* buffer,
unsigned int size,
int& error)
override;
431 std::string peer_ip(
bool strip_zone_index =
false)
const;
441 virtual int peer_port(
int& error)
const override;
449 int ideal_send_buffer_size();
455 void retrigger(socket_event_flag event);
457 virtual int shutdown()
override;
459 virtual void set_event_handler(
event_handler* pEvtHandler)
override;
467 flag_keepalive = 0x02
470 int flags()
const {
return flags_; }
473 void set_flags(
int flags,
bool enable);
476 void set_flags(
int flags);
483 void set_keepalive_interval(duration
const& d);
485 virtual int shutdown_read()
override {
return 0; }
488 friend class socket_base;
489 friend class listen_socket;
495 duration keepalive_interval_;
521 virtual void set_event_handler(
event_handler* handler)
override;
535 virtual int peer_port(
int& error)
const override {
return next_layer_.peer_port(error); }
561 virtual int shutdown_read()
override;
582 void set_event_passthrough();
585 socket_interface& next_layer_;
586 bool event_passthrough_{};
597 std::string FZ_PUBLIC_SYMBOL socket_error_string(
int error);
608 #define EISCONN WSAEISCONN
611 #define EINPROGRESS WSAEINPROGRESS
614 #define EAFNOSUPPORT WSAEAFNOSUPPORT
617 #define EADDRINUSE WSAEADDRINUSE
620 #define ENOBUFS WSAENOBUFS
622 #ifndef EPROTONOSUPPORT
623 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
626 #define EALREADY WSAEALREADY
629 #define ECONNREFUSED WSAECONNREFUSED
632 #define ENOTSOCK WSAENOTSOCK
635 #define ETIMEDOUT WSAETIMEDOUT
638 #define ENETUNREACH WSAENETUNREACH
641 #define EHOSTUNREACH WSAEHOSTUNREACH
644 #define ENOTCONN WSAENOTCONN
647 #define ENETRESET WSAENETRESET
650 #define EOPNOTSUPP WSAEOPNOTSUPP
653 #define ESHUTDOWN WSAESHUTDOWN
656 #define EMSGSIZE WSAEMSGSIZE
659 #define ECONNABORTED WSAECONNABORTED
662 #define ECONNRESET WSAECONNRESET
665 #define EHOSTDOWN WSAEHOSTDOWN
A simple scoped lock.
Definition: mutex.hpp:61
Interface for sockets.
Definition: socket.hpp:315
Definition: socket.hpp:204
simple_event< socket_event_type, socket_event_source *, socket_event_flag, int > socket_event
Definition: socket.hpp:68
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:54
socket_interface & next()
The next layer further down. Usually another layer or the actual socket.
Definition: socket.hpp:538
Declares the event_handler class.
Common base clase for fz::socket and fz::listen_socket.
Definition: socket.hpp:119
Socket has been closed. Further events disabled.
Definition: socket.hpp:239
Various functions to deal with IP address strings.
socket_event_source * root() const
Gets the root source.
Definition: socket.hpp:54
Socket is in its normal working state. You can get send and receive events.
Socket has failed. Further events disabled.
This is the recommended event class.
Definition: event.hpp:63
simple_event< hostaddress_event_type, socket_event_source *, std::string > hostaddress_event
Definition: socket.hpp:93
IPv6 capable, non-blocking socket class.
Definition: socket.hpp:359
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:511
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:33
socket_state
State transitions are monotonically increasing.
Definition: socket.hpp:283
Only in listening state you can get a connection event.
The namespace used by libfilezilla.
Definition: apply.hpp:16
listen_socket_state
Definition: socket.hpp:195
native_string socket_error_description(int error)
Gets a human-readable, translated description of the error.
All classes sending socket events should derive from this.
Definition: socket.hpp:45
Sets some global macros and further includes string.hpp.
How the socket is initially.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:23
Definition: thread_pool.hpp:59
Write side has finished shutting down. Receive still working normally.
virtual native_string peer_host() const override
Definition: socket.hpp:528
virtual int peer_port(int &error) const override
Definition: socket.hpp:535