1 //===-- RNBSocket.h ---------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Created by Greg Clayton on 12/12/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef __RNBSocket_h__
15 #define __RNBSocket_h__
16 
17 #include "RNBDefs.h"
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #include <string>
21 #include "DNBTimer.h"
22 
23 class RNBSocket
24 {
25 public:
26     typedef void (*PortBoundCallback) (const void *baton, in_port_t port);
27 
28     RNBSocket () :
29         m_fd (-1),
30 #ifdef WITH_LOCKDOWN
31         m_fd_from_lockdown (false),
32 #endif
33         m_timer (true)      // Make a thread safe timer
34     {
35     }
36     ~RNBSocket (void)
37     {
38         Disconnect (false);
39     }
40 
41     rnb_err_t Listen (in_port_t port, PortBoundCallback callback, const void *callback_baton);
42     rnb_err_t Connect (const char *host, uint16_t port);
43 
44     rnb_err_t useFD(int fd);
45 
46 #ifdef WITH_LOCKDOWN
47     rnb_err_t ConnectToService();
48 #endif
49     rnb_err_t OpenFile (const char *path);
50     rnb_err_t Disconnect (bool save_errno);
51     rnb_err_t Read (std::string &p);
52     rnb_err_t Write (const void *buffer, size_t length);
53 
54     bool IsConnected () const { return m_fd != -1; }
55     void SaveErrno (int curr_errno);
56     DNBTimer& Timer() { return m_timer; }
57 
58     static int SetSocketOption(int fd, int level, int option_name, int option_value);
59 private:
60     // Outlaw some constructors
61     RNBSocket (const RNBSocket &);
62 
63 protected:
64     rnb_err_t ClosePort (int& fd, bool save_errno);
65 
66     int m_fd;    // Socket we use to communicate once conn established
67 
68 #ifdef WITH_LOCKDOWN
69     bool m_fd_from_lockdown;
70 #endif
71 
72     DNBTimer m_timer;
73 };
74 
75 
76 #endif // #ifndef __RNBSocket_h__
77