forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNotificationConnection.cpp
More file actions
executable file
·104 lines (84 loc) · 2.56 KB
/
Copy pathNotificationConnection.cpp
File metadata and controls
executable file
·104 lines (84 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "NotificationConnection.h"
#include "ServerError.hpp"
#include <WinSock2.h>
#include <cpprest/json.h>
#define odslog(msg) { std::stringstream ss; ss << msg << std::endl; OutputDebugStringA(ss.str().c_str()); }
void ALTDeviceReceivedNotification(const char* notification, void* user_data)
{
NotificationConnection* connection = (NotificationConnection*)user_data;
if (std::string(notification).size() > 0 && connection->active())
{
connection->receivedNotificationHandler()(notification);
}
}
NotificationConnection::NotificationConnection(std::shared_ptr<Device> device, np_client_t client) : _device(device), _client(client)
{
this->setReceivedNotificationHandler([](std::string notification) {
odslog("Received Notification: " << notification);
});
}
NotificationConnection::~NotificationConnection()
{
this->Disconnect();
}
void NotificationConnection::Disconnect()
{
if (_client == NULL)
{
return;
}
np_client_free(_client);
_client = NULL;
// Prevent us from receiving callbacks after deallocation.
_active = false;
}
void NotificationConnection::StartListening(std::vector<std::string> notifications)
{
const char** notificationNames = (const char**)malloc((notifications.size() + 1) * sizeof(char *));
for (int i = 0; i < notifications.size(); i++)
{
const char* cName = notifications[i].c_str();
notificationNames[i] = cName;
}
notificationNames[notifications.size()] = NULL; // Must have terminating NULL entry.
np_error_t result = np_observe_notifications(this->client(), notificationNames);
if (result != NP_E_SUCCESS)
{
throw ServerError(ServerErrorCode::LostConnection);
}
result = np_set_notify_callback(this->client(), ALTDeviceReceivedNotification, this);
if (result != NP_E_SUCCESS)
{
throw ServerError(ServerErrorCode::LostConnection);
}
_active = true;
free(notificationNames);
}
void NotificationConnection::SendNotification(std::string notification)
{
np_error_t result = np_post_notification(this->client(), notification.c_str());
if (result != NP_E_SUCCESS)
{
throw ServerError(ServerErrorCode::LostConnection);
}
}
std::shared_ptr<Device> NotificationConnection::device() const
{
return _device;
}
np_client_t NotificationConnection::client() const
{
return _client;
}
std::function<void(std::string)> NotificationConnection::receivedNotificationHandler() const
{
return _receivedNotificationHandler;
}
void NotificationConnection::setReceivedNotificationHandler(std::function<void(std::string)> handler)
{
_receivedNotificationHandler = handler;
}
bool NotificationConnection::active() const
{
return _active;
}