$search
00001 #ifndef UTILMM_SOCKET_HH 00002 #define UTILMM_SOCKET_HH 00003 00004 #include <boost/noncopyable.hpp> 00005 #include <string> 00006 #include <vector> 00007 #include <sys/types.h> 00008 #include <stdint.h> 00009 00010 namespace utilmm 00011 { 00016 class base_socket : boost::noncopyable 00017 { 00018 public: 00019 enum Domain { Unix, Inet }; 00020 enum Type { Stream, Datagram }; 00021 enum Wait { WaitRead = 1, WaitWrite = 2, WaitException = 4}; 00022 00023 private: 00024 static int to_unix(Domain d); 00025 static int to_unix(Type t); 00026 00027 struct bad_address : public std::exception { }; 00028 00029 int m_fd; 00030 Domain m_domain; 00031 Type m_type; 00032 00033 int wait(int what, timeval* tv) const; 00034 00035 protected: 00036 std::vector<uint8_t> to_sockaddr(std::string const& to) const; 00037 00038 public: 00039 base_socket(int fd); 00040 base_socket(Domain domain, Type type); 00041 00043 ~base_socket(); 00044 00046 int fd() const; 00047 00053 bool try_wait(int what) const; 00054 00059 void wait(int what) const; 00060 00062 void flush() const; 00063 }; 00064 00069 class socket : public base_socket 00070 { 00071 friend class server_socket; 00072 00073 public: 00075 socket(int fd); 00076 00082 socket(Domain domain, Type type, std::string const& connect_to); 00083 00088 void connect(std::string const& to); 00089 00095 int read(void* buf, size_t size) const; 00096 00102 int write(void const* buf, size_t size) const; 00103 }; 00104 00109 class server_socket : public base_socket 00110 { 00111 public: 00112 server_socket(Domain domain, Type type, std::string const& bind_to, int backlog = 256); 00113 00114 void bind(std::string const& to); 00115 00120 socket* accept() const; 00121 00125 bool try_wait() const; 00126 00128 void wait() const; 00129 }; 00130 } 00131 00132 #endif 00133