MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
Public Member Functions | Data Fields | Private Member Functions | Private Attributes | Friends
Socket::Connection Class Reference

This class is for easy communicating through sockets, either TCP or Unix. More...

#include <socket.h>

Public Member Functions

 Connection ()
 Create a new disconnected base socket. More...
 
 Connection ()
 Create a new disconnected base socket. More...
 
 Connection (int sockNo)
 Create a new base socket. More...
 
 Connection (int sockNo)
 Create a new base socket. More...
 
 Connection (std::string hostname, int port, bool nonblock)
 Create a new TCP socket. More...
 
 Connection (std::string hostname, int port, bool nonblock)
 Create a new TCP socket. More...
 
 Connection (std::string adres, bool nonblock=false)
 Create a new Unix Socket. More...
 
 Connection (std::string adres, bool nonblock=false)
 Create a new Unix Socket. More...
 
 Connection (int write, int read)
 Simulate a socket using two file descriptors. More...
 
 Connection (int write, int read)
 Simulate a socket using two file descriptors. More...
 
void close ()
 Close connection. More...
 
void close ()
 Close connection. More...
 
bool connected () const
 Returns the connected-state for this socket. More...
 
bool connected () const
 Returns the connected-state for this socket. More...
 
unsigned int connTime ()
 Returns the time this socket has been connected. More...
 
unsigned int connTime ()
 Returns the time this socket has been connected. More...
 
unsigned int dataDown ()
 Returns total amount of bytes received. More...
 
unsigned int dataDown ()
 Returns total amount of bytes received. More...
 
unsigned int dataUp ()
 Returns total amount of bytes sent. More...
 
unsigned int dataUp ()
 Returns total amount of bytes sent. More...
 
void drop ()
 Close connection without shutdown. More...
 
void drop ()
 Close connection without shutdown. More...
 
std::string getBinHost ()
 
std::string getBinHost ()
 Gets hostname for connection, if available. More...
 
std::string getError ()
 Returns a string describing the last error that occured. More...
 
std::string getError ()
 Returns a string describing the last error that occured. More...
 
std::string getHost () const
 Gets hostname for connection, if available. More...
 
std::string getHost () const
 Gets hostname for connection, if available. More...
 
int getPureSocket ()
 Returns non-piped internal socket number. More...
 
int getPureSocket ()
 Returns non-piped internal socket number. More...
 
int getSocket ()
 Returns internal socket number. More...
 
int getSocket ()
 Returns internal socket number. More...
 
std::string getStats (std::string C)
 Returns a std::string of stats, ended by a newline. More...
 
std::string getStats (std::string C)
 Returns a std::string of stats, ended by a newline. More...
 
bool isAddress (std::string addr)
 Returns true if the given address can be matched with the remote host. More...
 
bool isAddress (std::string addr)
 
bool isBlocking ()
 Check if this socket is blocking (true) or nonblocking (false). More...
 
bool isBlocking ()
 Check if this socket is blocking (true) or nonblocking (false). More...
 
 operator bool () const
 
 operator bool () const
 Returns true if the socket is valid. More...
 
bool operator!= (const Connection &B) const
 
bool operator!= (const Connection &B) const
 Returns true if these sockets are not the same socket. More...
 
bool operator== (const Connection &B) const
 
bool operator== (const Connection &B) const
 Returns true if these sockets are the same socket. More...
 
bool peek ()
 Clears the downbuffer and fills it with peek. More...
 
bool peek ()
 Clears the downbuffer and fills it with peek. More...
 
BufferReceived ()
 Returns a reference to the download buffer. More...
 
BufferReceived ()
 Returns a reference to the download buffer. More...
 
void SendNow (const std::string &data)
 Will not buffer anything but always send right away. Blocks. More...
 
void SendNow (const std::string &data)
 Will not buffer anything but always send right away. Blocks. More...
 
void SendNow (const char *data)
 Will not buffer anything but always send right away. Blocks. More...
 
void SendNow (const char *data)
 Will not buffer anything but always send right away. Blocks. More...
 
void SendNow (const char *data, size_t len)
 Will not buffer anything but always send right away. Blocks. More...
 
