[Friends] Query friends from the worker

This commit is contained in:
momo5502 2017-01-28 19:03:44 +01:00
parent 46d3045d6f
commit 20139d4a52
10 changed files with 282 additions and 87 deletions

View File

@ -55,33 +55,11 @@ namespace Components
void Friends::UpdateFriends()
{
if (!Steam::Proxy::SteamFriends) return;
std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
Proto::IPC::Function function;
function.set_name("getFriends");
*function.add_params() = Utils::String::VA("%d", 4);
auto listCopy = Friends::FriendsList;
Friends::FriendsList.clear();
int count = Steam::Proxy::SteamFriends->GetFriendCount(4);
Friends::FriendsList.reserve(count);
for(int i = 0; i < count; ++i)
{
SteamID friendId = Steam::Proxy::SteamFriends->GetFriendByIndex(i, 4);
//if(!Steam::Proxy::SteamFriends->GetFriendPersonaState(friendId)) continue; // Offline
auto entry = std::find_if(listCopy.begin(), listCopy.end(), [friendId](Friends::Friend entry)
{
return (entry.userId.Bits == friendId.Bits);
});
if (entry != listCopy.end())
{
Friends::FriendsList.push_back(*entry);
}
Friends::UpdateUserInfo(friendId);
Steam::Proxy::SteamFriends->RequestFriendRichPresence(friendId);
}
IPCHandler::SendWorker("friends", function.SerializeAsString());
}
unsigned int Friends::GetFriendCount()
@ -145,6 +123,71 @@ namespace Components
Friends::CurrentFriend = index;
}
void Friends::NameResponse(std::vector<std::string> params)
{
if (params.size() >= 2)
{
std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
SteamID id;
id.Bits = strtoull(params[0].data(), nullptr, 16);
for(auto& entry : Friends::FriendsList)
{
if(entry.userId.Bits == id.Bits)
{
entry.name = params[1];
break;
}
}
}
}
void Friends::FriendsResponse(std::vector<std::string> params)
{
std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
auto oldFriends = Friends::FriendsList;
Friends::FriendsList.clear();
for (auto param : params)
{
SteamID id;
id.Bits = strtoull(param.data(), nullptr, 16);
Friends::Friend entry;
entry.userId = id;
entry.online = false;
entry.prestige = 0;
entry.experience = 0;
auto oldEntry = std::find_if(oldFriends.begin(), oldFriends.end(), [id](Friends::Friend entry)
{
return (entry.userId.Bits == id.Bits);
});
if (oldEntry != oldFriends.end()) entry = *oldEntry;
Friends::FriendsList.push_back(entry);
Proto::IPC::Function function;
function.set_name("getName");
*function.add_params() = Utils::String::VA("%llx", id.Bits);
IPCHandler::SendWorker("friends", function.SerializeAsString());
function.Clear();
function.set_name("getPresence");
*function.add_params() = Utils::String::VA("%llx", id.Bits);
*function.add_params() = "iw4x_status";
IPCHandler::SendWorker("friends", function.SerializeAsString());
function.Clear();
function.set_name("requestPresence");
*function.add_params() = Utils::String::VA("%llx", id.Bits);
IPCHandler::SendWorker("friends", function.SerializeAsString());
}
}
Friends::Friends()
{
// Callback to update user information
@ -168,33 +211,9 @@ namespace Components
UIFeeder::Add(6.0f, Friends::GetFriendCount, Friends::GetFriendText, Friends::SelectFriend);
IPCHandler::OnWorker("friends", [](std::string data)
{
Proto::IPC::Function function;
if(function.ParseFromString(data))
{
if(function.name() == "friendsResponse")
{
auto params = function.params();
Logger::Print("Received friendslist: %d\n", params.size());
for(auto param : params)
{
Logger::Print("%s\n", param.data());
}
}
}
});
Command::Add("getFriends", [](Command::Params*)
{
Proto::IPC::Function function;
function.set_name("getFriends");
function.add_params()->append(Utils::String::VA("%d", 4));
IPCHandler::SendWorker("friends", function.SerializeAsString());
});
auto fInterface = IPCHandler::NewInterface("friends");
fInterface->map("friendsResponse", Friends::FriendsResponse);
fInterface->map("nameResponse", Friends::NameResponse);
}
Friends::~Friends()
@ -202,11 +221,6 @@ namespace Components
Steam::Proxy::UnregisterCallback(304);
Steam::Proxy::UnregisterCallback(336);
if (Steam::Proxy::SteamFriends)
{
Steam::Proxy::SteamFriends->SetRichPresence("iw4x_status", nullptr);
}
{
std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
Friends::FriendsList.clear();

View File

@ -48,5 +48,8 @@ namespace Components
static unsigned int GetFriendCount();
static const char* GetFriendText(unsigned int index, int column);
static void SelectFriend(unsigned int index);
static void FriendsResponse(std::vector<std::string> params);
static void NameResponse(std::vector<std::string> params);
};
}

