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