blob: 6128915540a957a4c98c890a994aa5557a1a64e6 [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;
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;
33
Jordan Bayles1c785bd2019-08-15 10:32:33 -070034 // Used by passive/server sockets to accept connection requests
35 // from a client.
36 virtual ErrorOr<std::unique_ptr<StreamSocket>> Accept() = 0;
37
38 // Bind to the address provided on socket construction. Socket should be
39 // unbound and not connected.
40 virtual Error Bind() = 0;
41
42 // Closes the socket. Socket must be opened before this method is called.
43 virtual Error Close() = 0;
44
45 // Connects the socket to a specified remote address. Socket should be
46 // initialized and bound, but not connected.
47 virtual Error Connect(const IPEndpoint& remote_endpoint) = 0;
48
49 // Marks the socket as passive, to receive incoming connections.
50 virtual Error Listen() = 0;
51 virtual Error Listen(int max_backlog_size) = 0;
52
53 // Returns the file descriptor (e.g. fd or HANDLE pointer) for this socket.
Ryan Keaneb812a3b2019-09-20 12:53:32 -070054 virtual const SocketHandle& socket_handle() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070055
56 // Returns the connected remote address, if socket is connected.
57 virtual absl::optional<IPEndpoint> remote_address() const = 0;
58
Ryan Keane235eb702019-09-16 10:20:48 -070059 // Returns the local address, if one is assigned.
60 virtual absl::optional<IPEndpoint> local_address() const = 0;
61
Jordan Bayles1c785bd2019-08-15 10:32:33 -070062 // Returns the state of the socket.
63 virtual SocketState state() const = 0;
64
65 // Returns the IP version of the socket.
66 virtual IPAddress::Version version() const = 0;
67};
68
69} // namespace platform
70} // namespace openscreen
71
72#endif // PLATFORM_IMPL_STREAM_SOCKET_H_