Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 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 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 22 | #include <deque> |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 23 | #include <memory> |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 24 | #include <string> |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 25 | |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame^] | 26 | #include "adb_unique_fd.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 27 | #include "fdevent.h" |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 28 | #include "types.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 29 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 30 | class atransport; |
| 31 | |
| 32 | /* An asocket represents one half of a connection between a local and |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 33 | * remote entity. A local asocket is bound to a file descriptor. A |
| 34 | * remote asocket is bound to the protocol engine. |
| 35 | */ |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 36 | struct asocket { |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 37 | /* the unique identifier for this asocket |
| 38 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 39 | unsigned id = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 40 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 41 | /* flag: set when the socket's peer has closed |
| 42 | * but packets are still queued for delivery |
| 43 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 44 | int closing = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 45 | |
| 46 | // flag: set when the socket failed to write, so the socket will not wait to |
| 47 | // write packets and close directly. |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 48 | bool has_write_error = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 49 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 50 | /* flag: quit adbd when both ends close the |
| 51 | * local service socket |
| 52 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 53 | int exit_on_close = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 54 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 55 | // the asocket we are connected to |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 56 | asocket* peer = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 57 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 58 | /* 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 Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 62 | fdevent* fde = nullptr; |
| 63 | int fd = -1; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 64 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 65 | // queue of data waiting to be written |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 66 | IOVector packet_queue; |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 67 | |
| 68 | std::string smart_socket_data; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 69 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 70 | /* 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 Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 76 | int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 77 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 78 | /* ready is called by the peer when it is ready for |
| 79 | * us to send data via enqueue again |
| 80 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 81 | void (*ready)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 82 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 83 | /* 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 Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 87 | void (*shutdown)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 88 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 89 | /* 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 Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 93 | void (*close)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 94 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 95 | /* A socket is bound to atransport */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 96 | atransport* transport = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 97 | |
| 98 | size_t get_max_payload() const; |
| 99 | }; |
| 100 | |
| 101 | asocket *find_local_socket(unsigned local_id, unsigned remote_id); |
| 102 | void install_local_socket(asocket *s); |
| 103 | void remove_socket(asocket *s); |
| 104 | void close_all_sockets(atransport *t); |
| 105 | |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame^] | 106 | asocket* create_local_socket(unique_fd fd); |
Josh Gao | 6dbf579 | 2018-12-13 14:21:00 -0800 | [diff] [blame] | 107 | asocket* create_local_service_socket(std::string_view destination, atransport* transport); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 108 | |
| 109 | asocket *create_remote_socket(unsigned id, atransport *t); |
Josh Gao | 4a037e2 | 2018-12-20 17:00:13 -0800 | [diff] [blame] | 110 | void connect_to_remote(asocket* s, std::string_view destination); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 111 | void connect_to_smartsocket(asocket *s); |
| 112 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 113 | // Internal functions that are only made available here for testing purposes. |
| 114 | namespace internal { |
| 115 | |
| 116 | #if ADB_HOST |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 117 | bool parse_host_service(std::string_view* out_serial, std::string_view* out_command, |
| 118 | std::string_view service); |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 119 | #endif |
| 120 | |
| 121 | } // namespace internal |
| 122 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 123 | #endif // __ADB_SOCKET_H |