7aa9e24fa986ff000442adf471e645c4669e7504
[senf.git] / Socket / FileHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief FileHandle non-inline non-template implementation
25  */
26
27 #include "FileHandle.hh"
28 //#include "FileHandle.ih"
29
30 // Custom includes
31 #include <unistd.h>
32 #include <sys/poll.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include "../Utils/Exception.hh"
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 prefix_ void senf::FileBody::v_close()
42 {
43     if (::close(fd_) != 0)
44         throwErrno();
45 }
46
47 prefix_ void senf::FileBody::v_terminate()
48 {
49     ::close(fd_);
50 }
51
52 prefix_ bool senf::FileBody::v_eof()
53     const
54 {
55     return false;
56 }
57
58 prefix_ bool senf::FileBody::v_valid()
59     const
60 {
61     return true;
62 }
63
64 prefix_ bool senf::FileBody::blocking()
65     const
66 {
67     int flags = ::fcntl(fd(),F_GETFL);
68     if (flags < 0) throwErrno();
69     return ! (flags & O_NONBLOCK);
70 }
71
72 prefix_ void senf::FileBody::blocking(bool status)
73 {
74     int flags = ::fcntl(fd(),F_GETFL);
75     if (flags < 0) throwErrno();
76     if (status) flags &= ~O_NONBLOCK;
77     else        flags |= O_NONBLOCK;
78     if (::fcntl(fd(), F_SETFL, flags) < 0) throwErrno();
79 }
80
81 /* We don't take POLLIN/POLLOUT as argument to avoid having to include
82    sys/poll.h in the .cci file (and therefore indirectly into the .hh
83    and then every file which uses FileHandle) */
84 prefix_ bool senf::FileBody::pollCheck(int fd, bool incoming, bool block)
85     const
86 {
87     struct ::pollfd pfd;
88     ::memset(&pfd,0,sizeof(pfd));
89     pfd.fd = fd;
90     pfd.events = incoming?POLLIN:POLLOUT;
91     int rv = -1;
92     do {
93         rv = ::poll(&pfd,1,block?-1:0);
94         if (rv<0)
95             switch (errno) {
96             case EINTR:
97                 break;
98             default:
99                 throwErrno();
100             }
101     } while (rv<0);
102     return rv>0;
103 }
104
105 ///////////////////////////////cc.e////////////////////////////////////////
106 #undef prefix_
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // c-file-style: "senf"
113 // indent-tabs-mode: nil
114 // ispell-local-dictionary: "american"
115 // compile-command: "scons -u test"
116 // comment-column: 40
117 // End: