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         m_fd_from_lockdown (false),
31         m_timer (true)      // Make a thread safe timer
32     {
33     }
34     ~RNBSocket (void)
35     {
36         Disconnect (false);
37     }
38 
39     rnb_err_t Listen (in_port_t port, PortBoundCallback callback, const void *callback_baton);
40     rnb_err_t Connect (const char *host, uint16_t port);
41 
42 #if defined (__arm__)
43     rnb_err_t ConnectToService();
44 #endif
45     rnb_err_t OpenFile (const char *path);
46     rnb_err_t Disconnect (bool save_errno);
47     rnb_err_t Read (std::string &p);
48     rnb_err_t Write (const void *buffer, size_t length);
49 
50     bool IsConnected () const { return m_fd != -1; }
51     void SaveErrno (int curr_errno);
52     DNBTimer& Timer() { return m_timer; }
53 
54     static int SetSocketOption(int fd, int level, int option_name, int option_value);
55 private:
56     // Outlaw some constructors
57     RNBSocket (const RNBSocket &);
58 
59 protected:
60     rnb_err_t ClosePort (int& fd, bool save_errno);
61 
62     int m_fd;    // Socket we use to communicate once conn established
63     bool m_fd_from_lockdown;
64     DNBTimer m_timer;
65 };
66 
67 
68 #endif // #ifndef __RNBSocket_h__
69