/** * @file network.h * @author myusgun@gmail.com * @brief network */ #ifndef __tcp_h__ #define __tcp_h__ #include "cf/exception.h" #include "cf/bin.h" #include namespace cf { /** * network */ namespace network { /** * TCP Socket */ class tcp { private: cf::socket_t mSocket; cf::int32_t mTimeout; std::string mHost; cf::uint16_t mPort; /** * receive * @param out created-bin * @throw cf::exception */ cf::void_t receive(bin & out) throw (cf::exception); public: /** * constructor * @param attachedSocket [option] socket descriptor for attachment * @throw cf::exception */ tcp(const cf::socket_t attachedSocket = -1) throw (cf::exception); /** * destructor */ ~tcp(); /** * close connection */ cf::void_t close(); /** * connect to host:port * @param host host * @param port port * @param timeout timeout * @throw cf::exception */ cf::void_t connect(const cf::char_t * host, const cf::uint16_t port, const cf::int32_t timeout = 0) throw (cf::exception); /** * server ready * @param port port * @param backlog [option] backlog * @throw cf::exception */ cf::void_t listen(const cf::uint16_t port, const cf::int32_t backlog = 5) throw (cf::exception); /** * accept client * @return an instance of tcp client * @throw cf::exception */ tcp accept() throw (cf::exception); /** * attach socket * @param sock socket descriptor * @throw cf::exception */ cf::void_t attach(const cf::socket_t sock) throw (cf::exception); /** * detach socket * @return socket descriptor * @throw cf::exception */ cf::socket_t detach() throw (cf::exception); /** * send * @param in data * @throw cf::exception */ cf::void_t send(const bin & in) throw (cf::exception); /** * receive * @return received data * @param size expected data length * @throw cf::exception */ bin receive(const cf::int32_t size) throw (cf::exception); /** * receive * @return received all of data * @throw cf::exception */ bin receive() throw (cf::exception); /** * get socket option */ cf::void_t getOption(const cf::int32_t optname, cf::void_t * optval, cf::int32_t * optlen) throw (cf::exception); /** * set socket option */ cf::void_t setOption(const cf::int32_t optname, const cf::void_t * optval, const cf::int32_t optlen) throw (cf::exception); /** * set non-blocking * @param flag true; false * @see setTimeout */ cf::void_t setNonBlocking(const cf::bool_t flag); /** * set timeout * @param seconds timeout seconds * @see setNonBlocking() */ cf::void_t setTimeout(const cf::int32_t seconds); }; /** * NIC(Network Interface Card) */ class nic { public: /** * get mac address * @return mac address * @throw cf::exception */ static std::string getMACAddress() throw (cf::exception); }; } } #endif // #ifndef __tcp_h__