void SendNow (const char *data, size_t len)
 Will not buffer anything but always send right away. Blocks. More...
 
void setBlocking (bool blocking)
 Set this socket to be blocking (true) or nonblocking (false). More...
 
void setBlocking (bool blocking)
 Set this socket to be blocking (true) or nonblocking (false). More...
 
void setHost (std::string host)
 Sets hostname for connection manually. More...
 
void setHost (std::string host)
 Sets hostname for connection manually. More...
 
bool spool ()
 Updates the downbufferinternal variables. More...
 
bool spool ()
 Updates the downbufferinternal variables. More...
 

Data Fields

bool Blocking
 Set to true if a socket is currently or wants to be blocking. More...
 
bool Error
 Set to true if a socket error happened. More...
 

Private Member Functions

int iread (void *buffer, int len, int flags=0)
 Incremental read call. More...
 
int iread (void *buffer, int len, int flags=0)
 Incremental read call. More...
 
bool iread (Buffer &buffer, int flags=0)
 Incremental write call that is compatible with Socket::Buffer. More...
 
bool iread (Buffer &buffer, int flags=0)
 Incremental write call that is compatible with Socket::Buffer. More...
 
unsigned int iwrite (const void *buffer, int len)
 Incremental write call. More...
 
unsigned int iwrite (const void *buffer, int len)
 Incremental write call. More...
 
bool iwrite (std::string &buffer)
 Write call that is compatible with std::string. More...
 
bool iwrite (std::string &buffer)
 Write call that is compatible with std::string. More...
 

Private Attributes

long long int conntime
 
unsigned int down
 
Buffer downbuffer
 Stores temporary data coming in. More...
 
int pipes [2]
 Internally saved file descriptors for pipe socket simulation. More...
 
std::string remotehost
 Stores remote host address. More...
 
int sock
 Internally saved socket number. More...
 
unsigned int up
 

Friends

class ::Buffer::user
 
class Server
 

Detailed Description

This class is for easy communicating through sockets, either TCP or Unix.

Constructor & Destructor Documentation

Socket::Connection::Connection ( )

Create a new disconnected base socket.

This is a basic constructor for placeholder purposes. A socket created like this is always disconnected and should/could be overwritten at some point.

Socket::Connection::Connection ( int  sockNo)

Create a new base socket.

This is a basic constructor for converting any valid socket to a Socket::Connection.

Parameters
sockNoInteger representing the socket to convert.
Socket::Connection::Connection ( std::string  host,
int  port,
bool  nonblock 
)

Create a new TCP socket.

Create a new TCP Socket.

This socket will (try to) connect to the given host/port right away.

Parameters
hostString containing the hostname to connect to.
portString containing the port to connect to.
nonblockWhether the socket should be nonblocking.
Socket::Connection::Connection ( std::string  address,
bool  nonblock = false 
)

Create a new Unix Socket.

This socket will (try to) connect to the given address right away.

Parameters
addressString containing the location of the Unix socket to connect to.
nonblockWhether the socket should be nonblocking. False by default.
Socket::Connection::Connection ( int  write,
int  read 
)

Simulate a socket using two file descriptors.

Parameters
writeThe filedescriptor to write to.
readThe filedescriptor to read from.
Socket::Connection::Connection ( )

Create a new disconnected base socket.

Socket::Connection::Connection ( int  sockNo)

Create a new base socket.

Socket::Connection::Connection ( std::string  hostname,
int  port,
bool  nonblock 
)

Create a new TCP socket.

Socket::Connection::Connection ( std::string  adres,
bool  nonblock = false 
)

Create a new Unix Socket.

Socket::Connection::Connection ( int  write,
int  read 
)

Simulate a socket using two file descriptors.

Member Function Documentation

void Socket::Connection::close ( )

Close connection.

The internal socket is closed and then set to -1. If the connection is already closed, nothing happens. This function calls shutdown, thus making the socket unusable in all other processes as well. Do not use on shared sockets that are still in use.

void Socket::Connection::close ( )

Close connection.

bool Socket::Connection::connected ( ) const

