952d09559e2c46453c73c88a0c2318bea7ac7a49
[senf.git] / senf / Socket / FileHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 <senf/Utils/Exception.hh>
37
38 #define prefix_
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 // senf::FileBody
43
44 prefix_ void senf::FileBody::close()
45 {
46     if (!valid())
47         throw SystemException(EBADF SENF_EXC_DEBUGINFO);
48     v_close();
49     fd_ = -1;
50 }
51
52 prefix_ void senf::FileBody::terminate()
53 {
54     if (valid()) {
55         v_terminate();
56         fd_ = -1;
57     }
58 }
59
60 prefix_ void senf::FileBody::destroyClose()
61 {
62     if (valid())
63         try {
64             close();
65         }
66         catch (...) {
67             terminate();
68         }
69 }
70
71 prefix_ void senf::FileBody::v_close()
72 {
73     if (::close(fd_) != 0)
74         SENF_THROW_SYSTEM_EXCEPTION("");
75 }
76
77 prefix_ void senf::FileBody::v_terminate()
78 {
79     ::close(fd_);
80 }
81
82 prefix_ bool senf::FileBody::v_eof()
83     const
84 {
85     return false;
86 }
87
88 prefix_ bool senf::FileBody::v_valid()
89     const
90 {
91     return true;
92 }
93
94 prefix_ bool senf::FileBody::blocking()
95     const
96 {
97     int flags = ::fcntl(fd(),F_GETFL);
98     if (flags < 0) SENF_THROW_SYSTEM_EXCEPTION("");
99     return ! (flags & O_NONBLOCK);
100 }
101
102 prefix_ void senf::FileBody::blocking(bool status)
103 {
104     int flags = ::fcntl(fd(),F_GETFL);
105     if (flags < 0) SENF_THROW_SYSTEM_EXCEPTION("");
106     if (status) flags &= ~O_NONBLOCK;
107     else        flags |= O_NONBLOCK;
108     if (::fcntl(fd(), F_SETFL, flags) < 0) SENF_THROW_SYSTEM_EXCEPTION("");
109 }
110
111 /* We don't take POLLIN/POLLOUT as argument to avoid having to include
112    sys/poll.h in the .cci file (and therefore indirectly into the .hh
113    and then every file which uses FileHandle) */
114 prefix_ bool senf::FileBody::pollCheck(int fd, bool incoming, int timeout, bool oob)
115     const
116 {
117     struct ::pollfd pfd;
118     ::memset(&pfd,0,sizeof(pfd));
119     pfd.fd = fd;
120     pfd.events = incoming?(oob?POLLPRI:POLLIN):POLLOUT;
121     int rv = -1;
122     do {
123         rv = ::poll(&pfd,1,timeout);
124         if (rv<0)
125             switch (errno) {
126             case EINTR:
127                 break;
128             default:
129                 SENF_THROW_SYSTEM_EXCEPTION("");
130             }
131     } while (rv<0);
132     return rv>0;
133 }
134
135 prefix_ senf::FileBody::~FileBody()
136 {}
137
138 //-/////////////////////////////////////////////////////////////////////////////////////////////////
139 #undef prefix_
140
141 \f
142 // Local Variables:
143 // mode: c++
144 // fill-column: 100
145 // c-file-style: "senf"
146 // indent-tabs-mode: nil
147 // ispell-local-dictionary: "american"
148 // compile-command: "scons -u test"
149 // comment-column: 40
150 // End: