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

Posix pipe. More...

#include <Pipe.hpp>

Public Member Functions

 Pipe ()
 Constructor for the pipe.
virtual ~Pipe ()
 Destructor.
PosixDescriptorgetReadDescriptor ()
 Get the write endpoint.
PosixDescriptorgetWriteDescriptor ()
 Get the read endpoint.
int read (Buffer *b, size_t size)
 Method to read from the pipe and fill a buffer.
int read (void *p, size_t size)
 Method to read from the pipe.
int write (Buffer *b, size_t size)
 Method to write data in a buffer to the pipe.
int write (const void *p, size_t size)
 Method to write in the pipe.
int write (const std::string &s)
 Method to write a string in the pipe.
void close ()
 Method to close the pipe.
long int getRealCapacity () const
 Get real capacity of the pipe.

Static Public Member Functions

static long int getPosixCapacity ()
 Get posix capacity of the pipe.

Private Member Functions

 Pipe (const Pipe &)

Private Attributes

PosixDescriptorread_
PosixDescriptorwrite_

Detailed Description

Posix pipe.

This class wraps two PosixDescriptors because in some circumstances (e.g., after a fork()) the code wants to explicitly close one of the endpoints. The getReadDescriptor() and getWriteDescriptor() methods allow to get the PosixDescriptor and explicitly close it.

Definition at line 41 of file Pipe.hpp.

Constructor & Destructor Documentation

Pipe ( const Pipe )
private
Pipe ( )

Constructor for the pipe.

Exceptions
runtime_errorin case of error

Definition at line 21 of file Pipe.cpp.

: read_(0), write_(0)
{
int fd[2];
if (pipe(fd) != 0) {
ERROR("Opening pipe");
throw std::runtime_error ("Open pipe error");
}
read_ = new PosixDescriptor(fd[0]);
write_ = new PosixDescriptor(fd[1]);
}
virtual ~Pipe ( )
inlinevirtual

Destructor.

The destructor of PosixDescriptor will close the descriptor.

Definition at line 57 of file Pipe.hpp.

{
if (write_ != 0)
delete write_;
if (read_ != 0)
delete read_;
}

Member Function Documentation

void close ( )
inline

Method to close the pipe.

Note: currently there is no method to re-open the pipe.

Definition at line 157 of file Pipe.hpp.

{
}

Here is the call graph for this function:

static long int getPosixCapacity ( )
inlinestatic

Get posix capacity of the pipe.

Returns
capacity of the pipe based on the Posix standard

Definition at line 167 of file Pipe.hpp.

{
return _POSIX_PIPE_BUF;
}
PosixDescriptor* getReadDescriptor ( )
inline

Get the write endpoint.

This method is useful if we want to explicitly close the read endpoint.

Returns
A pointer to the PosixDescriptor for reading

Definition at line 71 of file Pipe.hpp.

{
return read_;
}
long int getRealCapacity ( ) const
inline

Get real capacity of the pipe.

Returns
real capacity of the pipe

Definition at line 176 of file Pipe.hpp.

{
return fpathconf(read_->getDescriptorNumber(),
_PC_PIPE_BUF);
}

Here is the call graph for this function:

PosixDescriptor* getWriteDescriptor ( )
inline

Get the read endpoint.

This method is useful if we want to explicitly close the write endpoint.

Returns
A pointer to the PosixDescriptor for writing

Definition at line 82 of file Pipe.hpp.

{
return write_;
}
int read ( Buffer b,
size_t  size 
)
inline

Method to read from the pipe and fill a buffer.

Note: this method may block current thread if data is not available. The buffer is filled with the read data.

Parameters
bPointer to the buffer to be filled
sizeNumber of bytes that must be read
Returns
-1 in case of error; the number of bytes read otherwise

Definition at line 96 of file Pipe.hpp.

{
return read_->read(b, size);
}

Here is the call graph for this function:

int read ( void *  p,
size_t  size 
)
inline

Method to read from the pipe.

Note: this method may block current thread if data is not available. The buffer is filled with the read data.

Parameters
pPointer to the memory space to be filled
sizeNumber of bytes that must be read
Returns
-1 in case of error; the number of bytes read otherwise

Definition at line 110 of file Pipe.hpp.

{
return read_->read(p, size);
}

Here is the call graph for this function:

int write ( Buffer b,
size_t  size 
)
inline

Method to write data in a buffer to the pipe.

Note: this method may block current thread if data cannot be written.

Parameters
bPointer to the buffer to be filled
sizeNumber of bytes that must be written
Returns
-1 in case of error; the number of bytes read otherwise

Definition at line 123 of file Pipe.hpp.

{
return write_->write(b, size);
}

Here is the call graph for this function:

int write ( const void *  p,
size_t  size 
)
inline

Method to write in the pipe.

Note: this method may block current thread if data cannot be written.

Parameters
pPointer to the memory space containing data
sizeNumber of bytes that must be written
Returns
-1 in case of error; the number of bytes read otherwise

Definition at line 136 of file Pipe.hpp.

{
return write_->write(p, size);
}

Here is the call graph for this function:

int write ( const std::string &  s)
inline

Method to write a string in the pipe.

Note: this method may block current thread if data cannot be written.

Parameters
stringto be written
Returns
-1 in case of error; the number of bytes read otherwise

Definition at line 148 of file Pipe.hpp.

{
return write_->write(s);
}

Here is the call graph for this function:

Member Data Documentation

PosixDescriptor* read_
private

Definition at line 43 of file Pipe.hpp.

PosixDescriptor* write_
private

Definition at line 44 of file Pipe.hpp.


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