blob: 64d05a92a1020bdae1a91e073cc97a42ba5ad3fe [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
Josh Gao2f8da952017-09-12 13:23:33 -070022#include <memory>
23
Yabin Cui2ce9d562015-09-15 16:27:09 -070024#include "fdevent.h"
25
26struct apacket;
27class atransport;
28
29/* An asocket represents one half of a connection between a local and
Josh Gao2f8da952017-09-12 13:23:33 -070030 * remote entity. A local asocket is bound to a file descriptor. A
31 * remote asocket is bound to the protocol engine.
32 */
Yabin Cui2ce9d562015-09-15 16:27:09 -070033struct asocket {
Josh Gao2f8da952017-09-12 13:23:33 -070034 /* chain pointers for the local/remote list of
35 * asockets that this asocket lives in
36 */
37 asocket* next;
38 asocket* prev;
Yabin Cui2ce9d562015-09-15 16:27:09 -070039
Josh Gao2f8da952017-09-12 13:23:33 -070040 /* the unique identifier for this asocket
41 */
Yabin Cui2ce9d562015-09-15 16:27:09 -070042 unsigned id;
43
Josh Gao2f8da952017-09-12 13:23:33 -070044 /* flag: set when the socket's peer has closed
45 * but packets are still queued for delivery
46 */
47 int closing;
Yabin Cui2ce9d562015-09-15 16:27:09 -070048
49 // flag: set when the socket failed to write, so the socket will not wait to
50 // write packets and close directly.
51 bool has_write_error;
52
Josh Gao2f8da952017-09-12 13:23:33 -070053 /* flag: quit adbd when both ends close the
54 * local service socket
55 */
56 int exit_on_close;
Yabin Cui2ce9d562015-09-15 16:27:09 -070057
Josh Gao2f8da952017-09-12 13:23:33 -070058 // the asocket we are connected to
59 asocket* peer;
Yabin Cui2ce9d562015-09-15 16:27:09 -070060
Josh Gao2f8da952017-09-12 13:23:33 -070061 /* 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 */
Yabin Cui2ce9d562015-09-15 16:27:09 -070065 fdevent fde;
66 int fd;
67
Josh Gao2f8da952017-09-12 13:23:33 -070068 // queue of apackets waiting to be written
69 apacket* pkt_first;
70 apacket* pkt_last;
Yabin Cui2ce9d562015-09-15 16:27:09 -070071
Josh Gao2f8da952017-09-12 13:23:33 -070072 /* enqueue is called by our peer when it has data
73 * for us. It should return 0 if we can accept more
74 * data or 1 if not. If we return 1, we must call
75 * peer->ready() when we once again are ready to
76 * receive data.
77 */
78 int (*enqueue)(asocket* s, apacket* pkt);
Yabin Cui2ce9d562015-09-15 16:27:09 -070079
Josh Gao2f8da952017-09-12 13:23:33 -070080 /* ready is called by the peer when it is ready for
81 * us to send data via enqueue again
82 */
83 void (*ready)(asocket* s);
Yabin Cui2ce9d562015-09-15 16:27:09 -070084
Josh Gao2f8da952017-09-12 13:23:33 -070085 /* shutdown is called by the peer before it goes away.
86 * the socket should not do any further calls on its peer.
87 * Always followed by a call to close. Optional, i.e. can be NULL.
88 */
89 void (*shutdown)(asocket* s);
Yabin Cui2ce9d562015-09-15 16:27:09 -070090
Josh Gao2f8da952017-09-12 13:23:33 -070091 /* close is called by the peer when it has gone away.
92 * we are not allowed to make any further calls on the
93 * peer once our close method is called.
94 */
95 void (*close)(asocket* s);
Yabin Cui2ce9d562015-09-15 16:27:09 -070096
Josh Gao2f8da952017-09-12 13:23:33 -070097 /* A socket is bound to atransport */
98 atransport* transport;
Yabin Cui2ce9d562015-09-15 16:27:09 -070099
100 size_t get_max_payload() const;
101};
102
103asocket *find_local_socket(unsigned local_id, unsigned remote_id);
104void install_local_socket(asocket *s);
105void remove_socket(asocket *s);
106void close_all_sockets(atransport *t);
107
108asocket *create_local_socket(int fd);
109asocket *create_local_service_socket(const char* destination,
110 const atransport* transport);
111
112asocket *create_remote_socket(unsigned id, atransport *t);
113void connect_to_remote(asocket *s, const char *destination);
114void connect_to_smartsocket(asocket *s);
115
David Pursellc929c6f2016-03-01 08:58:26 -0800116// Internal functions that are only made available here for testing purposes.
117namespace internal {
118
119#if ADB_HOST
Dan Austinca35e9e2016-03-28 15:32:37 -0700120char* skip_host_serial(char* service);
David Pursellc929c6f2016-03-01 08:58:26 -0800121#endif
122
123} // namespace internal
124
Yabin Cui2ce9d562015-09-15 16:27:09 -0700125#endif // __ADB_SOCKET_H