Further documentation (mainly SocketPolicy)
[senf.git] / Socket / ClientSocketHandle.hh
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 #ifndef HH_ClientSocketHandle_
24 #define HH_ClientSocketHandle_ 1
25
26 // Custom includes
27 #include <boost/call_traits.hpp>
28 #include "SocketHandle.hh"
29
30 //#include "ClientSocketHandle.mpp"
31 ///////////////////////////////hh.p////////////////////////////////////////
32
33 namespace senf {
34     
35     /// \addtogroup handle_group
36     /// @{
37     
38     template <class Policy> class ServerSocketHandle;
39
40     /** \brief
41         
42         \todo Move all not template-parameter dependent code into a
43         non-template base class
44       */
45     template <class Policy>
46     class ClientSocketHandle
47         : public SocketHandle<Policy>
48     {
49     public:
50         ///////////////////////////////////////////////////////////////////////////
51         // Types
52
53         typedef typename Policy::AddressingPolicy::Address Address;
54         typedef typename boost::call_traits<Address>::param_type AddressParam;
55         typedef ServerSocketHandle<Policy> ServerSocketHandle;
56
57         ///////////////////////////////////////////////////////////////////////////
58         ///\name Structors and default members
59         ///@{
60
61         // no default constructor
62         // default copy constructor
63         // default copy assignment
64         // default destructor
65
66         // conversion constructors
67         template <class OtherPolicy>
68         ClientSocketHandle(ClientSocketHandle<OtherPolicy> other,
69                            typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type * = 0);
70
71         template <class OtherPolicy>
72         typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type const & 
73         operator=(ClientSocketHandle<OtherPolicy> other);
74
75         ///@}
76         ///////////////////////////////////////////////////////////////////////////
77
78         ///////////////////////////////////////////////////////////////////////////
79         ///\name reading and writing
80         ///@{
81
82         // read from socket (connected or unconnected)
83         std::string  read         (unsigned limit=0);
84         void         read         (std::string & buffer, unsigned limit=0);
85         unsigned     read         (char * buffer, unsigned size);
86
87         // read from unconnected socket returning peer address
88         std::pair<std::string, Address> 
89                      readfrom     ();
90         void         readfrom     (std::string & buffer, Address & from);
91         unsigned     readfrom     (char * buffer, unsigned size, Address & from);
92
93         // write to connected socket
94         unsigned     write        (std::string const & data);
95         unsigned     write        (char const * buffer, unsigned size);
96
97         // write to unconnected socket
98         unsigned     writeto      (AddressParam addr, std::string const & data);
99         unsigned     writeto      (AddressParam addr, char const * buffer, unsigned size);
100
101         ///@}
102
103         ///////////////////////////////////////////////////////////////////////////
104         ///\name Addressing
105         ///@{
106
107         void         connect      (AddressParam addr);
108         void         bind         (AddressParam addr);
109
110         Address      peer         ();
111         void         peer         (Address & addr);
112         Address      local        ();
113         void         local        (Address & addr);
114
115         ///@}
116
117         ///////////////////////////////////////////////////////////////////////////
118         ///\name Buffering
119         ///@{
120         
121         unsigned     rcvbuf      ();
122         void         rcvbuf      (unsigned size);        
123         unsigned     sndbuf      ();
124         void         sndbuf      (unsigned size);
125         
126         ///@}
127         
128         static ClientSocketHandle cast_static(FileHandle handle);
129         static ClientSocketHandle cast_dynamic(FileHandle handle);
130
131         // we need to override both since SocketHandle is *not* polymorphic
132         void state(SocketStateMap & map, unsigned lod=0);
133         std::string dumpState(unsigned lod=0);
134
135     protected:
136         ClientSocketHandle(FileHandle other, bool isChecked);
137         explicit ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol,
138                                     int fd = -1);
139
140     private:
141         unsigned available();
142
143         friend class senf::ServerSocketHandle<Policy>;
144     };
145
146     /// @}
147 }
148
149 ///////////////////////////////hh.e////////////////////////////////////////
150 //#include "ClientSocketHandle.cci"
151 #include "ClientSocketHandle.ct"
152 #include "ClientSocketHandle.cti"
153 #endif
154
155 \f
156 // Local Variables:
157 // mode: c++
158 // c-file-style: "senf"
159 // End: