1 // Wrappers around sockets.
2 //
3 // (c) Victor Agababov (vagababov@gmail.com) 2011
4 // Apache license goes here.
5 
6 
7 #ifndef _SLOW_HTTP_TEST_SOCKET_
8 #define _SLOW_HTTP_TEST_SOCKET_
9 
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include <netdb.h>
13 
14 namespace slowhttptest {
15 // This class is a simple wrapper around BSD Sockets.
16 // It works as factory producing instances of the Socket class
17 // on the heap. The instances are initialized with the addrinfo structure.
18 class Socket {
19  public:
20   // Creates and intializes socket wrapper with given addrinfo structure.
21   // Will return NULL if the initialization of the socket fails.
22   static Socket* Create(const addrinfo* addr);
23   virtual ~Socket();
24 
25   // Sends size bytes from data. Will return how bytes have been
26   // actually sent.
27   virtual int Send(const char* data, const int size);
28 
29   // Receives at most size byes in the data buffer.
30   // Returns how many byts have actually been received.
31   virtual int Recv(char* data, const int size);
32 
33  protected:
34   virtual void Close();
35   Socket();
36   bool Init(const addrinfo* addr);
get_socket()37   int get_socket() const { return the_socket_; }
38 
39  private:
40   int the_socket_;
41 };
42 
43 
44 }  // namespace slowhttptest
45 
46 #endif  // _SLOW_HTTP_TEST_SOCKET_
47