Socket: Ignore ECONNREFUSED on write to datagram socket
[senf.git] / PPI / SocketReader.hh
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief SocketReader public header */
23
24 #ifndef HH_SocketReader_
25 #define HH_SocketReader_ 1
26
27 // Custom includes
28 #include "Packets/Packets.hh"
29 #include "Socket/ClientSocketHandle.hh"
30 #include "Socket/SocketPolicy.hh"
31 #include "Socket/ReadWritePolicy.hh"
32 #include "Socket/FramingPolicy.hh"
33 #include "Module.hh"
34 #include "Connectors.hh"
35 #include "IOEvent.hh"
36
37 //#include "SocketReader.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace ppi {
42
43     /** \brief Read helper for module::ActiveSocketReader
44
45         This read helper will read a datagram from a datagram socket. This datagram will then be
46         interpreted as a packet of type \a Packet as defined in the packet library. \a Packet
47         defaults to \ref DataPacket, which will place the data uninterpreted into a packet data
48         structure.
49      */
50     template <class Packet=DataPacket>
51     class PacketReader
52     {
53     public:
54         typedef senf::ClientSocketHandle<
55             senf::MakeSocketPolicy< senf::ReadablePolicy,
56                                     senf::DatagramFramingPolicy >::policy > Handle;
57                                         ///< Handle type supported by this reader
58
59         Packet operator()(Handle handle);
60                                         ///< Read packet from \a handle
61                                         /**< Read a datagram from \a handle and interpret is as
62                                              packet of type \c Packet.
63                                              \param[in] handle Handle to read data from
64                                              \returns Pointer to new packet instance or 0, if no
65                                                  packet could be read */
66     };
67
68 }}
69
70 namespace senf {
71 namespace ppi {
72 namespace module {
73
74     /** \brief Input module reading data from an arbitrary FileHandle
75
76         This input module will read data from a FileHandle object and parse the data according to
77         the \a Reader.
78         
79         The \a Reader must fulfill the following interface:
80         \code
81           class SomeReader
82           {
83           public:
84               typedef unspecified_type Handle;   // type of handle requested
85               SomeReader();                      // default constructible
86               Packet operator()(Handle handle);  // extraction function
87           };
88         \endcode
89         
90         Whenever the FileHandle object is ready for reading, the \a Reader's \c operator() is called
91         to read a packet. The default \a Reader is \c PacketReader<>, which will read packets from a
92         datagram SocketHandle into DataPacket's. You may 
93
94         \ingroup io_modules
95      */
96     template <class Reader=PacketReader<> >
97     class ActiveSocketReader 
98         : public Module
99     {
100         SENF_PPI_MODULE(ActiveSocketReader);
101
102     public:
103         typedef typename Reader::Handle Handle; ///< Handle type requested by the reader
104
105         connector::ActiveOutput output; ///< Output connector to which the data received is written
106         
107         ActiveSocketReader(Handle handle); ///< Create new reader for the given handle
108                                         /**< Data will be read from \a handle and be parsed by \a
109                                              Reader.
110                                              \param[in] handle Handle to read data from */
111
112     private:
113         void read();
114         
115         Handle handle_;
116         IOEvent event_;
117         Reader reader_;
118     };
119
120 }}}
121
122 ///////////////////////////////hh.e////////////////////////////////////////
123 //#include "SocketReader.cci"
124 #include "SocketReader.ct"
125 //#include "SocketReader.cti"
126 #endif
127
128 \f
129 // Local Variables:
130 // mode: c++
131 // fill-column: 100
132 // c-file-style: "senf"
133 // indent-tabs-mode: nil
134 // ispell-local-dictionary: "american"
135 // compile-command: "scons -u test"
136 // comment-column: 40
137 // End: