switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / FileHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief FileHandle non-inline non-template implementation
30  */
31
32 #include "FileHandle.hh"
33 //#include "FileHandle.ih"
34
35 // Custom includes
36 #include <unistd.h>
37 #include <sys/poll.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <senf/Utils/Exception.hh>
42
43 #define prefix_
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 // senf::FileBody
48
49 prefix_ void senf::FileBody::close()
50 {
51     if (!valid())
52         throw SystemException(EBADF SENF_EXC_DEBUGINFO);
53     v_close();
54     fd_ = -1;
55 }
56
57 prefix_ void senf::FileBody::terminate()
58 {
59     if (valid()) {
60         v_terminate();
61         fd_ = -1;
62     }
63 }
64
65 prefix_ void senf::FileBody::destroyClose()
66 {
67     if (valid())
68         try {
69             close();
70         }
71         catch (...) {
72             terminate();
73         }
74 }
75
76 prefix_ void senf::FileBody::v_close()
77 {
78     if (::close(fd_) != 0)
79         SENF_THROW_SYSTEM_EXCEPTION("");
80 }
81
82 prefix_ void senf::FileBody::v_terminate()
83 {
84     ::close(fd_);
85 }
86
87 prefix_ bool senf::FileBody::v_eof()
88     const
89 {
90     return false;
91 }
92
93 prefix_ bool senf::FileBody::v_valid()
94     const
95 {
96     return true;
97 }
98
99 prefix_ bool senf::FileBody::blocking()
100     const
101 {
102     int flags = ::fcntl(fd(),F_GETFL);
103     if (flags < 0) SENF_THROW_SYSTEM_EXCEPTION("");
104     return ! (flags & O_NONBLOCK);
105 }
106
107 prefix_ void senf::FileBody::blocking(bool status)
108 {
109     int flags = ::fcntl(fd(),F_GETFL);
110     if (flags < 0) SENF_THROW_SYSTEM_EXCEPTION("");
111     if (status) flags &= ~O_NONBLOCK;
112     else        flags |= O_NONBLOCK;
113     if (::fcntl(fd(), F_SETFL, flags) < 0) SENF_THROW_SYSTEM_EXCEPTION("");
114 }
115
116 /* We don't take POLLIN/POLLOUT as argument to avoid having to include
117    sys/poll.h in the .cci file (and therefore indirectly into the .hh
118    and then every file which uses FileHandle) */
119 prefix_ bool senf::FileBody::pollCheck(int fd, bool incoming, int timeout, bool oob)
120     const
121 {
122     struct ::pollfd pfd;
123     ::memset(&pfd,0,sizeof(pfd));
124     pfd.fd = fd;
125     pfd.events = incoming?(oob?POLLPRI:POLLIN):POLLOUT;
126     int rv = -1;
127     do {
128         rv = ::poll(&pfd,1,timeout);
129         if (rv<0)
130             switch (errno) {
131             case EINTR:
132                 break;
133             default:
134                 SENF_THROW_SYSTEM_EXCEPTION("");
135             }
136     } while (rv<0);
137     return rv>0;
138 }
139
140 prefix_ senf::FileBody::~FileBody()
141 {}
142
143 //-/////////////////////////////////////////////////////////////////////////////////////////////////
144 #undef prefix_
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // comment-column: 40
155 // End: