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

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

#1 get address from local socket or peer socket

File size: 3.8 KB
Line 
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
14namespace cf
15{
16 /**
17 * network
18 */
19 namespace network
20 {
21 /**
22 * network host
23 */
24 class host
25 {
26 private:
27 std::string mAddress;
28 cf::uint16_t mPort;
29
30 public:
31 /**
32 * constructor
33 * @param address host address
34 * @param port port number
35 */
36 host(const std::string & address = "",
37 const cf::uint16_t port = 0);
38
39 /**
40 * get host address
41 * @return host address
42 */
43 std::string address() const;
44
45 /**
46 * get port number
47 * @return port number
48 */
49 cf::uint16_t port() const;
50
51 /**
52 * is empty ?
53 * @return true; false
54 */
55 cf::bool_t empty() const;
56 };
57
58 /**
59 * TCP Socket
60 */
61 class tcp
62 {
63 private:
64 cf::socket_t mSocket;
65 cf::int32_t mTimeout;
66
67 /**
68 * receive
69 * @param out created-bin
70 * @throw cf::exception
71 */
72 cf::void_t receive(bin & out) const
73 throw (cf::exception);
74
75 public:
76 /**
77 * constructor
78 * @param attachedSocket [option] socket descriptor for attachment
79 * @throw cf::exception
80 */
81 tcp(const cf::socket_t attachedSocket = -1)
82 throw (cf::exception);
83
84 /**
85 * destructor
86 */
87 ~tcp();
88
89 /**
90 * close connection
91 */
92 cf::void_t close();
93
94 /**
95 * connect to address:port
96 * @param peer peer host
97 * @param timeout timeout
98 * @throw cf::exception
99 */
100 cf::void_t connect(const host & peer,
101 const cf::int32_t timeout = 0)
102 throw (cf::exception);
103
104 /**
105 * server ready
106 * @param port port
107 * @param backlog [option] backlog
108 * @throw cf::exception
109 */
110 cf::void_t listen(const cf::uint16_t port,
111 const cf::int32_t backlog = 5) const
112 throw (cf::exception);
113
114 /**
115 * accept client
116 * @return an instance of tcp client
117 * @throw cf::exception
118 */
119 tcp accept() const
120 throw (cf::exception);
121
122 /**
123 * attach socket
124 * @param sock socket descriptor
125 * @throw cf::exception
126 */
127 cf::void_t attach(const cf::socket_t sock)
128 throw (cf::exception);
129
130 /**
131 * detach socket
132 * @return socket descriptor
133 * @throw cf::exception
134 */
135 cf::socket_t detach()
136 throw (cf::exception);
137
138 /**
139 * send
140 * @param in data
141 * @throw cf::exception
142 */
143 cf::void_t send(const bin & in) const
144 throw (cf::exception);
145
146 /**
147 * receive
148 * @return received data
149 * @param size expected data length
150 * @throw cf::exception
151 */
152 bin receive(const cf::int32_t size) const
153 throw (cf::exception);
154
155 /**
156 * receive
157 * @return received all of data
158 * @throw cf::exception
159 */
160 bin receive() const
161 throw (cf::exception);
162
163 /**
164 * get socket option
165 */
166 cf::void_t getOption(const cf::int32_t optname,
167 cf::void_t * optval,
168 cf::int32_t * optlen) const
169 throw (cf::exception);
170
171 /**
172 * set socket option
173 */
174 cf::void_t setOption(const cf::int32_t optname,
175 const cf::void_t * optval,
176 const cf::int32_t optlen) const
177 throw (cf::exception);
178
179 /**
180 * set timeout
181 * @param seconds timeout seconds
182 * @see setNonBlocking()
183 */
184 cf::void_t setTimeout(const cf::int32_t seconds);
185
186 /**
187 * get local host
188 * @return local host
189 * @see cf::network::host
190 */
191 host local() const
192 throw (cf::exception);
193
194 /**
195 * get peer host
196 * @return peer host
197 * @see cf::network::host
198 */
199 host peer() const
200 throw (cf::exception);
201 };
202
203 /**
204 * NIC(Network Interface Card)
205 */
206 class nic
207 {
208 public:
209 /**
210 * get mac address
211 * @return mac address
212 * @throw cf::exception
213 */
214 static std::string getMACAddress()
215 throw (cf::exception);
216 };
217 }
218}
219
220#endif // #ifndef __tcp_h__
Note: See TracBrowser for help on using the repository browser.