Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List
  • File Members

FileHandle.cc

Go to the documentation of this file.
00001 // $Id: FileHandle.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2006
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00027 #include "FileHandle.hh"
00028 //#include "FileHandle.ih"
00029 
00030 // Custom includes
00031 #include <unistd.h>
00032 #include <sys/poll.h>
00033 #include <string.h>
00034 #include <errno.h>
00035 #include <fcntl.h>
00036 #include <senf/Utils/Exception.hh>
00037 
00038 #define prefix_
00039 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00040 
00041 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00042 // senf::FileBody
00043 
00044 prefix_ void senf::FileBody::close()
00045 {
00046     if (!valid())
00047         throw SystemException(EBADF SENF_EXC_DEBUGINFO);
00048     v_close();
00049     fd_ = -1;
00050 }
00051 
00052 prefix_ void senf::FileBody::terminate()
00053 {
00054     if (valid()) {
00055         v_terminate();
00056         fd_ = -1;
00057     }
00058 }
00059 
00060 prefix_ void senf::FileBody::destroyClose()
00061 {
00062     if (valid())
00063         try {
00064             close();
00065         }
00066         catch (...) {
00067             terminate();
00068         }
00069 }
00070 
00071 prefix_ void senf::FileBody::v_close()
00072 {
00073     if (::close(fd_) != 0)
00074         SENF_THROW_SYSTEM_EXCEPTION("");
00075 }
00076 
00077 prefix_ void senf::FileBody::v_terminate()
00078 {
00079     ::close(fd_);
00080 }
00081 
00082 prefix_ bool senf::FileBody::v_eof()
00083     const
00084 {
00085     return false;
00086 }
00087 
00088 prefix_ bool senf::FileBody::v_valid()
00089     const
00090 {
00091     return true;
00092 }
00093 
00094 prefix_ bool senf::FileBody::blocking()
00095     const
00096 {
00097     int flags = ::fcntl(fd(),F_GETFL);
00098     if (flags < 0) SENF_THROW_SYSTEM_EXCEPTION("");
00099     return ! (flags & O_NONBLOCK);
00100 }
00101 
00102 prefix_ void senf::FileBody::blocking(bool status)
00103 {
00104     int flags = ::fcntl(fd(),F_GETFL);
00105     if (flags < 0) SENF_THROW_SYSTEM_EXCEPTION("");
00106     if (status) flags &= ~O_NONBLOCK;
00107     else        flags |= O_NONBLOCK;
00108     if (::fcntl(fd(), F_SETFL, flags) < 0) SENF_THROW_SYSTEM_EXCEPTION("");
00109 }
00110 
00111 /* We don't take POLLIN/POLLOUT as argument to avoid having to include
00112    sys/poll.h in the .cci file (and therefore indirectly into the .hh
00113    and then every file which uses FileHandle) */
00114 prefix_ bool senf::FileBody::pollCheck(int fd, bool incoming, int timeout, bool oob)
00115     const
00116 {
00117     struct ::pollfd pfd;
00118     ::memset(&pfd,0,sizeof(pfd));
00119     pfd.fd = fd;
00120     pfd.events = incoming?(oob?POLLPRI:POLLIN):POLLOUT;
00121     int rv = -1;
00122     do {
00123         rv = ::poll(&pfd,1,timeout);
00124         if (rv<0)
00125             switch (errno) {
00126             case EINTR:
00127                 break;
00128             default:
00129                 SENF_THROW_SYSTEM_EXCEPTION("");
00130             }
00131     } while (rv<0);
00132     return rv>0;
00133 }
00134 
00135 prefix_ senf::FileBody::~FileBody()
00136 {}
00137 
00138 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00139 #undef prefix_
00140 
00141 
00142 // Local Variables:
00143 // mode: c++
00144 // fill-column: 100
00145 // c-file-style: "senf"
00146 // indent-tabs-mode: nil
00147 // ispell-local-dictionary: "american"
00148 // compile-command: "scons -u test"
00149 // comment-column: 40
00150 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research