View File

@ -8,6 +8,15 @@ namespace Components
std::unique_ptr<Utils::IPC::BidirectionalChannel> IPCHandler::WorkerChannel;
std::unique_ptr<Utils::IPC::BidirectionalChannel> IPCHandler::ClientChannel;
std::unordered_map<std::string, std::shared_ptr<IPCHandler::FunctionInterface>> IPCHandler::FunctionInterfaces;
std::shared_ptr<IPCHandler::FunctionInterface> IPCHandler::NewInterface(std::string command)
{
std::shared_ptr<IPCHandler::FunctionInterface> fInterface(new IPCHandler::FunctionInterface());
IPCHandler::FunctionInterfaces[command] = fInterface;
return fInterface;
}
void IPCHandler::SendWorker(std::string message, std::string data)
{
IPCHandler::InitChannels();
@ -98,10 +107,15 @@ namespace Components
if (command.ParseFromString(packet))
{
auto callback = IPCHandler::WorkerCallbacks.find(command.name());
auto fInterface = IPCHandler::FunctionInterfaces.find(command.name());
if (callback != IPCHandler::WorkerCallbacks.end())
{
callback->second(command.data());
}
else if(fInterface != IPCHandler::FunctionInterfaces.end())
{
fInterface->second->handle(command.data());
}
}
}
}
@ -122,7 +136,12 @@ namespace Components
IPCHandler::~IPCHandler()
{
IPCHandler::FunctionInterfaces.clear();
IPCHandler::WorkerCallbacks.clear();
IPCHandler::ClientCallbacks.clear();
IPCHandler::WorkerChannel.release();
IPCHandler::ClientChannel.release();
}
}

View File

