[SteamCallbacks] Transmit callbacks from the worker to the client

This commit is contained in:
momo5502 2017-01-28 22:01:49 +01:00
parent 07469816bb
commit 4271e3b108
8 changed files with 115 additions and 16 deletions

View File

@ -192,18 +192,30 @@ namespace Components
{
Friends::UpdateFriends();
auto fInterface = IPCHandler::NewInterface("steamCallbacks");
// Callback to update user information
Steam::Proxy::RegisterCallback(336, [](void* data)
fInterface->map("304", [](std::vector<std::string> params)
{
Friends::FriendRichPresenceUpdate* update = static_cast<Friends::FriendRichPresenceUpdate*>(data);
Friends::UpdateUserInfo(update->m_steamIDFriend);
if (params.size() >= 1 && params[0].size() == sizeof(Friends::FriendRichPresenceUpdate))
{
const Friends::FriendRichPresenceUpdate* update = reinterpret_cast<const Friends::FriendRichPresenceUpdate*>(params[0].data());
Friends::UpdateUserInfo(update->m_steamIDFriend);
}
});
// Persona state has changed
Steam::Proxy::RegisterCallback(304, [](void* data)
fInterface->map("304", [](std::vector<std::string> params)
{
Friends::PersonaStateChange* state = static_cast<Friends::PersonaStateChange*>(data);
if(Steam::Proxy::SteamFriends) Steam::Proxy::SteamFriends->RequestFriendRichPresence(state->m_ulSteamID);
if(params.size() >= 1 && params[0].size() == sizeof(Friends::PersonaStateChange))
{
const Friends::PersonaStateChange* state = reinterpret_cast<const Friends::PersonaStateChange*>(params[0].data());
Proto::IPC::Function function;
function.set_name("requestPresence");
*function.add_params() = Utils::String::VA("%llx", state->m_ulSteamID.Bits);
IPCHandler::SendWorker("friends", function.SerializeAsString());
}
});
UIScript::Add("LoadFriends", [](UIScript::Token)
@ -213,16 +225,13 @@ namespace Components
UIFeeder::Add(6.0f, Friends::GetFriendCount, Friends::GetFriendText, Friends::SelectFriend);
auto fInterface = IPCHandler::NewInterface("friends");
fInterface = IPCHandler::NewInterface("friends");
fInterface->map("friendsResponse", Friends::FriendsResponse);
fInterface->map("nameResponse", Friends::NameResponse);
}
Friends::~Friends()
{
Steam::Proxy::UnregisterCallback(304);
Steam::Proxy::UnregisterCallback(336);
{
std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
Friends::FriendsList.clear();

View File

@ -82,7 +82,7 @@ namespace Steam
Proxy::Callbacks.erase(callId);
}
void Proxy::RunCallback(int32_t callId, void* data)
void Proxy::RunCallback(int32_t callId, void* data, size_t size)
{
std::lock_guard<std::recursive_mutex> _(Proxy::CallMutex);
@ -91,6 +91,11 @@ namespace Steam
{
::Utils::Hook::Call<void(void*)>(callback->second)(data);
}
if(Worker::IsWorker())
{
Handlers::SteamCallbacks::HandleCallback(callId, data, size);
}
}
void Proxy::RunFrame()
@ -110,7 +115,7 @@ namespace Steam
#endif
//Steam::Callbacks::RunCallback(message.m_iCallback, message.m_pubParam);
Proxy::RunCallback(message.m_iCallback, message.m_pubParam);
Proxy::RunCallback(message.m_iCallback, message.m_pubParam, message.m_cubParam);
Proxy::SteamFreeLastCallback(Proxy::SteamPipe);
}
@ -151,7 +156,7 @@ namespace Steam
continue;
}
Proxy::RunCallback(call.callId, buffer);
Proxy::RunCallback(call.callId, buffer, call.dataSize);
}
}
}

View File

@ -396,7 +396,7 @@ namespace Steam
static std::function<SteamFreeLastCallbackFn> SteamFreeLastCallback;
static std::function<SteamGetAPICallResultFn> SteamGetAPICallResult;
static void RunCallback(int32_t callId, void* data);
static void RunCallback(int32_t callId, void* data, size_t size);
static void UnregisterCalls();

View File

@ -92,6 +92,7 @@ namespace Handlers
Proto::IPC::Function response;
response.set_name("presenceResponse");
*response.add_params() = Utils::String::VA("%llX", id.Bits);
*response.add_params() = params[1].data();
*response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data());
endpoint.send(this->getCommand(), response.SerializeAsString());

View File

@ -0,0 +1,55 @@
#include "STDInclude.hpp"
namespace Handlers
{
void SteamCallbacks::handle(Worker::Endpoint endpoint, std::string data)
{
Proto::IPC::Function function;
if (function.ParseFromString(data))
{
auto handler = this->functions.find(function.name());
if (handler != this->functions.end())
{
printf("Handing function %s\n", function.name().data());
auto params = function.params();
handler->second(endpoint, std::vector<std::string>(params.begin(), params.end()));
}
else
{
printf("No handler for function %s\n", function.name().data());
}
}
}
void SteamCallbacks::addFunction(std::string function, Friends::Callback callback)
{
this->functions[function] = callback;
}
void SteamCallbacks::HandleCallback(int32_t callId, void* data, size_t size)
{
if(Worker::Runner::Channel)
{
Proto::IPC::Function response;
response.set_name(Utils::String::VA("%d", callId));
response.add_params()->append(static_cast<char*>(data), size);
Proto::IPC::Command command;
command.set_name(SteamCallbacks().getCommand());
command.set_data(response.SerializeAsString());
Worker::Runner::Channel->send(command.SerializeAsString());
}
}
SteamCallbacks::SteamCallbacks()
{
}
SteamCallbacks::~SteamCallbacks()
{
}
}

View File

@ -0,0 +1,22 @@
#pragma once
namespace Handlers
{
class SteamCallbacks : public Worker::Runner::Handler
{
public:
typedef std::function<void(Worker::Endpoint, std::vector<std::string>)> Callback;
SteamCallbacks();
~SteamCallbacks();
std::string getCommand() override { return "steamCallbacks"; };
void handle(Worker::Endpoint endpoint, std::string data) override;
static void HandleCallback(int32_t callId, void* data, size_t size);
private:
std::unordered_map<std::string, Callback> functions;
void addFunction(std::string function, Callback callback);
};
}

View File

@ -2,14 +2,16 @@
namespace Worker
{
Utils::IPC::BidirectionalChannel* Runner::Channel;
Runner::Runner(int pid) : processId(pid), terminate(false)
{
Runner::Channel = nullptr;
}
Runner::~Runner()
{
Runner::Channel = nullptr;
}
void Runner::run()
@ -46,6 +48,7 @@ namespace Worker
{
printf("Worker started\n");
Utils::IPC::BidirectionalChannel channel("IW4x-Worker-Channel", !Worker::IsWorker());
Runner::Channel = &channel;
while (!this->terminate)
{
@ -74,5 +77,6 @@ namespace Worker
}
printf("Terminating worker\n");
Runner::Channel = nullptr;
}
}

View File

@ -43,6 +43,8 @@ namespace Worker
void attachHandler(Runner::Handler* handler);
static Utils::IPC::BidirectionalChannel* Channel;
private:
void worker();
@ -53,3 +55,4 @@ namespace Worker
}
#include "Handlers/Friends.hpp"
#include "Handlers/SteamCallbacks.hpp"