blob: 81bbfdca18e8c903c41cd98e2212800ac1f02de8 [file] [log] [blame]
Jordan Bayles1c785bd2019-08-15 10:32:33 -07001// 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"
13#include "platform/base/error.h"
14#include "platform/base/ip_address.h"
15#include "platform/base/macros.h"
Ryan Keane3b88a252019-09-17 09:55:33 -070016#include "platform/impl/socket_handle.h"
Jordan Bayles8f0e0432021-02-01 12:07:16 -080017#include "platform/impl/socket_state.h"
Jordan Bayles1c785bd2019-08-15 10:32:33 -070018
19namespace openscreen {
Jordan Bayles1c785bd2019-08-15 10:32:33 -070020
Jordan Bayles1c785bd2019-08-15 10:32:33 -070021// StreamSocket is an incomplete abstraction of synchronous platform methods for
22// creating, initializing, and closing stream sockets. Callers can use this
23// class to define complete TCP and TLS socket classes, both synchronous and
24// asynchronous.
25class StreamSocket {
26 public:
Ryan Keaneb812a3b2019-09-20 12:53:32 -070027 StreamSocket() = default;
28 StreamSocket(const StreamSocket& other) = delete;
Jordan Bayles8f0e0432021-02-01 12:07:16 -080029 StreamSocket(StreamSocket&& other) noexcept = default;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070030 virtual ~StreamSocket() = default;
31
Ryan Keaneb812a3b2019-09-20 12:53:32 -070032 StreamSocket& operator=(const StreamSocket& other) = delete;
Ryan Keane32e72712019-10-08 13:59:41 -070033 StreamSocket& operator=(StreamSocket&& other) = default;
Ryan Keaneb812a3b2019-09-20 12:53:32 -070034
Jordan Bayles1c785bd2019-08-15 10:32:33 -070035 // Used by passive/server sockets to accept connection requests
36 // from a client.
37 virtual ErrorOr<std::unique_ptr<StreamSocket>> Accept() = 0;
38
39 // Bind to the address provided on socket construction. Socket should be
40 // unbound and not connected.
41 virtual Error Bind() = 0;
42
43 // Closes the socket. Socket must be opened before this method is called.
44 virtual Error Close() = 0;
45
46 // Connects the socket to a specified remote address. Socket should be
47 // initialized and bound, but not connected.
48 virtual Error Connect(const IPEndpoint& remote_endpoint) = 0;
49
50 // Marks the socket as passive, to receive incoming connections.
51 virtual Error Listen() = 0;
52 virtual Error Listen(int max_backlog_size) = 0;
53
54 // Returns the file descriptor (e.g. fd or HANDLE pointer) for this socket.
Ryan Keaneb812a3b2019-09-20 12:53:32 -070055 virtual const SocketHandle& socket_handle() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070056
57 // Returns the connected remote address, if socket is connected.
58 virtual absl::optional<IPEndpoint> remote_address() const = 0;
59
Ryan Keane235eb702019-09-16 10:20:48 -070060 // Returns the local address, if one is assigned.
61 virtual absl::optional<IPEndpoint> local_address() const = 0;
62
Jordan Bayles1c785bd2019-08-15 10:32:33 -070063 // Returns the state of the socket.
Jordan Bayles8f0e0432021-02-01 12:07:16 -080064 virtual TcpSocketState state() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070065
66 // Returns the IP version of the socket.
67 virtual IPAddress::Version version() const = 0;
68};
69
Jordan Bayles1c785bd2019-08-15 10:32:33 -070070} // namespace openscreen
71
72#endif // PLATFORM_IMPL_STREAM_SOCKET_H_