@ -5,6 +5,34 @@ namespace Components
class IPCHandler : public Component
{
public:
class FunctionInterface
{
public:
typedef std::function<void(std::vector<std::string>)> Callback;
void map(std::string name, Callback function)
{
this->functions[name] = function;
}
void handle(std::string data)
{
Proto::IPC::Function function;
if (function.ParseFromString(data))
{
auto handler = this->functions.find(function.name());
if (handler != this->functions.end())
{
auto params = function.params();
handler->second(std::vector<std::string>(params.begin(), params.end()));
}
}
}
private:
std::unordered_map<std::string, Callback> functions;
};
typedef Utils::Slot<void(std::string)> Callback;
IPCHandler();
@ -20,12 +48,16 @@ namespace Components
static void OnWorker(std::string message, Callback callback);
static void OnClient(std::string message, Callback callback);
static std::shared_ptr<FunctionInterface> NewInterface(std::string command);
private:
static std::unique_ptr<Utils::IPC::BidirectionalChannel> WorkerChannel;
static std::unique_ptr<Utils::IPC::BidirectionalChannel> ClientChannel;
static std::unordered_map<std::string, Callback> WorkerCallbacks;
static std::unordered_map<std::string, Callback> ClientCallbacks;
static std::unordered_map<std::string, std::shared_ptr<FunctionInterface>> FunctionInterfaces;
static void InitChannels();
static void StartWorker();

View File

@ -31,6 +31,7 @@
#include <future>
#include <queue>
#include <unordered_map>
#include <queue>
// Experimental C++17 features
#include <filesystem>

View File

@ -4,14 +4,24 @@ namespace Utils
{
namespace IPC
{
Channel::Channel(std::string _name, int _queueSize, int _bufferSize, bool _remove) : name(_name), remove(_remove)
Channel::Channel(std::string _name, int _queueSize, int _bufferSize, bool _remove) : terminateQueue(false), remove(_remove), name(_name)
{
if(this->remove) boost::interprocess::message_queue::remove(this->name.data());
queue.reset(new boost::interprocess::message_queue(boost::interprocess::open_or_create, this->name.data(), _queueSize, _bufferSize + sizeof(Channel::Header)));
this->queueThread = std::thread(&Channel::queueWorker, this);
}
Channel::~Channel()
{
this->terminateQueue = true;
this->queueEvent.notify_all();
if(this->queueThread.joinable())
{
this->queueThread.join();
}
if (this->remove) boost::interprocess::message_queue::remove(this->name.data());
}
@ -84,19 +94,47 @@ namespace Utils
std::string buffer;
buffer.append(reinterpret_cast<char*>(&header), sizeof(Channel::Header));
buffer.append(data.data() + sentSize, header.fragmentSize);
Channel::sendMessage(buffer);
Channel::enqueueMessage(buffer);
sentSize += header.fragmentSize;
}
}
void Channel::sendMessage(std::string data)
void Channel::enqueueMessage(std::string data)
{
if (data.size() <= this->queue->get_max_msg_size())
{
while (!this->queue->try_send(data.data(), data.size(), 0))
std::lock_guard<std::mutex> _(this->queueMutex);
this->internalQueue.push(data);
this->queueEvent.notify_all();
}
}
void Channel::queueWorker()
{
while (!this->terminateQueue)
{
std::unique_lock<std::mutex> lock(this->queueMutex);
while(!this->terminateQueue && this->internalQueue.empty())
{
std::this_thread::sleep_for(100us);
this->queueEvent.wait(lock);
}
while(!this->terminateQueue && !this->internalQueue.empty())
{
std::string data = this->internalQueue.front();
this->internalQueue.pop();
if (data.size() <= this->queue->get_max_msg_size())
{
while (!this->terminateQueue && !this->queue->try_send(data.data(), data.size(), 0))
{
lock.unlock();
std::this_thread::sleep_for(1000us);
lock.lock();
}
}
}
}
}

View File

@ -45,7 +45,14 @@ namespace Utils
unsigned int fragmentPart;
};
void sendMessage(std::string data);
void enqueueMessage(std::string data);
void queueWorker();
bool terminateQueue;
std::condition_variable queueEvent;
std::mutex queueMutex;
std::thread queueThread;
std::queue<std::string> internalQueue;
bool remove;
std::unique_ptr<boost::interprocess::message_queue> queue;

View File

