MCS  0.3.3-alpha7
mcs::Pipe Class Reference

A high level class to use system pipes. More...

#include <mcs.hh>

Public Member Functions

void closeRead ()
 Close the file descriptor associated with the consumer side of the pipe. More...
 
void closeWrite ()
 Close the file descriptor associated with the producer side of the pipe. More...
 
bool consumerHasGone ()
 Tell if the consumer thread is still reading. More...
 
void create ()
 Create an unnamed pipe, both threads will read/write using file descriptors. More...
 
string createNamed ()
 Create a named pipe, that is a FIFO special file and returns the filename. More...
 
string filename ()
 If the pipe has been set-up through the createNamed() method this returns the generated file name. More...
 
bool isReady ()
 Returns true if the pipe has been set-up (through the create() or createNamed() methods). More...
 
int openRead ()
 Return the file descriptor for the consumer side of the pipe. More...
 
int openWrite ()
 Return the file descriptor for the producer side of the pipe. More...
 
 Pipe ()
 Constructor. More...
 
 ~Pipe ()
 Destructor. More...
 

Private Attributes

bool flcreated
 Tell if a pipe has been created. More...
 
bool named
 If it is a named pipe (a FIFO file) or not. More...
 
int pipefd [2]
 Pipe file descriptors (0 read side, 1 write side). More...
 
string pipefn
 Name of the FIFO file. More...
 
Synchro synchro
 To protect the filecount variable. More...
 

Static Private Attributes

static int filecount = 0
 Used to generate a unique filename. More...
 

Detailed Description

A high level class to use system pipes.

This class can be used to perform communication between threads using system pipes. There must always be two threads: a "producer" (write) and a "consumer" (read) and the same Pipe object must be accessible from both threads.

The communication is initiated by one of the threads through the create() or createNamed() methods, while the other is waiting until the isReady() method returns true (and maybe using sleep_ms()). When the create() method is used an unnamed pipe will be created and both threads will read/write using file descriptors. If createNamed() method is used then a FIFO special file will be created and (only) one thread will access the pipe as if it was a normal file (that is using open, or fopen, etc...). The filename for the FIFO is generated automatically and can be retrieved wih the filename() method.

The consumer thread obtain the file descriptor to read using the openRead() method, the producer thread obtain the file descriptor to write using the openWrite() method. To each openRead() (or openWrite()) call must correspond one closeRead() (or closeWrite()) call.

If the producer thread is using the file descriptor obtained through openWrite() then it can check whether the other side of the pipe (namely the consumer side) is still open by calling the consumerHasGone() method.

Definition at line 7694 of file mcs.hh.

Constructor & Destructor Documentation

◆ Pipe()

Pipe::Pipe ( )

Constructor.

Definition at line 42 of file Readers.cc.

◆ ~Pipe()

Pipe::~Pipe ( )

Destructor.

Definition at line 51 of file Readers.cc.

Member Function Documentation

◆ closeRead()

void Pipe::closeRead ( )

Close the file descriptor associated with the consumer side of the pipe.

Definition at line 172 of file Readers.cc.

◆ closeWrite()

void Pipe::closeWrite ( )

Close the file descriptor associated with the producer side of the pipe.

Definition at line 188 of file Readers.cc.

◆ consumerHasGone()

bool Pipe::consumerHasGone ( )

Tell if the consumer thread is still reading.

This method can be used only if the producer thread is using the file descriptor obtained through openWrite(). In this case the producer should check that the consumer is still reading before each write attempt.

Definition at line 65 of file Readers.cc.

◆ create()

void Pipe::create ( )

Create an unnamed pipe, both threads will read/write using file descriptors.

Definition at line 75 of file Readers.cc.

◆ createNamed()

string Pipe::createNamed ( )

Create a named pipe, that is a FIFO special file and returns the filename.

Definition at line 90 of file Readers.cc.

◆ filename()

string Pipe::filename ( )

If the pipe has been set-up through the createNamed() method this returns the generated file name.

Definition at line 61 of file Readers.cc.

◆ isReady()

bool Pipe::isReady ( )

Returns true if the pipe has been set-up (through the create() or createNamed() methods).

Definition at line 58 of file Readers.cc.

◆ openRead()

int Pipe::openRead ( )

Return the file descriptor for the consumer side of the pipe.

Definition at line 132 of file Readers.cc.

◆ openWrite()

int Pipe::openWrite ( )

Return the file descriptor for the producer side of the pipe.

Definition at line 152 of file Readers.cc.

Member Data Documentation

◆ filecount

int Pipe::filecount = 0
staticprivate

Used to generate a unique filename.

Definition at line 7700 of file mcs.hh.

◆ flcreated

bool mcs::Pipe::flcreated
private

Tell if a pipe has been created.

A pipe will be created when a create() or createNamed() method is invoked. Note that if a file is in the local filesystem the pipe won't be created.

Definition at line 7715 of file mcs.hh.

◆ named

bool mcs::Pipe::named
private

If it is a named pipe (a FIFO file) or not.

Definition at line 7718 of file mcs.hh.

◆ pipefd

int mcs::Pipe::pipefd[2]
private

Pipe file descriptors (0 read side, 1 write side).

Definition at line 7706 of file mcs.hh.

◆ pipefn

string mcs::Pipe::pipefn
private

Name of the FIFO file.

Definition at line 7703 of file mcs.hh.

◆ synchro

Synchro mcs::Pipe::synchro
private

To protect the filecount variable.

Definition at line 7697 of file mcs.hh.


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

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018