DebugServer2
Loading...
Searching...
No Matches
Socket.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Host/Channel.h"
14
15#include <memory>
16#if defined(OS_WIN32)
17#include <winsock2.h>
18#elif defined(OS_POSIX)
19#include <sys/socket.h>
20#endif
21
22namespace ds2 {
23namespace Host {
24
25class Socket : public Channel, public make_unique_enabler<Socket> {
26private:
27#if defined(OS_WIN32)
28 typedef int socklen_t;
29#elif defined(OS_POSIX)
30 typedef int SOCKET;
31 static SOCKET const INVALID_SOCKET = -1;
32#endif
33
34protected:
35 enum class State { Invalid, Listening, Connected };
36
37protected:
38 SOCKET _handle;
39 State _state;
40 int _lastError;
41
42public:
43 Socket();
44 Socket(SOCKET handle);
45 ~Socket() override;
46
47public:
48 void close() override;
49
50public:
51 inline bool valid() const { return (_handle != INVALID_SOCKET); }
52
53public:
54 // The underlying native socket handle. Used on Windows to hand a listening
55 // socket's handle to a relaunched child process (there is no fork() to
56 // rely on there); see Sources/main.cpp's Windows SlaveMain.
57 inline SOCKET native_handle() const { return _handle; }
58
59public:
60 inline bool listening() const { return (_state == State::Listening); }
61 inline bool connected() const override {
62 return (_state == State::Connected);
63 }
64
65protected:
66 bool create(int af);
67
68public:
69 bool listen(std::string const &address, std::string const &port);
70#if defined(OS_POSIX)
71 bool listen(std::string const &path, bool abstract = false);
72#endif
73 std::unique_ptr<Socket> accept();
74 bool connect(std::string const &host, std::string const &port);
75
76protected:
77 bool getSocketInfo(struct sockaddr_storage *ss) const;
78
79public:
80 std::string address() const;
81 std::string port() const;
82
83public:
84 std::string error() const;
85
86public:
87 bool wait(int ms = -1) override;
88
89public:
90 bool setNonBlocking();
91
92public:
93 ssize_t send(void const *buffer, size_t length) override;
94 ssize_t receive(void *buffer, size_t length) override;
95};
96} // namespace Host
97} // namespace ds2
Definition Channel.h:18
Definition Socket.h:25
Definition Base.h:132