blob: 7278e05b8dc7ceb07e384fa8e616d24ed60d2bc6 [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"
16#include "platform/base/socket_state.h"
Ryan Keane3b88a252019-09-17 09:55:33 -070017#include "platform/impl/socket_handle.h"
Jordan Bayles1c785bd2019-08-15 10:32:33 -070018
19namespace openscreen {
20namespace platform {
21
Jordan Bayles1c785bd2019-08-15 10:32:33 -070022// 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.
26class StreamSocket {
27 public:
Ryan Keaneb812a3b2019-09-20 12:53:32 -070028 StreamSocket() = default;
29 StreamSocket(const StreamSocket& other) = delete;
Ryan Keane32e72712019-10-08 13:59:41 -070030 StreamSocket(StreamSocket&& other) = default;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070031 virtual ~StreamSocket() = default;
32
Ryan Keaneb812a3b2019-09-20 12:53:32 -070033 StreamSocket& operator=(const StreamSocket& other) = delete;
Ryan Keane32e72712019-10-08 13:59:41 -070034 StreamSocket& operator=(StreamSocket&& other) = default;
Ryan Keaneb812a3b2019-09-20 12:53:32 -070035
Jordan Bayles1c785bd2019-08-15 10:32:33 -070036 // Used by passive/server sockets to accept connection requests
37 // from a client.
38 virtual ErrorOr<std::unique_ptr<StreamSocket>> Accept() = 0;
39
40 // Bind to the address provided on socket construction. Socket should be
41 // unbound and not connected.
42 virtual Error Bind() = 0;
43
44 // Closes the socket. Socket must be opened before this method is called.
45 virtual Error Close() = 0;
46
47 // Connects the socket to a specified remote address. Socket should be
48 // initialized and bound, but not connected.
49 virtual Error Connect(const IPEndpoint& remote_endpoint) = 0;
50
51 // Marks the socket as passive, to receive incoming connections.
52 virtual Error Listen() = 0;
53 virtual Error Listen(int max_backlog_size) = 0;
54
55 // Returns the file descriptor (e.g. fd or HANDLE pointer) for this socket.
Ryan Keaneb812a3b2019-09-20 12:53:32 -070056 virtual const SocketHandle& socket_handle() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070057
58 // Returns the connected remote address, if socket is connected.
59 virtual absl::optional<IPEndpoint> remote_address() const = 0;
60
Ryan Keane235eb702019-09-16 10:20:48 -070061 // Returns the local address, if one is assigned.
62 virtual absl::optional<IPEndpoint> local_address() const = 0;
63
Jordan Bayles1c785bd2019-08-15 10:32:33 -070064 // Returns the state of the socket.
65 virtual SocketState state() const = 0;
66
67 // Returns the IP version of the socket.
68 virtual IPAddress::Version version() const = 0;
69};
70
71} // namespace platform
72} // namespace openscreen
73
74#endif // PLATFORM_IMPL_STREAM_SOCKET_H_