Returns the connected-state for this socket.

bool Socket::Connection::connected ( ) const

Returns the connected-state for this socket.

Note that this function might be slightly behind the real situation. The connection status is updated after every read/write attempt, when errors occur and when the socket is closed manually.

Returns
True if socket is connected, false otherwise.
unsigned int Socket::Connection::connTime ( )

Returns the time this socket has been connected.

unsigned int Socket::Connection::connTime ( )

Returns the time this socket has been connected.

unsigned int Socket::Connection::dataDown ( )

Returns total amount of bytes received.

unsigned int Socket::Connection::dataDown ( )

Returns total amount of bytes received.

unsigned int Socket::Connection::dataUp ( )

Returns total amount of bytes sent.

unsigned int Socket::Connection::dataUp ( )

Returns total amount of bytes sent.

void Socket::Connection::drop ( )

Close connection without shutdown.

void Socket::Connection::drop ( )

Close connection without shutdown.

Close connection.

The internal socket is closed and then set to -1. If the connection is already closed, nothing happens. This function does not call shutdown, allowing continued use in other processes.

std::string Socket::Connection::getBinHost ( )
std::string Socket::Connection::getBinHost ( )

Gets hostname for connection, if available.

Guaranteed to be either empty or 16 bytes long.

std::string Socket::Connection::getError ( )

Returns a string describing the last error that occured.

std::string Socket::Connection::getError ( )

Returns a string describing the last error that occured.

Only reports errors if an error actually occured - returns the host address or empty string otherwise.

std::string Socket::Connection::getHost ( ) const

Gets hostname for connection, if available.

std::string Socket::Connection::getHost ( ) const

Gets hostname for connection, if available.

int Socket::Connection::getPureSocket ( )

Returns non-piped internal socket number.

int Socket::Connection::getPureSocket ( )

Returns non-piped internal socket number.

int Socket::Connection::getSocket ( )

Returns internal socket number.

int Socket::Connection::getSocket ( )

Returns internal socket number.

std::string Socket::Connection::getStats ( std::string  C)

Returns a std::string of stats, ended by a newline.

Requires the current connector name as an argument.

std::string Socket::Connection::getStats ( std::string  C)

Returns a std::string of stats, ended by a newline.

int Socket::Connection::iread ( void *  buffer,
int  len,
int  flags = 0 
)
private

Incremental read call.

This function tries to read len bytes to the buffer from the socket, returning the amount of bytes it actually read.

Parameters
bufferLocation of the buffer to read to.
lenAmount of bytes to read.
flagsFlags to use in the recv call. Ignored on fake sockets.
Returns
The amount of bytes actually read.
int Socket::Connection::iread ( void *  buffer,
int  len,
int  flags = 0 
)
private

Incremental read call.

bool Socket::Connection::iread ( Buffer buffer,
int  flags = 0 
)
private

Incremental write call that is compatible with Socket::Buffer.

bool Socket::Connection::iread ( Buffer buffer,
int  flags = 0 
)
private

Incremental write call that is compatible with Socket::Buffer.

Read call that is compatible with Socket::Buffer.

Data is read using iread (which is nonblocking if the Socket::Connection itself is), then appended to end of buffer.

Parameters
bufferSocket::Buffer to append data to.
flagsFlags to use in the recv call. Ignored on fake sockets.
Returns
True if new data arrived, false otherwise.
bool Socket::Connection::isAddress ( std::string  addr)

Returns true if the given address can be matched with the remote host.

Can no longer return true after any socket error have occurred.

bool Socket::Connection::isAddress ( std::string  addr)
bool Socket::Connection::isBlocking ( )

Check if this socket is blocking (true) or nonblocking (false).

bool Socket::Connection::isBlocking ( )

Check if this socket is blocking (true) or nonblocking (false).

Set this socket to be blocking (true) or nonblocking (false).

unsigned int Socket::Connection::iwrite ( const void *  buffer,
int  len 
)
private

Incremental write call.

This function tries to write len bytes to the socket from the buffer, returning the amount of bytes it actually wrote.

