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 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 26 | #include "fdevent.h" |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 27 | #include "types.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 28 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 29 | class atransport; |
| 30 | |
| 31 | /* An asocket represents one half of a connection between a local and |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 32 | * remote entity. A local asocket is bound to a file descriptor. A |
| 33 | * remote asocket is bound to the protocol engine. |
| 34 | */ |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 35 | struct asocket { |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 36 | /* the unique identifier for this asocket |
| 37 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 38 | unsigned id = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 39 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 40 | /* flag: set when the socket's peer has closed |
| 41 | * but packets are still queued for delivery |
| 42 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 43 | int closing = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 44 | |
| 45 | // flag: set when the socket failed to write, so the socket will not wait to |
| 46 | // write packets and close directly. |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 47 | bool has_write_error = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 48 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 49 | /* flag: quit adbd when both ends close the |
| 50 | * local service socket |
| 51 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 52 | int exit_on_close = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 53 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 54 | // the asocket we are connected to |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 55 | asocket* peer = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 56 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 57 | /* For local asockets, the fde is used to bind |
| 58 | * us to our fd event system. For remote asockets |
| 59 | * these fields are not used. |
| 60 | */ |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame^] | 61 | fdevent* fde = nullptr; |
| 62 | int fd = -1; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 63 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 64 | // queue of data waiting to be written |
| 65 | std::deque<Range> packet_queue; |
| 66 | |
| 67 | std::string smart_socket_data; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 68 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 69 | /* enqueue is called by our peer when it has data |
| 70 | * for us. It should return 0 if we can accept more |
| 71 | * data or 1 if not. If we return 1, we must call |
| 72 | * peer->ready() when we once again are ready to |
| 73 | * receive data. |
| 74 | */ |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 75 | int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 76 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 77 | /* ready is called by the peer when it is ready for |
| 78 | * us to send data via enqueue again |
| 79 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 80 | void (*ready)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 81 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 82 | /* shutdown is called by the peer before it goes away. |
| 83 | * the socket should not do any further calls on its peer. |
| 84 | * Always followed by a call to close. Optional, i.e. can be NULL. |
| 85 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 86 | void (*shutdown)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 87 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 88 | /* close is called by the peer when it has gone away. |
| 89 | * we are not allowed to make any further calls on the |
| 90 | * peer once our close method is called. |
| 91 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 92 | void (*close)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 93 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 94 | /* A socket is bound to atransport */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 95 | atransport* transport = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 96 | |
| 97 | size_t get_max_payload() const; |
| 98 | }; |
| 99 | |
| 100 | asocket *find_local_socket(unsigned local_id, unsigned remote_id); |
| 101 | void install_local_socket(asocket *s); |
| 102 | void remove_socket(asocket *s); |
| 103 | void close_all_sockets(atransport *t); |
| 104 | |
| 105 | asocket *create_local_socket(int fd); |
Josh Gao | ebc1c31 | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 106 | asocket* create_local_service_socket(const char* destination, atransport* transport); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 107 | |
| 108 | asocket *create_remote_socket(unsigned id, atransport *t); |
| 109 | void connect_to_remote(asocket *s, const char *destination); |
| 110 | void connect_to_smartsocket(asocket *s); |
| 111 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 112 | // Internal functions that are only made available here for testing purposes. |
| 113 | namespace internal { |
| 114 | |
| 115 | #if ADB_HOST |
Dan Austin | ca35e9e | 2016-03-28 15:32:37 -0700 | [diff] [blame] | 116 | char* skip_host_serial(char* service); |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | } // namespace internal |
| 120 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 121 | #endif // __ADB_SOCKET_H |