Skip to content

Commit a68f99b

Browse files
author
Jon
committed
Webby fixed for "Upgrade" header
Removed unused plugin function defs Auto check the server vars every 2 seconds to changes (not all changes are notified to the plugin)
1 parent 6bffc1f commit a68f99b

6 files changed

Lines changed: 42 additions & 35 deletions

File tree

API.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# define _WINSOCK_DEPRECATED_NO_WARNINGS
99
#endif
1010
#include "API.h"
11+
#include "plugin.h"
1112

1213
#include "dependencies/webby/webby.c"
1314

@@ -101,12 +102,11 @@ int TS3_API::onHttpRequest(struct WebbyConnection* connection) {
101102
read_len = sizeof(buf) - 1;
102103
if (WebbyRead(connection, buf, read_len))
103104
return 1;
104-
return Instance().sendAllInfo(connection);
105+
Instance().sendAllInfo(connection);
106+
return 0;
105107
}
106108
int TS3_API::onWebsocketConnection(struct WebbyConnection* connection) {
107-
onLogMessage("onWebsocketConnection unsupported");
108-
connection->user_data = (void*)1;
109-
return 0; // Not supported atm
109+
return 0; // Allow all
110110
}
111111
int TS3_API::writeJson(struct WebbyConnection* connection, std::string& message) {
112112
WebbyHeader headers[1] = { {"Content-Type","application/json"} };
@@ -126,13 +126,11 @@ bool TS3_API::isWebsocketConnection(struct WebbyConnection* connection) {
126126
}
127127
int TS3_API::write(struct WebbyConnection* connection, const char* response, struct WebbyHeader* headers, int header_count) {
128128
size_t response_len = strlen(response);
129-
if (isWebsocketConnection(connection)) {
130-
if (WebbyBeginSocketFrame(connection, WEBBY_WS_OP_TEXT_FRAME))
131-
return 1;
132-
int ok = WebbyWrite(connection, response, response_len);
133-
//int ok = WebbyPrintf(connection, "%s", response);
129+
if (is_websocket_request(connection)) {
130+
WebbyBeginSocketFrame(connection, WEBBY_WS_OP_TEXT_FRAME);
131+
WebbyPrintf(connection, "%s\r\n", response);
134132
WebbyEndSocketFrame(connection);
135-
return ok;
133+
return 0;
136134
}
137135
else {
138136
size_t response_len = strlen(response);
@@ -272,7 +270,7 @@ int TS3_API::sendAllInfo(struct WebbyConnection* connection) {
272270
std::lock_guard<std::recursive_mutex> guard(mutex);
273271
nlohmann::json jsonClients;
274272
nlohmann::json jsonBody;
275-
for (auto it : clients) {
273+
for (auto& it : clients) {
276274
TS3User* client = it.second;
277275
if (!client) continue;
278276
nlohmann::json clientJson;
@@ -369,6 +367,13 @@ void TS3_API::update() {
369367
std::lock_guard<std::recursive_mutex> guard(mutex);
370368
if (ws_server)
371369
WebbyServerUpdate(ws_server);
370+
static clock_t last_update = 0;
371+
if (!last_update)
372+
last_update = clock();
373+
if (clock() - last_update > 2 * CLOCKS_PER_SEC) {
374+
last_update = clock();
375+
refreshAll();
376+
}
372377
}
373378
TS3_API::TS3User* TS3_API::getUser(anyID clientId) {
374379
std::lock_guard<std::recursive_mutex> guard(mutex);

API.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#pragma once
2+
3+
24
#include <cstdio>
35
#include <cstdlib>
46

@@ -79,6 +81,7 @@ class TS3_API {
7981
int writeJson(struct WebbyConnection* connection, nlohmann::json & json);
8082
// Write out to a connection
8183
int write(struct WebbyConnection* connection, const char* response, struct WebbyHeader* headers = nullptr, int header_count = 0);
84+
int writeHttp(struct WebbyConnection* connection, const char* response, struct WebbyHeader* headers = nullptr, int header_count = 0);
8285
bool isWebsocketConnection(struct WebbyConnection* connection);
8386
public:
8487
static TS3_API& Instance() {

dependencies/webby/webby.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,13 +891,13 @@ static int is_websocket_request(struct WebbyConnection* conn)
891891
if (NULL == (hdr = WebbyFindHeader(conn, "Connection")))
892892
return 0;
893893

894-
if (0 != strcasecmp(hdr, "Upgrade"))
894+
if (NULL == strstr(hdr, "Upgrade"))
895895
return 0;
896896

897897
if (NULL == (hdr = WebbyFindHeader(conn, "Upgrade")))
898898
return 0;
899899

900-
if (0 != strcasecmp(hdr, "websocket"))
900+
if (NULL == strstr(hdr, "websocket"))
901901
return 0;
902902

903903
return 1;

dependencies/webby/webby.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ WebbyServerMemoryNeeded(const struct WebbyServerConfig *config);
165165
struct WebbyServer*
166166
WebbyServerInit(struct WebbyServerConfig *config, void *memory, size_t memory_size);
167167

168+
int is_websocket_request(struct WebbyConnection* conn);
169+
168170
/* Update the server. Call frequently (at least once per frame). */
169171
void
170172
WebbyServerUpdate(struct WebbyServer *srv);

plugin.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static struct TS3Functions ts3Functions;
4747

4848
static char* pluginID = NULL;
4949

50+
static UINT WM_TEAMSPEAK3_JSON_API_STARTED = 0;
51+
5052
/*********************************** Required functions ************************************/
5153
/*
5254
* If any of these required functions is not implemented, TS3 will refuse to load the plugin
@@ -123,18 +125,19 @@ int refreshAll() {
123125
int ret = ERROR_ok;
124126
if (!is_connected()) {
125127
// Not fully connected; make sure client and server vars are flushed
126-
if (instance.server.id) {
128+
if (instance.server.host) {
127129
instance.resetServerVariables();
128130
instance.sendAllInfo();
129131
}
130-
instance.server.id = 0;
132+
instance.server.host[0] = 0;
131133
return 0;
132134
}
133-
bool new_connection = !instance.server.id;
135+
bool new_connection = !instance.server.host;
136+
need_to_send |= new_connection;
134137
char* s;
135138
if ((ret = ts3Functions.getServerVariableAsString(serverConnectionHandlerID, VIRTUALSERVER_NAME, &s)) != ERROR_ok) {
136139
API::onLogMessage("Failed to get server name, code 0x%04x", ret);
137-
return 1;
140+
goto send_if_needed;
138141
}
139142
if (strcmp(s, server.name) != 0) {
140143
need_to_send = true;
@@ -145,11 +148,11 @@ int refreshAll() {
145148
// Get server info
146149
if ((ret = ts3Functions.getServerConnectInfo(serverConnectionHandlerID, server.host, &server.port, server.password, sizeof(server.password))) != ERROR_ok) {
147150
API::onLogMessage("Failed to get getServerConnectInfo, code 0x%04x", ret);
148-
return 1;
151+
goto send_if_needed;
149152
}
150153
if ((ret = ts3Functions.getServerVariableAsString(serverConnectionHandlerID, VIRTUALSERVER_NAME, &s)) != ERROR_ok) {
151154
API::onLogMessage("Failed to get server name, code 0x%04x", ret);
152-
return 1;
155+
goto send_if_needed;
153156
}
154157
strncpy_s(server.name, s, sizeof(server.name) - 1);
155158
ts3Functions.freeMemory(s);
@@ -160,13 +163,13 @@ int refreshAll() {
160163

161164
if ((ret = ts3Functions.getClientID(serverConnectionHandlerID, &server.my_client_id)) != ERROR_ok) {
162165
API::onLogMessage("Failed to get my id, code 0x%04x", ret);
163-
return 1;
166+
goto send_if_needed;
164167
}
165168
// Update all connected users
166169
anyID* clientIDs;
167170
if ((ret = ts3Functions.getClientList(serverConnectionHandlerID, &clientIDs)) != ERROR_ok) {
168171
API::onLogMessage("Failed to get getClientList, code 0x%04x", ret);
169-
return 1;
172+
goto send_if_needed;
170173
}
171174
bool has_changed = false;
172175

@@ -175,24 +178,24 @@ int refreshAll() {
175178
need_to_send |= has_changed;
176179
}
177180
ts3Functions.freeMemory(clientIDs);
178-
181+
send_if_needed:
179182
if (need_to_send) {
180183
instance.sendAllInfo();
181184
}
182-
return 0;
185+
return ret;
183186
}
184187
int ts3plugin_updateAPIServer() {
185188
return refreshAll();
186189
}
187190

188-
189-
190191
/*
191192
* Custom code called right after loading the plugin. Returns 0 on success, 1 on failure.
192193
* If the function returns 1 on failure, the plugin will be unloaded again.
193194
*/
194195
int ts3plugin_init() {
195196
TS3_API::Instance();
197+
WM_TEAMSPEAK3_JSON_API_STARTED = RegisterWindowMessageW(L"WM_TEAMSPEAK3_JSON_API_STARTED");
198+
SendNotifyMessageW(HWND_BROADCAST, WM_TEAMSPEAK3_JSON_API_STARTED, NULL, NULL);
196199
return 0;
197200
}
198201
/* Custom code called right before the plugin is unloaded */

plugin.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ extern "C" {
1818
#endif
1919

2020

21+
22+
PLUGINS_EXPORTDLL uint64 is_connected();
23+
PLUGINS_EXPORTDLL int refreshAll();
2124
/* Required functions */
2225
PLUGINS_EXPORTDLL const char* ts3plugin_name();
2326
PLUGINS_EXPORTDLL const char* ts3plugin_version();
@@ -38,23 +41,14 @@ PLUGINS_EXPORTDLL void ts3plugin_onConnectStatusChangeEvent(uint64 serverConnect
3841
PLUGINS_EXPORTDLL void ts3plugin_onUpdateChannelEvent(uint64 serverConnectionHandlerID, uint64 channelID);
3942
PLUGINS_EXPORTDLL void ts3plugin_onUpdateChannelEditedEvent(uint64 serverConnectionHandlerID, uint64 channelID, anyID invokerID, const char* invokerName, const char* invokerUniqueIdentifier);
4043
PLUGINS_EXPORTDLL void ts3plugin_onUpdateClientEvent(uint64 serverConnectionHandlerID, anyID clientID, anyID invokerID, const char* invokerName, const char* invokerUniqueIdentifier);
41-
PLUGINS_EXPORTDLL void ts3plugin_onClientMoveEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, const char* moveMessage);
42-
PLUGINS_EXPORTDLL void ts3plugin_onClientMoveSubscriptionEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility);
43-
PLUGINS_EXPORTDLL void ts3plugin_onClientMoveTimeoutEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, const char* timeoutMessage);
44-
PLUGINS_EXPORTDLL void ts3plugin_onClientMoveMovedEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, anyID moverID, const char* moverName, const char* moverUniqueIdentifier, const char* moveMessage);
45-
PLUGINS_EXPORTDLL void ts3plugin_onClientKickFromChannelEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, anyID kickerID, const char* kickerName, const char* kickerUniqueIdentifier, const char* kickMessage);
44+
PLUGINS_EXPORTDLL void ts3plugin_onClientMoveEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, const char* moveMessage);PLUGINS_EXPORTDLL void ts3plugin_onClientKickFromChannelEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, anyID kickerID, const char* kickerName, const char* kickerUniqueIdentifier, const char* kickMessage);
4645
PLUGINS_EXPORTDLL void ts3plugin_onClientKickFromServerEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, anyID kickerID, const char* kickerName, const char* kickerUniqueIdentifier, const char* kickMessage);
47-
PLUGINS_EXPORTDLL void ts3plugin_onClientIDsEvent(uint64 serverConnectionHandlerID, const char* uniqueClientIdentifier, anyID clientID, const char* clientName);
48-
PLUGINS_EXPORTDLL void ts3plugin_onClientIDsFinishedEvent(uint64 serverConnectionHandlerID);
4946
PLUGINS_EXPORTDLL void ts3plugin_onServerEditedEvent(uint64 serverConnectionHandlerID, anyID editerID, const char* editerName, const char* editerUniqueIdentifier);
50-
PLUGINS_EXPORTDLL void ts3plugin_onServerUpdatedEvent(uint64 serverConnectionHandlerID);
51-
PLUGINS_EXPORTDLL int ts3plugin_onServerErrorEvent(uint64 serverConnectionHandlerID, const char* errorMessage, unsigned int error, const char* returnCode, const char* extraMessage);
5247
PLUGINS_EXPORTDLL void ts3plugin_onServerStopEvent(uint64 serverConnectionHandlerID, const char* shutdownMessage);
5348
PLUGINS_EXPORTDLL int ts3plugin_onTextMessageEvent(uint64 serverConnectionHandlerID, anyID targetMode, anyID toID, anyID fromID, const char* fromName, const char* fromUniqueIdentifier, const char* message, int ffIgnored);
5449
PLUGINS_EXPORTDLL void ts3plugin_onTalkStatusChangeEvent(uint64 serverConnectionHandlerID, int status, int isReceivedWhisper, anyID clientID);
5550

5651
/* Clientlib rare */
57-
PLUGINS_EXPORTDLL void ts3plugin_onClientSelfVariableUpdateEvent(uint64 serverConnectionHandlerID, int flag, const char* oldValue, const char* newValue);
5852
PLUGINS_EXPORTDLL void ts3plugin_onClientChannelGroupChangedEvent(uint64 serverConnectionHandlerID, uint64 channelGroupID, uint64 channelID, anyID clientID, anyID invokerClientID, const char* invokerName, const char* invokerUniqueIdentity);
5953
PLUGINS_EXPORTDLL void ts3plugin_onServerGroupClientAddedEvent(uint64 serverConnectionHandlerID, anyID clientID, const char* clientName, const char* clientUniqueIdentity, uint64 serverGroupID, anyID invokerClientID, const char* invokerName, const char* invokerUniqueIdentity);
6054
PLUGINS_EXPORTDLL void ts3plugin_onServerGroupClientDeletedEvent(uint64 serverConnectionHandlerID, anyID clientID, const char* clientName, const char* clientUniqueIdentity, uint64 serverGroupID, anyID invokerClientID, const char* invokerName, const char* invokerUniqueIdentity);

0 commit comments

Comments
 (0)