blob: 5fd6ac2a57042be3fc1821941f74d6f07b429695 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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:
Greg Clayton8b82f082011-04-12 05:54:46 +000026 typedef void (*PortBoundCallback) (const void *baton, in_port_t port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28 RNBSocket () :
Greg Clayton8b82f082011-04-12 05:54:46 +000029 m_fd (-1),
30 m_fd_from_lockdown (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031 m_timer (true) // Make a thread safe timer
32 {
33 }
34 ~RNBSocket (void)
35 {
36 Disconnect (false);
37 }
38
Greg Clayton8b82f082011-04-12 05:54:46 +000039 rnb_err_t Listen (in_port_t port, PortBoundCallback callback, const void *callback_baton);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000040 rnb_err_t Connect (const char *host, uint16_t port);
41
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#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
Greg Clayton8b82f082011-04-12 05:54:46 +000050 bool IsConnected () const { return m_fd != -1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 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);
55private:
56 // Outlaw some constructors
57 RNBSocket (const RNBSocket &);
58
59protected:
60 rnb_err_t ClosePort (int& fd, bool save_errno);
61
Greg Clayton8b82f082011-04-12 05:54:46 +000062 int m_fd; // Socket we use to communicate once conn established
63 bool m_fd_from_lockdown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064 DNBTimer m_timer;
65};
66
67
68#endif // #ifndef __RNBSocket_h__