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 inline bool listening() const { return (_state == State::Listening); }
55 inline bool connected() const override {
56 return (_state == State::Connected);
57 }
58
59protected:
60 bool create(int af);
61
62public:
63 bool listen(std::string const &address, std::string const &port);
64#if defined(OS_POSIX)
65 bool listen(std::string const &path, bool abstract = false);
66#endif
67 std::unique_ptr<Socket> accept();
68 bool connect(std::string const &host, std::string const &port);
69
70protected:
71 bool getSocketInfo(struct sockaddr_storage *ss) const;
72
73public:
74 std::string address() const;
75 std::string port() const;
76
77public:
78 std::string error() const;
79
80public:
81 bool wait(int ms = -1) override;
82
83public:
84 bool setNonBlocking();
85
86public:
87 ssize_t send(void const *buffer, size_t length) override;
88 ssize_t receive(void *buffer, size_t length) override;
89};
90} // namespace Host
91} // namespace ds2
Definition Channel.h:18
Definition Socket.h:25
Definition Base.h:138