NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
AbstractSocket Class Reference

Base abstract class for protocols. More...

#include <abstract_socket.hpp>

Inheritance diagram for AbstractSocket:
Inheritance graph

Public Member Functions

bool close ()
 Close the socket.
AbstractSystemSocketgetSocket ()
 get the socket
int receive (__buffer buf, std::size_t size)
 Receive operation.
int send (__buffer buf, std::size_t size)
 Send operation.

Protected Member Functions

 AbstractSocket (const protocol &prot)
 Constructor.

Private Member Functions

int __receive (void *buffer, size_t size)
 Low-level receive.
int __send (const void *buffer, size_t size)
 Low-level send.

Private Attributes

std::unique_ptr< std::thread > receive_worker_
 Thread for asynchronous receive operations.
std::unique_ptr< std::thread > send_worker_
 Thread for asynchronous send operations.
std::mutex receive_lock_
 Mutex for synchronization with asynchronous receive operations.
std::mutex send_lock_
 Mutex for synchronization with asynchronous send operations.

Detailed Description

Base abstract class for protocols.

This is the base abstract class for protocols. Any supported protocol must inherit from this class.

Definition at line 76 of file abstract_socket.hpp.

Constructor & Destructor Documentation

AbstractSocket ( const protocol prot)
inlineprotected

Constructor.

Constructor is protected because only derived classes can construct this class. The object is constructed through net::createSocket(), which creates a platform-dependent socket.

Parameters
theprotocol type

Definition at line 118 of file abstract_socket.hpp.

:
socket_{createSocket(prot)} {}

Here is the call graph for this function:

Member Function Documentation

int __receive ( void *  buffer,
size_t  size 
)
private

Low-level receive.

This method is private because it is meant to be used through the other receive() method. Note: it can block the caller, because it continues receiving until the given number of bytes have been received.

Parameters
bufferPointer to the buffer where received bytes must be stored
sizeNumber of bytes to be received
Exceptions
runtime_errorif the read() returns an error
Returns
The number of actually received bytes or -1 in case of error

Definition at line 118 of file abstract_socket.cpp.

{
size_t remaining = size;
while (remaining > 0) {
ssize_t ret = socket_->read (((char*)buffer)+(size-remaining),
remaining);
if (ret == 0){
// End of file reached
DEBUG("End of file reached");
break;
} else if (ret < 0) {
ERROR("Receive error");
throw std::runtime_error ("Receive error");
return -1;
}
remaining -= ret;
}
return (size-remaining);
}

Here is the caller graph for this function:

int __send ( const void *  buffer,
size_t  size 
)
private

Low-level send.

This method is private because it is meant to be used through the other send() method. Note: it can block the caller, because it continues writing until the given number of bytes have been written.

Parameters
bufferPointer to the buffer containing bytes to be written
sizeNumber of bytes to be written
Exceptions
runtime_errorif the write() returns 0 or an error
Returns
The number of actually written bytes or -1 in case of error

Definition at line 152 of file abstract_socket.cpp.

{
size_t remaining = size;
while (remaining > 0) {
ssize_t ret = socket_->write (((char*)buffer)+(size-remaining), remaining);
if (ret == 0){
DEBUG("Cannot send any further");
// Cannot send more
break;
} else if (ret < 0) {
ERROR("Send error");
throw std::runtime_error ("Send error");
return -1;
}
remaining -= ret;
}
return (size-remaining);
}

Here is the caller graph for this function:

bool close ( )
inline

Close the socket.

Method to close the socket. Currently, there is no mechanism to re-open a closed socket.

Returns
true in case of success; false otherwise

Definition at line 87 of file abstract_socket.hpp.

{
return socket_->close();
}
AbstractSystemSocket* getSocket ( )
inline

get the socket

This method is used to let derived classes of AbstractSocket invoke specific protocol-dependent functions (e.g., accept()).

Returns
the socket used by this instance of the class

Definition at line 100 of file abstract_socket.hpp.

{
return socket_.get();
}
int receive ( __buffer  buf,
std::size_t  size 
)

Receive operation.

This function sendsendsensendsendsendcare of synchronization with any other asynchronous operations. Note: it can block the caller, because it calls __receive() which continues receiving until the given number of bytes have been received.

Parameters
bufPointer where received data must be put
sizeSize of data to be received
Returns
Number of bytes actually received
Exceptions
runtime_errorin case of too small buffer

Example of usage: std::array<char, 5> buf; AbstractSocket::receive(net::buffer(b), 3);

Definition at line 51 of file abstract_socket.cpp.

{
int ret;
if (buf.size_ == 0 || size > buf.size_){
ERROR("Wrong buffer size!");
throw std::runtime_error ("Wrong buffer size");
}
receive_lock_.lock();
try {
ret = __receive(buf.ptr_, size);
} catch (...) {
ERROR("Receive error!");
}
receive_lock_.unlock();
return ret;
}

Here is the call graph for this function:

int send ( __buffer  buf,
std::size_t  size 
)

Send operation.

This function sends data to the socket taking care of synchronization with any other asynchronous operations. Note: it can block the caller, because it calls __send() which continues writing until the given number of bytes have been written.

Parameters
bufPointer to data to be written
sizeSize of data to be written
Returns
Number of bytes actually written
Exceptions
runtime_errorin case of too small buffer

Example of usage: std::array<char, 5> buf; AbstractSocket::send(net::buffer(b), 3);

Definition at line 87 of file abstract_socket.cpp.

{
int ret;
if (buf.size_ == 0 || size > buf.size_){
ERROR("Wrong buffer size!");
throw std::runtime_error ("Wrong buffer size");
}
send_lock_.lock();
try {
ret = __send(buf.ptr_, size);
} catch (...) {
ERROR("Send error!");
}
send_lock_.unlock();
return ret;
}

Here is the call graph for this function:

Member Data Documentation

std::mutex receive_lock_
private

Mutex for synchronization with asynchronous receive operations.

Definition at line 147 of file abstract_socket.hpp.

std::unique_ptr<std::thread> receive_worker_
private

Thread for asynchronous receive operations.

Definition at line 137 of file abstract_socket.hpp.

std::mutex send_lock_
private

Mutex for synchronization with asynchronous send operations.

Definition at line 152 of file abstract_socket.hpp.

std::unique_ptr<std::thread> send_worker_
private

Thread for asynchronous send operations.

Definition at line 142 of file abstract_socket.hpp.


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