Bredbandskollen CLI  1.2
Asynchronous network task engine
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Friends | List of all members
Socket Class Reference

This is a slave to the Engine class. You can't use it directly, only through its subclasses, SocketConnection or ServerSocket. More...

#include <socket.h>

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

Public Member Functions

 Socket (const std::string &label, Task *owner, const std::string &hostname, uint16_t port)
 
 Socket (const std::string &label, Task *owner, int fd)
 
Taskowner () const
 Return task owning the socket.
 
std::string hostname () const
 Return name of the host to which the socket is supposed to connect.
 
uint16_t port () const
 Return port number to which the socket is supposed to connect.
 
PollState state () const
 Return current socket state.
 
int getUnixDomainPeer () const
 Return the peer socket descriptor. More...
 
virtual std::string cacheLabel ()
 Return the socket's cache group, or an empty string. More...
 
int id () const
 Return unique connection ID if connected. More...
 
virtual void setOwner (Task *t)
 Set the given task as owner of the socket.
 
void setExpiry (double s)
 Set a time to live for the socket. More...
 
bool hasExpired (const TimePoint &when) const
 Return true if the given TimePoint is after the socket's expiry.
 
const char * localIp () const
 Return local IP address in static buffer.
 
struct addrinfo * getAddressInfo (uint16_t iptype=0)
 Perform DNS lookup of remote host.
 
- Public Member Functions inherited from Logger
 Logger (std::string label)
 
std::string label () const
 Return the object's log label.
 
void resetLabel (const std::string &new_label)
 Modify the object's log label.
 

Static Public Member Functions

static const char * getIp (int fd, uint16_t *port=nullptr, bool peer=true)
 Return IP address of connected socket in static buffer. More...
 
static const char * getIp (struct sockaddr *address, uint16_t *port=nullptr)
 Return IP address in static buffer.
 
static const char * getIp (struct addrinfo *address, uint16_t *port=nullptr)
 Return IP address in static buffer.
 
- Static Public Member Functions inherited from Logger
static void setLogFile (std::ostream &stream)
 Set global log destination. More...
 
static void reopenLogFile (const std::string &filename)
 If current log is a file (ofstream), reopen it with new filename:
 
static void setLogLimit (unsigned int loglines=0, unsigned int warnlines=0, unsigned int errlines=0)
 Set max number of lines of info/warn/err log. More...
 
static void sayTime (std::ostream &stream)
 Write current local time to the given stream.
 
static bool inError ()
 Return true if any error has been logged (globally since start)
 
static std::ostream & err_log (const std::string &label)
 Write a line of error log. More...
 
static std::ostream & warn_log (const std::string &label)
 Write a line of warning log. More...
 
static std::ostream & log (const std::string &label)
 Write a line of info log. More...
 
static void flushLogFile ()
 
static void pauseLogging ()
 Disable all log output until next call to Logger::setLogFile.
 
static double secondsSince (const TimePoint &t)
 
static double secondsTo (const TimePoint &t)
 
static int64_t msSince (const TimePoint &t)
 
static int64_t msTo (const TimePoint &t)
 
static bool hasExpired (const TimePoint &t)
 Return true if current time is after the given TimePoint.
 
static TimePoint timeNow ()
 Return current time.
 
static TimePoint timeAfter (double s)
 Return current time plus s seconds.
 
static TimePoint timeMax ()
 Return a very distant time.
 
static std::chrono::microseconds toUs (double t)
 Convert s (seconds) to std::chrono::microseconds.
 
static std::string dateString (time_t t=0)
 Return local time, formatted as 2023-10-14T09:38:47+0200.
 
static std::string dateString2 (time_t t=0)
 Return local time, formatted as Sat, 14 Oct 2023 09:38:47.
 
static std::string createHashKey (unsigned int length=20)
 Return a random string. More...
 

Protected Member Functions

virtual bool wantToSend ()
 Return true if socket is watched for writeability.
 
virtual PollState checkReadBlock ()
 This will be called regularly on READ_BLOCKED sockets to check if the block can be lifted. More...
 
void setWantToSend ()
 Notify intention of sending large amounts of data. More...
 
int socket () const
 Return file descriptor.
 
void closeMe ()
 Tell the network engine that the connection should be closed.
 
void createNonBlockingSocket (struct addrinfo *addressEntry, struct addrinfo *localAddr=nullptr)
 Create socket and initiate the connection. More...
 
bool setNonBlocking (int fd)
 Set socket as non-blocking.
 
bool inError () const
 Return true if the socket has encountered a fatal error.
 
- Protected Member Functions inherited from Logger
std::ostream & errno_log () const
 Write a line of error log after a failed system call has set the global errno to a non-zero value. More...
 
std::ostream & err_log () const
 Write a line of error log. More...
 
std::ostream & warn_log () const
 Write a line of warning log. More...
 
std::ostream & log () const
 Write a line of info log. More...
 
std::ostream & dbg_log () const
 Write a line of debug log. More...
 

Static Protected Member Functions

static bool isTempError ()
 Return true unless last syscall encountered a fatal error.
 
static int closeSocket (int fd)
 Close a file descriptor.
 
static bool socketInError (int fd)
 Return true if the file descriptor has encountered a fatal error.
 

Friends

class Engine
 

Detailed Description

This is a slave to the Engine class. You can't use it directly, only through its subclasses, SocketConnection or ServerSocket.

Member Function Documentation

◆ cacheLabel()

virtual std::string Socket::cacheLabel ( )
inlinevirtual

Return the socket's cache group, or an empty string.

By default, if we have a keepalive (cached) connection to the same host and port, it will be used instead of creating a new connection. Override this method to disable keepalive (returning empty string) or to use only a special type of cached connection (returning a label for that special type).

Reimplemented in HttpClientConnection, and ServerSocket.

◆ checkReadBlock()

virtual PollState Socket::checkReadBlock ( )
inlineprotectedvirtual

This will be called regularly on READ_BLOCKED sockets to check if the block can be lifted.

If your subclass ever returns READ_BLOCKED, it should override this method to return the new state when the block should be removed.

◆ createNonBlockingSocket()

void Socket::createNonBlockingSocket ( struct addrinfo *  addressEntry,
struct addrinfo *  localAddr = nullptr 
)
protected

Create socket and initiate the connection.

Will do no nothing if socket has already been created. On error, socket() will return -1.

◆ getIp()

const char * Socket::getIp ( int  fd,
uint16_t *  port = nullptr,
bool  peer = true 
)
static

Return IP address of connected socket in static buffer.

Return the local IP address if peer==false, otherwise the peer IP.

◆ getUnixDomainPeer()

int Socket::getUnixDomainPeer ( ) const
inline

Return the peer socket descriptor.

If this is a Unix Domain socket, return the peer socket descriptor. If not, return 0.

May be used in another thread or process.

◆ id()

int Socket::id ( ) const
inline

Return unique connection ID if connected.

Return a positive number that is unique to this connection if it is active, otherwise -1.

◆ setExpiry()

void Socket::setExpiry ( double  s)
inline

Set a time to live for the socket.

Call this to have the socket removed automatically before a given number of seconds. Note: the network engine might remove the socket 1-2 seconds before the timeout, so adjust the timeout value accordingly!

◆ setWantToSend()

void Socket::setWantToSend ( )
inlineprotected

Notify intention of sending large amounts of data.

Normally, this is done simply by returning PollState::READ_WRITE from a scoket callback. This method is useful if you're not inside such a callback when you find out you need to send (large amounts of) data.


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