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

Class for platform-dependent code. More...

#include <posix_socket.hpp>

Inheritance diagram for PosixSocket:
Inheritance graph

Public Member Functions

 PosixSocket (const protocol &prot)
 Constructor.
virtual ~PosixSocket ()
 Destructor.
virtual void connect (const Address &addr)
 Method to connect() the socket to an address.
virtual void bind (const Address &addr)
 Method to bind() the socket to an address.
virtual int read (void *buffer, size_t size)
 Low-level read.
virtual int write (const void *buffer, size_t size)
 Low-level write.
virtual bool close ()
 Method to close the socket.
virtual void accept (AbstractSystemSocket *sock)
 Method to accept() a connection on the socket.
virtual void listen (int maxPendingConnections)
 Listen operation.
- Public Member Functions inherited from AbstractSystemSocket
protocol getProtocol () const
 Method to get the socket protocol.

Private Attributes

int fd_
 Number of the file descriptor.

Additional Inherited Members

- Protected Member Functions inherited from AbstractSystemSocket
 AbstractSystemSocket (const protocol &prot)
 Constructor.
- Protected Attributes inherited from AbstractSystemSocket
protocol protocol_
 Protocol used by the socket.

Detailed Description

Class for platform-dependent code.

This is the class containing the code for Posix platforms.

Definition at line 45 of file posix_socket.hpp.

Constructor & Destructor Documentation

PosixSocket ( const protocol prot)

Constructor.

The concrete class constructed depends on the specific protocol.

Parameters
protprotocol used by the socket
Exceptions
runtime_errorin case of unknown protocol

Definition at line 54 of file posix_socket.cpp.

:
{
if (prot == protocol(STREAM, LOCAL))
fd_ = socket(AF_LOCAL, SOCK_STREAM, 0);
else if (prot == protocol(DGRAM, LOCAL))
fd_ = socket(AF_LOCAL, SOCK_DGRAM, 0);
else if (prot == protocol(STREAM, IPv4))
fd_ = socket(AF_INET, SOCK_STREAM, 0);
else if (prot == protocol(DGRAM, IPv4))
fd_ = socket(AF_INET, SOCK_DGRAM, 0);
else {
ERROR("Error: protocol unkown");
throw std::runtime_error ("Protocol unknown");
}
if (fd_ < 0) {
ERROR("Error when creating socket");
throw std::runtime_error ("Socket error");
}
}
~PosixSocket ( )
virtual

Destructor.

It just calls close()

Definition at line 80 of file posix_socket.cpp.

{
close();
}

Here is the call graph for this function:

Member Function Documentation

void accept ( AbstractSystemSocket sock)
virtual

Method to accept() a connection on the socket.

This method calls accept(). This method is usually invoked on the server-side for stream communications.

Parameters
sockSocket on which the new connection must be accepted.
Exceptions
runtime_errorin case of error in accept()

Implements AbstractSystemSocket.

Definition at line 130 of file posix_socket.cpp.

{
if ((sock->getProtocol() != getProtocol()) || (getProtocol().getType() != net::STREAM)) {
ERROR("Accept not available!");
throw std::runtime_error("Accept not available");
}
fd_ = ::accept((dynamic_cast<PosixSocket*> (sock))->fd_, NULL, 0);
if (fd_ < 0) {
ERROR("Error in accept()!");
throw std::runtime_error("Accept error");
}
}

Here is the call graph for this function:

void bind ( const Address addr)
virtual

Method to bind() the socket to an address.

This method calls bind(). This method is usually invoked on the server-side.

Parameters
addrAddress which the socket must be bound to
Exceptions
runtime_errorin case of error in bind()

Implements AbstractSystemSocket.

Definition at line 174 of file posix_socket.cpp.

{
if (getProtocol().getDomain() == net::LOCAL) {
DEBUG("Local protocol found");
struct sockaddr_un serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_LOCAL;
strncpy(serv_addr.sun_path, addr.getAddress().c_str(),
sizeof(serv_addr.sun_path) - 1);
if (::bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == 0)
return;
} else if (getProtocol().getDomain() == net::IPv4) {
DEBUG("Network protocol found");
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons((dynamic_cast<const ip4::tcp::address*>(&addr))->getPort());
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (::bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == 0)
return;
} else {
ERROR("Unknown protocol found");
}
error:
ERROR("Error when binding socket");
throw std::runtime_error ("Bind error");
}

Here is the call graph for this function:

bool close ( )
virtual

Method to close the socket.

Returns
true in case of success; false otherwise

Implements AbstractSystemSocket.

Definition at line 118 of file posix_socket.cpp.

{
return !(::close(fd_));
}

Here is the caller graph for this function:

void connect ( const Address addr)
virtual

Method to connect() the socket to an address.

This method calls connect(). This method is usually invoked on the client-side.

Parameters
addrAddress which the socket must be connected to
Exceptions
runtime_errorin case of error in connect()

Implements AbstractSystemSocket.

Definition at line 213 of file posix_socket.cpp.

{
if (getProtocol().getDomain() == net::LOCAL) {
struct sockaddr_un serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_LOCAL;
strncpy(serv_addr.sun_path, addr.getAddress().c_str(), sizeof(serv_addr.sun_path) - 1);
if (::connect(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == 0)
return;
} else if (getProtocol().getDomain() == net::IPv4) {
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons((dynamic_cast<const ip4::tcp::address*>(&addr))->getPort());
struct in_addr add;
inet_aton(addr.getAddress().c_str(), &add);
bcopy(&add, &serv_addr.sin_addr.s_addr, sizeof(add));
if (::connect(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == 0)
return;
}
error:
ERROR("Error when creating client socket");
throw std::runtime_error ("Client socket error");
}

Here is the call graph for this function:

void listen ( int  max_pending_connections)
virtual

Listen operation.

This method calls listen() and allows to specify the number of maximum allowed pending connections. This method is usually invoked on the server-side.

Parameters
max_pending_connectionsNumber of maximum allowed pending connections.
Exceptions
runtime_errorin case of error in listen()

Implements AbstractSystemSocket.

Definition at line 153 of file posix_socket.cpp.

{
if (getProtocol().getType() != net::STREAM) {
ERROR("Listen not available!");
throw std::runtime_error("Listen not available");
}
if (::listen(fd_, max_pending_connections) < 0) {
ERROR("Error when listening");
throw std::runtime_error ("Listen error");
}
}

Here is the call graph for this function:

int read ( void *  buffer,
size_t  size 
)
virtual

Low-level read.

Parameters
bufferPointer to the buffer where read bytes must be stored
sizeNumber of bytes to be read
Returns
The number of actually read bytes or -1 in case of error

Implements AbstractSystemSocket.

Definition at line 93 of file posix_socket.cpp.

{
}
int write ( const void *  buffer,
size_t  size 
)
virtual

Low-level write.

Parameters
bufferPointer to the buffer containing bytes to be written
sizeNumber of bytes to be written
Returns
The number of actually written bytes or -1 in case of error

Implements AbstractSystemSocket.

Definition at line 107 of file posix_socket.cpp.

{
}

Member Data Documentation

int fd_
private

Number of the file descriptor.

This is the return value of open(), socket() or accept().

Definition at line 65 of file posix_socket.hpp.


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