[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(); Friends::UpdateFriends();
auto fInterface = IPCHandler::NewInterface("steamCallbacks");
// Callback to update user information // 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); if (params.size() >= 1 && params[0].size() == sizeof(Friends::FriendRichPresenceUpdate))
Friends::UpdateUserInfo(update->m_steamIDFriend); {
const Friends::FriendRichPresenceUpdate* update = reinterpret_cast<const Friends::FriendRichPresenceUpdate*>(params[0].data());
Friends::UpdateUserInfo(update->m_steamIDFriend);
}
}); });
// Persona state has changed // 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(params.size() >= 1 && params[0].size() == sizeof(Friends::PersonaStateChange))
if(Steam::Proxy::SteamFriends) Steam::Proxy::SteamFriends->RequestFriendRichPresence(state->m_ulSteamID); {
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) UIScript::Add("LoadFriends", [](UIScript::Token)
@ -213,16 +225,13 @@ namespace Components
UIFeeder::Add(6.0f, Friends::GetFriendCount, Friends::GetFriendText, Friends::SelectFriend); 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("friendsResponse", Friends::FriendsResponse);
fInterface->map("nameResponse", Friends::NameResponse); fInterface->map("nameResponse", Friends::NameResponse);
} }
Friends::~Friends() Friends::~Friends()
{ {
Steam::Proxy::UnregisterCallback(304);
Steam::Proxy::UnregisterCallback(336);
{ {
std::lock_guard<std::recursive_mutex> _(Friends::Mutex); std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
Friends::FriendsList.clear(); Friends::FriendsList.clear();

View File

@ -82,7 +82,7 @@ namespace Steam
Proxy::Callbacks.erase(callId); 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); std::lock_guard<std::recursive_mutex> _(Proxy::CallMutex);
@ -91,6 +91,11 @@ namespace Steam
{ {
::Utils::Hook::Call<void(void*)>(callback->second)(data); ::Utils::Hook::Call<void(void*)>(callback->second)(data);
} }
if(Worker::IsWorker())
{
Handlers::SteamCallbacks::HandleCallback(callId, data, size);
}
} }
void Proxy::RunFrame() void Proxy::RunFrame()
@ -110,7 +115,7 @@ namespace Steam
#endif #endif
//Steam::Callbacks::RunCallback(message.m_iCallback, message.m_pubParam); //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); Proxy::SteamFreeLastCallback(Proxy::SteamPipe);
} }
@ -151,7 +156,7 @@ namespace Steam
continue; 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<SteamFreeLastCallbackFn> SteamFreeLastCallback;
static std::function<SteamGetAPICallResultFn> SteamGetAPICallResult; 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(); static void UnregisterCalls();

View File

@ -92,6 +92,7 @@ namespace Handlers
Proto::IPC::Function response; Proto::IPC::Function response;
response.set_name("presenceResponse"); response.set_name("presenceResponse");
*response.add_params() = Utils::String::VA("%llX", id.Bits); *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()); *response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data());
endpoint.send(this->getCommand(), response.SerializeAsString()); 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 namespace Worker
{ {
Utils::IPC::BidirectionalChannel* Runner::Channel;
Runner::Runner(int pid) : processId(pid), terminate(false) Runner::Runner(int pid) : processId(pid), terminate(false)
{ {
Runner::Channel = nullptr;
} }
Runner::~Runner() Runner::~Runner()
{ {
Runner::Channel = nullptr;
} }
void Runner::run() void Runner::run()
@ -46,6 +48,7 @@ namespace Worker
{ {
printf("Worker started\n"); printf("Worker started\n");
Utils::IPC::BidirectionalChannel channel("IW4x-Worker-Channel", !Worker::IsWorker()); Utils::IPC::BidirectionalChannel channel("IW4x-Worker-Channel", !Worker::IsWorker());
Runner::Channel = &channel;
while (!this->terminate) while (!this->terminate)
{ {
@ -74,5 +77,6 @@ namespace Worker
} }
printf("Terminating worker\n"); printf("Terminating worker\n");
Runner::Channel = nullptr;
} }
} }

View File

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