Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PLATFORM_IMPL_STREAM_SOCKET_H_ |
| 6 | #define PLATFORM_IMPL_STREAM_SOCKET_H_ |
| 7 | |
| 8 | #include <cstdint> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "platform/api/network_interface.h" |
Ryan Keane | 6e54252 | 2019-09-10 16:05:44 -0700 | [diff] [blame] | 13 | #include "platform/api/socket_handle.h" |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 14 | #include "platform/base/error.h" |
| 15 | #include "platform/base/ip_address.h" |
| 16 | #include "platform/base/macros.h" |
| 17 | #include "platform/base/socket_state.h" |
| 18 | |
| 19 | namespace openscreen { |
| 20 | namespace platform { |
| 21 | |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 22 | // StreamSocket is an incomplete abstraction of synchronous platform methods for |
| 23 | // creating, initializing, and closing stream sockets. Callers can use this |
| 24 | // class to define complete TCP and TLS socket classes, both synchronous and |
| 25 | // asynchronous. |
| 26 | class StreamSocket { |
| 27 | public: |
| 28 | virtual ~StreamSocket() = default; |
| 29 | |
| 30 | // Used by passive/server sockets to accept connection requests |
| 31 | // from a client. |
| 32 | virtual ErrorOr<std::unique_ptr<StreamSocket>> Accept() = 0; |
| 33 | |
| 34 | // Bind to the address provided on socket construction. Socket should be |
| 35 | // unbound and not connected. |
| 36 | virtual Error Bind() = 0; |
| 37 | |
| 38 | // Closes the socket. Socket must be opened before this method is called. |
| 39 | virtual Error Close() = 0; |
| 40 | |
| 41 | // Connects the socket to a specified remote address. Socket should be |
| 42 | // initialized and bound, but not connected. |
| 43 | virtual Error Connect(const IPEndpoint& remote_endpoint) = 0; |
| 44 | |
| 45 | // Marks the socket as passive, to receive incoming connections. |
| 46 | virtual Error Listen() = 0; |
| 47 | virtual Error Listen(int max_backlog_size) = 0; |
| 48 | |
| 49 | // Returns the file descriptor (e.g. fd or HANDLE pointer) for this socket. |
Ryan Keane | 6e54252 | 2019-09-10 16:05:44 -0700 | [diff] [blame] | 50 | virtual SocketHandle socket_handle() const = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 51 | |
| 52 | // Returns the connected remote address, if socket is connected. |
| 53 | virtual absl::optional<IPEndpoint> remote_address() const = 0; |
| 54 | |
Ryan Keane | 235eb70 | 2019-09-16 10:20:48 -0700 | [diff] [blame^] | 55 | // Returns the local address, if one is assigned. |
| 56 | virtual absl::optional<IPEndpoint> local_address() const = 0; |
| 57 | |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 58 | // Returns the state of the socket. |
| 59 | virtual SocketState state() const = 0; |
| 60 | |
| 61 | // Returns the IP version of the socket. |
| 62 | virtual IPAddress::Version version() const = 0; |
| 63 | }; |
| 64 | |
| 65 | } // namespace platform |
| 66 | } // namespace openscreen |
| 67 | |
| 68 | #endif // PLATFORM_IMPL_STREAM_SOCKET_H_ |