vpr::FileHandleImplUNIX Class Reference

Wrapper around UNIX file descriptors. More...

#include <vpr/IO/FileHandle.h>

List of all members.

Public Member Functions

 FileHandleImplUNIX ()
 Constructor for unnamed file-based devices.
 FileHandleImplUNIX (const std::string &file_name)
 Constructor.
 ~FileHandleImplUNIX ()
 Destructor.
const std::string & getName () const
 Gets the name of this file.
vpr::ReturnStatus open ()
 Opens the file handle.
vpr::ReturnStatus close ()
 Closes the file handle.
bool isOpen () const
 Gets the open state of this file handle.
vpr::ReturnStatus setBlocking (bool blocking)
 Reconfigures the file handle so that it is in blocking or non-blocking mode.
vpr::IOSys::Handle getHandle () const
 Returns the contained handle.
bool isBlocking () const
 Gets the current blocking state for the file handle.
void setOpenReadOnly ()
 Sets the open flags so that the I/O device is opened in read-only mode.
void setOpenWriteOnly ()
 Sets the open flags so that the I/O device is opened in write-only mode.
void setOpenReadWrite ()
 Sets the open flags so that the I/O device is opened in read/write mode.
vpr::ReturnStatus setAppend (bool flag)
 Reconfigures the file handle to be in append mode or not.
vpr::ReturnStatus setSynchronousWrite (bool flag)
 Reconfigures the file handle so that writes are synchronous or asynchronous depending on the value of the parameter.
bool isReadOnly () const
 Tests if the I/O device is read-only.
bool isWriteOnly () const
 Tests if the I/O device is write-only.
bool isReadWrite () const
 Tests if the I/O device is read/write.
vpr::ReturnStatus getReadBufferSize (vpr::Int32 &buffer) const
 Queries the amount of data currently in the read buffer.
