// // socket.hpp // // socket classes by Jeremy Siek // (they are quite similar in design to the ACE socket classes) #ifndef DSL_SOCKET_HPP #define DSL_SOCKET_HPP #include #include #include extern "C" { #include } namespace dsl { class socket_error : public std::exception { public: socket_error(const std::string& msg) : m_msg(msg) { } const char* what() const { return m_msg.c_str(); } private: std::string m_msg; }; struct location_t { std::string host_name; int port; }; inline std::ostream& operator<<(std::ostream& os, const location_t& loc) { os << "host name: " << loc.host_name << " port: " << loc.port; return os; } // This will be generated... // this is the hook for serialization template void describe(Description& d, location_t& loc) { d & loc.host_name & loc.port; } class socket_acceptor { public: socket_acceptor(const location_t& l); ~socket_acceptor(); // closes the socket descriptor // need to provide hook for fcntl O_NONBLOCK? private: int descriptor; struct sockaddr_in socket_address; int port_number; friend class server_connection; }; class socket_connection { public: socket_connection() { } std::size_t send(const void* buff, std::size_t len) const; std::size_t receive(void* buff, std::size_t len) const; void send(const std::string& msg) const; void receive(std::string& msg) const; protected: ~socket_connection() { } int descriptor; private: socket_connection(const socket_connection&) { } }; class server_connection : public socket_connection { public: server_connection(const socket_acceptor&); ~server_connection(); }; class client_connection : public socket_connection { public: client_connection(const location_t& l); ~client_connection(); // closes the socket descriptor private: struct sockaddr_in socket_address; int port_number; }; } // namespace dsl #endif // DSL_SOCKET_HPP