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