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