htc: implement much of worker receive logic

This commit is contained in:
Michael Scire
2021-02-08 14:11:01 -08:00
committed by SciresM
parent 8f85cc17dc
commit 679fec2ddc
19 changed files with 565 additions and 12 deletions

View File

@@ -22,4 +22,76 @@ namespace ams::htclow {
PacketDeleter{m_allocator}(packet);
}
std::unique_ptr<Packet, PacketDeleter> PacketFactory::MakeSendPacketCommon(impl::ChannelInternalType channel, s16 version, int body_size) {
/* Allocate memory for the packet. */
if (void *buffer = m_allocator->Allocate(sizeof(Packet), alignof(Packet)); buffer != nullptr) {
/* Convert the buffer to a packet. */
Packet *packet = static_cast<Packet *>(buffer);
/* Construct the packet. */
std::construct_at(packet, m_allocator, body_size + sizeof(PacketHeader));
/* Create the unique pointer. */
std::unique_ptr<Packet, PacketDeleter> ptr(packet, PacketDeleter{m_allocator});
/* Set packet header fields. */
if (ptr && ptr->IsAllocationSucceeded()) {
PacketHeader *header = ptr->GetHeader();
header->signature = HtcGen2Signature;
header->offset = 0;
header->reserved = 0;
header->body_size = body_size;
header->version = version;
header->channel = channel;
header->share = 0;
}
return ptr;
} else {
return std::unique_ptr<Packet, PacketDeleter>(nullptr, PacketDeleter{m_allocator});
}
}
std::unique_ptr<Packet, PacketDeleter> PacketFactory::MakeDataPacket(impl::ChannelInternalType channel, s16 version, const void *body, int body_size, u64 share, u32 offset) {
auto packet = this->MakeSendPacketCommon(channel, version, body_size);
if (packet) {
PacketHeader *header = packet->GetHeader();
header->packet_type = PacketType_Data;
header->offset = offset;
header->share = share;
packet->CopyBody(body, body_size);
AMS_ASSERT(packet->GetBodySize() == body_size);
}
return packet;
}
std::unique_ptr<Packet, PacketDeleter> PacketFactory::MakeMaxDataPacket(impl::ChannelInternalType channel, s16 version, u64 share) {
auto packet = this->MakeSendPacketCommon(channel, version, 0);
if (packet) {
PacketHeader *header = packet->GetHeader();
header->packet_type = PacketType_MaxData;
header->share = share;
}
return packet;
}
std::unique_ptr<Packet, PacketDeleter> PacketFactory::MakeErrorPacket(impl::ChannelInternalType channel) {
auto packet = this->MakeSendPacketCommon(channel, 0, 0);
if (packet) {
PacketHeader *header = packet->GetHeader();
header->packet_type = PacketType_Error;
}
return packet;
}
}