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