libfilezilla
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
socket Class Referencefinal

IPv6 capable, non-blocking socket class. More...

#include <socket.hpp>

Inheritance diagram for socket:
Inheritance graph
[legend]
Collaboration diagram for socket:
Collaboration graph
[legend]

Public Types

enum  { flag_nodelay = 0x01 , flag_keepalive = 0x02 }
 
- Public Types inherited from socket_base
typedef intptr_t socket_t
 

Public Member Functions

 socket (thread_pool &pool, event_handler *evt_handler)
 
 socket (socket const &)=delete
 
socketoperator= (socket const &)=delete
 
socket_state get_state () const override
 
bool is_connected () const
 
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. More...
 
virtual int read (void *buffer, unsigned int size, int &error) override
 Read data from socket. More...
 
virtual int write (void const *buffer, unsigned int size, int &error) override
 Write data to socket. More...
 
std::string peer_ip (bool strip_zone_index=false) const
 Returns remote address of a connected socket. More...
 
virtual native_string peer_host () const override
 Returns the hostname passed to connect()
 
virtual int peer_port (int &error) const override
 Returns remote port of a connected socket. More...
 
int ideal_send_buffer_size ()
 
virtual int shutdown () override
 Signals peers that we want to close the connections. More...
 
virtual void set_event_handler (event_handler *pEvtHandler, fz::socket_event_flag retrigger_block=fz::socket_event_flag{}) override
 Changes the associated event handler. More...
 
int flags () const
 
void set_flags (int flags, bool enable)
 Enables or disabled the passed flags.
 
void set_flags (int flags)
 Sets the entire mask of enabled flag, disabling all others.
 
void set_keepalive_interval (duration const &d)
 
virtual int shutdown_read () override
 
socket_t get_descriptor ()
 
- Public Member Functions inherited from socket_base
int set_buffer_sizes (int size_receive, int size_send)
 Sets socket buffer sizes. More...
 
address_type address_family () const
 If connected, either ipv4, ipv6 or unix, unknown otherwise.
 
std::string local_ip (bool strip_zone_index=false) const
 Returns local address of a connected socket. More...
 
int local_port (int &error) const
 Returns local port of a connected socket. More...
 
bool bind (std::string const &address)
 Bind socket to the specific local IP. More...
 
- Public Member Functions inherited from socket_interface
 socket_interface (socket_interface const &)=delete
 
socket_interfaceoperator= (socket_interface const &)=delete
 
template<typename T , std::enable_if_t< std::is_signed_v< T >, int > = 0>
int read (void *buffer, T size, int &error)
 
template<typename T , std::enable_if_t< std::is_unsigned_v< T > &&(sizeof(T) > sizeof(unsigned int)), int > = 0>
int read (void *buffer, T size, int &error)
 
template<typename T , std::enable_if_t< std::is_signed_v< T >, int > = 0>
int write (void const *buffer, T size, int &error)
 
template<typename T , std::enable_if_t< std::is_unsigned_v< T > &&(sizeof(T) > sizeof(unsigned int)), int > = 0>
int write (void const *buffer, T size, int &error)
 
- Public Member Functions inherited from socket_event_source
socket_event_sourceroot () const
 Gets the root source. More...
 

Static Public Member Functions

static std::unique_ptr< socketfrom_descriptor (socket_descriptor &&desc, thread_pool &pool, int &error, fz::event_handler *handler=nullptr)
 
- Static Public Member Functions inherited from socket_base
static std::string address_to_string (sockaddr const *addr, int addr_len, bool with_port=true, bool strip_zone_index=false)
 
static std::string address_to_string (char const *buf, int buf_len)
 

Friends

class socket_thread
 
class socket_base
 
class listen_socket
 

Additional Inherited Members

- Protected Member Functions inherited from socket_base
 socket_base (thread_pool &pool, event_handler *evt_handler, socket_event_source *ev_source)
 
int close ()
 
void detach_thread (scoped_lock &l)
 
- Protected Member Functions inherited from socket_interface
 socket_interface (socket_event_source *root)
 
