7d177abd39ce1e6cf3c36514f981da3ee27d7e92
[senf.git] / senf / Socket / ReadWritePolicy.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 ReadPolicy and WritePolicy non-inline non-template implementation
30  */
31
32 #include "ReadWritePolicy.hh"
33 //#include "ReadWritePolicy.ih"
34
35 // Custom includes
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <unistd.h>
39 #include <errno.h>
40
41 //#include "ReadWritePolicy.mpp"
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 prefix_ unsigned senf::ReadablePolicy::read(FileHandle & handle, char * buffer,
46                                                    unsigned size)
47 {
48     int rv = -1;
49     do {
50         rv = ::read(handle.fd(), buffer, size);
51         if (rv < 0)
52             switch(errno) {
53             case EINTR:
54                 break;
55             case EAGAIN:
56                 // This means, the socket is non-blocking an no data was available
57                 rv = 0;
58                 break;
59             default:
60                 SENF_THROW_SYSTEM_EXCEPTION(":::read");
61             }
62     } while (rv<0);
63     return rv;
64 }
65
66 prefix_ unsigned senf::ReadablePolicy::do_readfrom(FileHandle & handle, char * buffer,
67                                                           unsigned size,
68                                                           struct ::sockaddr * addr, socklen_t * len)
69 {
70     int rv = -1;
71     do {
72         rv = ::recvfrom(handle.fd(),buffer, size, 0, addr, len);
73         if (rv < 0)
74             switch (errno) {
75             case EINTR:
76                 break;
77             case EAGAIN:
78                 rv = 0;
79                 break;
80             default:
81                 SENF_THROW_SYSTEM_EXCEPTION("::recvfrom");
82             }
83     } while (rv<0);
84     return rv;
85 }
86
87 prefix_ unsigned senf::WriteablePolicy::do_write(FileHandle & handle, char const * buffer,
88                                                         unsigned size)
89 {
90     int rv = -1;
91     do {
92         rv = ::write(handle.fd(), buffer, size);
93         if (rv < 0)
94             switch (errno) {
95             case EINTR:
96                 break;
97             case EAGAIN:
98             case ENOBUFS:
99                 // According to the man page this should not happen, since packets are just silently being dropped.
100                 // It does happen in NetEmu using small TxQueues on WLAN interfaces 
101             case ECONNREFUSED:
102                 // Writing to a UDP socket seems return this error code if a corresponding ICMP
103                 // error code has been received before (at least on linux). This is inconsistent
104                 // since I cannot rely on getting ECONNREFUSED. I therefore ignore this error. TCP
105                 // sockets will return this error on connect() and not on write(). Therefore we can
106                 // unconditionally ignore this error here.
107                 rv = 0;
108                 break;
109             default:
110                 SENF_THROW_SYSTEM_EXCEPTION("::write");
111             }
112     } while (rv<0);
113     return rv;
114 }
115
116 prefix_ unsigned senf::WriteablePolicy::do_writeto(FileHandle & handle,
117                                                           char const * buffer, unsigned size,
118                                                           struct sockaddr const * addr, socklen_t len)
119 {
120     int rv = -1;
121     do {
122         rv = ::sendto(handle.fd(), buffer, size, 0, addr, len);
123         if (rv < 0)
124             switch (errno) {
125             case EINTR:
126                 break;
127             case EAGAIN:
128             case ENOBUFS:
129                 // According to the man page this should not happen, since packets are just silently being dropped.
130                 // It does happen in NetEmu using small TxQueues on WLAN interfaces 
131                 rv = 0;
132                 break;
133             default:
134                 SENF_THROW_SYSTEM_EXCEPTION("::sendto");
135             }
136     } while (rv<0);
137     return rv;
138 }
139
140 //-/////////////////////////////////////////////////////////////////////////////////////////////////
141 #undef prefix_
142 //#include "ReadWritePolicy.mpp"
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // comment-column: 40
153 // End: