ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
DgramSocketServerDescriptor Class Reference

Socket descriptor for connection-less communications. More...

#include <DgramSocketServerDescriptor.hpp>

Inheritance diagram for DgramSocketServerDescriptor:
Inheritance graph

Public Member Functions

 DgramSocketServerDescriptor (const uint16_t port)
 Constructor for UDP sockets.
 DgramSocketServerDescriptor (const std::string &name)
 Constructor for local connection-less sockets.
virtual ~DgramSocketServerDescriptor ()
 Destructor.
void close ()
 Close the descriptor.
- Public Member Functions inherited from PosixDescriptor
virtual ~PosixDescriptor ()
 Destructor.
void async_read (void(*handler)(Buffer *b, size_t size), Buffer *b, size_t size)
 Run asynchronous read operation.
void async_read (void(*handler)(void *b, size_t size), void *b, size_t size)
 Run asynchronous read operation.
void async_write (void(*handler)(Buffer *b, size_t size), Buffer *b, size_t size)
 Run asynchronous write operation.
void async_write (void(*handler)(void *b, size_t size), void *b, size_t size)
 Run asynchronous write operation.
int read (Buffer *b, size_t size)
 Method to read from the descriptor and fill a buffer.
int read (void *p, size_t size)
 Method to read from the descriptor.
int write (Buffer *b, size_t size)
 Method to write data in a buffer to the descriptor.
int write (const void *p, size_t size)
 Method to write to the descriptor.
int write (const std::string &s)
 Method to write a string to the descriptor.
int getDescriptorNumber () const
 Method to get descriptor number.
 PosixDescriptor (const PosixDescriptor &src)
 Copy constructor.
PosixDescriptoroperator= (const PosixDescriptor &src)
 Assignment operator.
bool flush ()
 Method to flush this specific descriptor.
int ioctl (int request)
 Ioctl on the file descriptor.
int ioctl (int request, void *argp)
 Ioctl on the file descriptor.

Private Member Functions

 DgramSocketServerDescriptor (const DgramSocketServerDescriptor &)
DgramSocketServerDescriptoroperator= (const DgramSocketServerDescriptor &)

Additional Inherited Members

- Protected Member Functions inherited from PosixDescriptor
int do_read (void *p, size_t size)
 Low-level read.
int do_write (const void *p, size_t size)
 Low-level write.
 PosixDescriptor ()
 Constructor.
- Protected Attributes inherited from PosixDescriptor
int fd_
 Number of the file descriptor.

Detailed Description

Socket descriptor for connection-less communications.

This is a class to accept connection-less connections.

Example of usage:

DgramSocketServerDescriptor serv ("/tmp/mysocket");
Buffer b (10);
serv.read(b, b.getSize());

Definition at line 52 of file DgramSocketServerDescriptor.hpp.

Constructor & Destructor Documentation

DgramSocketServerDescriptor ( const uint16_t  port)

Constructor for UDP sockets.

This constructor creates a connection-less AF_INET socket. It calls socket()+bind().

Parameters
portPort of the socket
Exceptions
runtime_errorin case of error in socket(), bind() or listen()

Definition at line 67 of file DgramSocketServerDescriptor.cpp.

{
// socket()
fd_ = socket(AF_INET, SOCK_DGRAM, 0);
if (fd_ < 0) {
ERROR("Socket creation");
throw std::runtime_error ("Socket error");
}
// bind()
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
ERROR("Socket binding");
throw std::runtime_error ("Bind error");
}
}

Here is the call graph for this function:

DgramSocketServerDescriptor ( const std::string &  name)

Constructor for local connection-less sockets.

This constructor creates a connection-less AF_UNIX socket. It calls socket()+bind().

Parameters
nameName of the local socket on the filesystem
Exceptions
runtime_errorin case of error in socket(), bind() or listen()

Definition at line 37 of file DgramSocketServerDescriptor.cpp.

{
// socket()
fd_ = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd_ < 0) {
ERROR("Socket creation");
throw std::runtime_error ("Socket error");
}
// bind()
struct sockaddr_un serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strncpy(serv_addr.sun_path, name.c_str(),
sizeof(serv_addr.sun_path) - 1);
if (bind(fd_, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
ERROR("Socket binding");
throw std::runtime_error ("Bind error");
}
}

Here is the call graph for this function:

virtual ~DgramSocketServerDescriptor ( )
inlinevirtual

Destructor.

It just calls close() to close the descriptor.

Definition at line 66 of file DgramSocketServerDescriptor.hpp.

{
close();
}

Here is the call graph for this function:

Member Function Documentation

void close ( )
inlinevirtual

Close the descriptor.

Reimplemented from PosixDescriptor.

Definition at line 73 of file DgramSocketServerDescriptor.hpp.

{
}

Here is the caller graph for this function:

DgramSocketServerDescriptor& operator= ( const DgramSocketServerDescriptor )
private

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