- Protected Member Functions inherited from socket_event_source
 socket_event_source (socket_event_source *root)
 
- Protected Attributes inherited from socket_base
thread_poolthread_pool_
 
event_handlerevt_handler_
 
socket_thread * socket_thread_ {}
 
socket_event_source *const ev_source_ {}
 
socket_t fd_ {-1}
 
unsigned int port_ {}
 
int family_
 
int buffer_sizes_ [2]
 
- Protected Attributes inherited from socket_event_source
socket_event_source *const root_ {}
 

Detailed Description

IPv6 capable, non-blocking socket class.

Uses and edge-triggered socket events.

Error codes are the same as used by the POSIX socket functions, see 'man 2 socket', 'man 2 connect', ...

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
flag_nodelay 

flag_nodelay disables Nagle's algorithm

flag_keepalive 

flag_keepalive enables TCP keepalive.

Member Function Documentation

◆ connect()

virtual int connect ( native_string const &  host,
unsigned int  port,
address_type  family = address_type::unknown 
)
overridevirtual

Starts connecting to the given host, given as name, IPv4 or IPv6 address.

Returns 0 on success, else an error code.

Success only means that the establishing of the connection has started. Once the connection gets fully established or establishment fails, a connection event gets sent, with the error parameter indicating success or failure.

If host is a name that can be resolved, a hostaddress socket event gets sent during establishment.

Implements socket_interface.

◆ ideal_send_buffer_size()

int ideal_send_buffer_size ( )

On a connected socket, gets the ideal send buffer size or -1 if it cannot be determined.

Currently only implemented for Windows.

◆ peer_ip()

std::string peer_ip ( bool  strip_zone_index = false) const

Returns remote address of a connected socket.

Returns
empty string on error

◆ peer_port()

virtual int peer_port ( int &  error) const
overridevirtual

Returns remote port of a connected socket.

Returns
-1 on error

Implements socket_interface.

◆ read()

virtual int read ( void *  buffer,
unsigned int  size,
int &  error 
)
overridevirtual

Read data from socket.

Reads data from socket, returns the number of octets read or -1 on error.

May return fewer octets than requested. Return of 0 bytes read indicates EOF.

Can be called after having receiving a socket event with the read flag and can thenceforth be called until until it returns an error.

If the error is EAGAIN, wait for the next read event. On other errors the socket has failed and should be closed.

Takes care of EINTR internally.

Implements socket_interface.

◆ set_event_handler()

virtual void set_event_handler ( event_handler pEvtHandler,
fz::socket_event_flag  retrigger_block = fz::socket_event_flag{} 
)
overridevirtual

Changes the associated event handler.

Pending events are rewritten to the new handler, or deleted if there is no new handler.

Initially, the new handler is assumed to be waiting on read and write events and if the socket is in a readable/writable state, the corresponding events are sent if not already pending.

As exception, events passed in retrigger_block are always removed and not resent if the socket is in the readable/writable state.

Implements socket_interface.

◆ set_keepalive_interval()

void set_keepalive_interval ( duration const &  d)

Sets the interval between TCP keepalive packets.

Duration must not be smaller than 5 minutes. The default interval is 2 hours.

◆ shutdown()

virtual int shutdown ( )
overridevirtual

Signals peers that we want to close the connections.

Only disallows further sends, does not affect reading from the socket.

Returns 0 on success, an error code otherwise. If it returns EGAIN, shutdown is not yet complete. Call shutdown again after the next write event.

Implements socket_interface.

◆ shutdown_read()

virtual int shutdown_read ( )
inlineoverridevirtual

◆ write()

virtual int write ( void const *  buffer,
unsigned int  size,
int &  error 
)
overridevirtual

Write data to socket.

Writes data to the socket, returns the number of octets written or -1 on error.

May return fewer octets than requested.

Can be called after having receiving a socket event with the write flag and can thenceforth be called until until it returns an error.

If the error is EAGAIN, wait for the next write event. On other errors the socket has failed and should be closed.

Takes care of EINTR internally.

Implements socket_interface.


The documentation for this class was generated from the following file: