Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 27cb7dca authored by Josh Gao's avatar Josh Gao
Browse files

adb: switch asocket::enqueue to std::string.

Switch asocket over to taking a std::string instead of apacket* for
data. This allows us to remove asocket specific fields from apacket*.

Test: python test_device.py with x86_64 emulator, walleye
Test: adb_test on host
Change-Id: I9d157ff331a75ba49a54fdd4194e3f6cdff722f4
parent 5caaebdc
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -474,13 +474,14 @@ void handle_packet(apacket *p, atransport *t)
            asocket* s = find_local_socket(p->msg.arg1, p->msg.arg0);
            asocket* s = find_local_socket(p->msg.arg1, p->msg.arg0);
            if (s) {
            if (s) {
                unsigned rid = p->msg.arg0;
                unsigned rid = p->msg.arg0;
                p->len = p->msg.data_length;


                if (s->enqueue(s, p) == 0) {
                // TODO: Convert apacket::data to a type that we can move out of.
                std::string copy(p->data, p->data + p->msg.data_length);

                if (s->enqueue(s, std::move(copy)) == 0) {
                    D("Enqueue the socket");
                    D("Enqueue the socket");
                    send_ready(s->id, rid, t);
                    send_ready(s->id, rid, t);
                }
                }
                return;
            }
            }
        }
        }
        break;
        break;
+0 −5
Original line number Original line Diff line number Diff line
@@ -73,11 +73,6 @@ struct amessage {
};
};


struct apacket {
struct apacket {
    apacket* next;

    size_t len;
    char* ptr;

    amessage msg;
    amessage msg;
    char data[MAX_PAYLOAD];
    char data[MAX_PAYLOAD];
};
};
+15 −17
Original line number Original line Diff line number Diff line
@@ -470,10 +470,9 @@ static void jdwp_socket_close(asocket* s) {
    free(s);
    free(s);
}
}


