Monitoring Client Connections
By using the IWMSPlayer object, you can monitor client connections to gather statistics and to control resources. For example, you may want to disconnect clients that are idle or that have a network address in a specified range.
The following examples illustrate how to control and monitor client connections.
Visual Basic .NET Example
Imports Microsoft.WindowsMediaServices.Interop
Imports System.Runtime.InteropServices
' Declare variables.
Dim Server As WMSServer
Dim strURL As String
Dim strName As String
Dim strIP As String
Dim lValue As Long
Dim lTotalODStreams As Long
Dim lTotalPPStreams As Long
Dim i, j As Integer
Try
' Create the WMSServer object.
Server = New WMSServer()
' Process each connected client.
For i = 0 To Server.Players.Count - 1
' Retrieve the client ID number.
lValue = Server.Players.Item(i).ID
' Retrieve the client user name.
strName = Server.Players.Item(i).UserName
' Retrieve the client requested URL.
strURL = Server.Players.Item(i).RequestedURL
' Retrieve the client IP address.
strIP = Server.Players.Item(i).NetworkAddress
' If the client's IP address is within a
' certain range, disconnect the client.
If (InStr(strIP, "128.65.23.") > 0) Then
Server.Players.Remove(i)
End If
' Check the client status and remove any idle clients.
If (Server.Players.Item(i).Status = WMS_CLIENT_STATUS.WMS_CLIENT_DISCONNECTED) Then
' TODO: Handle disconnected clients.
ElseIf (Server.Players.Item(i).Status = WMS_CLIENT_STATUS.WMS_CLIENT_IDLE) Then
Server.Players.Remove(i)
ElseIf (Server.Players.Item(i).Status = WMS_CLIENT_STATUS.WMS_CLIENT_OPEN) Then
' TODO: Handle open clients.
ElseIf (Server.Players.Item(i).Status = WMS_CLIENT_STATUS.WMS_CLIENT_STREAMING) Then
' TODO: Handle streaming clients.
End If
Next
' Count the total outgoing distribution connections
' actually streaming.
For i = 0 To Server.OutgoingDistributionConnections.Count - 1
If (Server.OutgoingDistributionConnections.Item(i).Status = _
WMS_CLIENT_STATUS.WMS_CLIENT_STREAMING) Then
lTotalODStreams = lTotalODStreams + 1
End If
Next
' Count the total publishing points currently
' streaming content.
For i = 0 To Server.PublishingPoints.Count - 1
' Check each client's status to see if
' it is currently streaming content.
For j = 0 To Server.PublishingPoints.Item(i).Players.Count - 1
If (Server.PublishingPoints.Item(i).Players.Item(j).Status = WMS_CLIENT_STATUS.WMS_CLIENT_STREAMING) Then
lTotalPPStreams = lTotalPPStreams + 1
Exit For
End If
Next
Next
Catch errCom As COMException
' TODO: Handle COM exceptions.
Catch err As Exception
' TODO: Exception handler goes here.
Finally
' TODO: Clean-up code goes here.
End Try
C# Example
using Microsoft.WindowsMediaServices.Interop;
using System.Runtime.InteropServices;
WMSServer Server;
string strURL="";
string strName="";
string strIP="";
long lValue=0;
long lTotalODStreams=0;
long lTotalPPStreams=0;
int i=0;
int j=0;
try
{
// Create the WMSServer object.
Server = new WMSServerClass();
// Process each connected client.
for (i=0; i < Server.Players.Count; i++)
{
// Retrieve the client ID number.
lValue = Server.Players[i].ID;
// Retrieve the client user name.
strName = Server.Players[i].UserName;
// Retrieve the client requested URL.
strURL = Server.Players[i].RequestedURL;
// Retrieve the client IP address.
strIP = Server.Players[i].NetworkAddress;
// If the client's IP address is within a
// certain range, disconnect the client.
if (strIP.IndexOf("128.65.23.") != -1)
{
Server.Players.Remove(i);
}
// Check the client status and remove any idle clients.
if (Server.Players[i].Status == WMS_CLIENT_STATUS.WMS_CLIENT_DISCONNECTED)
{
// TODO: Handle disconnected clients.
}
else if (Server.Players[i].Status == WMS_CLIENT_STATUS.WMS_CLIENT_IDLE)
{
Server.Players.Remove(i);
}
else if (Server.Players[i].Status == WMS_CLIENT_STATUS.WMS_CLIENT_OPEN)
{
// TODO: Handle open clients.
}
else if (Server.Players[i].Status == WMS_CLIENT_STATUS.WMS_CLIENT_STREAMING)
{
// TODO: Handle streaming clients.
}
}
// Count the total outgoing distribution connections
// actually streaming.
for (i = 0; i < Server.OutgoingDistributionConnections.Count; i++)
{
if (Server.OutgoingDistributionConnections[i].Status == WMS_CLIENT_STATUS.WMS_CLIENT_STREAMING)
{
lTotalODStreams = lTotalODStreams + 1;
}
}
// Count the total publishing points currently
// streaming content.
for (i = 0; i < Server.PublishingPoints.Count; i++)
{
// Check each client's status to see if
// it is currently streaming content.
for (j = 0; j < Server.PublishingPoints[i].Players.Count; j++)
{
if (Server.PublishingPoints[i].Players[j].Status == WMS_CLIENT_STATUS.WMS_CLIENT_STREAMING)
{
lTotalPPStreams = lTotalPPStreams + 1;
}
}
}
}
catch (COMException comExc)
{
// TODO: Handle COM exceptions.
}
catch (Exception exc)
{
// TODO: Exception handler goes here.
}
finally
{
// TODO: Clean-up code goes here.
}
C++ Example
#include <windows.h>
#include <atlbase.h> // Includes CComBSTR and CComVariant.
#include "wmsserver.h"
// Declare variables and interfaces.
IWMSServer *pServer;
IWMSPlayers *pPlayers;
IWMSPlayer *pPlayer;
IWMSOutgoingDistributionConnections *pODConnections;
IWMSOutgoingDistributionConnection *pODConnection;
IWMSPublishingPoints *pPubPoints;
IWMSPublishingPoint *pPubPoint;
HRESULT hr;
CComVariant varIndex;
CComBSTR bstrURL;
CComBSTR bstrName;
CComBSTR bstrIP;
long lCount;
long lValue;
long lTotalODStreams = 0;
long lTotalPPStreams = 0;
// Initialize the COM library and retrieve a pointer
// to an IWMSServer interface.
hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_WMSServer,
NULL,
CLSCTX_ALL,
IID_IWMSServer,
(void **)&pServer);
if (FAILED(hr)) goto EXIT;
// Retrieve a pointer to the IWMSPlayers interface
// and retrieve the number of clients.
hr = pServer->get_Players(&pPlayers);
if (FAILED(hr)) goto EXIT;
hr = pPlayers->get_Count(&lCount);
if (FAILED(hr)) goto EXIT;
// Process each connected client.
for (int x = 0; x < lCount; x++)
{
// Retrieve a pointer to the next
// IWMSPlayer interface.
varIndex = x;
hr = pPlayers->get_Item(varIndex, &pPlayer);
if (FAILED(hr)) goto EXIT;
// Retrieve the client ID number.
hr = pPlayer->get_ID(&lValue);
if (FAILED(hr)) goto EXIT;
// Retrieve the client user name.
hr = pPlayer->get_UserName(&bstrName);
if (FAILED(hr)) goto EXIT;
// Retrieve the client requested URL.
hr = pPlayer->get_RequestedURL(&bstrURL);
if (FAILED(hr)) goto EXIT;
// Retrieve the client IP address.
hr = pPlayer->get_NetworkAddress(&bstrIP);
if (FAILED(hr)) goto EXIT;
// If the client's IP address is within a
// certain range, disconnect the client.
if (strstr(bstrIP, "128.65.23.") != NULL)
{
pPlayers->Remove(varIndex);
}
// Retrieve the client status.
WMS_CLIENT_STATUS cliStatus;
hr = pPlayer->get_Status(&cliStatus);
if (FAILED(hr)) goto EXIT;
switch (cliStatus)
{
case WMS_CLIENT_DISCONNECTED:
// TODO: Handle disconnected clients.
break;
case WMS_CLIENT_IDLE:
// Disconnect any idle clients.
pPlayers->Remove(varIndex);
break;
case WMS_CLIENT_OPEN:
// TODO: Handle open clients.
break;
case WMS_CLIENT_STREAMING:
// TODO: Handle streaming clients.
break;
}
// Release the IWMSPlayer object.
pPlayer->Release();
}
// Retrieve a pointer to the
// IWMSOutgoingDistributionConnections interface
// and retrieve the number of distribution connections.
hr = pServer->get_OutgoingDistributionConnections(&pODConnections);
if (FAILED(hr)) goto EXIT;
hr = pODConnections->get_Count(&lCount);
if (FAILED(hr)) goto EXIT;
// Count the total outgoing distribution connections
// actually streaming.
for (x = 0; x < lCount; x++)
{
// Retrieve a pointer to the next
// IWMSOutgoingDistributionConnection interface.
varIndex = x;
hr = pODConnections->get_Item(varIndex, &pODConnection);
if (FAILED(hr)) goto EXIT;
// Retrieve the status of the connection.
hr = pODConnection->get_Status(&cliStatus)
// If the connection is currently streaming,
// add it to the total number of current streams.
if (cliStatus == WMS_CLIENT_STREAMING)
lTotalODStreams++;
if (FAILED(hr)) goto EXIT;
// Release the IWMSOutgoingDistributionConnection object.
pODConnection->Release();
}
// Retrieve a pointer to the IWMSPublishingPoints
// interface and retrieve the number of publishing points.
hr = pServer->get_PublishingPoints(&pPubPoints);
if (FAILED(hr)) goto EXIT;
hr = pPubPoints->get_Count(&lCount);
if (FAILED(hr)) goto EXIT;
// Count the total publishing points currently
// streaming content.
for (x = 0; x < lCount; x++)
{
// Retrieve a pointer to the next
// IWMSPublishingPoint interface.
varIndex = x;
hr = pPubPoints->get_Item(varIndex, &pPubPoint);
if (FAILED(hr)) goto EXIT;
// Retrieve a pointer to the IWMSPlayers
// interface and retrieve the number of clients
// connected to the publishing point.
hr = pPubPoint->get_Players(&pPlayers);
if (FAILED(hr)) goto EXIT;
hr = pPlayers->get_Count(&lCount);
if (FAILED(hr)) goto EXIT;
// Check each client's status to see if
// it is currently streaming content.
for (int y = 0; y < lCount; y++)
{
// Retrieve a pointer to the next
// IWMSPlayer interface.
varIndex = y;
hr = pPlayers->get_Item(varIndex, &pPlayer);
if (FAILED(hr)) goto EXIT;
// Retrive the client status.
hr = pPlayer->get_Status(&cliStatus);
if (FAILED(hr)) goto EXIT;
// If the client is currently streaming, increment
// the total number of streaming publishing points.
if (cliStatus == WMS_CLIENT_STREAMING)
{
lTotalPPStreams++;
break;
}
// Release the IWMSPlayer object.
pPlayer->Release();
}
// Release objects.
pPubPoint->Release();
pPlayers->Release();
}
EXIT:
// TODO: Release temporary COM objects and uninitialize COM.
See Also (General)
See Also (Visual Basic .NET)
IWMSOutgoingDistributionConnectionIWMSOutgoingDistributionConnection Object (Visual Basic .NET)
IWMSPlayerIWMSPlayer Object (Visual Basic .NET)
See Also (C#)
IWMSOutgoingDistributionConnectionIWMSOutgoingDistributionConnection Object (C#)
IWMSPlayerIWMSPlayer Object (C#)
See Also (C++)
IWMSOutgoingDistributionConnectionIWMSOutgoingDistributionConnection Interface
IWMSPlayerIWMSPlayer Interface