ONPOSIX
2.0
|
Abstraction of a POSIX descriptor. More...
#include <PosixDescriptor.hpp>
Classes | |
struct | job |
Single asynchronous operation. More... | |
class | shared_queue |
Class for synchronization between the main thread and the worker thread. More... | |
class | Worker |
Worker thread to perform asynchronous operations. More... |
Public Member Functions | |
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. | |
virtual void | close () |
Method to close the descriptor. | |
int | getDescriptorNumber () const |
Method to get descriptor number. | |
PosixDescriptor (const PosixDescriptor &src) | |
Copy constructor. | |
PosixDescriptor & | operator= (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. |
Protected Member Functions | |
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 | |
int | fd_ |
Number of the file descriptor. |
Private Member Functions | |
PosixDescriptor (int fd) | |
Private constructor used by derived classes. |
Private Attributes | |
Worker * | worker_ |
Pointer to the worker that performs asynchronous operations. | |
shared_queue * | queue_ |
Pointer to the shared_queue for synchronization with the worker thread. | |
bool | worker_started_ |
If the worker thread has been already started. |
Friends | |
class | Pipe |
class | AsyncThread |
Abstraction of a POSIX descriptor.
This is an abstract class for the concept of Posix descriptor. The descriptor can correspond to a file (class FileDescriptor) or to a socket (class StreamSocketServerDescriptor).
Definition at line 58 of file PosixDescriptor.hpp.
|
inlineprivate |
Private constructor used by derived classes.
It allocates queue_ and worker_.
fd | File descriptor number returned by open(), socket(), accept(), etc. |
Definition at line 352 of file PosixDescriptor.hpp.
|
inlineprotected |
|
inlinevirtual |
Destructor.
It closes the file descriptor and deallocates queue_ and worker_.
Definition at line 388 of file PosixDescriptor.hpp.
|
inline |
Copy constructor.
The copy constructor is called to copy an existing object to another object that is being constructed. Examples:
It allocates queue_ and worker_.
runtime_error | if the ::dup() returns an error |
Definition at line 546 of file PosixDescriptor.hpp.
Run asynchronous read operation.
This method schedules an asynchronous read operation. The operation is internally run on a different thread.
handler | Function to be run when the read operation has finished. This function will have two parameters: a pointer to the Buffer where data has been saved, and the number of bytes actually read. |
b | Pointer to the Buffer to be provided to the handler function as argument |
size | Number of bytes to be read |
Definition at line 413 of file PosixDescriptor.hpp.
|
inline |
Run asynchronous read operation.
This method schedules an asynchronous read operation. The operation is internally run on a different thread.
handler | Function to be run when the read operation has finished. This function will have two parameters: a void* where data has been saved, and the number of bytes actually read. |
b | Pointer to be provided to the handler function as argument |
size | Number of bytes to be read |
Definition at line 437 of file PosixDescriptor.hpp.
Run asynchronous write operation.
This method schedules an asynchronous write operation. The operation is internally run on a different thread.
handler | Function to be run when the write operation has finished. This function will have two parameters: a pointer to the Buffer where original data was stored, and the number of bytes actually written. |
b | Pointer to the Buffer to be provided to the handler function as argument |
size | Number of bytes to be written. |
Definition at line 462 of file PosixDescriptor.hpp.
|
inline |
Run asynchronous write operation.
This method schedules an asynchronous write operation. The operation is internally run on a different thread.
handler | Function to be run when the write operation has finished. This function will have two parameters: a void* where original data was stored, and the number of bytes actually written. |
b | Pointer to be provided to the handler function as argument |
size | Number of bytes to be written |
Definition at line 485 of file PosixDescriptor.hpp.
|
inlinevirtual |
Method to close the descriptor.
Note: currently there is no method to re-open the descriptor. In case the worker thread has been started, it signals the worker that it must not block on wait anymore (through set_flush_and_close()); then it unblocks the worker (through signal_not_empty()).
Reimplemented in DgramSocketServerDescriptor.
Definition at line 510 of file PosixDescriptor.hpp.
|
protected |
Low-level read.
This method is private because it is meant to be used through the other read() methods. Note: it can block the caller, because it continues reading until the given number of bytes have been read.
buffer | Pointer to the buffer where read bytes must be stored |
size | Number of bytes to be read |
runtime_error | if the ::read() returns an error |
Definition at line 156 of file PosixDescriptor.cpp.
|
protected |
Low-level write.
This method is private because it is meant to be used through the other write() methods. Note: it can block the caller, because it continues writing until the given number of bytes have been written.
buffer | Pointer to the buffer containing bytes to be written |
size | Number of bytes to be written |
runtime_error | if the ::write() returns 0 or an error |
Definition at line 224 of file PosixDescriptor.cpp.
|
inline |
Method to flush this specific descriptor.
Definition at line 582 of file PosixDescriptor.hpp.
|
inline |
Method to get descriptor number.
Definition at line 528 of file PosixDescriptor.hpp.
|
inline |
Ioctl on the file descriptor.
Definition at line 592 of file PosixDescriptor.hpp.
|
inline |
Ioctl on the file descriptor.
Definition at line 599 of file PosixDescriptor.hpp.
|
inline |
Assignment operator.
The assignment operator is called to copy an existing object to another object that is already existing as well. Examples:
runtime_error | if the ::dup() returns an error |
Definition at line 569 of file PosixDescriptor.hpp.
int read | ( | Buffer * | b, |
size_t | size | ||
) |
Method to read from the descriptor and fill a buffer.
Note: this method may block current thread if data is not available. The buffer is filled with the read data.
b | Pointer to the buffer to be filled |
size | Number of bytes that must be read |
Definition at line 184 of file PosixDescriptor.cpp.
int read | ( | void * | p, |
size_t | size | ||
) |
Method to read from the descriptor.
Note: this method may block current thread if data is not available. The buffer is filled with the read data.
p | Pointer to the memory space to be filled |
size | Number of bytes that must be read |
Definition at line 203 of file PosixDescriptor.cpp.
int write | ( | Buffer * | b, |
size_t | size | ||
) |
Method to write data in a buffer to the descriptor.
Note: this method may block current thread if data cannot be written.
b | Pointer to the buffer to be filled |
size | Number of bytes that must be written |
Definition at line 251 of file PosixDescriptor.cpp.
int write | ( | const void * | p, |
size_t | size | ||
) |
Method to write to the descriptor.
Note: this method may block current thread if data cannot be written.
p | Pointer to the memory space containing data |
size | Number of bytes that must be written |
Definition at line 269 of file PosixDescriptor.cpp.
int write | ( | const std::string & | s | ) |
Method to write a string to the descriptor.
Note: this method may block current thread if data cannot be written.
string | to be written |
Definition at line 282 of file PosixDescriptor.cpp.
|
friend |
Definition at line 358 of file PosixDescriptor.hpp.
|
friend |
Definition at line 357 of file PosixDescriptor.hpp.
|
protected |
Number of the file descriptor.
This is the return value of open(), socket() or accept().
Definition at line 366 of file PosixDescriptor.hpp.
|
private |
Pointer to the shared_queue for synchronization with the worker thread.
This data structure is allocated on the heap in the constructors (2 standard + 1 copy) and deallocated in the destructor.
Definition at line 336 of file PosixDescriptor.hpp.
|
private |
Pointer to the worker that performs asynchronous operations.
The worker is allocated on the heap in the constructors (2 standard + 1 copy) and deallocated in the destructor.
Definition at line 327 of file PosixDescriptor.hpp.
|
private |
If the worker thread has been already started.
This variable is modified in async_read() and async_write();
Definition at line 343 of file PosixDescriptor.hpp.