Android-cuttlefish cvd tool
socket.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __ADB_SOCKET_H
18#define __ADB_SOCKET_H
19
20#include <stddef.h>
21
22#include <deque>
23#include <memory>
24#include <optional>
25#include <string>
26
27#include "adb_unique_fd.h"
28#include "fdevent/fdevent.h"
29#include "types.h"
30
31class atransport;
32
33/* An asocket represents one half of a connection between a local and
34 remote entity. A local asocket is bound to a file descriptor. A
35 remote asocket is bound to the protocol engine (transport).
36
37 Example of a Local Socket (LS) with undetermined peer:
38
39 LOCAL SOCKET (THIS) TRANSPORT
40 ┌────────────────────────────────────────────────┐ ┌──┐
41 ┌──┐ write(3) │ ┌─────┐ enqueue() │ │
42 │ │◄─────────┼──┤Queue├─────────────◄──────────────◄──────────┼─────────(A_WRTE)◄──
43 │fd│ │ └─────┘ │ │ │
44 │ ├──────────►─────────────────┐ │ ─ │ │
45 └──┘ read(3) └─────────────────┼──────────────────────────────┘ │ │
46 │ │ │
47 ┌─────────────────▼─────────────────▲────────────┐ │ │
48 │ │ │ │ │
49 │ │ │ │ │
50 │ └─────────────────────►──────────────────(A_WRTE)───►
51 │ enqueue() │ │ │
52 └────────────────────────────────────────────────┘ └──┘
53 REMOTE SOCKET (PEER)
54
55 Note that sockets can be peered regardless of their kind. A Remote Socket (RS) can be peered
56 with a Local Socket (LS) or a Local Service Socket (LSS).
57 */
58struct asocket {
59 /* the unique identifier for this asocket
60 */
61 unsigned id = 0;
62
63 // Start Local socket fields
64 // TODO: move all the local socket fields together
65
66 /* flag: set when the socket's peer has closed
67 * but packets are still queued for delivery
68 */
69 bool closing = false;
70
71 // flag: set when the socket failed to write, so the socket will not wait to
72 // write packets and close directly.
73 bool has_write_error = false;
74
75 /* flag: quit adbd when both ends close the
76 * local service socket
77 */
79
80 // End Local socket fields
81
82 // the asocket we are connected to
83 asocket* peer = nullptr;
84
85 /* enqueue is called by our peer when it has data
86 * for us. It should return 0 if we can accept more
87 * data or 1 if not. If we return 1, we must call
88 * peer->ready() when we once again are ready to
89 * receive data.
90 */
92
93 /* ready is called by the peer when it is ready for
94 * us to send data via enqueue again
95 */
96 void (*ready)(asocket* s) = nullptr;
97
98 /* shutdown is called by the peer before it goes away.
99 * the socket should not do any further calls on its peer.
100 * Always followed by a call to close. Optional, i.e. can be NULL.
101 */
102 void (*shutdown)(asocket* s) = nullptr;
103
104 /* close is called by the peer when it has gone away.
105 * we are not allowed to make any further calls on the
106 * peer once our close method is called.
107 */
108 void (*close)(asocket* s) = nullptr;
109
110 /* A socket is bound to atransport */
112
113 size_t get_max_payload() const;
114
115 // TODO: Make asocket an actual class and use inheritance instead of having an ever-growing
116 // struct with random use-specific fields stuffed into it.
117
118 // Start Local socket fields
119 fdevent* fde = nullptr;
120 int fd = -1;
121
122 // Queue of data that we've received from our peer, and are waiting to write into fd.
124 // End Local socket fields
125
126 // The number of bytes that have been acknowledged by the other end if delayed_ack is available.
127 // This value can go negative: if we have a MAX_PAYLOAD's worth of bytes available to send,
128 // we'll send out a full packet.
129 std::optional<int64_t> available_send_bytes;
130
131 // Start Smart socket fields
132 // A temporary buffer used to hold a partially-read service string for smartsockets.
133 std::string smart_socket_data;
134 // End Smart socket fields
135};
136
137asocket *find_local_socket(unsigned local_id, unsigned remote_id);
139void remove_socket(asocket *s);
141
142void local_socket_ack(asocket* s, std::optional<int32_t> acked_bytes);
143
145asocket* create_local_service_socket(std::string_view destination, atransport* transport);
146
147asocket *create_remote_socket(unsigned id, atransport *t);
148void connect_to_remote(asocket* s, std::string_view destination);
149
150#if ADB_HOST
151void connect_to_smartsocket(asocket *s);
152#endif
153
154// Internal functions that are only made available here for testing purposes.
155namespace internal {
156
157#if ADB_HOST
158bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
159 std::string_view service);
160#endif
161
162} // namespace internal
163
164#endif // __ADB_SOCKET_H
void local_socket_ack(asocket *s, std::optional< int32_t > acked_bytes)
Definition: sockets.cpp:418
void connect_to_remote(asocket *s, std::string_view destination)
Definition: sockets.cpp:560
asocket * create_remote_socket(unsigned id, atransport *t)
Definition: sockets.cpp:544
asocket * create_local_socket(unique_fd fd)
Definition: sockets.cpp:444
asocket * find_local_socket(unsigned local_id, unsigned remote_id)
Definition: sockets.cpp:64
asocket * create_local_service_socket(std::string_view destination, atransport *transport)
Definition: sockets.cpp:459
void remove_socket(asocket *s)
Definition: sockets.cpp:94
void install_local_socket(asocket *s)
Definition: sockets.cpp:81
void close_all_sockets(atransport *t)
Definition: sockets.cpp:102
Definition: unique_fd.h:61
Definition: transport.h:261
char data[Size]
Definition: incremental_server.cpp:1
Definition: socket.h:155
Definition: types.h:35
Definition: types.h:163
Definition: socket.h:58
size_t get_max_payload() const
Definition: sockets.cpp:982
std::optional< int64_t > available_send_bytes
Definition: socket.h:129
asocket * peer
Definition: socket.h:83
IOVector packet_queue
Definition: socket.h:123
int exit_on_close
Definition: socket.h:78
fdevent * fde
Definition: socket.h:119
void(* shutdown)(asocket *s)
Definition: socket.h:102
bool closing
Definition: socket.h:69
int fd
Definition: socket.h:120
void(* ready)(asocket *s)
Definition: socket.h:96
std::string smart_socket_data
Definition: socket.h:133
atransport * transport
Definition: socket.h:111
bool has_write_error
Definition: socket.h:73
int(* enqueue)(asocket *s, apacket::payload_type data)
Definition: socket.h:91
void(* close)(asocket *s)
Definition: socket.h:108
Definition: fdevent.h:57