mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2025-12-23 23:37:48 -05:00
SDK plugin control
This commit is contained in:
@@ -815,6 +815,28 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case NET_PACKET_ID_REQUEST_PLUGIN_LIST:
|
||||
SendReply_PluginList(client_sock);
|
||||
break;
|
||||
|
||||
case NET_PACKET_ID_PLUGIN_SPECIFIC:
|
||||
{
|
||||
unsigned int plugin_pkt_type = *((unsigned int*)(data));
|
||||
unsigned int plugin_pkt_size = header.pkt_size - (sizeof(unsigned int));
|
||||
unsigned char* plugin_data = (unsigned char*)(data + sizeof(unsigned int));
|
||||
|
||||
if(header.pkt_dev_idx < plugins.size())
|
||||
{
|
||||
NetworkPlugin plugin = plugins[header.pkt_dev_idx];
|
||||
unsigned char* output = plugin.callback(plugin.callback_arg, plugin_pkt_type, plugin_data, &plugin_pkt_size);
|
||||
if(output != nullptr)
|
||||
{
|
||||
SendReply_PluginSpecific(client_sock, plugin_pkt_type, output, plugin_pkt_size);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
@@ -1002,8 +1024,154 @@ void NetworkServer::SendReply_ProfileList(SOCKET client_sock)
|
||||
send(client_sock, (const char *)reply_data, reply_size, 0);
|
||||
}
|
||||
|
||||
void NetworkServer::SendReply_PluginList(SOCKET client_sock)
|
||||
{
|
||||
unsigned int data_size = 0;
|
||||
unsigned int data_ptr = 0;
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Calculate data size |
|
||||
\*---------------------------------------------------------*/
|
||||
unsigned short num_plugins = plugins.size();
|
||||
|
||||
data_size += sizeof(data_size);
|
||||
data_size += sizeof(num_plugins);
|
||||
|
||||
for(unsigned int i = 0; i < num_plugins; i++)
|
||||
{
|
||||
data_size += sizeof(unsigned short) * 3;
|
||||
data_size += strlen(plugins[i].name.c_str()) + 1;
|
||||
data_size += strlen(plugins[i].description.c_str()) + 1;
|
||||
data_size += strlen(plugins[i].version.c_str()) + 1;
|
||||
data_size += sizeof(unsigned int) * 2;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Create data buffer |
|
||||
\*---------------------------------------------------------*/
|
||||
unsigned char *data_buf = new unsigned char[data_size];
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Copy in data size |
|
||||
\*---------------------------------------------------------*/
|
||||
memcpy(&data_buf[data_ptr], &data_size, sizeof(data_size));
|
||||
data_ptr += sizeof(data_size);
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Copy in num_plugins |
|
||||
\*---------------------------------------------------------*/
|
||||
memcpy(&data_buf[data_ptr], &num_plugins, sizeof(num_plugins));
|
||||
data_ptr += sizeof(num_plugins);
|
||||
|
||||
for(unsigned int i = 0; i < num_plugins; i++)
|
||||
{
|
||||
/*---------------------------------------------------------*\
|
||||
| Copy in plugin name (size+data) |
|
||||
\*---------------------------------------------------------*/
|
||||
unsigned short str_len = strlen(plugins[i].name.c_str()) + 1;
|
||||
|
||||
memcpy(&data_buf[data_ptr], &str_len, sizeof(unsigned short));
|
||||
data_ptr += sizeof(unsigned short);
|
||||
|
||||
strcpy((char *)&data_buf[data_ptr], plugins[i].name.c_str());
|
||||
data_ptr += str_len;
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Copy in plugin description (size+data) |
|
||||
\*---------------------------------------------------------*/
|
||||
str_len = strlen(plugins[i].description.c_str()) + 1;
|
||||
|
||||
memcpy(&data_buf[data_ptr], &str_len, sizeof(unsigned short));
|
||||
data_ptr += sizeof(unsigned short);
|
||||
|
||||
strcpy((char *)&data_buf[data_ptr], plugins[i].description.c_str());
|
||||
data_ptr += str_len;
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Copy in plugin version (size+data) |
|
||||
\*---------------------------------------------------------*/
|
||||
str_len = strlen(plugins[i].version.c_str()) + 1;
|
||||
|
||||
memcpy(&data_buf[data_ptr], &str_len, sizeof(unsigned short));
|
||||
data_ptr += sizeof(unsigned short);
|
||||
|
||||
strcpy((char *)&data_buf[data_ptr], plugins[i].version.c_str());
|
||||
data_ptr += str_len;
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Copy in plugin index (data) |
|
||||
\*---------------------------------------------------------*/
|
||||
memcpy(&data_buf[data_ptr], &i, sizeof(unsigned int));
|
||||
data_ptr += sizeof(unsigned int);
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Copy in plugin sdk version (data) |
|
||||
\*---------------------------------------------------------*/
|
||||
memcpy(&data_buf[data_ptr], &plugins[i].protocol_version, sizeof(int));
|
||||
data_ptr += sizeof(int);
|
||||
}
|
||||
|
||||
NetPacketHeader reply_hdr;
|
||||
unsigned int reply_size;
|
||||
|
||||
memcpy(&reply_size, data_buf, sizeof(reply_size));
|
||||
|
||||
reply_hdr.pkt_magic[0] = 'O';
|
||||
reply_hdr.pkt_magic[1] = 'R';
|
||||
reply_hdr.pkt_magic[2] = 'G';
|
||||
reply_hdr.pkt_magic[3] = 'B';
|
||||
|
||||
reply_hdr.pkt_dev_idx = 0;
|
||||
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_PLUGIN_LIST;
|
||||
reply_hdr.pkt_size = reply_size;
|
||||
|
||||
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
|
||||
send(client_sock, (const char *)data_buf, reply_size, 0);
|
||||
|
||||
delete [] data_buf;
|
||||
}
|
||||
|
||||
void NetworkServer::SendReply_PluginSpecific(SOCKET client_sock, unsigned int pkt_type, unsigned char* data, unsigned int data_size)
|
||||
{
|
||||
NetPacketHeader reply_hdr;
|
||||
unsigned char* reply = new unsigned char[sizeof(pkt_type) + data_size];
|
||||
|
||||
memcpy(reply, &pkt_type, sizeof(pkt_type));
|
||||
memcpy(reply + sizeof(pkt_type), data, data_size);
|
||||
|
||||
reply_hdr.pkt_magic[0] = 'O';
|
||||
reply_hdr.pkt_magic[1] = 'R';
|
||||
reply_hdr.pkt_magic[2] = 'G';
|
||||
reply_hdr.pkt_magic[3] = 'B';
|
||||
|
||||
reply_hdr.pkt_dev_idx = 0;
|
||||
reply_hdr.pkt_id = NET_PACKET_ID_PLUGIN_SPECIFIC;
|
||||
reply_hdr.pkt_size = data_size + sizeof(pkt_type);
|
||||
|
||||
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
|
||||
send(client_sock, (const char *)reply, data_size, 0);
|
||||
delete [] reply;
|
||||
delete [] data;
|
||||
}
|
||||
|
||||
void NetworkServer::SetProfileManager(ProfileManagerInterface* profile_manager_pointer)
|
||||
{
|
||||
profile_manager = profile_manager_pointer;
|
||||
}
|
||||
|
||||
void NetworkServer::RegisterPlugin(NetworkPlugin plugin)
|
||||
{
|
||||
plugins.push_back(plugin);
|
||||
}
|
||||
|
||||
void NetworkServer::UnregisterPlugin(std::string plugin_name)
|
||||
{
|
||||
for(std::vector<NetworkPlugin>::iterator it = plugins.begin(); it != plugins.end(); it++)
|
||||
{
|
||||
if(it->name == plugin_name)
|
||||
{
|
||||
plugins.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user