libfilezilla
Public Types | 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 ()
 
void retrigger (socket_event_flag event)
 
virtual int shutdown () override
 Signals peers that we want to close the connections. More...
 
virtual void set_event_handler (event_handler *pEvtHandler) override
 
int flags () const
 
void set_flags (int flags)
 
void set_keepalive_interval (duration const &d)
 
virtual int shutdown_read () override
 
- 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 or ipv6, 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
 
- Public Member Functions inherited from socket_event_source
socket_event_sourceroot () const
 Gets the root source. More...
 

Friends

class socket_thread
 
class socket_base
 
class listen_socket
 

Additional Inherited Members

- 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)
 
- Protected Member Functions inherited from socket_base
 socket_base (thread_pool &pool, event_handler *evt_handler, socket_event_source *ev_source)
 
int close ()
 
void do_set_event_handler (event_handler *pEvtHandler)
 
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_t fd_ {-1}
 
socket_thread * socket_thread_ {}
 
unsigned int port_ {}
 
int family_
 
int buffer_sizes_ [2]
 
socket_event_source *const ev_source_ {}
 
- 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.

◆ retrigger()

void retrigger ( socket_event_flag  event)

Allows re-triggering the read and write events. Slow and cumbersome, use sparingly.

◆ 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.

Implements socket_interface.

◆ 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: