2015-09-16 01:27:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ADB_SOCKET_H
|
|
|
|
#define __ADB_SOCKET_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
#include <deque>
|
2017-09-12 22:23:33 +02:00
|
|
|
#include <memory>
|
2018-02-01 22:17:50 +01:00
|
|
|
#include <string>
|
2017-09-12 22:23:33 +02:00
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
#include "adb_unique_fd.h"
|
2019-06-28 22:50:37 +02:00
|
|
|
#include "fdevent/fdevent.h"
|
2018-03-08 01:52:28 +01:00
|
|
|
#include "types.h"
|
2015-09-16 01:27:09 +02:00
|
|
|
|
|
|
|
class atransport;
|
|
|
|
|
|
|
|
/* An asocket represents one half of a connection between a local and
|
2017-09-12 22:23:33 +02:00
|
|
|
* remote entity. A local asocket is bound to a file descriptor. A
|
|
|
|
* remote asocket is bound to the protocol engine.
|
|
|
|
*/
|
2015-09-16 01:27:09 +02:00
|
|
|
struct asocket {
|
2017-09-12 22:23:33 +02:00
|
|
|
/* the unique identifier for this asocket
|
|
|
|
*/
|
2018-02-13 02:24:00 +01:00
|
|
|
unsigned id = 0;
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2017-09-12 22:23:33 +02:00
|
|
|
/* flag: set when the socket's peer has closed
|
|
|
|
* but packets are still queued for delivery
|
|
|
|
*/
|
2018-02-13 02:24:00 +01:00
|
|
|
int closing = 0;
|
2015-09-16 01:27:09 +02:00
|
|
|
|
|
|
|
// flag: set when the socket failed to write, so the socket will not wait to
|
|
|
|
// write packets and close directly.
|
2018-02-13 02:24:00 +01:00
|
|
|
bool has_write_error = 0;
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2017-09-12 22:23:33 +02:00
|
|
|
/* flag: quit adbd when both ends close the
|
|
|
|
* local service socket
|
|
|
|
*/
|
2018-02-13 02:24:00 +01:00
|
|
|
int exit_on_close = 0;
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2017-09-12 22:23:33 +02:00
|
|
|
// the asocket we are connected to
|
2018-02-13 02:24:00 +01:00
|
|
|
asocket* peer = nullptr;
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2017-09-12 22:23:33 +02:00
|
|
|
/* For local asockets, the fde is used to bind
|
|
|
|
* us to our fd event system. For remote asockets
|
|
|
|
* these fields are not used.
|
|
|
|
*/
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent* fde = nullptr;
|
|
|
|
int fd = -1;
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
// queue of data waiting to be written
|
2018-04-03 23:37:11 +02:00
|
|
|
IOVector packet_queue;
|
2018-02-01 22:17:50 +01:00
|
|
|
|
|
|
|
std::string smart_socket_data;
|
2017-09-12 22:23:33 +02:00
|
|
|
|
|
|
|
/* enqueue is called by our peer when it has data
|
|
|
|
* for us. It should return 0 if we can accept more
|
|
|
|
* data or 1 if not. If we return 1, we must call
|
|
|
|
* peer->ready() when we once again are ready to
|
|
|
|
* receive data.
|
|
|
|
*/
|
2018-03-08 01:52:28 +01:00
|
|
|
int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr;
|
2017-09-12 22:23:33 +02:00
|
|
|
|
|
|
|
/* ready is called by the peer when it is ready for
|
|
|
|
* us to send data via enqueue again
|
|
|
|
*/
|
2018-02-13 02:24:00 +01:00
|
|
|
void (*ready)(asocket* s) = nullptr;
|
2017-09-12 22:23:33 +02:00
|
|
|
|
|
|
|
/* shutdown is called by the peer before it goes away.
|
|
|
|
* the socket should not do any further calls on its peer.
|
|
|
|
* Always followed by a call to close. Optional, i.e. can be NULL.
|
|
|
|
*/
|
2018-02-13 02:24:00 +01:00
|
|
|
void (*shutdown)(asocket* s) = nullptr;
|
2017-09-12 22:23:33 +02:00
|
|
|
|
|
|
|
/* close is called by the peer when it has gone away.
|
|
|
|
* we are not allowed to make any further calls on the
|
|
|
|
* peer once our close method is called.
|
|
|
|
*/
|
2018-02-13 02:24:00 +01:00
|
|
|
void (*close)(asocket* s) = nullptr;
|
2017-09-12 22:23:33 +02:00
|
|
|
|
|
|
|
/* A socket is bound to atransport */
|
2018-02-13 02:24:00 +01:00
|
|
|
atransport* transport = nullptr;
|
2015-09-16 01:27:09 +02:00
|
|
|
|
|
|
|
size_t get_max_payload() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
asocket *find_local_socket(unsigned local_id, unsigned remote_id);
|
|
|
|
void install_local_socket(asocket *s);
|
|
|
|
void remove_socket(asocket *s);
|
|
|
|
void close_all_sockets(atransport *t);
|
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* create_local_socket(unique_fd fd);
|
2018-12-13 23:21:00 +01:00
|
|
|
asocket* create_local_service_socket(std::string_view destination, atransport* transport);
|
2015-09-16 01:27:09 +02:00
|
|
|
|
|
|
|
asocket *create_remote_socket(unsigned id, atransport *t);
|
2018-12-21 02:00:13 +01:00
|
|
|
void connect_to_remote(asocket* s, std::string_view destination);
|
2015-09-16 01:27:09 +02:00
|
|
|
void connect_to_smartsocket(asocket *s);
|
|
|
|
|
2016-03-01 17:58:26 +01:00
|
|
|
// Internal functions that are only made available here for testing purposes.
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2018-12-19 22:37:41 +01:00
|
|
|
bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
|
|
|
|
std::string_view service);
|
2016-03-01 17:58:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2015-09-16 01:27:09 +02:00
|
|
|
#endif // __ADB_SOCKET_H
|