blob: 21cd7274ff7d87e7ce4daff6d9519d5808a47003 [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"
17
18namespace openscreen {
19namespace platform {
20
21struct FileDescriptor;
22
23// StreamSocket is an incomplete abstraction of synchronous platform methods for
24// creating, initializing, and closing stream sockets. Callers can use this
25// class to define complete TCP and TLS socket classes, both synchronous and
26// asynchronous.
27class StreamSocket {
28 public:
29 virtual ~StreamSocket() = default;
30
31 // Used by passive/server sockets to accept connection requests
32 // from a client.
33 virtual ErrorOr<std::unique_ptr<StreamSocket>> Accept() = 0;
34
35 // Bind to the address provided on socket construction. Socket should be
36 // unbound and not connected.
37 virtual Error Bind() = 0;
38
39 // Closes the socket. Socket must be opened before this method is called.
40 virtual Error Close() = 0;
41
42 // Connects the socket to a specified remote address. Socket should be
43 // initialized and bound, but not connected.
44 virtual Error Connect(const IPEndpoint& remote_endpoint) = 0;
45
46 // Marks the socket as passive, to receive incoming connections.
47 virtual Error Listen() = 0;
48 virtual Error Listen(int max_backlog_size) = 0;
49
50 // Returns the file descriptor (e.g. fd or HANDLE pointer) for this socket.
51 virtual FileDescriptor file_descriptor() const = 0;
52
53 // Returns the connected remote address, if socket is connected.
54 virtual absl::optional<IPEndpoint> remote_address() const = 0;
55
56 // Returns the state of the socket.
57 virtual SocketState state() const = 0;
58
59 // Returns the IP version of the socket.
60 virtual IPAddress::Version version() const = 0;
61};
62
63} // namespace platform
64} // namespace openscreen
65
66#endif // PLATFORM_IMPL_STREAM_SOCKET_H_