BASSenc

ENCODECLIENTPROC callback


User defined callback function to receive notification of client connections and disconnections, and optionally refuse connections.

BOOL CALLBACK EncodeClientProc(
    HENCODE handle,
    BOOL connect,
    char *client,
    char *headers,
    void *user
);

Parameters

handleThe encoder/server that the client is connecting to or disconnecting from.
connectThe client is connecting? TRUE = connecting, FALSE = disconnecting.
clientThe client's IP address and port number... "xxx.xxx.xxx.xxx:port".
headersThe 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 response headers should not exceed 1KB in length.
userThe user instance data given when BASS_Encode_ServerInit was called.

Return value

If the client is connecting, FALSE means the connection is denied, otherwise it is accepted. The return value is ignored if the client is disconnecting.

Remarks

This function can be used to keep track of how many clients are connected, and who is connected. The request headers can be used to authenticate clients, and response headers can be used to pass information back to the clients. By default, connecting clients will be sent an "HTTP/1.0 200 OK" status line if accepted, and an "HTTP/1.0 403 Forbidden" status line if denied. That can be overridden in the first response header.

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 has its own thread that handles new connections and sends data to its 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.

Example

A callback function that only allows connections from the 196.168/16 network, and only 5 clients.
int listeners=0; // client count

BOOL CALLBACK EncodeClientProc(HENCODE handle, BOOL connect, 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, char *client, char *headers, void *user)
{
    if (connect) {
        char *p=headers;
        while (*p) {
            if (!strncimp(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;
}

See also

BASS_Encode_ServerInit, BASS_Encode_ServerKick