User defined callback function to receive notification of client connections and disconnections, and optionally refuse connections.
BOOL CALLBACK EncodeClientProc( HENCODE handle, BOOL connect, const char *client, char *headers, void *user );
handle | The encoder/server that the client is connecting to or disconnecting from. |
connect | The client is connecting? TRUE = connecting, FALSE = disconnecting. |
client | The client's IP address and port number. |
headers | The request headers... NULL = the client is disconnecting or HTTP headers have been disabled via the BASS_ENCODE_SERVER_NOHTTP flag. The headers are in the same form as would be given by BASS_ChannelGetTags, which is a series of null-terminated strings, the final string ending with a double null. The request headers can optionally be replaced with response headers to send back to the client, each ending with a carriage return and line feed ("\r\n"). The length of the response headers must not exceed 1024 bytes. |
user | The user instance data given when BASS_Encode_ServerInit was called. |
If a "Content-Type" response header is not provided then one will be set automatically when the encoder/server output is detected as MP3/2/1 (audio/mpeg) or OGG (audio/ogg) or WAV (audio/wav) or AAC/ADTS (audio/aac). The server should be started before any data is encoded to ensure that the server sees the headers, from which the format is detected.
Disconnection notifications will be received for clients that have disconnected themselves or that have been kicked by BASS_Encode_ServerKick, but there will no notification of any clients that are disconnected by the encoder being freed.
Each server port (there can be multiple servers per port) has its own thread that handles new connections and sends data to the clients. The notification callbacks also come from that thread, so the callback function should avoid introducing long delays as that could result in clients missing some data and delay other clients connecting.
int listeners = 0; // client count BOOL CALLBACK EncodeClientProc(HENCODE handle, BOOL connect, const char *client, char *headers, void *user) { if (connect) { if (listeners == 5) { // hit client limit strcpy(headers, "HTTP/1.0 403 Server Full\r\n"); // set custom status return FALSE; // refuse the connection } if (strncmp(client, "192.168." ,8)) // not on the 196.168/16 network return FALSE; // refuse the connection listeners++; // increment the client count } else listeners--; // decrement the client count return TRUE; }
A callback function that only allows connections with a particular "User-Agent" request header.
BOOL CALLBACK EncodeClientProc(HENCODE handle, BOOL connect, const char *client, char *headers, void *user) { if (connect) { char *p=headers; while (*p) { if (!strnicmp(p, "User-Agent:", 11)) { // found the User-Agent header if (strcmp(p + 12, "Special Agent")) // not the wanted agent return FALSE; // refuse the connection break; } p += strlen(p) + 1; // go to next header } } return TRUE; }