Parameters
bufferLocation of the buffer to write from.
lenAmount of bytes to write.
Returns
The amount of bytes actually written.
unsigned int Socket::Connection::iwrite ( const void *  buffer,
int  len 
)
private

Incremental write call.

bool Socket::Connection::iwrite ( std::string &  buffer)
private

Write call that is compatible with std::string.

Incremental write call that is compatible with std::string.

Data is written using iwrite (which is nonblocking if the Socket::Connection itself is), then removed from front of buffer.

Parameters
bufferstd::string to remove data from.
Returns
True if more data was sent, false otherwise.
bool Socket::Connection::iwrite ( std::string &  buffer)
private

Write call that is compatible with std::string.

Socket::Connection::operator bool ( ) const

Returns true if the socket is valid.

Aliases for Socket::Connection::connected()

Socket::Connection::operator bool ( ) const
bool Socket::Connection::operator!= ( const Connection B) const
bool Socket::Connection::operator!= ( const Connection B) const

Returns true if these sockets are not the same socket.

Does not check the internal stats - only the socket itself.

bool Socket::Connection::operator== ( const Connection B) const
bool Socket::Connection::operator== ( const Connection B) const

Returns true if these sockets are the same socket.

Does not check the internal stats - only the socket itself.

bool Socket::Connection::peek ( )

Clears the downbuffer and fills it with peek.

clear buffer

bool Socket::Connection::peek ( )

Clears the downbuffer and fills it with peek.

Buffer& Socket::Connection::Received ( )

Returns a reference to the download buffer.

Socket::Buffer & Socket::Connection::Received ( )

Returns a reference to the download buffer.

void Socket::Connection::SendNow ( const std::string &  data)

Will not buffer anything but always send right away. Blocks.

void Socket::Connection::SendNow ( const std::string &  data)

Will not buffer anything but always send right away. Blocks.

Will not buffer anything but always send right away.

Blocks. Any data that could not be send will block until it can be send or the connection is severed.

void Socket::Connection::SendNow ( const char *  data)

Will not buffer anything but always send right away. Blocks.

Will not buffer anything but always send right away.

Blocks. Any data that could not be send will block until it can be send or the connection is severed.

void Socket::Connection::SendNow ( const char *  data)

Will not buffer anything but always send right away. Blocks.

void Socket::Connection::SendNow ( const char *  data,
size_t  len 
)

Will not buffer anything but always send right away. Blocks.

void Socket::Connection::SendNow ( const char *  data,
size_t  len 
)

Will not buffer anything but always send right away. Blocks.

Will not buffer anything but always send right away.

Blocks. Any data that could not be send will block until it can be send or the connection is severed.

void Socket::Connection::setBlocking ( bool  blocking)

Set this socket to be blocking (true) or nonblocking (false).

void Socket::Connection::setBlocking ( bool  blocking)

Set this socket to be blocking (true) or nonblocking (false).

void Socket::Connection::setHost ( std::string  host)

Sets hostname for connection manually.

Overwrites the detected host, thus possibily making it incorrect.

void Socket::Connection::setHost ( std::string  host)

Sets hostname for connection manually.

bool Socket::Connection::spool ( )

Updates the downbufferinternal variables.

bool Socket::Connection::spool ( )

Updates the downbufferinternal variables.

Updates the downbuffer internal variable.

Returns true if new data was received, false otherwise.

Todo:
Provide better mechanism to prevent overbuffering.

Friends And Related Function Documentation

friend class ::Buffer::user
friend
Server
friend

Field Documentation

bool Socket::Connection::Blocking

Set to true if a socket is currently or wants to be blocking.

long long int Socket::Connection::conntime
private
unsigned int Socket::Connection::down
private
Buffer Socket::Connection::downbuffer
private

Stores temporary data coming in.

bool Socket::Connection::Error

Set to true if a socket error happened.

int Socket::Connection::pipes
private

Internally saved file descriptors for pipe socket simulation.

std::string Socket::Connection::remotehost
private

Stores remote host address.

int Socket::Connection::sock
private

Internally saved socket number.

unsigned int Socket::Connection::up
private

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