static int jdwp_socket_enqueue(asocket* s, apacket* p) {
static int jdwp_socket_enqueue(asocket* s, std::string) {
    /* you can't write to this asocket */
    /* you can't write to this asocket */
    D("LS(%d): JDWP socket received data?", s->id);
    D("LS(%d): JDWP socket received data?", s->id);
    put_apacket(p);
    s->peer->close(s->peer);
    s->peer->close(s->peer);
    return -1;
    return -1;
}
}
@@ -486,9 +485,11 @@ static void jdwp_socket_ready(asocket* s) {
     * on the second one, close the connection
     * on the second one, close the connection
     */
     */
    if (!jdwp->pass) {
    if (!jdwp->pass) {
        apacket* p = get_apacket();
        std::string data;
        p->len = jdwp_process_list((char*)p->data, s->get_max_payload());
        data.resize(s->get_max_payload());
        peer->enqueue(peer, p);
        size_t len = jdwp_process_list(&data[0], data.size());
        data.resize(len);
        peer->enqueue(peer, std::move(data));
        jdwp->pass = true;
        jdwp->pass = true;
    } else {
    } else {
        peer->close(peer);
        peer->close(peer);
@@ -524,17 +525,14 @@ struct JdwpTracker : public asocket {
static std::vector<std::unique_ptr<JdwpTracker>> _jdwp_trackers;
static std::vector<std::unique_ptr<JdwpTracker>> _jdwp_trackers;


static void jdwp_process_list_updated(void) {
static void jdwp_process_list_updated(void) {
    char buffer[1024];
    std::string data;
    int len = jdwp_process_list_msg(buffer, sizeof(buffer));
    data.resize(1024);
    data.resize(jdwp_process_list_msg(&data[0], data.size()));


    for (auto& t : _jdwp_trackers) {
    for (auto& t : _jdwp_trackers) {
        apacket* p = get_apacket();
        memcpy(p->data, buffer, len);
        p->len = len;

        if (t->peer) {
        if (t->peer) {
            // The tracker might not have been connected yet.
            // The tracker might not have been connected yet.
            t->peer->enqueue(t->peer, p);
            t->peer->enqueue(t->peer, data);
        }
        }
    }
    }
}
}
@@ -560,17 +558,17 @@ static void jdwp_tracker_ready(asocket* s) {
    JdwpTracker* t = (JdwpTracker*)s;
    JdwpTracker* t = (JdwpTracker*)s;


    if (t->need_initial) {
    if (t->need_initial) {
        apacket* p = get_apacket();
        std::string data;
        data.resize(s->get_max_payload());
        data.resize(jdwp_process_list_msg(&data[0], data.size()));
        t->need_initial = false;
        t->need_initial = false;
        p->len = jdwp_process_list_msg((char*)p->data, s->get_max_payload());
        s->peer->enqueue(s->peer, std::move(data));
        s->peer->enqueue(s->peer, p);
    }
    }
}
}


static int jdwp_tracker_enqueue(asocket* s, apacket* p) {
static int jdwp_tracker_enqueue(asocket* s, std::string) {
    /* you can't write to this socket */
    /* you can't write to this socket */
    D("LS(%d): JDWP tracker received data?", s->id);
    D("LS(%d): JDWP tracker received data?", s->id);
    put_apacket(p);
    s->peer->close(s->peer);
    s->peer->close(s->peer);
    return -1;
    return -1;
}
}

adb/range.h

0 → 100644
+65 −0
Original line number Original line Diff line number Diff line
#pragma once

/*
 * Copyright (C) 2018 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.
 */

#include <string>

#include <android-base/logging.h>

struct Range {
    explicit Range(std::string data) : data_(std::move(data)) {}

    Range(const Range& copy) = delete;
    Range& operator=(const Range& copy) = delete;

    Range(Range&& move) = default;
    Range& operator=(Range&& move) = default;

    bool empty() const {
        return size() == 0;
    }

    size_t size() const {
        return data_.size() - begin_offset_ - end_offset_;
    };

    void drop_front(size_t n) {
        CHECK_GE(size(), n);
        begin_offset_ += n;
    }

    void drop_end(size_t n) {
        CHECK_GE(size(), n);
        end_offset_ += n;
    }

    char* data() {
        return &data_[0] + begin_offset_;
    }

    std::string::iterator begin() {
        return data_.begin() + begin_offset_;
    }

    std::string::iterator end() {
        return data_.end() - end_offset_;
    }

    std::string data_;
    size_t begin_offset_ = 0;
    size_t end_offset_ = 0;
};
+8 −4
Original line number Original line Diff line number Diff line
@@ -19,9 +19,12 @@


#include <stddef.h>
#include <stddef.h>


#include <deque>
#include <memory>
#include <memory>
#include <string>


#include "fdevent.h"
#include "fdevent.h"
#include "range.h"


struct apacket;
struct apacket;
class atransport;
class atransport;
@@ -59,9 +62,10 @@ struct asocket {
    fdevent fde;
    fdevent fde;
    int fd;
    int fd;


    // queue of apackets waiting to be written
    // queue of data waiting to be written
    apacket* pkt_first;
    std::deque<Range> packet_queue;
    apacket* pkt_last;

    std::string smart_socket_data;


    /* enqueue is called by our peer when it has data
    /* enqueue is called by our peer when it has data
     * for us.  It should return 0 if we can accept more
     * for us.  It should return 0 if we can accept more
@@ -69,7 +73,7 @@ struct asocket {
     * peer->ready() when we once again are ready to
     * peer->ready() when we once again are ready to
     * receive data.
     * receive data.
     */
     */
    int (*enqueue)(asocket* s, apacket* pkt);
    int (*enqueue)(asocket* s, std::string data);


    /* ready is called by the peer when it is ready for
    /* ready is called by the peer when it is ready for
     * us to send data via enqueue again
     * us to send data via enqueue again
Loading