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

ReadWritePolicy.cc

Go to the documentation of this file.
00001 // $Id: ReadWritePolicy.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 "ReadWritePolicy.hh"
00028 //#include "ReadWritePolicy.ih"
00029 
00030 // Custom includes
00031 #include <sys/types.h>
00032 #include <sys/socket.h>
00033 #include <unistd.h>
00034 #include <errno.h>
00035 
00036 
00037 //#include "ReadWritePolicy.mpp"
00038 #define prefix_
00039 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00040 
00041 prefix_ unsigned senf::ReadablePolicy::read(FileHandle & handle, char * buffer,
00042                                                    unsigned size)
00043 {
00044     int rv = -1;
00045     do {
00046         rv = ::read(handle.fd(), buffer, size);
00047         if (rv < 0)
00048             switch(errno) {
00049             case EINTR:
00050                 break;
00051             case EAGAIN:
00052                 // This means, the socket is non-blocking an no data was available
00053                 rv = 0;
00054                 break;
00055             default:
00056                 SENF_THROW_SYSTEM_EXCEPTION("");
00057             }
00058     } while (rv<0);
00059     return rv;
00060 }
00061 
00062 prefix_ unsigned senf::ReadablePolicy::do_readfrom(FileHandle & handle, char * buffer,
00063                                                           unsigned size,
00064                                                           struct ::sockaddr * addr, socklen_t * len)
00065 {
00066     int rv = -1;
00067     do {
00068         rv = ::recvfrom(handle.fd(),buffer, size, 0, addr, len);
00069         if (rv < 0)
00070             switch (errno) {
00071             case EINTR:
00072                 break;
00073             case EAGAIN:
00074                 rv = 0;
00075                 break;
00076             default:
00077                 SENF_THROW_SYSTEM_EXCEPTION("");
00078             }
00079     } while (rv<0);
00080     return rv;
00081 }
00082 
00083 prefix_ unsigned senf::WriteablePolicy::do_write(FileHandle & handle, char const * buffer,
00084                                                         unsigned size)
00085 {
00086     int rv = -1;
00087     do {
00088         rv = ::write(handle.fd(), buffer, size);
00089         if (rv < 0)
00090             switch (errno) {
00091             case EINTR:
00092                 break;
00093             case EAGAIN:
00094             case ENOBUFS:
00095                 // According to the man page this should not happen, since packets are just silently being dropped.
00096                 // It does happen in NetEmu using small TxQueues on WLAN interfaces 
00097             case ECONNREFUSED:
00098                 // Writing to a UDP socket seems return this error code if a corresponding ICMP
00099                 // error code has been received before (at least on linux). This is inconsistent
00100                 // since I cannot rely on getting ECONNREFUSED. I therefore ignore this error. TCP
00101                 // sockets will return this error on connect() and not on write(). Therefore we can
00102                 // unconditionally ignore this error here.
00103                 rv = 0;
00104                 break;
00105             default:
00106                 SENF_THROW_SYSTEM_EXCEPTION("");
00107             }
00108     } while (rv<0);
00109     return rv;
00110 }
00111 
00112 prefix_ unsigned senf::WriteablePolicy::do_writeto(FileHandle & handle,
00113                                                           char const * buffer, unsigned size,
00114                                                           struct sockaddr const * addr, socklen_t len)
00115 {
00116     int rv = -1;
00117     do {
00118         rv = ::sendto(handle.fd(), buffer, size, 0, addr, len);
00119         if (rv < 0)
00120             switch (errno) {
00121             case EINTR:
00122                 break;
00123             case EAGAIN:
00124             case ENOBUFS:
00125                 // According to the man page this should not happen, since packets are just silently being dropped.
00126                 // It does happen in NetEmu using small TxQueues on WLAN interfaces 
00127                 rv = 0;
00128                 break;
00129             default:
00130                 SENF_THROW_SYSTEM_EXCEPTION("");
00131             }
00132     } while (rv<0);
00133     return rv;
00134 }
00135 
00136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00137 #undef prefix_
00138 //#include "ReadWritePolicy.mpp"
00139 
00140 
00141 // Local Variables:
00142 // mode: c++
00143 // fill-column: 100
00144 // c-file-style: "senf"
00145 // indent-tabs-mode: nil
00146 // ispell-local-dictionary: "american"
00147 // compile-command: "scons -u test"
00148 // comment-column: 40
00149 // End:

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