blob: 83e35869b1303db1e708409b79aa916618ec64c6 [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"
Ryan Keane6e542522019-09-10 16:05:44 -070013#include "platform/api/socket_handle.h"
Jordan Bayles1c785bd2019-08-15 10:32:33 -070014#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
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:
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 Keane6e542522019-09-10 16:05:44 -070050 virtual SocketHandle socket_handle() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070051
52 // Returns the connected remote address, if socket is connected.
53 virtual absl::optional<IPEndpoint> remote_address() const = 0;
54
55 // Returns the state of the socket.
56 virtual SocketState state() const = 0;
57
58 // Returns the IP version of the socket.
59 virtual IPAddress::Version version() const = 0;
60};
61
62} // namespace platform
63} // namespace openscreen
64
65#endif // PLATFORM_IMPL_STREAM_SOCKET_H_