Util--  1.1
socket.hh
Go to the documentation of this file.
1 #ifndef UTILMM_SOCKET_HH
2 #define UTILMM_SOCKET_HH
3 
4 #include <boost/noncopyable.hpp>
5 #include <string>
6 #include <vector>
7 #include <sys/types.h>
8 #include <stdint.h>
9 
10 namespace utilmm
11 {
16  class base_socket : boost::noncopyable
17  {
18  public:
19  enum Domain { Unix, Inet };
20  enum Type { Stream, Datagram };
21  enum Wait { WaitRead = 1, WaitWrite = 2, WaitException = 4};
22 
23  private:
24  static int to_unix(Domain d);
25  static int to_unix(Type t);
26 
27  struct bad_address : public std::exception { };
28 
29  int m_fd;
30  Domain m_domain;
31  Type m_type;
32 
33  int wait(int what, timeval* tv) const;
34 
35  protected:
36  std::vector<uint8_t> to_sockaddr(std::string const& to) const;
37 
38  public:
39  base_socket(int fd);
40  base_socket(Domain domain, Type type);
41 
43  ~base_socket();
44 
46  int fd() const;
47 
53  bool try_wait(int what) const;
54 
59  void wait(int what) const;
60 
62  void flush() const;
63  };
64 
69  class socket : public base_socket
70  {
71  friend class server_socket;
72 
73  public:
75  socket(int fd);
76 
82  socket(Domain domain, Type type, std::string const& connect_to);
83 
88  void connect(std::string const& to);
89 
95  int read(void* buf, size_t size) const;
96 
102  int write(void const* buf, size_t size) const;
103  };
104 
109  class server_socket : public base_socket
110  {
111  public:
112  server_socket(Domain domain, Type type, std::string const& bind_to, int backlog = 256);
113 
114  void bind(std::string const& to);
115 
120  socket* accept() const;
121 
125  bool try_wait() const;
126 
128  void wait() const;
129  };
130 }
131 
132 #endif
133 
void flush() const
Wait
Definition: socket.hh:21
Definition: socket.hh:19
Definition: socket.hh:21
Definition: socket.hh:21
Type
Definition: socket.hh:20
std::vector< uint8_t > to_sockaddr(std::string const &to) const
Definition: socket.hh:16
Definition: socket.hh:21
Domain
Definition: socket.hh:19
Definition: socket.hh:69
Definition: auto_flag.hh:6
bool try_wait(int what) const
Definition: socket.hh:20
Definition: socket.hh:109
Definition: socket.hh:19
Definition: socket.hh:20

Generated on Mon Oct 22 2018 18:39:28 for Util-- by doxygen 1.8.13
SourceForge.net Project Page