The Open Master Hearing Aid (openMHA)  openMHA
Open community platform for hearing aid algorithm research
MHA_TCP::Connection Class Reference

Connection handles Communication between client and server, is used on both sides. More...

Inheritance diagram for MHA_TCP::Connection:
Inheritance graph

Public Member Functions

Sockread_Eventget_read_event ()
 
Sockwrite_Eventget_write_event ()
 
std::string get_peer_address ()
 Get peer's IP Address. More...
 
unsigned short get_peer_port ()
 Get peer's TCP port. More...
 
SOCKET get_fd () const
 Return the (protected) file descriptor of the connection. More...
 
virtual ~Connection ()
 Destructor closes the underlying file descriptor. More...
 
bool eof ()
 Checks if the peer has closed the connection. More...
 
bool can_read_line (char delim='\n')
 Checks if a full line of text has arrived by now. More...
 
bool can_read_bytes (unsigned howmany)
 Checks if the specified ammount of data can be read. More...
 
std::string read_line (char delim='\n')
 Reads a single line of data from the socket. More...
 
std::string read_bytes (unsigned howmany)
 Reads the specified ammount of dat from the socket. More...
 
void try_write (const std::string &data="")
 Adds data to the internal "outgoing" buffer, and then tries to write as much data from that buffer to the socket as possible without blocking. More...
 
void write (const std::string &data="")
 Adds data to the internal "outgoing" buffer, and then writes that that buffer to the socket, regardless of blocking. More...
 
bool needs_write ()
 Checks if the internal "outgoing" buffer contains data. More...
 
unsigned buffered_incoming_bytes () const
 Returns the number of bytes in the internal "incoming" buffer. More...
 
unsigned buffered_outgoing_bytes () const
 Returns the number of bytes in the internal "outgoing" buffer. More...
 

Protected Member Functions

 Connection (SOCKET _fd)
 Create a connection instance from a socket filedescriptor. More...
 

Protected Attributes

SOCKET fd
 The file descriptor of the TCP Socket. More...
 

Private Member Functions

void init_peer_data ()
 determine peer address and port More...
 
bool can_sysread ()
 Determine wether at least 1 byte can be read without blocking. More...
 
bool can_syswrite ()
 Determine wether at least 1 byte can be written without blocking. More...
 
std::string sysread (unsigned bytes)
 Call the system's read function and try to read bytes. More...
 
std::string syswrite (const std::string &data)
 Call the system's write function and try to write all characters in the string data. More...
 

Private Attributes

std::string outbuf
 
std::string inbuf
 
Sockread_Eventread_event
 
Sockwrite_Eventwrite_event
 
bool closed
 
struct sockaddr_in peer_addr
 

Detailed Description

Connection handles Communication between client and server, is used on both sides.

Constructor & Destructor Documentation

◆ Connection()

MHA_TCP::Connection::Connection ( SOCKET  _fd)
protected

Create a connection instance from a socket filedescriptor.

Parameters
_fdThe file descriptor of the TCP Socket. This file descriptor is closed again in the destructor.
Exceptions
MHA_ErrorIf the file descriptor is < 0.

◆ ~Connection()

Connection::~Connection ( )
virtual

Destructor closes the underlying file descriptor.

Member Function Documentation

◆ init_peer_data()

void MHA_TCP::Connection::init_peer_data ( )
private

determine peer address and port

◆ can_sysread()

bool Connection::can_sysread ( )
private

Determine wether at least 1 byte can be read without blocking.

◆ can_syswrite()

bool Connection::can_syswrite ( )
private

Determine wether at least 1 byte can be written without blocking.

◆ sysread()

std::string Connection::sysread ( unsigned  bytes)
private

Call the system's read function and try to read bytes.

This will block in a situation where can_sysread returns false.

Parameters
bytesThe desired number of characters.
Returns
The characters read from the socket. The result may have fewer characters than specified by bytes. If the result is an empty string, then the socket has been closed by the peer.

◆ syswrite()

std::string Connection::syswrite ( const std::string &  data)
private

Call the system's write function and try to write all characters in the string data.

May write fewer characters, but will at least write one character.

Parameters
dataA string of characters to write to the socket.
Returns
The rest of the characters that have not yet been written.

◆ get_read_event()

Sockread_Event * Connection::get_read_event ( )

◆ get_write_event()

Sockwrite_Event * Connection::get_write_event ( )

◆ get_peer_address()

std::string Connection::get_peer_address ( )

Get peer's IP Address.

◆ get_peer_port()

unsigned short Connection::get_peer_port ( )

Get peer's TCP port.

◆ get_fd()

SOCKET MHA_TCP::Connection::get_fd ( ) const
inline

Return the (protected) file descriptor of the connection.

Will be required for SSL.

◆ eof()

bool Connection::eof ( )

Checks if the peer has closed the connection.

As a side effect, this method fills the internal "incoming" buffer if it was empty and the socket is readable and not eof.

◆ can_read_line()

bool Connection::can_read_line ( char  delim = '\n')

Checks if a full line of text has arrived by now.

This method reads data from the socket into the internal "incoming" buffer if it can be done without blocking.

Parameters
delimThe line delimiter.
Returns
true if at least one full line of text is present in the internal buffer after this method call, false otherwise.

◆ can_read_bytes()

bool Connection::can_read_bytes ( unsigned  howmany)

Checks if the specified ammount of data can be read.

This method reads data from the socket into an internal "incoming" buffer if it can be done without blocking.

Parameters
howmanyThe number of bytes that the caller wants to have checked.
Returns
true if at least the specified ammount of data is present in the internal buffer after this method call, false otherwise

◆ read_line()

std::string Connection::read_line ( char  delim = '\n')

Reads a single line of data from the socket.

Blocks if necessary.

Parameters
delimThe line delimiter.
Returns
The string of characters in this line, including the trailing delimiter. The delimiter may be missing if the last line before EOF does not have a delimiter.

◆ read_bytes()

std::string Connection::read_bytes ( unsigned  howmany)

Reads the specified ammount of dat from the socket.

Blocks if necessary.

Parameters
howmanyThe number of bytes to read.
Returns
The string of characters read. The string may be shorter if EOF is encountered.

◆ try_write()

void Connection::try_write ( const std::string &  data = "")

Adds data to the internal "outgoing" buffer, and then tries to write as much data from that buffer to the socket as possible without blocking.

Parameters
datadata to send over the socket.

◆ write()

void Connection::write ( const std::string &  data = "")

Adds data to the internal "outgoing" buffer, and then writes that that buffer to the socket, regardless of blocking.

Parameters
datadata to send over the socket.

◆ needs_write()

bool Connection::needs_write ( )

Checks if the internal "outgoing" buffer contains data.

◆ buffered_incoming_bytes()

unsigned Connection::buffered_incoming_bytes ( ) const

Returns the number of bytes in the internal "incoming" buffer.

◆ buffered_outgoing_bytes()

unsigned Connection::buffered_outgoing_bytes ( ) const

Returns the number of bytes in the internal "outgoing" buffer.

Member Data Documentation

◆ outbuf

std::string MHA_TCP::Connection::outbuf
private

◆ inbuf

std::string MHA_TCP::Connection::inbuf
private

◆ read_event

Sockread_Event* MHA_TCP::Connection::read_event
private

◆ write_event

Sockwrite_Event* MHA_TCP::Connection::write_event
private

◆ closed

bool MHA_TCP::Connection::closed
private

◆ peer_addr

struct sockaddr_in MHA_TCP::Connection::peer_addr
private

◆ fd

SOCKET MHA_TCP::Connection::fd
protected

The file descriptor of the TCP Socket.


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