Skip to content

Commit

Permalink
Add commented out aria2 WebSocket RPC implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed Apr 14, 2024
1 parent e141e59 commit 9f234af
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
124 changes: 124 additions & 0 deletions NanaGet/NanaGetCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ namespace NanaGet

namespace winrt
{
//using Windows::Networking::Sockets::MessageWebSocketReceiveMode;
//using Windows::Networking::Sockets::SocketMessageType;
using Windows::Storage::ApplicationData;
//using Windows::Storage::Streams::Buffer;
//using Windows::Storage::Streams::DataReader;
using Windows::Storage::Streams::IBuffer;
//using Windows::Storage::Streams::IInputStream;
//using Windows::Storage::Streams::InputStreamOptions;
using Windows::Web::Http::HttpResponseMessage;
using Windows::Web::Http::HttpStringContent;
}
Expand Down Expand Up @@ -573,8 +579,126 @@ void NanaGet::Aria2Instance::UpdateInstance(
{
this->m_ServerUri = ServerUri;
this->m_ServerToken = ServerToken;

/*this->CloseMessageWebSocket();
if (!this->m_ServerUri)
{
return;
}
this->m_MessageWebSocket = winrt::MessageWebSocket();
this->m_MessageWebSocket.Control().MessageType(
winrt::SocketMessageType::Utf8);
this->m_MessageWebSocket.Control().ReceiveMode(
winrt::MessageWebSocketReceiveMode::FullMessage);
this->m_MessageWebSocket.MessageReceived(
{ this, &NanaGet::Aria2Instance::MessageWebSocketMessageReceived });
this->m_MessageWebSocket.Closed(
{ this, &NanaGet::Aria2Instance::WebSocketClosed });
this->m_MessageWebSocket.ConnectAsync(
winrt::Uri(winrt::hstring(Mile::FormatWideString(
L"ws://%s:%d/jsonrpc",
this->ServerUri().Host().c_str(),
this->ServerUri().Port())))).get();*/
}

//void NanaGet::Aria2Instance::CloseMessageWebSocket()
//{
// if (this->m_MessageWebSocket)
// {
// this->m_MessageWebSocket.Close();
// this->m_MessageWebSocket = nullptr;
// }
//}
//
//std::string ReadFromInputStream(
// winrt::IInputStream const& Stream)
//{
// std::string Result;
//
// winrt::Buffer ResultBuffer(4096);
// do
// {
// Stream.ReadAsync(ResultBuffer,
// ResultBuffer.Capacity(),
// winrt::InputStreamOptions::Partial).get();
// Result += std::string(
// reinterpret_cast<char*>(ResultBuffer.data()),
// ResultBuffer.Length());
// } while (ResultBuffer.Length() > 0);
//
// return Result;
//}
//
//void NanaGet::Aria2Instance::MessageWebSocketMessageReceived(
// winrt::MessageWebSocket const& sender,
// winrt::MessageWebSocketMessageReceivedEventArgs const& e)
//{
// UNREFERENCED_PARAMETER(sender);
//
// std::string Message;
// try
// {
// Message = ::ReadFromInputStream(e.GetDataStream());
// }
// catch (...)
// {
// this->CloseMessageWebSocket();
// return;
// }
//
// try
// {
//
// }
// catch (const std::exception&)
// {
//
// }
//
//
// /*nlohmann::json ResponseJson;
// try
// {
// ResponseJson = nlohmann::json::parse(ResponseString);
// }
// catch (std::exception const& ex)
// {
// throw winrt::hresult_illegal_method_call(winrt::to_hstring(ex.what()));
// }
//
// if ("2.0" != ResponseJson["jsonrpc"].get<std::string>() ||
// Identifier != ResponseJson["id"].get<std::string>())
// {
// throw winrt::hresult_illegal_method_call();
// }
//
// if (ResponseJson.end() != ResponseJson.find("error"))
// {
// throw winrt::hresult_illegal_method_call(
// winrt::to_hstring(ResponseJson["error"].get<std::string>()));
// }
//ResponseJson["result"].dump(2);*/
//
//
// ::OutputDebugStringW(Mile::FormatWideString(
// L"\r\n\r\n%s\r\n\r\n",
// winrt::to_hstring(Message).c_str()).c_str());
//}
//
//void NanaGet::Aria2Instance::WebSocketClosed(
// winrt::IWebSocket const& sender,
// winrt::WebSocketClosedEventArgs const& e)
//{
// UNREFERENCED_PARAMETER(sender);
// UNREFERENCED_PARAMETER(e);
//
// this->CloseMessageWebSocket();
//}

NanaGet::Aria2UriInformation NanaGet::Aria2Instance::ParseUriInformation(
nlohmann::json const& Value)
{
Expand Down
25 changes: 25 additions & 0 deletions NanaGet/NanaGetCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
#include <Mile.Helpers.CppWinRT.h>

#include <winrt/Windows.Foundation.h>
//#include <winrt/Windows.Networking.Sockets.h>
#include <winrt/Windows.Web.Http.h>

#include <filesystem>
//#include <map>
#include <set>

#include <json.hpp>
Expand All @@ -36,6 +38,16 @@ namespace winrt
using Windows::Web::Http::HttpClient;
}

//namespace winrt
//{
// namespace WNS = Windows::Networking::Sockets;
//
// using WNS::IWebSocket;
// using WNS::MessageWebSocket;
// using WNS::MessageWebSocketMessageReceivedEventArgs;
// using WNS::WebSocketClosedEventArgs;
//}

namespace NanaGet
{
std::filesystem::path GetApplicationFolderPath();
Expand Down Expand Up @@ -186,6 +198,19 @@ namespace NanaGet
winrt::Uri m_ServerUri = nullptr;
std::string m_ServerToken;

/*winrt::MessageWebSocket m_MessageWebSocket = nullptr;
std::map<std::string, std::string> m_MessageResults;
void CloseMessageWebSocket();
void MessageWebSocketMessageReceived(
winrt::MessageWebSocket const& sender,
winrt::MessageWebSocketMessageReceivedEventArgs const& e);
void WebSocketClosed(
winrt::IWebSocket const& sender,
winrt::WebSocketClosedEventArgs const& e);*/

winrt::HttpClient m_HttpClient;

winrt::slim_mutex m_InstanceLock;
Expand Down

0 comments on commit 9f234af

Please sign in to comment.