1 #ifndef LIBFILEZILLA_SOCKET_HEADER
2 #define LIBFILEZILLA_SOCKET_HEADER
82 socket_event_source*
const root_{};
86 struct socket_event_type;
111 struct hostaddress_event_type{};
151 int set_buffer_sizes(
int size_receive,
int size_send);
154 address_type address_family()
const;
161 std::string local_ip(
bool strip_zone_index =
false)
const;
168 int local_port(
int& error)
const;
170 static std::string address_to_string(sockaddr
const* addr,
int addr_len,
bool with_port =
true,
bool strip_zone_index =
false);
171 static std::string address_to_string(
char const* buf,
int buf_len);
178 bool bind(std::string
const& address);
181 typedef intptr_t socket_t;
183 typedef int socket_t;
187 friend class socket_thread;
202 socket_thread* socket_thread_{};
208 unsigned int port_{};
212 int buffer_sizes_[2];
239 std::swap(fd_, rhs.fd_);
243 socket_base::socket_t detach() {
244 socket_base::socket_t ret = fd_;
249 explicit operator bool()
const {
return fd_ != -1; }
252 socket_base::socket_t fd_{-1};
265 friend class socket_thread;
281 int listen(address_type family,
int port = 0);
284 std::unique_ptr<socket> accept(
int& error);
297 do_set_event_handler(pEvtHandler);
344 virtual int read(
void*
buffer,
unsigned int size,
int& error) = 0;
345 virtual int write(
void const* buffer,
unsigned int size,
int& error) = 0;
347 virtual void set_event_handler(
event_handler* pEvtHandler) = 0;
350 virtual int peer_port(
int& error)
const = 0;
352 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown) = 0;
366 virtual int shutdown() = 0;
369 virtual int shutdown_read() = 0;
389 friend class socket_thread;
400 bool is_connected()
const {
418 virtual int connect(
native_string const& host,
unsigned int port, address_type family = address_type::unknown)
override;
435 virtual int read(
void *
buffer,
unsigned int size,
int& error)
override;
452 virtual int write(
void const* buffer,
unsigned int size,
int& error)
override;
459 std::string peer_ip(
bool strip_zone_index =
false)
const;
469 virtual int peer_port(
int& error)
const override;
477 int ideal_send_buffer_size();
485 virtual int shutdown()
override;
487 virtual void set_event_handler(
event_handler* pEvtHandler)
override;
495 flag_keepalive = 0x02
498 int flags()
const {
return flags_; }
501 void set_flags(
int flags,
bool enable);
504 void set_flags(
int flags);
511 void set_keepalive_interval(duration
const& d);
551 virtual void set_event_handler(
event_handler* handler)
override;
565 virtual int peer_port(
int& error)
const override {
return next_layer_.peer_port(error); }
591 virtual int shutdown_read()
override;
612 void set_event_passthrough();
615 socket_interface& next_layer_;
616 bool event_passthrough_{};
638 #define EISCONN WSAEISCONN
641 #define EINPROGRESS WSAEINPROGRESS
644 #define EAFNOSUPPORT WSAEAFNOSUPPORT
647 #define EADDRINUSE WSAEADDRINUSE
650 #define ENOBUFS WSAENOBUFS
652 #ifndef EPROTONOSUPPORT
653 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
656 #define EALREADY WSAEALREADY
659 #define ECONNREFUSED WSAECONNREFUSED
662 #define ENOTSOCK WSAENOTSOCK
665 #define ETIMEDOUT WSAETIMEDOUT
668 #define ENETUNREACH WSAENETUNREACH
671 #define EHOSTUNREACH WSAEHOSTUNREACH
674 #define ENOTCONN WSAENOTCONN
677 #define ENETRESET WSAENETRESET
680 #define EOPNOTSUPP WSAEOPNOTSUPP
683 #define ESHUTDOWN WSAESHUTDOWN
686 #define EMSGSIZE WSAEMSGSIZE
689 #define ECONNABORTED WSAECONNABORTED
692 #define ECONNRESET WSAECONNRESET
695 #define EHOSTDOWN WSAEHOSTDOWN
std::string socket_error_string(int error)
Gets a symbolic name for socket errors.
A simple scoped lock.
Definition: mutex.hpp:64
Interface for sockets.
Definition: socket.hpp:338
Lightweight holder for socket descriptors.
Definition: socket.hpp:227
simple_event< socket_event_type, socket_event_source *, socket_event_flag, int > socket_event
Definition: socket.hpp:86
virtual int shutdown_read() override
Definition: socket.hpp:513
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:568
Declares the event_handler class.
Socket has failed. Further events disabled.
Common base clase for fz::socket and fz::listen_socket.
Definition: socket.hpp:141
Simple Listen socket.
Definition: socket.hpp:262
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:72
void change_socket_event_handler(event_handler *old_handler, event_handler *new_handler, socket_event_source const *const source)
Changes all pending socket events from source.
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:116
IPv6 capable, non-blocking socket class.
Definition: socket.hpp:387
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:541
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:306
Only in listening state you can get a connection event.
The namespace used by libfilezilla.
Definition: apply.hpp:17
listen_socket_state
Definition: socket.hpp:217
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.
All classes sending socket events should derive from this.
Definition: socket.hpp:63
The duration class represents a time interval in milliseconds.
Definition: time.hpp:271
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:23
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:62
virtual native_string peer_host() const override
Definition: socket.hpp:558
virtual int peer_port(int &error) const override
Definition: socket.hpp:565
socket_event_flag
The type of a socket event.
Definition: socket.hpp:27