BASS.NET API for the Un4seen BASS Audio Library

ENCODECLIENTPROC Delegate

BASS.NET API for the Un4seen BASS Audio Library
User defined callback function to receive notification of client connections and disconnections, and optionally refuse connections.

Namespace:  Un4seen.Bass.AddOn.Enc
Assembly:  Bass.Net (in Bass.Net.dll) Version: 2.4.17.5
Syntax

public delegate bool ENCODECLIENTPROC(
	int handle,
	bool connect,
	string client,
	IntPtr headers,
	IntPtr user
)

Parameters

handle
Type: SystemInt32
The encoder/server that the client is connecting to or disconnecting from (as returned by BASS_Encode_Start(Int32, String, BASSEncode, ENCODEPROC, IntPtr)).
connect
Type: SystemBoolean
The client is connecting? TRUE = connecting, FALSE = disconnecting.
client
Type: SystemString
The client's IP address and port number... "xxx.xxx.xxx.xxx:port".
headers
Type: SystemIntPtr
The request headers... = 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(Int32, BASSTag), 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.
user
Type: SystemIntPtr
The user instance data given when BASS_Encode_ServerInit(Int32, String, Int32, Int32, BASSEncodeServer, ENCODECLIENTPROC, IntPtr) was called.

Return Value

Type: Boolean
If the client is connecting, 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(Int32, String), 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.

To access the 'headers' data use one of the following utility methods: To read the headers use the IntPtrToArrayNullTermAnsi(IntPtr) helper method; to write to the headers use the StringToNullTermAnsi(String, IntPtr) helper method.

Examples

A callback function that only allows connections from the 196.168/16 network, and only 5 clients:
C#
int _listeners = 0; // client count

private bool EncodeClientProc(int handle, bool connect, string client, IntPtr headers, IntPtr user)
{
   if (connect)
   {
       if (_listeners == 5)
       {
           // hit client limit
           string[] resHeaders = new string[1] { "HTTP/1.0 403 Server Full" };
           // set custom status
           Utils.StringToNullTermAnsi(resHeaders, headers, true);
           // refuse the connection
           return false;
       }
       if (!client.StartsWith("192.168."))
       {
           // not on the 196.168/16 network
           // refuse the connection
           return false;
       }
       // increment the client count
       _listeners++;
   }
   else
   {
       // decrement the client count
       _listeners--;
   }
   return true;
}
A callback function that only allows connections with a particular "User-Agent" request header:
C#
private bool EncodeClientProc(int handle, bool connect, string client, IntPtr headers, IntPtr user)
{
   if (connect)
   {
       // get the received headers
       string[] getHeaders = Utils.IntPtrToArrayNullTermAnsi(headers);
       // find the User-Agent header
       foreach (string content in getHeaders)
       {
           if (content.StartsWith("User-Agent:"))
           {
               // found the User-Agent header
               if (content.Substring(11) != "Special Agent")
                   return false;
               break;
           }
       }
   }
   return true;
}
See Also

Reference