 |
libfilezilla
|
Go to the documentation of this file. 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{};
161 std::string
local_ip(
bool strip_zone_index =
false)
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;
389 friend class socket_thread;
400 bool is_connected()
const {
402 return s == socket_state::connected || s == socket_state::shutting_down || s == socket_state::shut_down;
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;
487 virtual void set_event_handler(
event_handler* pEvtHandler)
override;
495 flag_keepalive = 0x02
498 int flags()
const {
return flags_; }
565 virtual int peer_port(
int& error)
const override {
return next_layer_.peer_port(error); }
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::unique_ptr< socket > accept(int &error)
Accepts incoming connection. If no socket is returned, error contains the reason.
std::string socket_error_string(int error)
Gets a symbolic name for socket errors.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:24
void set_flags(int flags, bool enable)
Enables or disabled the passed flags.
listen_socket_state
Definition: socket.hpp:218
virtual int peer_port(int &error) const override
Definition: socket.hpp:565
virtual void set_event_handler(event_handler *handler) override
The handler for any events generated (or forwarded) by this layer.
virtual int peer_port(int &error) const override
Returns remote port of a connected socket.
virtual int shutdown_read()=0
void set_event_passthrough()
socket_state
State transitions are monotonically increasing.
Definition: socket.hpp:307
@ none
How the socket is initially.
The duration class represents a time interval in milliseconds.
Definition: time.hpp:272
void forward_socket_event(socket_event_source *source, socket_event_flag t, int error)
This is the recommended event class.
Definition: event.hpp:64
virtual int read(void *buffer, unsigned int size, int &error) override
Read data from socket.
virtual int shutdown_read() override
Check that all layers further down also have reached EOF.
int listen(address_type family, int port=0)
Starts listening.
virtual native_string peer_host() const override
Returns the hostname passed to connect()
std::string local_ip(bool strip_zone_index=false) const
Returns local address of a connected socket.
A base class for socket layers.
Definition: socket.hpp:542
virtual int shutdown()=0
Signals peers that we want to close the connections.
IPv6 capable, non-blocking socket class.
Definition: socket.hpp:388
void retrigger(socket_event_flag event)
socket_event_flag
The type of a socket event.
Definition: socket.hpp:28
virtual int shutdown() override
Signals peers that we want to close the connections.
int ideal_send_buffer_size()
bool bind(std::string const &address)
Bind socket to the specific local IP.
socket_event_source * root() const
Gets the root source.
Definition: socket.hpp:72
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.
Declares the event_handler class.
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
virtual int shutdown_read() override
Definition: socket.hpp:513
void remove_socket_events(event_handler *handler, socket_event_source const *const source)
Remove all pending socket events from source sent to handler.
virtual native_string peer_host() const override
Definition: socket.hpp:558
Lightweight holder for socket descriptors.
Definition: socket.hpp:228
address_type address_family() const
If connected, either ipv4 or ipv6, unknown otherwise.
void set_keepalive_interval(duration const &d)
Simple Listen socket.
Definition: socket.hpp:263
simple_event< socket_event_type, socket_event_source *, socket_event_flag, int > socket_event
Definition: socket.hpp:86
int local_port(int &error) const
Returns local port of a connected socket.
Interface for sockets.
Definition: socket.hpp:339
virtual int write(void const *buffer, unsigned int size, int &error) override
Write data to socket.
Various functions to deal with IP address strings.
native_string socket_error_description(int error)
Gets a human-readable, translated description of the error.
void set_flags(int flags)
Sets the entire mask of enabled flag, disabling all others.
All classes sending socket events should derive from this.
Definition: socket.hpp:64
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
socket_descriptor fast_accept(int &error)
Like accept, but only returns a socket descriptor.
Sets some global macros and further includes string.hpp.
A simple scoped lock.
Definition: mutex.hpp:65
@ none
How the socket is initially.
std::string peer_ip(bool strip_zone_index=false) const
Returns remote address of a connected socket.
socket_interface & next()
The next layer further down. Usually another layer or the actual socket.
Definition: socket.hpp:568
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:63
Common base clase for fz::socket and fz::listen_socket.
Definition: socket.hpp:142
simple_event< hostaddress_event_type, socket_event_source *, std::string > hostaddress_event
Definition: socket.hpp:116
The namespace used by libfilezilla.
Definition: apply.hpp:17
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.
void forward_hostaddress_event(socket_event_source *source, std::string const &address)
int set_buffer_sizes(int size_receive, int size_send)
Sets socket buffer sizes.