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