vpr::ReturnStatus read_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytesRead, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the read template method.
vpr::ReturnStatus readn_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytesRead, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the readn template method.
vpr::ReturnStatus write_i (const void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytesWritten, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the write template method.
vpr::Uint32 availableBytes () const
 Returns the number of bytes available for reading in the receive buffer.

Protected Member Functions

int getFlags () const
 Gets the current file handle flags.
int setFlags (const int flags)
 Overwrites the current file handle flags with the given value.
vpr::ReturnStatus isReadable (const vpr::Interval timeout) const
 Tests if the file handle is ready for reading within the timeout period.
vpr::ReturnStatus isWriteable (const vpr::Interval timeout) const
 Tests if the file handle is ready for writing within the timeout period.

Protected Attributes

std::string mName
 The name of this file.
bool mOpen
 Open state of this file.
bool mOpenBlocking
bool mBlocking
 Blocking state of this file.
int mFdesc
 File descriptor.
int mOpenMode
 The open mode of the device.

Friends

class SerialPortImplTermios
class SocketDatagramImplBSD
class SocketImplBSD
class SocketStreamImplBSD


Detailed Description

Wrapper around UNIX file descriptors.

This is used with vpr::FileHandle_t<T> to create the typedef vpr::FileHandle.

Definition at line 63 of file FileHandleImplUNIX.h.


Constructor & Destructor Documentation

vpr::FileHandleImplUNIX::FileHandleImplUNIX (  ) 

Constructor for unnamed file-based devices.

This initializes the member variables to reasonable defaults and stores the given file name for later use.

Postcondition:
All member variables are initialized except mName.

Definition at line 79 of file FileHandleImplUNIX.cpp.

00080    : mOpen(false)
00081    , mOpenBlocking(true)
00082    , mBlocking(true)
00083    , mFdesc(-1)
00084    , mOpenMode(O_RDWR)
00085 {
00086    /* Do nothing. */ ;
00087 }

vpr::FileHandleImplUNIX::FileHandleImplUNIX ( const std::string &  file_name  ) 

Constructor.

This initializes the member variables to reasonable defaults and stores the given file name for later use.

Postcondition:
All member variables are initialized including mName that is assigned the string in file_name.
Parameters:
file_name The name of the file to be handled.

Definition at line 91 of file FileHandleImplUNIX.cpp.

00092    : mName(file_name)
00093    , mOpen(false)
00094    , mOpenBlocking(true)
00095    , mBlocking(true)
00096    , mFdesc(-1)
00097    , mOpenMode(O_RDWR)
00098 {
00099    /* Do nothing. */ ;
00100 }

vpr::FileHandleImplUNIX::~FileHandleImplUNIX (  ) 

Destructor.

If the file handle is in an open state, it is closed.

Postcondition:
If the file handle is still open, it is closed.

Definition at line 103 of file FileHandleImplUNIX.cpp.

References close(), and mOpen.

00104 {
00105    if ( mOpen )
00106    {
00107       close();
00108    }
00109 }


Member Function Documentation

const std::string& vpr::FileHandleImplUNIX::getName (  )  const [inline]

Gets the name of this file.

Postcondition:
Returns:
An object containing the name of this file.

Definition at line 104 of file FileHandleImplUNIX.h.

References mName.

Referenced by vpr::SocketImplBSD::getName(), vpr::SerialPortImplTermios::getName(), vpr::SocketImplBSD::getOption(), vpr::SocketDatagramImplBSD::SocketDatagramImplBSD(), and vpr::SocketStreamImplBSD::SocketStreamImplBSD().

00105    {
00106       return mName;
00107    }

vpr::ReturnStatus vpr::FileHandleImplUNIX::open (  ) 

Opens the file handle.

Precondition:
The file handle is not already open.
Postcondition:
An attempt is made to open the file. The resulting status is returned to the caller. If the file is opened, mOpen is set to true.
Returns:
vpr::ReturnStatus::Succeed is returned if the file handle was opened successfully. vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 112 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, mBlocking, mFdesc, mName, mOpen, mOpenBlocking, mOpenMode, vpr::ReturnStatus::setCode(), vprDBG_CRITICAL_LVL, vprDBG_ERROR(), vprDEBUG, vprDEBUG_FLUSH, and vpr::ReturnStatus::WouldBlock.

Referenced by vpr::SerialPortImplTermios::open().

00113 {
00114    vpr::ReturnStatus status;
00115 
00116    int open_flags(mOpenMode);
00117 
00118    if ( ! mOpenBlocking )
00119    {
00120       open_flags |= O_NONBLOCK;
00121    }
00122 
00123    mFdesc = ::open(mName.c_str(), open_flags);
00124 
00125    // If the file handle was not returned successfully, print an error
00126    // message explaining why.
00127    if ( mFdesc == -1 )
00128    {
00129       // If we are opening in non-blocking mode, we do not want to bomb out.
00130       if ( errno == EWOULDBLOCK && ! mOpenBlocking )
00131       {
00132          status.setCode(vpr::ReturnStatus::WouldBlock);
00133          mOpen = true;
00134       }
00135       // Otherwise, report the error.
00136       else
00137       {
00138          vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00139             << "[vpr::FileHandleImplUNIX::open()] Could not open " << mName
00140             << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00141          status.setCode(ReturnStatus::Fail);
00142          mOpen = false;
00143       }
00144    }
00145    // Otherwise, set mOpen to true.
00146    else
00147    {
00148       mOpen     = true;
00149       mBlocking = mOpenBlocking;
00150    }
00151 
00152    return status;
00153 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::close (  ) 

Closes the file handle.

Precondition:
The file handle is open.
Postcondition:
An attempt is made to close the file. The resulting status is returned to the caller. If the file is closed, mOpen is set to false.
Returns:
vpr::ReturnStatus::Succeed is returned if the file handle was closed successfully.

vpr::ReturnStatus::Fail is returned if the file handle could not be closed for some reason.

Definition at line 156 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, mFdesc, mName, mOpen, vpr::ReturnStatus::setCode(), vprDBG_ALL(), vprDBG_VERB_LVL, vprDBG_WARNING_LVL, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by vpr::SocketImplBSD::close(), vpr::SerialPortImplTermios::close(), and ~FileHandleImplUNIX().

00157 {
00158    vpr::ReturnStatus status;
00159 
00160    vprDEBUG(vprDBG_ALL, vprDBG_VERB_LVL)
00161       << "[vpr::FileHandleImplUNIX::close()] Closing file descriptor "
00162       << mFdesc << std::endl << vprDEBUG_FLUSH;
00163 
00164    if ( ::close(mFdesc) == -1 )
00165    {
00166       vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00167          << "[vpr::FileHandleImplUNIX::close()] Could not close " << mName
00168          << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00169       status.setCode(ReturnStatus::Fail);
00170    }
00171    else
00172    {
00173       mFdesc = -1;
00174       mOpen  = false;
00175    }
00176 
00177    return status;
00178 }

bool vpr::FileHandleImplUNIX::isOpen (  )  const [inline]

Gets the open state of this file handle.

Precondition:
None.
Postcondition:
The boolean value giving the open state is returned to the caller.
Returns:
true is returned if this file handle is open; false otherwise.

Definition at line 147 of file FileHandleImplUNIX.h.

References mOpen.

Referenced by vpr::SocketImplBSD::isOpen().

00148    {
00149       return mOpen;
00150    }

vpr::ReturnStatus vpr::FileHandleImplUNIX::setBlocking ( bool  blocking  ) 

Reconfigures the file handle so that it is in blocking or non-blocking mode.

If this file handle has not been opened yet, it will be opened in blocking or non-blocking mode appropriately when open() is called.

Postcondition:
Processes may block (or not) when accessing the file.
Parameters:
blocking A value of true indicates that the file handle will use blocking I/O. A value of false indicates that it will use non-blocking I/O.
Returns:
vpr::ReturnStatus::Succeed is returned if the blocking mode was changed successfully; vpr::ReturnStatus::Fail otherwise.

Definition at line 181 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, getFlags(), mBlocking, mName, mOpen, mOpenBlocking, vpr::ReturnStatus::setCode(), setFlags(), vprDBG_CRITICAL_LVL, vprDBG_ERROR(), vprDEBUG, and vprDEBUG_FLUSH.

Referenced by vpr::SocketImplBSD::open(), vpr::SocketImplBSD::setBlocking(), and vpr::SerialPortImplTermios::setBlocking().

00182 {
00183    vpr::ReturnStatus retval;
00184 
00185    if ( ! mOpen )
00186    {
00187       mOpenBlocking = blocking;
00188    }
00189    else
00190    {
00191       int cur_flags, new_flags;
00192 
00193       // Get the current flags.
00194       cur_flags = getFlags();
00195 
00196       if ( blocking )
00197       {
00198 #ifdef _SGI_SOURCE
00199          // On IRIX, mask FNONBLK and FNDELAY.  We mask FNDELAY to ensure that
00200          // it is not set by the operating system at some level.
00201          new_flags = cur_flags & ~(FNONBLK | FNDELAY);
00202 #else
00203          // On everything else, mask O_NONBLOCK and O_NDELAY.  We mask O_NDELAY
00204          // to ensure that it is not set by the operating system at some level.
00205          new_flags = cur_flags & ~(O_NONBLOCK | O_NDELAY);
00206 #endif
00207       }
00208       else
00209       {
00210 #ifdef _SVR4_SOURCE
00211          // On SysV, set FNONBLK.  We do not set FNDELAY because it just adds
00212          // confusion.  FNONBLK is preferred.
00213          new_flags = cur_flags | FNONBLK;
00214 #else
00215          // On everything else, set O_NONBLOCK.  We do not set O_NDELAY because
00216          // it just adds confusion.  O_NONBLOCK is preferred.
00217          new_flags = cur_flags | O_NONBLOCK;
00218 #endif
00219       }
00220 
00221       // Set the file descriptor to be blocking with the new flags.
00222       if ( setFlags(new_flags) == -1 )
00223       {
00224          vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00225             << "[vpr::FileHandleImplUNIX::setBlocking()] Failed to set "
00226             << (blocking ? "blocking" : "non-blocking") << " state on "
00227             << mName << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00228          retval.setCode(ReturnStatus::Fail);
00229       }
00230       else
00231       {
00232          mBlocking = blocking;
00233       }
00234    }
00235 
00236    return retval;
00237 }

vpr::IOSys::Handle vpr::FileHandleImplUNIX::getHandle (  )  const

Returns the contained handle.

Definition at line 239 of file FileHandleImplUNIX.cpp.

References mFdesc, vpr::IOSysUnix::NullHandle, vprDBG_ALL(), vprDBG_CRITICAL_LVL, and vprDEBUG.

Referenced by vpr::SocketImplBSD::getHandle(), and vpr::SerialPortImplTermios::getHandle().

00240 {
00241 #ifdef VPR_USE_NSPR
00242    vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
00243        << "ERROR: Cannot get handle for UNIX file descriptor with NSPR!\n";
00244    return vpr::IOSys::NullHandle;
00245 #else
00246    return mFdesc;
00247 #endif
00248 }

bool vpr::FileHandleImplUNIX::isBlocking (  )  const [inline]

Gets the current blocking state for the file handle.

Precondition:
mBlocking is set correctly.
Returns:
true is returned if the file handle is in blocking mode. Otherwise, false is returned.

Definition at line 181 of file FileHandleImplUNIX.h.

References mBlocking.

Referenced by vpr::SocketImplBSD::isBlocking(), and vpr::SerialPortImplTermios::isBlocking().

00182    {
00183       return mBlocking;
00184    }

void vpr::FileHandleImplUNIX::setOpenReadOnly (  ) 

Sets the open flags so that the I/O device is opened in read-only mode.

Precondition:
None.
Postcondition:
The open flags are updated so that when the device is opened, it it is opened in read-only mode. If the device is already open, this has no effect.

Definition at line 250 of file FileHandleImplUNIX.cpp.

References mOpenMode.

Referenced by vpr::SerialPortImplTermios::setOpenReadOnly().

00251 {
00252    mOpenMode = O_RDONLY;
00253 }

void vpr::FileHandleImplUNIX::setOpenWriteOnly (  ) 

Sets the open flags so that the I/O device is opened in write-only mode.

Precondition:
None.
Postcondition:
The open flags are updated so that when the device is opened, it is opened in write-only mode. If the device is already open, this has no effect.

Definition at line 255 of file FileHandleImplUNIX.cpp.

References mOpenMode.

Referenced by vpr::SerialPortImplTermios::setOpenWriteOnly().

00256 {
00257    mOpenMode = O_WRONLY;
00258 }

void vpr::FileHandleImplUNIX::setOpenReadWrite (  ) 

Sets the open flags so that the I/O device is opened in read/write mode.

Precondition:
None.
Postcondition:
The open flags are updated so that when the device is opened, it is opened in read/write mode. If the device is already open, this has no effect.

Definition at line 260 of file FileHandleImplUNIX.cpp.

References mOpenMode.

Referenced by vpr::SerialPortImplTermios::setOpenReadWrite().

00261 {
00262    mOpenMode = O_RDWR;
00263 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::setAppend ( bool  flag  ) 

Reconfigures the file handle to be in append mode or not.

Precondition:
The file handle is open.
Postcondition:
The file handle's write mode is set to append or not.
Parameters:
flag A value of true indicates that this file handle will use append mode for writing. A value of false indicates that it will not.
Returns:
vpr::ReturnStatus::Succeed is returned if the write mode was changed successfully. vpr::ReturnStatus::Fail is returned if the write mode could not be changed for some reason.

Definition at line 266 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, getFlags(), mName, vpr::ReturnStatus::setCode(), setFlags(), vprDBG_CRITICAL_LVL, vprDBG_ERROR(), vprDEBUG, and vprDEBUG_FLUSH.

00267 {
00268    int cur_flags, new_flags, retval;
00269    vpr::ReturnStatus status;
00270 
00271    // Get the current flags.
00272    cur_flags = getFlags();
00273 
00274    // Set O_APPEND.
00275    if ( append )
00276    {
00277       new_flags = cur_flags | O_APPEND;
00278    }
00279    // Clear O_APPEND.
00280    else
00281    {
00282       new_flags = cur_flags & ~O_APPEND;
00283    }
00284 
00285    // Set the file descriptor to be blocking with the new flags.
00286    retval = setFlags(new_flags);
00287 
00288    if ( retval == -1 )
00289    {
00290       vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00291          << "[vpr::FileHandleImplUNIX::setAppend()] Failed to "
00292          << (append ? "enable" : "disable") << " append mode on "
00293          << mName << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00294       status.setCode(ReturnStatus::Fail);
00295    }
00296 
00297    return status;
00298 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::setSynchronousWrite ( bool  flag  ) 

Reconfigures the file handle so that writes are synchronous or asynchronous depending on the value of the parameter.

Precondition:
The file handle is open.
Postcondition:
Writes are performed synchronously.
Parameters:
flag A value of true indicates that synchronous writes will be used. A value of false indicates that asynchronous writes will be used.
Returns:
vpr::ReturnStatus::Succeed is returned if the write mode was changed successfully. vpr::ReturnStatus::Fail is returned if the write mode could not be changed for some reason.

Definition at line 301 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, getFlags(), mName, vpr::ReturnStatus::setCode(), setFlags(), vprDBG_ALL(), vprDBG_CRITICAL_LVL, vprDBG_ERROR(), vprDEBUG, and vprDEBUG_FLUSH.

00302 {
00303    vpr::ReturnStatus status;
00304 #if ! defined(_POSIX_SOURCE) && defined(O_SYNC) && defined(O_ASYNC)
00305    int cur_flags, new_flags, retval;
00306 
00307    // Get the current flags.
00308    cur_flags = getFlags();
00309 
00310    // Synchronous writes.
00311    if ( sync )
00312    {
00313       new_flags = cur_flags | O_SYNC;
00314    }
00315    // Asynchronous writes.
00316    else
00317    {
00318       new_flags = cur_flags | O_ASYNC;
00319    }
00320 
00321    // Set the file descriptor to be blocking with the new flags.
00322    retval = setFlags(new_flags);
00323 
00324    if ( retval == -1 )
00325    {
00326       vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00327          << "[vpr::FileHandleImplUNIX::setSynchronousWrite()] Failed to enable "
00328          << (sync ? "synchronous" : "asynchronous") << " writes on "
00329          << mName << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00330       status.setCode(vpr::ReturnStatus::Fail);
00331    }
00332 #else
00333    vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
00334       << "[vpr::FileHandleImplUNIX::setSynchronousWrite()] Cannot enable "
00335       << (sync ? "synchronous" : "asynchronous")
00336       << " writes on this platform!\n" << vprDEBUG_FLUSH;
00337    status.setCode(vpr::ReturnStatus::Fail);
00338 #endif
00339 
00340    return status;
00341 }

bool vpr::FileHandleImplUNIX::isReadOnly (  )  const

Tests if the I/O device is read-only.

Precondition:
The I/O device is open.
Postcondition:
The access mode is tested for read-only mode, and the result is returned to the caller.
Returns:
true is returned if the device is in read-only mode; false otherwise.

Definition at line 343 of file FileHandleImplUNIX.cpp.

References mOpenMode.

Referenced by vpr::SerialPortImplTermios::isReadOnly().

00344 {
00345    return (mOpenMode == O_RDONLY);
00346 }

bool vpr::FileHandleImplUNIX::isWriteOnly (  )  const

Tests if the I/O device is write-only.

Precondition:
The I/O device is open.
Postcondition:
The access mode is tested for write-only mode, and the result is returned to the caller.
Returns:
true is returned if the device is in write-only mode; false otherwise.

Definition at line 348 of file FileHandleImplUNIX.cpp.

References mOpenMode.

Referenced by vpr::SerialPortImplTermios::isWriteOnly().

00349 {
00350    return (mOpenMode == O_WRONLY);
00351 }

bool vpr::FileHandleImplUNIX::isReadWrite (  )  const

Tests if the I/O device is read/write.

Precondition:
The I/O device is open.
Postcondition:
The access mode is tested for read/write mode, and the result is returned to the caller.
Returns:
true is returned if the device is in read/write mode; false otherwise.

Definition at line 353 of file FileHandleImplUNIX.cpp.

References mOpenMode.

Referenced by vpr::SerialPortImplTermios::isReadWrite().

00354 {
00355    return (mOpenMode == O_RDWR);
00356 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::getReadBufferSize ( vpr::Int32 buffer  )  const

Queries the amount of data currently in the read buffer.

Precondition:
The file descriptor is valid.
Postcondition:
The buffer size is returned via the by-reference parameter.

Definition at line 358 of file FileHandleImplUNIX.cpp.

References vpr::ReturnStatus::Fail, mFdesc, and vpr::ReturnStatus::setCode().

Referenced by vpr::SocketImplBSD::isConnected().

00359 {
00360    vpr::ReturnStatus status;
00361 
00362    if ( ioctl(mFdesc, FIONREAD, &buffer) == -1 )
00363    {
00364       status.setCode(vpr::ReturnStatus::Fail);
00365    }
00366 
00367    return status;
00368 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::read_i ( void *  buffer,
const vpr::Uint32  length,
vpr::Uint32 bytesRead,
const vpr::Interval  timeout = vpr::Interval::NoTimeout 
)

Implementation of the read template method.

This reads at most the specified number of bytes from the file into the given buffer.

Precondition:
The device is open for reading, and the buffer is at least length bytes long.
Postcondition:
The given buffer has length bytes copied into it from the device, and the number of bytes read successfully is returned to the caller via the bytesRead parameter.
Parameters:
buffer A pointer to the buffer where the device's buffer contents are to be stored.
length The number of bytes to be read.
bytesRead The number of bytes read into the buffer.
timeout The maximum amount of time to wait for data to be available for reading. This argument is optional and defaults to vpr::Interval::NoTimeout.
Returns:
vpr::ReturnStatus::Succeed is returned if the read operation completed successfully.

vpr::ReturnStatus::WouldBlock if the file is in non-blocking mode, and there is no data to read.

vpr::ReturnStatus::Timeout is returned if the read could not begin within the timeout interval.

vpr::ReturnStatus::Fail is returned if the read operation failed.

Definition at line 376 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, isReadable(), mBlocking, mFdesc, mName, vpr::ReturnStatus::setCode(), vpr::ReturnStatus::success(), vprDBG_CRITICAL_LVL, vprDBG_ERROR(), vprDBG_WARNING_LVL, vprDEBUG, vprDEBUG_FLUSH, and vpr::ReturnStatus::WouldBlock.

Referenced by vpr::SocketImplBSD::read_i(), and vpr::SerialPortImplTermios::read_i().

00380 {
00381    vpr::ReturnStatus status;
00382 
00383    status = isReadable(timeout);
00384 
00385    if ( status.success() )
00386    {
00387       ssize_t bytes;
00388 
00389       bytes = ::read(mFdesc, buffer, length);
00390 
00391       // Something went wrong while attempting to read from the file.
00392       if ( bytes < 0 )
00393       {
00394          bytesRead = 0;
00395 
00396          if ( errno == EAGAIN && ! mBlocking )
00397          {
00398             status.setCode(vpr::ReturnStatus::WouldBlock);
00399          }
00400          // If the error is EAGAIN and we are in non-blocking mode, we do not
00401          // bother to print the message.
00402          if ( ! (errno == EAGAIN && ! mBlocking) )
00403          {
00404             vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00405                << "[vpr::FileHandleImplUNIX::read_i()] Error reading from "
00406                << mName << ": " << strerror(errno) << std::endl
00407                << vprDEBUG_FLUSH;
00408             status.setCode(ReturnStatus::Fail);
00409          }
00410       }
00411       // If 0 bytes were read or an error was returned, we print an error
00412       // message.
00413       else if ( bytes == 0 && errno != 0 )
00414       {
00415          // XXX: Failure status may not be exactly what we want to return.
00416          status.setCode(ReturnStatus::Fail);
00417          bytesRead = 0;
00418 //     errno != ENOENT
00419          vprDEBUG(vprDBG_ERROR, vprDBG_WARNING_LVL)
00420             << "[vpr::FileHandleImplUNIX::read_i()] Nothing read from "
00421             << mName << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00422       }
00423       else
00424       {
00425          bytesRead = bytes;
00426       }
00427    }
00428    else
00429    {
00430       bytesRead = 0;
00431    }
00432 
00433    return status;
00434 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::readn_i ( void *  buffer,
const vpr::Uint32  length,
vpr::Uint32 bytesRead,
const vpr::Interval  timeout = vpr::Interval::NoTimeout 
)

Implementation of the readn template method.

This reads exactly the specified number of bytes from the file into the given buffer.

Precondition:
The device is open for reading, and the buffer is at least length bytes long.
Postcondition:
The given buffer has length bytes copied into it from the device, and the number of bytes read successfully is returned to the caller via the bytesRead parameter.
Parameters:
buffer A pointer to the buffer where the device's buffer contents are to be stored.
length The number of bytes to be read.
bytesRead The number of bytes read into the buffer.
timeout The maximum amount of time to wait for data to be available for reading. This argument is optional and defaults to vpr::Interval::NoTimeout.
Returns:
vpr::ReturnStatus::Succeed is returned if the read operation completed successfully.

vpr::ReturnStatus::WouldBlock if the file is in non-blocking mode, and there is no data to read.

vpr::ReturnStatus::Timeout is returned if the read could not begin within the timeout interval.

Definition at line 439 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, mFdesc, vpr::Interval::NoTimeout, vpr::ReturnStatus::setCode(), vprDBG_ALL(), vprDBG_HVERB_LVL, vprDBG_WARNING_LVL, vprDEBUG, vprDEBUG_FLUSH, and vprDEBUG_NEXT.

Referenced by vpr::SocketImplBSD::readn_i(), and vpr::SerialPortImplTermios::readn_i().

00443 {
00444    size_t bytes_left;
00445    ssize_t bytes;
00446    vpr::ReturnStatus status;
00447 
00448    if ( vpr::Interval::NoTimeout != timeout )
00449    {
00450       vprDEBUG(vprDBG_ALL,vprDBG_WARNING_LVL) << "Timeout not supported\n"
00451                                               << vprDEBUG_FLUSH;
00452    }
00453 
00454    bytesRead = 0;
00455    bytes_left = buffer_size;
00456 
00457    while ( bytes_left > 0 )
00458    {
00459       vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL)
00460          << "[vpr::FileHandleImplUNIX::readn_i()] Reading " << bytes_left
00461          << " bytes from file handle " << mFdesc << std::endl
00462          << vprDEBUG_FLUSH;
00463 
00464       bytes = ::read(mFdesc, buffer, bytes_left);
00465 
00466       vprDEBUG_NEXT(vprDBG_ALL, vprDBG_HVERB_LVL)
00467          << "Read " << bytes << " bytes from file handle " << mFdesc
00468          << std::endl << vprDEBUG_FLUSH;
00469 
00470       // Read error.
00471       if ( bytes < 0 )
00472       {
00473          // Restart the read process if we were interrupted by the OS.
00474          if ( errno == EINTR )
00475          {
00476             continue;
00477          }
00478          // Otherwise, we have an error situation, so return failure status.
00479          else
00480          {
00481             status.setCode(ReturnStatus::Fail);
00482             bytesRead = 0;
00483             return status;
00484          }
00485       }
00486       // We have read EOF, so there is nothing more to read.  At this point,
00487       // bytesRead contains an accurate count of the bytes read so far
00488       // (posisbly less than buffer_size).
00489       else if ( bytes == 0 )
00490       {
00491          vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL)
00492             << "[vpr::FileHandleImplUNIX::readn_i()] Read EOF with "
00493             << bytes_left << " bytes left to read from file handle "
00494             << mFdesc << " and " << bytesRead << " bytes read in total."
00495             << std::endl << vprDEBUG_FLUSH;
00496 
00497          return status;
00498       }
00499       else
00500       {
00501          buffer = (void*) ((char*) buffer + bytes);
00502          bytes_left -= bytes;
00503          bytesRead  += bytes;
00504       }
00505    }
00506 
00507    return status;
00508 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::write_i ( const void *  buffer,
const vpr::Uint32  length,
vpr::Uint32 bytesWritten,
const vpr::Interval  timeout = vpr::Interval::NoTimeout 
)

Implementation of the write template method.

This writes the buffer to the file.

Precondition:
The device is open for writing.
Postcondition:
The given buffer is written to the I/O device, and the number of bytes written successfully is returned to the caller via the bytesWritten parameter.
Parameters:
buffer A pointer to the buffer to be written.
length The length of the buffer.
bytesWritten The number of bytes written to the device.
timeout The maximum amount of time to wait for data to be available for writing. This argument is optional and defaults to vpr::Interval::NoTimeout.
Returns:
vpr::ReturnStatus::Succeed is returned if the write operation completed successfully.

vpr::ReturnStatus::WouldBLock is returned if the handle is in non-blocking mode, and the write operation could not be completed.

vpr::ReturnStatus::Timeout is returned if the write could not begin within the timeout interval.

vpr::ReturnStatus::Fail is returned if the write operation failed.

Definition at line 511 of file FileHandleImplUNIX.cpp.

References errno, vpr::ReturnStatus::Fail, isWriteable(), mBlocking, mFdesc, mName, vpr::ReturnStatus::setCode(), vpr::ReturnStatus::success(), vprDBG_CRITICAL_LVL, vprDBG_ERROR(), vprDEBUG, vprDEBUG_FLUSH, and vpr::ReturnStatus::WouldBlock.

Referenced by vpr::SocketImplBSD::write_i(), and vpr::SerialPortImplTermios::write_i().

00515 {
00516    vpr::ReturnStatus status;
00517 
00518    status = isWriteable(timeout);
00519 
00520    if ( status.success() )
00521    {
00522       ssize_t bytes;
00523 
00524       bytes = ::write(mFdesc, buffer, length);
00525 
00526       if ( bytes <= 0 )
00527       {
00528          bytesWritten = 0;
00529 
00530          if ( errno == EAGAIN && ! mBlocking )
00531          {
00532             status.setCode(vpr::ReturnStatus::WouldBlock);
00533          }
00534          else
00535          {
00536             vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00537                << "[vpr::FileHandleImplUNIX::write_i()] Error writing to "
00538                << mName << ": " << strerror(errno) << std::endl
00539                << vprDEBUG_FLUSH;
00540             status.setCode(ReturnStatus::Fail);
00541          }
00542       }
00543       else
00544       {
00545          bytesWritten = bytes;
00546       }
00547    }
00548 
00549    return status;
00550 }

vpr::Uint32 vpr::FileHandleImplUNIX::availableBytes (  )  const

Returns the number of bytes available for reading in the receive buffer.

Returns:
A value greater than 0 is returned if there are bytes to be read. If there is nothing to read or an error occurred, 0 is returned.

Definition at line 552 of file FileHandleImplUNIX.cpp.

References mFdesc.

Referenced by vpr::SocketImplBSD::availableBytes().

00553 {
00554    int result;
00555 
00556    if ( ioctl(mFdesc, FIONREAD, &result) < 0 )
00557    {
00558       result = 0;
00559    }
00560 
00561    return result;
00562 }

int vpr::FileHandleImplUNIX::getFlags (  )  const [protected]

Gets the current file handle flags.

Precondition:
The file handle is open.
Postcondition:
The current flags for the handle are returned to the caller.
Returns:
A value larger than -1 is returned giving the current flags for the file handle.

-1 is returned if the current flags could not be requested.

Definition at line 565 of file FileHandleImplUNIX.cpp.

References mFdesc.

Referenced by setAppend(), setBlocking(), and setSynchronousWrite().

00566 {
00567    return fcntl(mFdesc, F_GETFL, 0);
00568 }

int vpr::FileHandleImplUNIX::setFlags ( const int  flags  )  [protected]

Overwrites the current file handle flags with the given value.

Precondition:
The file handle is open.
Postcondition:
The flags for the file handle are overwritten with the new value.
Returns:
0 is returned if the flags were updated successfully.

-1 is returned if the current flags could not be overwritten.

Definition at line 571 of file FileHandleImplUNIX.cpp.

References mFdesc.

Referenced by setAppend(), setBlocking(), and setSynchronousWrite().

00572 {
00573    return fcntl(mFdesc, F_SETFL, flags);
00574 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::isReadable ( const vpr::Interval  timeout  )  const [protected]

Tests if the file handle is ready for reading within the timeout period.

Definition at line 576 of file FileHandleImplUNIX.cpp.

References vpr::ReturnStatus::Fail, mFdesc, vpr::Interval::msec(), vpr::Interval::NoTimeout, vpr::Interval::NoWait, vpr::ReturnStatus::setCode(), and vpr::ReturnStatus::Timeout.

Referenced by vpr::SocketStreamImplBSD::accept(), vpr::SocketImplBSD::isConnected(), read_i(), and vpr::SocketDatagramImplBSD::recvfrom().

00577 {
00578    vpr::ReturnStatus ready;
00579    fd_set read_set;
00580    int num_events;
00581    struct timeval timeout_obj;
00582 
00583    if ( mFdesc == -1 )
00584    {
00585       ready.setCode(vpr::ReturnStatus::Fail);
00586    }
00587    else
00588    {
00589       if ( timeout == vpr::Interval::NoWait )
00590       {
00591          timeout_obj.tv_sec  = 0;
00592          timeout_obj.tv_usec = 0;
00593       }
00594       else
00595       {
00596          if ( timeout.msec() >= 1000 )
00597          {
00598             timeout_obj.tv_sec  = timeout.msec() / 1000;
00599             timeout_obj.tv_usec = (timeout.msec() % 1000) * 1000000;
00600          }
00601          else
00602          {
00603             timeout_obj.tv_sec  = 0;
00604             timeout_obj.tv_usec = timeout.msec() * 1000;
00605          }
00606       }
00607 
00608       FD_ZERO(&read_set);
00609       FD_SET(mFdesc, &read_set);
00610 
00611       num_events = select(mFdesc + 1, &read_set, NULL, NULL,
00612                           (timeout != vpr::Interval::NoTimeout) ? &timeout_obj :
00613                                                                   NULL);
00614 
00615       if ( num_events == 0 )
00616       {
00617          if ( ! FD_ISSET(mFdesc, &read_set) )
00618          {
00619             ready.setCode(vpr::ReturnStatus::Timeout);
00620          }
00621       }
00622       else if ( num_events < 0 )
00623       {
00624          ready.setCode(vpr::ReturnStatus::Fail);
00625       }
00626    }
00627 
00628    return ready;
00629 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::isWriteable ( const vpr::Interval  timeout  )  const [protected]

Tests if the file handle is ready for writing within the timeout period.

Definition at line 631 of file FileHandleImplUNIX.cpp.

References vpr::ReturnStatus::Fail, mFdesc, vpr::Interval::msec(), vpr::Interval::NoTimeout, vpr::Interval::NoWait, vpr::ReturnStatus::setCode(), and vpr::ReturnStatus::Timeout.

Referenced by vpr::SocketImplBSD::connect(), vpr::SocketDatagramImplBSD::sendto(), and write_i().

00632 {
00633    vpr::ReturnStatus ready;
00634    fd_set write_set;
00635    int num_events;
00636    struct timeval timeout_obj;
00637 
00638    if ( mFdesc == -1 )
00639    {
00640       ready.setCode(vpr::ReturnStatus::Fail);
00641    }
00642    else
00643    {
00644       if ( timeout == vpr::Interval::NoWait )
00645       {
00646          timeout_obj.tv_sec  = 0;
0