[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 | /**
|
---|
| 113 | * server ready
|
---|
| 114 | * @param port port
|
---|
| 115 | * @param backlog [option] backlog
|
---|
| 116 | * @throw cf::exception
|
---|
| 117 | */
|
---|
| 118 | cf::void_t listen(const cf::uint16_t port,
|
---|
[5] | 119 | const cf::int32_t backlog = 5) const
|
---|
[4] | 120 | throw (cf::exception);
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * accept client
|
---|
| 124 | * @return an instance of tcp client
|
---|
| 125 | * @throw cf::exception
|
---|
| 126 | */
|
---|
[5] | 127 | tcp accept() const
|
---|
[4] | 128 | throw (cf::exception);
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * attach socket
|
---|
| 132 | * @param sock socket descriptor
|
---|
| 133 | * @throw cf::exception
|
---|
| 134 | */
|
---|
| 135 | cf::void_t attach(const cf::socket_t sock)
|
---|
| 136 | throw (cf::exception);
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * detach socket
|
---|
| 140 | * @return socket descriptor
|
---|
| 141 | * @throw cf::exception
|
---|
| 142 | */
|
---|
| 143 | cf::socket_t detach()
|
---|
| 144 | throw (cf::exception);
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * send
|
---|
| 148 | * @param in data
|
---|
| 149 | * @throw cf::exception
|
---|
| 150 | */
|
---|
[5] | 151 | cf::void_t send(const bin & in) const
|
---|
[4] | 152 | throw (cf::exception);
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * receive
|
---|
| 156 | * @return received data
|
---|
| 157 | * @param size expected data length
|
---|
| 158 | * @throw cf::exception
|
---|
| 159 | */
|
---|
[5] | 160 | bin receive(const cf::int32_t size) const
|
---|
[4] | 161 | throw (cf::exception);
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * receive
|
---|
| 165 | * @return received all of data
|
---|
| 166 | * @throw cf::exception
|
---|
| 167 | */
|
---|
[5] | 168 | bin receive() const
|
---|
[4] | 169 | throw (cf::exception);
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * get socket option
|
---|
| 173 | */
|
---|
| 174 | cf::void_t getOption(const cf::int32_t optname,
|
---|
| 175 | cf::void_t * optval,
|
---|
[5] | 176 | cf::int32_t * optlen) const
|
---|
[4] | 177 | throw (cf::exception);
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * set socket option
|
---|
| 181 | */
|
---|
| 182 | cf::void_t setOption(const cf::int32_t optname,
|
---|
| 183 | const cf::void_t * optval,
|
---|
[5] | 184 | const cf::int32_t optlen) const
|
---|
[4] | 185 | throw (cf::exception);
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
[7] | 188 | * set non-blocking
|
---|
| 189 | * @param flag true; false
|
---|
| 190 | */
|
---|
| 191 | cf::void_t setNonBlocking(const cf::bool_t flag);
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
[4] | 194 | * set timeout
|
---|
| 195 | * @param seconds timeout seconds
|
---|
| 196 | * @see setNonBlocking()
|
---|
| 197 | */
|
---|
| 198 | cf::void_t setTimeout(const cf::int32_t seconds);
|
---|
[5] | 199 |
|
---|
| 200 | /**
|
---|
| 201 | * get local host
|
---|
| 202 | * @return local host
|
---|
| 203 | * @see cf::network::host
|
---|
| 204 | */
|
---|
| 205 | host local() const
|
---|
| 206 | throw (cf::exception);
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * get peer host
|
---|
| 210 | * @return peer host
|
---|
| 211 | * @see cf::network::host
|
---|
| 212 | */
|
---|
| 213 | host peer() const
|
---|
| 214 | throw (cf::exception);
|
---|
[4] | 215 | };
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * NIC(Network Interface Card)
|
---|
| 219 | */
|
---|
| 220 | class nic
|
---|
| 221 | {
|
---|
| 222 | public:
|
---|
| 223 | /**
|
---|
| 224 | * get mac address
|
---|
| 225 | * @return mac address
|
---|
| 226 | * @throw cf::exception
|
---|
| 227 | */
|
---|
| 228 | static std::string getMACAddress()
|
---|
| 229 | throw (cf::exception);
|
---|
| 230 | };
|
---|
[7] | 231 |
|
---|
| 232 | /**
|
---|
| 233 | * byteOrder
|
---|
| 234 | */
|
---|
| 235 | class byteOrder
|
---|
| 236 | {
|
---|
| 237 | public:
|
---|
| 238 | /**
|
---|
| 239 | * host to network long
|
---|
| 240 | * @return long for network
|
---|
| 241 | * @param long for host
|
---|
| 242 | */
|
---|
| 243 | static cf::uint32_t htonl(cf::uint32_t in);
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
| 246 | * host to network short
|
---|
| 247 | * @return short for network
|
---|
| 248 | * @param short for host
|
---|
| 249 | */
|
---|
| 250 | static cf::uint16_t htons(cf::uint16_t in);
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | * network to host long
|
---|
| 254 | * @return long for host
|
---|
| 255 | * @param long for network
|
---|
| 256 | */
|
---|
| 257 | static cf::uint32_t ntohl(cf::uint32_t in);
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
| 260 | * network to host short
|
---|
| 261 | * @return short for host
|
---|
| 262 | * @param short for network
|
---|
| 263 | */
|
---|
| 264 | static cf::uint16_t ntohs(cf::uint16_t in);
|
---|
| 265 | };
|
---|
[4] | 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | #endif // #ifndef __tcp_h__
|
---|