]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/src/System/Socket.hpp
This commit was generated by cvs2svn to compensate for changes in r11742,
[u/mrichter/AliRoot.git] / HLT / MUON / src / System / Socket.hpp
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Author: Artur Szostak
4 // Email:  artur@alice.phy.uct.ac.za | artursz@iafrica.com
5 //
6 ////////////////////////////////////////////////////////////////////////////////
7
8 #ifndef dHLT_SYSTEM_SOCKET_HPP
9 #define dHLT_SYSTEM_SOCKET_HPP
10
11 #include "BasicTypes.hpp"
12 #include "Utils.hpp"
13 #include "System/SystemError.hpp"
14 #include "System/SystemTypes.hpp"
15
16 #include <ostream>
17 #include <vector>
18 #include <algorithm>
19
20 namespace dHLT
21 {
22 namespace System
23 {
24
25
26 class SocketError : public System::Error
27 {
28 public:
29         SocketError() throw();
30         virtual const char* Message() const throw();
31 };
32
33
34 class Address
35 {
36 public:
37
38         Address();
39         Address(const SocketAddress& address);
40         Address(const InetSocketAddress& address);
41         Address(const UInt ipv4address, const UShort port);
42         Address(const UShort netbyte_order_port, const UInt netbyte_order_address);
43         Address(const char* string);
44
45         static Address GetHostAddress(const UShort port = 0);
46
47
48         UInt Ipv4Address() const;
49         void Ipv4Address(const UInt value);
50
51         UShort Port() const;
52         void Port(const UShort port);
53
54         const char* AsString() const;
55         static const char* AsString(const UInt ipv4address);
56         static const char* AsStringNB(const UInt netbyte_order_address);
57
58         UInt Size() const
59         {
60                 return sizeof(data);
61         };
62
63         operator const SocketAddress* () const
64         {
65                 return (SocketAddress*) &data;
66         };
67
68         operator SocketAddress* ()
69         {
70                 return (SocketAddress*) &data;
71         };
72
73         operator const char* () const
74         {
75                 return AsString();
76         };
77
78
79         friend std::ostream& operator << (std::ostream& os, const Address& addr);
80
81 private:
82
83         InetSocketAddress data;
84 };
85
86
87 class Socket
88 {
89 public:
90
91         Socket();
92         Socket(const SocketHandle s);
93         ~Socket();
94
95         bool CanBroadcast() const;
96         void AllowBroadcast(bool value = true);
97         bool CanReuseAddress() const;
98         void AllowAddressReuse(bool value = true);
99
100         UInt MaxMessageSize() const;
101
102         Address BroadcastAddress(const UInt localaddress = INADDR_ANY) const;
103
104         Address LocalAddress() const;
105
106         void Bind(const Address& address);
107
108         const Address& TargetAddress() const { return targetaddr; };
109         void TargetAddress(const Address& address) { targetaddr = address; };
110         UInt Send(const char* message, const UInt length);
111
112         UInt BytesAvailable() const;
113
114         const Address& SourceAddress() const { return sourceaddr; };
115         UInt Receive(char* message, const UInt length);
116
117         SocketHandle Handle() const { return handle; };
118         operator SocketHandle () const { return handle; };
119
120         bool CanRead() const;
121         bool CanWrite() const;
122
123         bool WaitUntilReadable(
124                         const UInt timeout_secs = 0xFFFFFFFF,
125                         const UInt timeout_usecs = 0xFFFFFFFF
126                 ) const;
127
128         bool WaitUntilWriteable(
129                         const UInt timeout_secs = 0xFFFFFFFF,
130                         const UInt timeout_usecs = 0xFFFFFFFF
131                 ) const;
132
133 private:
134
135         SocketHandle handle;
136         Address targetaddr;
137         Address sourceaddr;
138 };
139
140
141
142 class SocketList
143 {
144 public:
145
146         void Add(Socket& s);
147         void Remove(Socket& s);
148         bool Contains(Socket& s);
149
150         UInt Size() const { return list.size(); };
151
152         const Socket* operator [] (UInt index) const;
153         Socket* operator [] (UInt index);
154
155         friend std::ostream& operator << (std::ostream& os, const SocketList& sl);
156
157 private:
158
159         std::vector<Socket*> list;
160 };
161
162
163 enum SocketStatus
164 {
165         SocketStatusUnknown,
166         CanReadSocket,
167         CanWriteSocket,
168         SocketHasFailed
169 };
170
171 enum SocketEventTypes
172 {
173         SocketReadable  = 0x01,
174         SocketWriteable = 0x02,
175         SocketFailure   = 0x04
176 };
177
178
179 Socket* WaitFor(
180                 SocketStatus& status,
181                 const SocketList& sockets,
182                 const int eventflags = SocketReadable | SocketWriteable | SocketFailure,
183                 const UInt timeout_secs = 0xFFFFFFFF,
184                 const UInt timeout_usecs = 0xFFFFFFFF
185         );
186
187 Socket* WaitForReadable(
188                 const SocketList& sockets,
189                 const UInt timeout_secs = 0xFFFFFFFF,
190                 const UInt timeout_usecs = 0xFFFFFFFF
191         );
192
193 Socket* WaitForWriteable(
194                 const SocketList& sockets,
195                 const UInt timeout_secs = 0xFFFFFFFF,
196                 const UInt timeout_usecs = 0xFFFFFFFF
197         );
198
199
200 } // System
201 } // dHLT
202
203 #endif // dHLT_SYSTEM_SOCKET_HPP