Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

vpr::FileHandleImplUNIX Class Reference

Wrapper around UNIX file descriptors. More...

#include <FileHandleImplUNIX.h>

List of all members.

Public Methods

 FileHandleImplUNIX ()
 Constructor for unnamed file-based devices. More...

 FileHandleImplUNIX (const std::string &file_name)
 Constructor. More...

 ~FileHandleImplUNIX ()
 Destructor. More...

const std::string & getName () const
 Gets the name of this file. More...

vpr::ReturnStatus open ()
 Opens the file handle. More...

vpr::ReturnStatus close ()
 Closes the file handle. More...

bool isOpen () const
 Gets the open state of this file handle. More...

vpr::ReturnStatus setBlocking (bool blocking)
 Reconfigures the file handle so that it is in blocking or non-blocking mode. More...

vpr::IOSys::Handle getHandle () const
 Returns the contained handle. More...

bool isBlocking () const
 Gets the current blocking state for the file handle. More...

void setOpenReadOnly ()
 Sets the open flags so that the I/O device is opened in read-only mode. More...

void setOpenWriteOnly ()
 Sets the open flags so that the I/O device is opened in write-only mode. More...

void setOpenReadWrite ()
 Sets the open flags so that the I/O device is opened in read/write mode. More...

vpr::ReturnStatus setAppend (bool flag)
 Reconfigures the file handle to be in append mode or not. More...

vpr::ReturnStatus setSynchronousWrite (bool flag)
 Reconfigures the file handle so that writes are synchronous or asynchronous depending on the value of the parameter. More...

bool isReadOnly () const
 Tests if the I/O device is read-only. More...

bool isWriteOnly () const
 Tests if the I/O device is write-only. More...

bool isReadWrite () const
 Tests if the I/O device is read/write. More...

vpr::ReturnStatus getReadBufferSize (vpr::Int32 &buffer) const
 Queries the amount of data currently in the read buffer. More...

vpr::ReturnStatus read_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytes_read, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the read template method. More...

vpr::ReturnStatus readn_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytes_read, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the readn template method. More...

vpr::ReturnStatus write_i (const void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytes_written, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the write template method. More...

vpr::Uint32 availableBytes () const
 Returns the number of bytes available for reading in the receive buffer. More...


Protected Methods

int getFlags () const
 Gets the current file handle flags. More...

int setFlags (const int flags)
 Overwrites the current file handle flags with the given value. More...

vpr::ReturnStatus isReadable (const vpr::Interval timeout) const
 Tests if the file handle is ready for reading within the timeout period. More...

vpr::ReturnStatus isWriteable (const vpr::Interval timeout) const
 Tests if the file handle is ready for writing within the timeout period. More...


Protected Attributes

std::string mName
 The name of this file. More...

bool mOpen
 Open state of this file. More...

bool mOpenBlocking
bool mBlocking
 Blocking state of this file. More...

int mFdesc
 File descriptor. More...

int mOpenMode
 The open mode of the device. More...


Friends

class SerialPortImplTermios
class SocketDatagramImplBSD
class SocketImplBSD
class SocketStreamImplBSD


Detailed Description

Wrapper around UNIX file descriptors.

Definition at line 61 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.

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 102 of file FileHandleImplUNIX.h.

References mName.

Referenced by vpr::SocketImplBSD::getName, and vpr::SocketImplBSD::getOption.

00103    {
00104       return mName;
00105    }

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, mBlocking, mFdesc, mName, mOpen, mOpenBlocking, mOpenMode, vpr::ReturnStatus::setCode, vprDBG_CRITICAL_LVL, vprDBG_ERROR, vprDEBUG, vprDEBUG_FLUSH, and vpr::ReturnStatus::WouldBlock.

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] 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, mFdesc, mName, mOpen, vpr::ReturnStatus::setCode, vprDBG_ALL, vprDBG_VERB_LVL, vprDBG_WARNING_LVL, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by vpr::SocketImplBSD::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] 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 145 of file FileHandleImplUNIX.h.

References mOpen.

Referenced by vpr::SocketImplBSD::isOpen.

00146    {
00147       return mOpen;
00148    }

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, getFlags, mBlocking, mName, mOpen, mOpenBlocking, vpr::ReturnStatus::setCode, setFlags, vprDBG_CRITICAL_LVL, vprDBG_ERROR, vprDEBUG, and vprDEBUG_FLUSH.

Referenced by vpr::SocketImplBSD::open, and vpr::SocketImplBSD::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] 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, vprDBG_ALL, vprDBG_CRITICAL_LVL, and vprDEBUG.

Referenced by vpr::SocketImplBSD::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 179 of file FileHandleImplUNIX.h.

References mBlocking.

Referenced by vpr::SocketImplBSD::isBlocking.

00180    {
00181       return mBlocking;
00182    }

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.

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.

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.

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, 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] 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] 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] 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.

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.

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.

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   bytes_read,
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 bytes_read 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.
bytes_read  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, 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.

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          bytes_read = 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] Error reading from " << mName
00406                << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00407             status.setCode(ReturnStatus::Fail);
00408          }
00409       }
00410       // If 0 bytes were read or an error was returned, we print an error
00411       // message.
00412       else if ( bytes == 0 && errno != 0 )
00413       {
00414          // XXX: Failure status may not be exactly what we want to return.
00415          status.setCode(ReturnStatus::Fail);
00416          bytes_read = 0;
00417 //     errno != ENOENT
00418          vprDEBUG(vprDBG_ERROR, vprDBG_WARNING_LVL)
00419             << "[vpr::FileHandleImplUNIX] Nothing read from " << mName
00420             << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00421       }
00422       else
00423       {
00424          bytes_read = bytes;
00425       }
00426    }
00427    else
00428    {
00429       bytes_read = 0;
00430    }
00431 
00432    return status;
00433 }

vpr::ReturnStatus vpr::FileHandleImplUNIX::readn_i void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   bytes_read,
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 bytes_read 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.
bytes_read  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 438 of file FileHandleImplUNIX.cpp.

References errno, 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.

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

vpr::ReturnStatus vpr::FileHandleImplUNIX::write_i const void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   bytes_written,
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 bytes_written parameter.
Parameters:
buffer  A pointer to the buffer to be written.
length  The length of the buffer.
bytes_written  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 503 of file FileHandleImplUNIX.cpp.

References errno, 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.

00507 {
00508    vpr::ReturnStatus status;
00509 
00510    status = isWriteable(timeout);
00511 
00512    if ( status.success() )
00513    {
00514       ssize_t bytes;
00515 
00516       bytes = ::write(mFdesc, buffer, length);
00517 
00518       if ( bytes <= 0 )
00519       {
00520          bytes_written = 0;
00521 
00522          if ( errno == EAGAIN && ! mBlocking )
00523          {
00524             status.setCode(vpr::ReturnStatus::WouldBlock);
00525          }
00526          else
00527          {
00528             vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00529                << "[vpr::FileHandleImplUNIX] Error writing to " << mName
00530                << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00531             status.setCode(ReturnStatus::Fail);
00532          }
00533       }
00534       else
00535       {
00536          bytes_written = bytes;
00537       }
00538    }
00539 
00540    return status;
00541 }

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 543 of file FileHandleImplUNIX.cpp.

References mFdesc.

Referenced by vpr::SocketImplBSD::availableBytes.

00544 {
00545    int result;
00546 
00547    if ( ioctl(mFdesc, FIONREAD, &result) < 0 )
00548    {
00549       result = 0;
00550    }
00551 
00552    return result;
00553 }

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 556 of file FileHandleImplUNIX.cpp.

References mFdesc.

Referenced by setAppend, setBlocking, and setSynchronousWrite.

00557 {
00558    return fcntl(mFdesc, F_GETFL, 0);
00559 }

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 562 of file FileHandleImplUNIX.cpp.

References mFdesc.

Referenced by setAppend, setBlocking, and setSynchronousWrite.

00563 {
00564    return fcntl(mFdesc, F_SETFL, flags);
00565 }

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 567 of file FileHandleImplUNIX.cpp.

References vpr::ReturnStatus::Fail, mFdesc, vpr::Interval::msec, vpr::Interval::NoTimeout, vpr::Interval::NoWait, vpr::ReturnStatus::setCode, vpr::ReturnStatus::Timeout, timeval::tv_sec, and timeval::tv_usec.

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

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

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 622 of file FileHandleImplUNIX.cpp.

References vpr::ReturnStatus::Fail, mFdesc, vpr::Interval::msec, vpr::Interval::NoTimeout, vpr::Interval::NoWait, vpr::ReturnStatus::setCode, vpr::ReturnStatus::Timeout, timeval::tv_sec, and timeval::tv_usec.

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

