blob: 0905aab303445ca3499ece205c51abebe771ce98 [file] [log] [blame]
Yabin Cuic1b1f6f2015-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 Gao27cb7dc2018-02-01 13:17:50 -080022#include <deque>
Josh Gao55c8b342017-09-12 13:23:33 -070023#include <memory>
Josh Gao27cb7dc2018-02-01 13:17:50 -080024#include <string>
Josh Gao55c8b342017-09-12 13:23:33 -070025
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070026#include "fdevent.h"
Josh Gao1ce99572018-03-07 16:52:28 -080027#include "types.h"
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070028
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070029class atransport;
30
31/* An asocket represents one half of a connection between a local and
Josh Gao55c8b342017-09-12 13:23:33 -070032 * remote entity. A local asocket is bound to a file descriptor. A
33 * remote asocket is bound to the protocol engine.
34 */
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070035struct asocket {
Josh Gao55c8b342017-09-12 13:23:33 -070036 /* the unique identifier for this asocket
37 */
Josh Gaoe0361d12018-02-12 17:24:00 -080038 unsigned id = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070039
Josh Gao55c8b342017-09-12 13:23:33 -070040 /* flag: set when the socket's peer has closed
41 * but packets are still queued for delivery
42 */
Josh Gaoe0361d12018-02-12 17:24:00 -080043 int closing = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070044
45 // flag: set when the socket failed to write, so the socket will not wait to
46 // write packets and close directly.
Josh Gaoe0361d12018-02-12 17:24:00 -080047 bool has_write_error = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070048
Josh Gao55c8b342017-09-12 13:23:33 -070049 /* flag: quit adbd when both ends close the
50 * local service socket
51 */
Josh Gaoe0361d12018-02-12 17:24:00 -080052 int exit_on_close = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070053
Josh Gao55c8b342017-09-12 13:23:33 -070054 // the asocket we are connected to
Josh Gaoe0361d12018-02-12 17:24:00 -080055 asocket* peer = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070056
Josh Gao55c8b342017-09-12 13:23:33 -070057 /* 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 Gao71f775a2018-05-14 11:14:33 -070061 fdevent* fde = nullptr;
62 int fd = -1;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070063
Josh Gao27cb7dc2018-02-01 13:17:50 -080064 // queue of data waiting to be written
Josh Gao7c738cd2018-04-03 14:37:11 -070065 IOVector packet_queue;
Josh Gao27cb7dc2018-02-01 13:17:50 -080066
67 std::string smart_socket_data;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070068
Josh Gao55c8b342017-09-12 13:23:33 -070069 /* 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 Gao1ce99572018-03-07 16:52:28 -080075 int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070076
Josh Gao55c8b342017-09-12 13:23:33 -070077 /* ready is called by the peer when it is ready for
78 * us to send data via enqueue again
79 */
Josh Gaoe0361d12018-02-12 17:24:00 -080080 void (*ready)(asocket* s) = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070081
Josh Gao55c8b342017-09-12 13:23:33 -070082 /* 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 Gaoe0361d12018-02-12 17:24:00 -080086 void (*shutdown)(asocket* s) = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070087
Josh Gao55c8b342017-09-12 13:23:33 -070088 /* 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 Gaoe0361d12018-02-12 17:24:00 -080092 void (*close)(asocket* s) = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070093
Josh Gao55c8b342017-09-12 13:23:33 -070094 /* A socket is bound to atransport */
Josh Gaoe0361d12018-02-12 17:24:00 -080095 atransport* transport = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070096
97 size_t get_max_payload() const;
98};
99
100asocket *find_local_socket(unsigned local_id, unsigned remote_id);
101void install_local_socket(asocket *s);
102void remove_socket(asocket *s);
103void close_all_sockets(atransport *t);
104
105asocket *create_local_socket(int fd);
Josh Gao44899ee2018-04-13 12:17:03 -0700106asocket* create_local_service_socket(const char* destination, atransport* transport);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700107
108asocket *create_remote_socket(unsigned id, atransport *t);
109void connect_to_remote(asocket *s, const char *destination);
110void connect_to_smartsocket(asocket *s);
111
David Pursell3f902aa2016-03-01 08:58:26 -0800112// Internal functions that are only made available here for testing purposes.
113namespace internal {
114
115#if ADB_HOST
Dan Austinb4cff492016-03-28 15:32:37 -0700116char* skip_host_serial(char* service);
David Pursell3f902aa2016-03-01 08:58:26 -0800117#endif
118
119} // namespace internal
120
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700121#endif // __ADB_SOCKET_H