Skip to content

Commit

Permalink
Add Mile::Cirno::Client::RequestAsync.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed Nov 10, 2024
1 parent b21ebd5 commit 21b0a0e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
68 changes: 42 additions & 26 deletions Mile.Cirno/Mile.Cirno.Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,19 @@ void Mile::Cirno::Client::FreeFileId(
}
}

void Mile::Cirno::Client::Request(
MILE_CIRNO_MESSAGE_TYPE const& RequestType,
std::vector<std::uint8_t> const& RequestContent,
MILE_CIRNO_MESSAGE_TYPE const& ResponseType,
std::vector<std::uint8_t>& ResponseContent)
std::uint16_t Mile::Cirno::Client::RequestAsync(
MILE_CIRNO_MESSAGE_TYPE const& Type,
std::vector<std::uint8_t> const& Content)
{
if (!this->m_ReceiveWorkerStarted)
{
return;
Mile::Cirno::ThrowException(
"!this->m_ReceiveWorkerStarted",
ERROR_NOT_SUPPORTED);
}

std::uint16_t Tag = MILE_CIRNO_NOTAG;
if (MileCirnoVersionRequestMessage != RequestType)
if (MileCirnoVersionRequestMessage != Type)
{
Tag = this->AllocateTag();
if (MILE_CIRNO_NOTAG == Tag)
Expand All @@ -224,47 +224,63 @@ void Mile::Cirno::Client::Request(
ERROR_INVALID_DATA);
}
}
auto TagCleanupHandler = Mile::ScopeExitTaskHandler([&]()
{
if (MILE_CIRNO_NOTAG != Tag)
{
this->FreeTag(Tag);
}
});

Mile::Cirno::Header RequestHeader;
RequestHeader.Size = static_cast<std::uint32_t>(RequestContent.size());
RequestHeader.Type = static_cast<std::uint8_t>(RequestType);
RequestHeader.Tag = Tag;
std::vector<std::uint8_t> RequestHeaderBuffer;
Mile::Cirno::PushHeader(RequestHeaderBuffer, RequestHeader);
Mile::Cirno::Header Header;
Header.Size = static_cast<std::uint32_t>(Content.size());
Header.Type = static_cast<std::uint8_t>(Type);
Header.Tag = Tag;
std::vector<std::uint8_t> HeaderBuffer;
Mile::Cirno::PushHeader(HeaderBuffer, Header);
{
std::lock_guard<std::mutex> Guard(this->m_SendOperationMutex);
DWORD NumberOfBytesSent = 0;
if (!::MileSocketSend(
this->m_Socket,
&RequestHeaderBuffer[0],
static_cast<DWORD>(RequestHeaderBuffer.size()),
&HeaderBuffer[0],
static_cast<DWORD>(HeaderBuffer.size()),
&NumberOfBytesSent,
0))
{
Mile::Cirno::ThrowException(
"MileSocketSend(RequestHeaderBuffer)",
"MileSocketSend(HeaderBuffer)",
::WSAGetLastError());
}
if (!::MileSocketSend(
this->m_Socket,
&RequestContent[0],
static_cast<DWORD>(RequestContent.size()),
&Content[0],
static_cast<DWORD>(Content.size()),
&NumberOfBytesSent,
0))
{
Mile::Cirno::ThrowException(
"MileSocketSend(RequestContent)",
"MileSocketSend(Content)",
::WSAGetLastError());
}
}

return Tag;
}

void Mile::Cirno::Client::Request(
MILE_CIRNO_MESSAGE_TYPE const& RequestType,
std::vector<std::uint8_t> const& RequestContent,
MILE_CIRNO_MESSAGE_TYPE const& ResponseType,
std::vector<std::uint8_t>& ResponseContent)
{
if (!this->m_ReceiveWorkerStarted)
{
return;
}

std::uint16_t Tag = this->RequestAsync(RequestType, RequestContent);
auto TagCleanupHandler = Mile::ScopeExitTaskHandler([&]()
{
if (MILE_CIRNO_NOTAG != Tag)
{
this->FreeTag(Tag);
}
});

std::vector<std::uint8_t> Content;

for (;;)
Expand Down
4 changes: 4 additions & 0 deletions Mile.Cirno/Mile.Cirno.Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ namespace Mile::Cirno
void FreeFileId(
std::uint32_t const& FileId);

std::uint16_t RequestAsync(
MILE_CIRNO_MESSAGE_TYPE const& Type,
std::vector<std::uint8_t> const& Content);

void Request(
MILE_CIRNO_MESSAGE_TYPE const& RequestType,
std::vector<std::uint8_t> const& RequestContent,
Expand Down

0 comments on commit 21b0a0e

Please sign in to comment.