00623 {
00624    vpr::ReturnStatus ready;
00625    fd_set write_set;
00626    int num_events;
00627    struct timeval timeout_obj;
00628 
00629    if ( mFdesc == -1 )
00630    {
00631       ready.setCode(vpr::ReturnStatus::Fail);
00632    }
00633    else
00634    {
00635       if ( timeout == vpr::Interval::NoWait )
00636       {
00637          timeout_obj.tv_sec  = 0;
00638          timeout_obj.tv_usec = 0;
00639       }
00640       else
00641       {
00642          if ( timeout.msec() >= 1000 )
00643          {
00644             timeout_obj.tv_sec  = timeout.msec() / 1000;
00645             timeout_obj.tv_usec = (timeout.msec() % 1000) * 1000000;
00646          }
00647          else
00648          {
00649             timeout_obj.tv_sec  = 0;
00650             timeout_obj.tv_usec = timeout.msec() * 1000;
00651          }
00652       }
00653 
00654       FD_ZERO(&write_set);
00655       FD_SET(mFdesc, &write_set);
00656 
00657       num_events = select(mFdesc + 1, NULL, &write_set, NULL,
00658                           (timeout != vpr::Interval::NoTimeout) ? &timeout_obj :
00659                                                                   NULL);
00660 
00661       if ( num_events == 0 )
00662       {
00663          if ( ! FD_ISSET(mFdesc, &write_set) )
00664          {
00665             ready.setCode(vpr::ReturnStatus::Timeout);
00666          }
00667       }
00668       else if ( num_events < 0 )
00669       {
00670          ready.setCode(vpr::ReturnStatus::Fail);
00671       }
00672    }
00673 
00674    return ready;
00675 }


Friends And Related Function Documentation

friend class SerialPortImplTermios [friend]
 

Definition at line 396 of file FileHandleImplUNIX.h.

friend class SocketDatagramImplBSD [friend]
 

Definition at line 397 of file FileHandleImplUNIX.h.

friend class SocketImplBSD [friend]
 

Definition at line 398 of file FileHandleImplUNIX.h.

friend class SocketStreamImplBSD [friend]
 

Definition at line 399 of file FileHandleImplUNIX.h.


Member Data Documentation

std::string vpr::FileHandleImplUNIX::mName [protected]
 

The name of this file.

Definition at line 437 of file FileHandleImplUNIX.h.

Referenced by close, getName, open, read_i, setAppend, setBlocking, setSynchronousWrite, and write_i.

bool vpr::FileHandleImplUNIX::mOpen [protected]
 

Open state of this file.

Definition at line 438 of file FileHandleImplUNIX.h.

Referenced by close, isOpen, vpr::SocketImplBSD::open, open, setBlocking, and vpr::SocketDatagramImplBSD::SocketDatagramImplBSD.

bool vpr::FileHandleImplUNIX::mOpenBlocking [protected]
 

Definition at line 439 of file FileHandleImplUNIX.h.

Referenced by open, and setBlocking.

bool vpr::FileHandleImplUNIX::mBlocking [protected]
 

Blocking state of this file.

Definition at line 440 of file FileHandleImplUNIX.h.

Referenced by isBlocking, open, read_i, setBlocking, vpr::SocketDatagramImplBSD::SocketDatagramImplBSD, and write_i.

int vpr::FileHandleImplUNIX::mFdesc [protected]
 

File descriptor.

Definition at line 442 of file FileHandleImplUNIX.h.

Referenced by vpr::SocketStreamImplBSD::accept, availableBytes, vpr::SocketImplBSD::bind, close, vpr::SocketImplBSD::connect, getFlags, getHandle, vpr::SocketImplBSD::getOption, getReadBufferSize, isReadable, isWriteable, vpr::SocketStreamImplBSD::listen, vpr::SocketImplBSD::open, open, read_i, readn_i, vpr::SocketDatagramImplBSD::recvfrom, vpr::SocketDatagramImplBSD::sendto, setFlags, vpr::SocketImplBSD::setOption, vpr::SocketDatagramImplBSD::SocketDatagramImplBSD, vpr::SocketStreamImplBSD::SocketStreamImplBSD, and write_i.

int vpr::FileHandleImplUNIX::mOpenMode [protected]
 

The open mode of the device.

Definition at line 443 of file FileHandleImplUNIX.h.

Referenced by isReadOnly, isReadWrite, isWriteOnly, open, setOpenReadOnly, setOpenReadWrite, and setOpenWriteOnly.


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:47:02 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002