|
1 | 1 | /* |
2 | | - * MIT License |
3 | | - * |
4 | | - * Copyright (c) 2025 NUClear Contributors |
5 | | - * |
6 | | - * This file is part of the NUClear codebase. |
7 | | - * See https://github.com/Fastcode/NUClear for further info. |
8 | | - * |
9 | | - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
10 | | - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
11 | | - * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and |
12 | | - * to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
13 | | - * |
14 | | - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of |
15 | | - * the Software. |
16 | | - * |
17 | | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
18 | | - * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
20 | | - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
21 | | - * IN THE SOFTWARE. |
22 | | - */ |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 NUClear Contributors |
| 5 | + * |
| 6 | + * This file is part of the NUClear codebase. |
| 7 | + * See https://github.com/Fastcode/NUClear for further info. |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 10 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 11 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and |
| 12 | + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of |
| 15 | + * the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 18 | + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 20 | + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | + * IN THE SOFTWARE. |
| 22 | + */ |
23 | 23 |
|
24 | 24 | #ifndef NUCLEAR_NETWORK_FILE_DESCRIPTOR_HPP |
25 | 25 | #define NUCLEAR_NETWORK_FILE_DESCRIPTOR_HPP |
26 | 26 |
|
27 | 27 | #include "../util/platform.hpp" |
28 | 28 |
|
29 | | - namespace NUClear { |
30 | | - namespace network { |
31 | | - |
32 | | - /** |
33 | | - * RAII wrapper for a file descriptor (socket). |
34 | | - * Automatically closes the file descriptor on destruction. |
35 | | - * Non-copyable, move-only. |
36 | | - */ |
37 | | - class FileDescriptor { |
38 | | - public: |
39 | | - /// Construct with an invalid descriptor |
40 | | - FileDescriptor() = default; |
41 | | - |
42 | | - /// Construct taking ownership of an existing file descriptor |
43 | | - explicit FileDescriptor(fd_t fd) : fd(fd) {} |
| 29 | +namespace NUClear { |
| 30 | +namespace network { |
44 | 31 |
|
45 | | - /// Destructor closes the descriptor if valid |
46 | | - ~FileDescriptor() { |
| 32 | + /** |
| 33 | + * RAII wrapper for a file descriptor (socket). |
| 34 | + * Automatically closes the file descriptor on destruction. |
| 35 | + * Non-copyable, move-only. |
| 36 | + */ |
| 37 | + class FileDescriptor { |
| 38 | + public: |
| 39 | + /// Construct with an invalid descriptor |
| 40 | + FileDescriptor() = default; |
| 41 | + |
| 42 | + /// Construct taking ownership of an existing file descriptor |
| 43 | + explicit FileDescriptor(fd_t fd) : fd(fd) {} |
| 44 | + |
| 45 | + /// Destructor closes the descriptor if valid |
| 46 | + ~FileDescriptor() { |
| 47 | + reset(); |
| 48 | + } |
| 49 | + |
| 50 | + // Non-copyable |
| 51 | + FileDescriptor(const FileDescriptor&) = delete; |
| 52 | + FileDescriptor& operator=(const FileDescriptor&) = delete; |
| 53 | + |
| 54 | + // Movable |
| 55 | + FileDescriptor(FileDescriptor&& other) noexcept : fd(other.fd) { |
| 56 | + other.fd = INVALID_SOCKET; |
| 57 | + } |
| 58 | + FileDescriptor& operator=(FileDescriptor&& other) noexcept { |
| 59 | + if (this != &other) { |
47 | 60 | reset(); |
48 | | - } |
49 | | - |
50 | | - // Non-copyable |
51 | | - FileDescriptor(const FileDescriptor&) = delete; |
52 | | - FileDescriptor& operator=(const FileDescriptor&) = delete; |
53 | | - |
54 | | - // Movable |
55 | | - FileDescriptor(FileDescriptor&& other) noexcept : fd(other.fd) { |
| 61 | + fd = other.fd; |
56 | 62 | other.fd = INVALID_SOCKET; |
57 | 63 | } |
58 | | - FileDescriptor& operator=(FileDescriptor&& other) noexcept { |
59 | | - if (this != &other) { |
60 | | - reset(); |
61 | | - fd = other.fd; |
62 | | - other.fd = INVALID_SOCKET; |
63 | | - } |
64 | | - return *this; |
65 | | - } |
66 | | - |
67 | | - /// Get the raw file descriptor |
68 | | - fd_t get() const { |
69 | | - return fd; |
70 | | - } |
71 | | - |
72 | | - /// Check if the descriptor is valid |
73 | | - bool valid() const { |
74 | | - return fd != INVALID_SOCKET; |
75 | | - } |
76 | | - |
77 | | - /// Implicit conversion to fd_t for use with system calls |
78 | | - operator fd_t() const { // NOLINT(google-explicit-constructor) |
79 | | - return fd; |
80 | | - } |
81 | | - |
82 | | - /// Release ownership without closing |
83 | | - fd_t release() { |
84 | | - const fd_t old = fd; |
85 | | - fd = INVALID_SOCKET; |
86 | | - return old; |
87 | | - } |
88 | | - |
89 | | - /// Close the current descriptor and take ownership of a new one |
90 | | - void reset(fd_t new_fd = INVALID_SOCKET) { |
91 | | - if (fd != INVALID_SOCKET) { |
92 | | - ::close(fd); |
93 | | - } |
94 | | - fd = new_fd; |
| 64 | + return *this; |
| 65 | + } |
| 66 | + |
| 67 | + /// Get the raw file descriptor |
| 68 | + fd_t get() const { |
| 69 | + return fd; |
| 70 | + } |
| 71 | + |
| 72 | + /// Check if the descriptor is valid |
| 73 | + bool valid() const { |
| 74 | + return fd != INVALID_SOCKET; |
| 75 | + } |
| 76 | + |
| 77 | + /// Implicit conversion to fd_t for use with system calls |
| 78 | + operator fd_t() const { // NOLINT(google-explicit-constructor) |
| 79 | + return fd; |
| 80 | + } |
| 81 | + |
| 82 | + /// Release ownership without closing |
| 83 | + fd_t release() { |
| 84 | + const fd_t old = fd; |
| 85 | + fd = INVALID_SOCKET; |
| 86 | + return old; |
| 87 | + } |
| 88 | + |
| 89 | + /// Close the current descriptor and take ownership of a new one |
| 90 | + void reset(fd_t new_fd = INVALID_SOCKET) { |
| 91 | + if (fd != INVALID_SOCKET) { |
| 92 | + ::close(fd); |
95 | 93 | } |
| 94 | + fd = new_fd; |
| 95 | + } |
96 | 96 |
|
97 | | - private: |
98 | | - fd_t fd{INVALID_SOCKET}; |
99 | | - }; |
| 97 | + private: |
| 98 | + fd_t fd{INVALID_SOCKET}; |
| 99 | + }; |
100 | 100 |
|
101 | | - } // namespace network |
| 101 | +} // namespace network |
102 | 102 | } // namespace NUClear |
103 | 103 |
|
104 | 104 | #endif // NUCLEAR_NETWORK_FILE_DESCRIPTOR_HPP |
0 commit comments