2021-04-04 21:04:12 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
2021-06-22 23:52:31 +01:00
|
|
|
#include <utility>
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <vector>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <arpa/inet.h>
|
2022-03-28 01:04:14 +01:00
|
|
|
#include <chrono>
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
#include "src/Helpers/EventNotifier.hpp"
|
2022-03-28 01:04:14 +01:00
|
|
|
#include "src/Helpers/EpollInstance.hpp"
|
2022-03-27 19:43:20 +01:00
|
|
|
|
|
|
|
|
#include "src/DebugServers/GdbRsp/Packet.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
#include "src/DebugServers/GdbRsp/ResponsePackets/ResponsePacket.hpp"
|
|
|
|
|
|
|
|
|
|
namespace Bloom::DebugServers::Gdb
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The Connection class represents an active connection between the GDB RSP server and client.
|
|
|
|
|
*/
|
|
|
|
|
class Connection
|
|
|
|
|
{
|
|
|
|
|
public:
|
2022-03-27 18:33:34 +01:00
|
|
|
explicit Connection(EventNotifier& interruptEventNotifier)
|
|
|
|
|
: interruptEventNotifier(interruptEventNotifier)
|
2022-03-24 19:17:41 +00:00
|
|
|
{};
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-03-28 01:04:14 +01:00
|
|
|
Connection() = delete;
|
|
|
|
|
Connection(const Connection&) = delete;
|
|
|
|
|
Connection(Connection&& other) noexcept
|
|
|
|
|
: interruptEventNotifier(other.interruptEventNotifier)
|
|
|
|
|
, socketFileDescriptor(other.socketFileDescriptor)
|
|
|
|
|
, epollInstance(std::move(other.epollInstance))
|
|
|
|
|
, readInterruptEnabled(other.readInterruptEnabled)
|
|
|
|
|
{
|
|
|
|
|
other.socketFileDescriptor = -1;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
/**
|
|
|
|
|
* Accepts a connection on serverSocketFileDescriptor.
|
|
|
|
|
*
|
|
|
|
|
* @param serverSocketFileDescriptor
|
|
|
|
|
*/
|
|
|
|
|
void accept(int serverSocketFileDescriptor);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Closes the connection with the client.
|
|
|
|
|
*/
|
|
|
|
|
void close() noexcept;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Obtains the human readable IP address of the connected client.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
std::string getIpAddress() {
|
2021-06-22 23:52:31 +01:00
|
|
|
std::array<char, INET_ADDRSTRLEN> ipAddress = {};
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (::inet_ntop(AF_INET, &(socketAddress.sin_addr), ipAddress.data(), INET_ADDRSTRLEN) == nullptr) {
|
|
|
|
|
throw Exceptions::Exception("Failed to convert client IP address to text form.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::string(ipAddress.data());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-27 19:43:20 +01:00
|
|
|
* Waits for incoming data from the client and returns the raw GDB packets.
|
2021-04-04 21:04:12 +01:00
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2022-03-27 19:43:20 +01:00
|
|
|
std::vector<RawPacketType> readRawPackets();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a response packet to the client.
|
|
|
|
|
*
|
|
|
|
|
* @param packet
|
|
|
|
|
*/
|
2021-05-24 20:58:49 +01:00
|
|
|
void writePacket(const ResponsePackets::ResponsePacket& packet);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2021-06-22 23:52:31 +01:00
|
|
|
[[nodiscard]] int getMaxPacketSize() const {
|
2021-04-04 21:04:12 +01:00
|
|
|
return this->maxPacketSize;
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int socketFileDescriptor = -1;
|
2022-03-28 01:04:14 +01:00
|
|
|
|
|
|
|
|
EpollInstance epollInstance = EpollInstance();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
|
|
|
|
struct sockaddr_in socketAddress = {};
|
|
|
|
|
int maxPacketSize = 1024;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The interruptEventNotifier allows us to interrupt blocking IO calls on the GDB debug server.
|
|
|
|
|
* Under the hood, this is just a wrapper for a Linux event notifier. See the EventNotifier class for more.
|
|
|
|
|
*/
|
2022-03-27 18:33:34 +01:00
|
|
|
EventNotifier& interruptEventNotifier;
|
2021-10-06 21:12:31 +01:00
|
|
|
bool readInterruptEnabled = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads data from the client into a raw buffer.
|
|
|
|
|
*
|
|
|
|
|
* @param bytes
|
|
|
|
|
* Number of bytes to read.
|
|
|
|
|
*
|
|
|
|
|
* @param interruptible
|
|
|
|
|
* If this flag is set to false, no other component within Bloom will be able to gracefully interrupt
|
|
|
|
|
* the read (via means of this->interruptEventNotifier). This flag has no effect if this->readInterruptEnabled
|
|
|
|
|
* is false.
|
|
|
|
|
*
|
2022-03-28 01:04:14 +01:00
|
|
|
* @param timeout
|
2021-10-06 21:12:31 +01:00
|
|
|
* The timeout in milliseconds. If not supplied, no timeout will be applied.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2022-03-27 18:34:08 +01:00
|
|
|
std::vector<unsigned char> read(
|
|
|
|
|
std::size_t bytes = 0,
|
|
|
|
|
bool interruptible = true,
|
2022-03-28 01:04:14 +01:00
|
|
|
std::optional<std::chrono::milliseconds> timeout = std::nullopt
|
2022-03-27 18:34:08 +01:00
|
|
|
);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does the same as Connection::read(), but only reads a single byte.
|
|
|
|
|
*
|
|
|
|
|
* @param interruptible
|
|
|
|
|
* See Connection::read().
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
std::optional<unsigned char> readSingleByte(bool interruptible = true);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Writes data from a raw buffer to the client connection.
|
|
|
|
|
*
|
|
|
|
|
* @param buffer
|
|
|
|
|
*/
|
|
|
|
|
void write(const std::vector<unsigned char>& buffer);
|
|
|
|
|
|
|
|
|
|
void disableReadInterrupts();
|
|
|
|
|
|
|
|
|
|
void enableReadInterrupts();
|
2021-04-04 21:04:12 +01:00
|
|
|
};
|
|
|
|
|
}
|