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