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 | namespace cf
|
---|
15 | {
|
---|
16 | /**
|
---|
17 | * network
|
---|
18 | */
|
---|
19 | namespace network
|
---|
20 | {
|
---|
21 | /**
|
---|
22 | * TCP Socket
|
---|
23 | */
|
---|
24 | class tcp
|
---|
25 | {
|
---|
26 | private:
|
---|
27 | cf::socket_t mSocket;
|
---|
28 | cf::int32_t mTimeout;
|
---|
29 | std::string mHost;
|
---|
30 | cf::uint16_t mPort;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * receive
|
---|
34 | * @param out created-bin
|
---|
35 | * @throw cf::exception
|
---|
36 | */
|
---|
37 | cf::void_t receive(bin & out)
|
---|
38 | throw (cf::exception);
|
---|
39 |
|
---|
40 | public:
|
---|
41 | /**
|
---|
42 | * constructor
|
---|
43 | * @param attachedSocket [option] socket descriptor for attachment
|
---|
44 | * @throw cf::exception
|
---|
45 | */
|
---|
46 | tcp(const cf::socket_t attachedSocket = -1)
|
---|
47 | throw (cf::exception);
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * destructor
|
---|
51 | */
|
---|
52 | ~tcp();
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * close connection
|
---|
56 | */
|
---|
57 | cf::void_t close();
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * connect to host:port
|
---|
61 | * @param host host
|
---|
62 | * @param port port
|
---|
63 | * @param timeout timeout
|
---|
64 | * @throw cf::exception
|
---|
65 | */
|
---|
66 | cf::void_t connect(const cf::char_t * host,
|
---|
67 | const cf::uint16_t port,
|
---|
68 | const cf::int32_t timeout = 0)
|
---|
69 | throw (cf::exception);
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * server ready
|
---|
73 | * @param port port
|
---|
74 | * @param backlog [option] backlog
|
---|
75 | * @throw cf::exception
|
---|
76 | */
|
---|
77 | cf::void_t listen(const cf::uint16_t port,
|
---|
78 | const cf::int32_t backlog = 5)
|
---|
79 | throw (cf::exception);
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * accept client
|
---|
83 | * @return an instance of tcp client
|
---|
84 | * @throw cf::exception
|
---|
85 | */
|
---|
86 | tcp accept()
|
---|
87 | throw (cf::exception);
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * attach socket
|
---|
91 | * @param sock socket descriptor
|
---|
92 | * @throw cf::exception
|
---|
93 | */
|
---|
94 | cf::void_t attach(const cf::socket_t sock)
|
---|
95 | throw (cf::exception);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * detach socket
|
---|
99 | * @return socket descriptor
|
---|
100 | * @throw cf::exception
|
---|
101 | */
|
---|
102 | cf::socket_t detach()
|
---|
103 | throw (cf::exception);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * send
|
---|
107 | * @param in data
|
---|
108 | * @throw cf::exception
|
---|
109 | */
|
---|
110 | cf::void_t send(const bin & in)
|
---|
111 | throw (cf::exception);
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * receive
|
---|
115 | * @return received data
|
---|
116 | * @param size expected data length
|
---|
117 | * @throw cf::exception
|
---|
118 | */
|
---|
119 | bin receive(const cf::int32_t size)
|
---|
120 | throw (cf::exception);
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * receive
|
---|
124 | * @return received all of data
|
---|
125 | * @throw cf::exception
|
---|
126 | */
|
---|
127 | bin receive()
|
---|
128 | throw (cf::exception);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * get socket option
|
---|
132 | */
|
---|
133 | cf::void_t getOption(const cf::int32_t optname,
|
---|
134 | cf::void_t * optval,
|
---|
135 | cf::int32_t * optlen)
|
---|
136 | throw (cf::exception);
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * set socket option
|
---|
140 | */
|
---|
141 | cf::void_t setOption(const cf::int32_t optname,
|
---|
142 | const cf::void_t * optval,
|
---|
143 | const cf::int32_t optlen)
|
---|
144 | throw (cf::exception);
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * set non-blocking
|
---|
148 | * @param flag true; false
|
---|
149 | * @see setTimeout
|
---|
150 | */
|
---|
151 | cf::void_t setNonBlocking(const cf::bool_t flag);
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * set timeout
|
---|
155 | * @param seconds timeout seconds
|
---|
156 | * @see setNonBlocking()
|
---|
157 | */
|
---|
158 | cf::void_t setTimeout(const cf::int32_t seconds);
|
---|
159 | };
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * NIC(Network Interface Card)
|
---|
163 | */
|
---|
164 | class nic
|
---|
165 | {
|
---|
166 | public:
|
---|
167 | /**
|
---|
168 | * get mac address
|
---|
169 | * @return mac address
|
---|
170 | * @throw cf::exception
|
---|
171 | */
|
---|
172 | static std::string getMACAddress()
|
---|
173 | throw (cf::exception);
|
---|
174 | };
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | #endif // #ifndef __tcp_h__
|
---|