source: libcf++/trunk/include/cf/network.h@ 18

Last change on this file since 18 was 18, checked in by cheese, 9 years ago

#1 unify descriptor for initializing

File size: 5.0 KB
Line 
1/**
2 * @file network.h
3 * @author myusgun@gmail.com
4 * @brief network
5 */
6#ifndef __cf_tcp_h__
7#define __cf_tcp_h__
8
9#include "cf/exception.h"
10#include "cf/bin.h"
11
12#include <string>
13
14#define UNUSED_SOCKET -1
15
16namespace cf
17{
18 /**
19 * network
20 */
21 namespace network
22 {
23 /**
24 * network host
25 */
26 class host
27 {
28 private:
29 std::string mAddress;
30 cf::uint16_t mPort;
31
32 public:
33 /**
34 * constructor
35 * @param address host address
36 * @param port port number
37 */
38 host(const std::string & address,
39 const cf::uint16_t port);
40
41 /**
42 * get host address
43 * @return host address
44 */
45 const std::string & address() const;
46
47 /**
48 * get port number
49 * @return port number
50 */
51 cf::uint16_t port() const;
52 };
53
54 /**
55 * TCP Socket
56 */
57 class tcp
58 {
59 private:
60 cf::socket_t mSocket;
61 cf::int32_t mTimeout;
62
63 /**
64 * receive
65 * @param out created-bin
66 * @throw cf::exception
67 */
68 cf::void_t receive(bin & out) const
69 throw (cf::exception);
70
71 public:
72 /**
73 * constructor
74 * @param attachedSocket [option] socket descriptor for attachment
75 * @throw cf::exception
76 */
77 tcp(const cf::socket_t attachedSocket = cf::nodesc)
78 throw (cf::exception);
79
80 /**
81 * destructor
82 */
83 virtual ~tcp();
84
85 /**
86 * get socket descriptor
87 * @return socket descriptor
88 */
89 cf::socket_t descriptor() const;
90
91 /**
92 * close connection
93 */
94 cf::void_t close();
95
96 /**
97 * connect to host
98 * @param peer peer host
99 * @param timeout timeout
100 * @throw cf::exception
101 */
102 cf::void_t connect(const host & peer,
103 const cf::int32_t timeout = 0)
104 throw (cf::exception);
105
106 /**
107 * connect to address:port
108 * @param address address
109 * @param port port
110 * @param timeout timeout
111 * @throw cf::exception
112 */
113 cf::void_t connect(const std::string & address,
114 const cf::uint16_t port,
115 const cf::int32_t timeout = 0)
116 throw (cf::exception);
117
118 /**
119 * bind
120 * @param port port
121 * @throw cf::exception
122 */
123 cf::void_t bind(const cf::uint16_t port) const
124 throw (cf::exception);
125
126 /**
127 * server ready
128 * @param backlog [option] backlog
129 * @throw cf::exception
130 */
131 cf::void_t listen(const cf::int32_t backlog = 5) const
132 throw (cf::exception);
133
134 /**
135 * accept client
136 * @return an instance of tcp client
137 * @throw cf::exception
138 */
139 tcp accept() const
140 throw (cf::exception);
141
142 /**
143 * attach socket
144 * @param sock socket descriptor
145 * @throw cf::exception
146 */
147 cf::void_t attach(const cf::socket_t sock)
148 throw (cf::exception);
149
150 /**
151 * detach socket
152 * @return socket descriptor
153 * @throw cf::exception
154 */
155 cf::socket_t detach()
156 throw (cf::exception);
157
158 /**
159 * send
160 * @param in data
161 * @throw cf::exception
162 */
163 cf::void_t send(const bin & in) const
164 throw (cf::exception);
165
166 /**
167 * receive
168 * @return received data
169 * @param size expected data length
170 * @throw cf::exception
171 */
172 bin receive(const cf::int32_t size) const
173 throw (cf::exception);
174
175 /**
176 * receive
177 * @return received all of data
178 * @throw cf::exception
179 */
180 bin receive() const
181 throw (cf::exception);
182
183 /**
184 * get socket option
185 */
186 cf::void_t getOption(const cf::int32_t optname,
187 cf::void_t * optval,
188 cf::int32_t * optlen) const
189 throw (cf::exception);
190
191 /**
192 * set socket option
193 */
194 cf::void_t setOption(const cf::int32_t optname,
195 const cf::void_t * optval,
196 const cf::int32_t optlen) const
197 throw (cf::exception);
198
199 /**
200 * set non-blocking
201 * @param flag true; false
202 */
203 cf::void_t setNonBlocking(const cf::bool_t flag);
204
205 /**
206 * set timeout
207 * @param seconds timeout seconds
208 * @see setNonBlocking()
209 */
210 cf::void_t setTimeout(const cf::int32_t seconds);
211
212 /**
213 * get local host
214 * @return local host
215 * @see cf::network::host
216 */
217 host local() const
218 throw (cf::exception);
219
220 /**
221 * get peer host
222 * @return peer host
223 * @see cf::network::host
224 */
225 host peer() const
226 throw (cf::exception);
227 };
228
229 /**
230 * NIC(Network Interface Card)
231 */
232 class nic
233 {
234 public:
235 /**
236 * get mac address
237 * @return mac address
238 * @throw cf::exception
239 */
240 static std::string getMACAddress()
241 throw (cf::exception);
242 };
243
244 /**
245 * byteOrder
246 */
247 class byteOrder
248 {
249 public:
250 /**
251 * host to network long
252 * @return long for network
253 * @param in long for host
254 */
255 static cf::uint32_t htonl(cf::uint32_t in);
256
257 /**
258 * host to network short
259 * @return short for network
260 * @param in short for host
261 */
262 static cf::uint16_t htons(cf::uint16_t in);
263
264 /**
265 * network to host long
266 * @return long for host
267 * @param in long for network
268 */
269 static cf::uint32_t ntohl(cf::uint32_t in);
270
271 /**
272 * network to host short
273 * @return short for host
274 * @param in short for network
275 */
276 static cf::uint16_t ntohs(cf::uint16_t in);
277 };
278 }
279}
280
281#endif // #ifndef __cf_tcp_h__
Note: See TracBrowser for help on using the repository browser.