Getting started
The callback mechanism
The communication from the Server Lib to the server application takes
place using callbacks. The server application has to define a
series of function pointers using the ServerLibFunctions
struct.
These callbacks are used to let the server application
hook into the library and receive notifications on certain actions.
A callback example in C:
static void my_onClientConnected_callback(uint64 serverID, anyID clientID, uint64 channelID, unsigned int* removeClientError) {
printf("Client %u connected on virtual server %u joining channel %u", clientID, serverID, channelID);
}
C++ developers can also use static member functions for the callbacks.
Before calling ts3server_initServerLib()
, create an instance of ServerLibFunctions
, initialize all function pointers with NULL and point
the structs function pointers to your implemented callback functions:
1unsigned int error;
2
3/* Create struct */
4ServerLibFunctions slFuncs;
5
6/* Initialize all function pointers with NULL */
7memset(&slFuncs, 0, sizeof(struct ServerLibFunctions));
8
9/* Assign those function pointers you implemented */
10slFuncs.onVoiceDataEvent = my_onVoiceDataEvent_callback;
11slFuncs.onClientStartTalkingEvent = my_onClientStartTalkingEvent_callback;
12slFuncs.onClientStopTalkingEvent = my_onClientStopTalkingEvent_callback;
13slFuncs.onClientConnected = my_onClientConnected_callback;
14slFuncs.onClientDisconnected = my_onClientDisconnected_callback;
15slFuncs.onClientMoved = my_onClientMoved_callback;
16slFuncs.onChannelCreated = my_onChannelCreated_callback;
17slFuncs.onChannelEdited = my_onChannelEdited_callback;
18slFuncs.onChannelDeleted = my_onChannelDeleted_callback;
19slFuncs.onServerTextMessageEvent = my_onServerTextMessageEvent_callback;
20slFuncs.onChannelTextMessageEvent = my_onChannelTextMessageEvent_callback;
21slFuncs.onUserLoggingMessageEvent = my_onUserLoggingMessageEvent_callback;
22slFuncs.onAccountingErrorEvent = my_onAccountingErrorEvent_callback;
23slFuncs.onCustomPacketEncryptEvent = NULL; // Not used by your application
24slFuncs.onCustomPacketDecryptEvent = NULL; // Not used by your application
25
26/* Initialize library with callback function pointers */
27error = ts3server_initServerLib(&slFuncs, LogType_FILE | LogType_CONSOLE);
28if (error != ERROR_ok) {
29 printf("Error initializing serverlib: %d\n", error);
30 (...)
31}
Important
As long as you initialize unimplemented callbacks with NULL, the Server Lib won’t attempt to call those function pointers. However, if you leave unimplemented callbacks undefined, the Server Lib will attempt to call them, crashing the application.
The individual callbacks are described in ServerLibFunctions
.
Initializing
When starting the server application, initialize the Server Lib with
-
unsigned int ts3server_initServerLib(const struct ServerLibFunctions *functionPointers, int usedLogTypes, const char *logFileFolder)
initializes the server library and defines callback functions
This is the first function you need to call, before this all calls to the server library will fail. In this call you will also set the functions you would like to have called when certain changes or events happen. This function must not be called multiple times.
- Parameters:
functionPointers – defines which functions in your code are to be called on specific events. Zero initialize it and assign the desired function to call to the respective members of the ServerLibFunctions struct
usedLogTypes – a combination of values from the LogTypes enum. Specifies which type(s) of logging you would like to use.
logFileFolder – path in which to create log files. Pass 0 to use the default of using a folder called logs in the working directory.
- Returns:
An Error code from the Ts3ErrorType enum indicating either success or the failure reason
Important
This function must not be called more than once.
Note
Logging to console can slow down the application on Windows. Hence we do not recommend to log to the console on Windows other than in debug builds.
Note
During initialization the serverlib will attempt to connect to the TeamSpeak licensing server. This function may block if the licensing server is unreachable.
Querying the library version
The Server Lib version can be queried with
-
unsigned int ts3server_getServerLibVersion(char **result)
Retrieve the server version string.
- Parameters:
result – address of a variable to receive the server version. Memory is allocated by the server library and must be freed by caller using ts3server_freeMemory
- Returns:
An Error code from the Ts3ErrorType enum indicating either success or the failure reason
To get only the version number, which is a part of the complete version string, as numeric value use
-
unsigned int ts3server_getServerLibVersionNumber(uint64 *result)
Retrieve the server version number.
- Parameters:
result – address of a variable to receive the server version number.
- Returns:
An Error code from the Ts3ErrorType enum indicating either success or the failure reason
Example
Query the Server Lib version:
unsigned int error;
char* version;
error = ts3server_getServerLibVersion(&version);
if (error != ERROR_ok) {
printf("Error querying serverlib version: %d\n", error);
return;
}
printf("Server library version: %s\n", version); /* Print version */
ts3server_freeMemory(version); /* Release string */
Shutting down
Before exiting the application, the Server Lib should be shut down using
-
unsigned int ts3server_destroyServerLib()
Destroys the server lib. Must not be called from within a callback.
All clients will lose connection and timeout, all servers will terminate. This is the last function to call. After this call you will no longer be able to use any server library functions.
- Returns:
An Error code from the Ts3ErrorType enum indicating either success or the failure reason
Any call to Server Lib functions after shutting down has undefined results.
Caution
Never destroy the Server Lib from within a callback function. This might result in a segmentation fault.
Error handling
Each Server Lib function returns either ERROR_ok
on success or an error
value as defined in the Ts3ErrorType
enum if the function fails.
The returned error codes are organized in groups, where the first byte defines the error group and the second the count within the group: The naming convention is ERROR_<group>_<error>, for example ERROR_client_invalid_id.
Note
Result variables should only be accessed if the function returned
ERROR_ok
. Otherwise the state of the result variable is undefined.
Important
Some Server Lib functions dynamically allocate memory which has to be
freed by the caller using ts3server_freeMemory()
.
It is important to only access and release the memory if the
function returned ERROR_ok
. Should the function return an error, the
result variable is uninitialized, so freeing or accessing it will
likely result in a segmentation fault.
See the section Calling Server Lib functions for additional notes and examples.
A printable error string for a specific error code can be queried with
-
unsigned int ts3server_getGlobalErrorMessage(unsigned int globalErrorCode, char **result)
get a human readable error description string for an error code
- Parameters:
globalErrorCode – the error code to retrieve the description for. One of the values from the Ts3ErrorType enum.
result – address of a variable to receive the error description as a utf8 encoded c string. Memory is allocated by the server library and must be freed by caller using ts3server_freeMemory
- Returns:
An Error code from the Ts3ErrorType enum indicating either success or the failure reason
Examples
1unsigned int error;
2char* welcomeMsg;
3
4/* welcomeMsg memory is allocated if error is ERROR_ok */
5error = ts3server_getVirtualServerVariableAsString(serverID, VIRTUALSERVER_WELCOMEMESSAGE, &welcomeMsg);
6if (error != ERROR_ok) {
7 /* Handle error */
8 return;
9}
10/* Use welcomeMsg... */
11ts3server_freeMemory(welcomeMsg); /* Release memory *only* if function did not return an error */
1unsigned int error;
2char* version;
3
4error = ts3server_getServerLibVersion(&version); /* Calling some Server Lib function */
5if (error != ERROR_ok) {
6 char* errorMsg;
7 if (ts3server_getGlobalErrorMessage(error, &errorMsg) == ERROR_ok) { /* Query printable error */
8 printf("Error querying client ID: %s\n", errorMsg);
9 ts3server_freeMemory(errorMsg); /* Release memory only if function succeeded */
10 }
11}