@ -27,29 +27,103 @@ namespace Handlers
this->functions[function] = callback;
}
void Friends::getFriends(Worker::Endpoint endpoint, std::vector<std::string> params)
{
if (params.size() >= 1 && Steam::Proxy::SteamFriends)
{
int flag = atoi(params[0].data());
int count = Steam::Proxy::SteamFriends->GetFriendCount(flag);
Proto::IPC::Function response;
response.set_name("friendsResponse");
for (int i = 0; i < count; ++i)
{
SteamID id = Steam::Proxy::SteamFriends->GetFriendByIndex(i, flag);
*response.add_params() = Utils::String::VA("%llX", id.Bits);
}
endpoint.send(this->getCommand(), response.SerializeAsString());
}
}
void Friends::getName(Worker::Endpoint endpoint, std::vector<std::string> params)
{
if(Steam::Proxy::SteamFriends)
{
std::string name;
SteamID id;
if(params.size() >= 1)
{
id.Bits = strtoull(params[0].data(), nullptr, 16);
name = Steam::Proxy::SteamFriends->GetFriendPersonaName(id);
}
else
{
id.Bits = 0;
name = Steam::Proxy::SteamFriends->GetPersonaName();
}
Proto::IPC::Function response;
response.set_name("nameResponse");
*response.add_params() = Utils::String::VA("%llX", id.Bits);
*response.add_params() = name;
endpoint.send(this->getCommand(), response.SerializeAsString());
}
}
void Friends::setPresence(Worker::Endpoint /*endpoint*/, std::vector<std::string> params)
{
if (params.size() >= 2 && Steam::Proxy::SteamFriends)
{
Steam::Proxy::SteamFriends->SetRichPresence(params[0].data(), params[1].data());
}
}
void Friends::getPresence(Worker::Endpoint endpoint, std::vector<std::string> params)
{
if (params.size() >= 2 && Steam::Proxy::SteamFriends)
{
SteamID id;
id.Bits = strtoull(params[0].data(), nullptr, 16);
Proto::IPC::Function response;
response.set_name("presenceResponse");
*response.add_params() = Utils::String::VA("%llX", id.Bits);
*response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data());
endpoint.send(this->getCommand(), response.SerializeAsString());
}
}
void Friends::requestPresence(Worker::Endpoint /*endpoint*/, std::vector<std::string> params)
{
if (params.size() >= 1 && Steam::Proxy::SteamFriends)
{
SteamID id;
id.Bits = strtoull(params[0].data(), nullptr, 16);
Steam::Proxy::SteamFriends->RequestFriendRichPresence(id);
}
}
Friends::Friends()
{
this->addFunction("getFriends", [&](Worker::Endpoint endpoint, std::vector<std::string> params)
using namespace std::placeholders;
this->addFunction("getFriends", std::bind(&Friends::getFriends, this, _1, _2));
this->addFunction("getName", std::bind(&Friends::getName, this, _1, _2));
this->addFunction("setPresence", std::bind(&Friends::setPresence, this, _1, _2));
this->addFunction("getPresence", std::bind(&Friends::getPresence, this, _1, _2));
this->addFunction("requestPresence", std::bind(&Friends::requestPresence, this, _1, _2));
}
Friends::~Friends()
{
if(Steam::Proxy::SteamFriends)
{
if (params.size() >= 1 && Steam::Proxy::SteamFriends)
{
int flag = atoi(params[0].data());
int count = Steam::Proxy::SteamFriends->GetFriendCount(flag);
Proto::IPC::Function response;
response.set_name("friendsResponse");
for (int i = 0; i < count; ++i)
{
std::string* param = response.add_params();
SteamID id = Steam::Proxy::SteamFriends->GetFriendByIndex(i, flag);
param->clear();
param->append(Utils::String::VA("%llX", id.Bits));
}
endpoint.send(this->getCommand(), response.SerializeAsString());
}
});
Steam::Proxy::SteamFriends->ClearRichPresence();
}
}
}

View File

@ -8,12 +8,19 @@ namespace Handlers
typedef std::function<void(Worker::Endpoint, std::vector<std::string>)> Callback;
Friends();
~Friends();
std::string getCommand() override { return "friends"; };
void handle(Worker::Endpoint endpoint, std::string data) override;
private:
void addFunction(std::string function, Callback callback);
std::unordered_map<std::string, Callback> functions;
void addFunction(std::string function, Callback callback);
void getFriends(Worker::Endpoint endpoint, std::vector<std::string> params);
void getName(Worker::Endpoint endpoint, std::vector<std::string> params);
void setPresence(Worker::Endpoint endpoint, std::vector<std::string> params);
void getPresence(Worker::Endpoint endpoint, std::vector<std::string> params);
void requestPresence(Worker::Endpoint endpoint, std::vector<std::string> params);
};
}

View File

@ -60,7 +60,7 @@ namespace Worker
auto handler = this->handlers.find(command.name());
if (handler != this->handlers.end())
{
printf("Dispathcing command %s to handler\n", command.name().data());
printf("Dispatching command %s to handler\n", command.name().data());
handler->second->handle(&channel, command.data());
}
else