blob: 0623204892370a7c44d7432665446f79c88a9d35 [file] [log] [blame]
Yabin Cui2ce9d562015-09-15 16:27:09 -07001/*
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
Josh Gaoa7d9d712018-02-01 13:17:50 -080022#include <deque>
Josh Gao2f8da952017-09-12 13:23:33 -070023#include <memory>
Josh Gaoa7d9d712018-02-01 13:17:50 -080024#include <string>
Josh Gao2f8da952017-09-12 13:23:33 -070025
Josh Gaoc2705962019-01-23 15:36:42 -080026#include "adb_unique_fd.h"
Josh Gaob51193a2019-06-28 13:50:37 -070027#include "fdevent/fdevent.h"
Josh Gaocd2a5292018-03-07 16:52:28 -080028#include "types.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070029
Yabin Cui2ce9d562015-09-15 16:27:09 -070030class atransport;
31
32/* An asocket represents one half of a connection between a local and
Josh Gao2f8da952017-09-12 13:23:33 -070033 * remote entity. A local asocket is bound to a file descriptor. A
34 * remote asocket is bound to the protocol engine.
35 */
Yabin Cui2ce9d562015-09-15 16:27:09 -070036struct asocket {
Josh Gao2f8da952017-09-12 13:23:33 -070037 /* the unique identifier for this asocket
38 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080039 unsigned id = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070040
Josh Gao2f8da952017-09-12 13:23:33 -070041 /* flag: set when the socket's peer has closed
42 * but packets are still queued for delivery
43 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080044 int closing = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070045
46 // flag: set when the socket failed to write, so the socket will not wait to
47 // write packets and close directly.
Josh Gao5cb76ce2018-02-12 17:24:00 -080048 bool has_write_error = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070049
Josh Gao2f8da952017-09-12 13:23:33 -070050 /* flag: quit adbd when both ends close the
51 * local service socket
52 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080053 int exit_on_close = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070054
Josh Gao2f8da952017-09-12 13:23:33 -070055 // the asocket we are connected to
Josh Gao5cb76ce2018-02-12 17:24:00 -080056 asocket* peer = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070057
Josh Gao2f8da952017-09-12 13:23:33 -070058 /* For local asockets, the fde is used to bind
59 * us to our fd event system. For remote asockets
60 * these fields are not used.
61 */
Josh Gao9528df22018-05-14 11:14:33 -070062 fdevent* fde = nullptr;
63 int fd = -1;
Yabin Cui2ce9d562015-09-15 16:27:09 -070064
Josh Gaoa7d9d712018-02-01 13:17:50 -080065 // queue of data waiting to be written
Josh Gao9f155db2018-04-03 14:37:11 -070066 IOVector packet_queue;
Josh Gaoa7d9d712018-02-01 13:17:50 -080067
68 std::string smart_socket_data;
Yabin Cui2ce9d562015-09-15 16:27:09 -070069
Josh Gao2f8da952017-09-12 13:23:33 -070070 /* enqueue is called by our peer when it has data
71 * for us. It should return 0 if we can accept more
72 * data or 1 if not. If we return 1, we must call
73 * peer->ready() when we once again are ready to
74 * receive data.
75 */
Josh Gaocd2a5292018-03-07 16:52:28 -080076 int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070077
Josh Gao2f8da952017-09-12 13:23:33 -070078 /* ready is called by the peer when it is ready for
79 * us to send data via enqueue again
80 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080081 void (*ready)(asocket* s) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070082
Josh Gao2f8da952017-09-12 13:23:33 -070083 /* shutdown is called by the peer before it goes away.
84 * the socket should not do any further calls on its peer.
85 * Always followed by a call to close. Optional, i.e. can be NULL.
86 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080087 void (*shutdown)(asocket* s) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070088
Josh Gao2f8da952017-09-12 13:23:33 -070089 /* close is called by the peer when it has gone away.
90 * we are not allowed to make any further calls on the
91 * peer once our close method is called.
92 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080093 void (*close)(asocket* s) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070094
Josh Gao2f8da952017-09-12 13:23:33 -070095 /* A socket is bound to atransport */
Josh Gao5cb76ce2018-02-12 17:24:00 -080096 atransport* transport = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070097
98 size_t get_max_payload() const;
99};
100
101asocket *find_local_socket(unsigned local_id, unsigned remote_id);
102void install_local_socket(asocket *s);
103void remove_socket(asocket *s);
104void close_all_sockets(atransport *t);
105
Josh Gaoc2705962019-01-23 15:36:42 -0800106asocket* create_local_socket(unique_fd fd);
Josh Gao6dbf5792018-12-13 14:21:00 -0800107asocket* create_local_service_socket(std::string_view destination, atransport* transport);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700108
109asocket *create_remote_socket(unsigned id, atransport *t);
Josh Gao4a037e22018-12-20 17:00:13 -0800110void connect_to_remote(asocket* s, std::string_view destination);
Josh Gao65d18e22020-04-22 20:57:26 -0700111
112#if ADB_HOST
Yabin Cui2ce9d562015-09-15 16:27:09 -0700113void connect_to_smartsocket(asocket *s);
Josh Gao65d18e22020-04-22 20:57:26 -0700114#endif
Yabin Cui2ce9d562015-09-15 16:27:09 -0700115
David Pursellc929c6f2016-03-01 08:58:26 -0800116// Internal functions that are only made available here for testing purposes.
117namespace internal {
118
119#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800120bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
121 std::string_view service);
David Pursellc929c6f2016-03-01 08:58:26 -0800122#endif
123
124} // namespace internal
125
Yabin Cui2ce9d562015-09-15 16:27:09 -0700126#endif // __ADB_SOCKET_H