Share via


IWMSCacheProxy.QueryCache (C#)

banner art

Previous Next

IWMSCacheProxy.QueryCache (C#)

The QueryCache method is called by the server to direct the cache plug-in to search the cache for specific content.

Syntax

  

Parameters

bstrOriginUrl

[in] string containing the origin URL.

pUserContext

[in] IWMSContext object containing the user context.

pCommandContext

[in] IWMSCommandContext object containing the command context.

pPresentationContext

[in] IWMSContext object containing the presentation context.

lQueryType

[in] Member of the WMS_CACHE_QUERY_TYPE_FLAGS enumeration type that indicates why the server called IWMSCacheProxy.QueryCache. This must be one of the following values.

Value Description
WMS_CACHE_QUERY_OPEN A client using a downstream proxy requested content.
WMS_CACHE_QUERY_GET_CONTENT_INFO A downstream proxy requested information about content cached on the remote computer.
WMS_CACHE_QUERY_CACHE_EVENT A cache event notice is being sent upstream. If the WMS_CACHE_QUERY_LOCAL_EVENT flag is set, the cache event was generated by the local computer. Otherwise, it was sent by a downstream proxy server.
WMS_CACHE_QUERY_REVERSE_PROXY A downstream server is configured to be a reverse proxy server. If a cache proxy plug-in supports reverse proxy, it can use this flag to determine whether it must map client requests to an upstream server farm.
WMS_CACHE_QUERY_LOCAL_EVENT The local server is generating an event to send upstream.

pCallback

[in] IWMSCacheProxyCallback object containing the callback function. The cache plug-in calls IWMSCacheProxyCallback.OnQueryCache to respond to a call to QueryCache.

varContext

[in] object containing a value defined by the server to identify which call to QueryCache the plug-in is responding to when it calls IWMSCacheProxyCallback.OnQueryCache. You must pass this value back unaltered.

Return Values

This method does not return a value. To report an error, the plug-in can throw a COMException object to the server. If the plug-in uses the IWMSEventLog object to log error information, it is recommended that it throw NS_E_PLUGIN_ERROR_REPORTED (0xC00D157D). Typically, the server attempts to make plug-in error information available to the server object model, the Windows Event Viewer, and the troubleshooting list in the details pane of the Windows Media Services MMC. However, if the plug-in uses the IWMSEventLog object to send custom error information to the Windows Event Viewer, throwing NS_E_PLUGIN_ERROR_REPORTED stops the server from also logging to the event viewer. For more information about plug-in error information, see Identifying Plug-in Errors.

Example Code

using Microsoft.WindowsMediaServices.Interop;
using System.Runtime.InteropServices;

void IWMSCacheProxy.QueryCache(string bstrOriginUrl , 
            IWMSContext pUserContext , 
            IWMSCommandContext pCommandContext , 
            IWMSContext pPresentationContext , 
            int lQueryType , 
            IWMSCacheProxyCallback pCallback , 
            object varContext )
{
  try
  {
    int nFlag = (int)WMS_CACHE_QUERY_TYPE_FLAGS.WMS_CACHE_QUERY_OPEN;
    int nOpen = lQueryType & nFlag;
    int nGCI = lQueryType & ((int)WMS_CACHE_QUERY_TYPE_FLAGS.WMS_CACHE_QUERY_GET_CONTENT_INFO);
    int nReverseProxy = lQueryType & ((int)WMS_CACHE_QUERY_TYPE_FLAGS.WMS_CACHE_QUERY_REVERSE_PROXY);
            
    // Either a get content information (GCI) or an open call is made.
    if((nOpen!=0)||(nGCI!=0)) 
    {
      // Allocate a content information context.
      IWMSContext Context;
      WMS_CACHE_QUERY_RESPONSE Response = WMS_CACHE_QUERY_RESPONSE.WMS_CACHE_QUERY_MISS;

      // The ContentInfo object is user-defined. It contains
      // information about an item of cached content.
      ContentInfo ci;
      GetContentInfo(bstrOriginUrl,out ci);
      ci.CacheProxyCallback = pCallback;
      ci.varContext = varContext;

      // Retrieve the new content information context.
      GetContentInfoContext(ci,out Context);

      bool bQueryCache = true;
      bool bOnDemand = true;

      // This is not a reverse proxy, and content has been cached.
      if((ci.CacheUrl!=null) && (nReverseProxy==0))
      {
        // Convert the current time to UTC time.
        DateTime now = DateTime.Now;
        now = now.ToUniversalTime();

        // If the content has not expired, declare a cache hit.
        if(ci.ExpirationTime > now)
        {
          // The content is a broadcast. Declare a cache
          // hit and set the cache policy to play the broadcast.
          if((ci.ContentType & 1 )!=0)
          {
            Response = WMS_CACHE_QUERY_RESPONSE.WMS_CACHE_QUERY_HIT_PLAY_BROADCAST;
            bOnDemand = false;
          }
          // The content is on demand. Declare a cache
          // hit and set the cache policy to play the content on demand.
          else
          {
            Response = WMS_CACHE_QUERY_RESPONSE.WMS_CACHE_QUERY_HIT_PLAY_ON_DEMAND;
            bOnDemand = true;
          }
        }

        // The content has expired. 
        else
        {
          if(nOpen!=0)
          {
            bQueryCache = false;
          }
        }
      }

      // The content has not expired. Call OnQueryCache() and send the
      // Response parameter and the content information context
      // back to the server. 
      if(bQueryCache)
      {
        // Retrieve the cache URL from the user-defined content
        // information object.
        string CacheUrl = ci.CacheUrl;
        if(bOnDemand)
        {
          CacheUrl = string.Format("file://{0}",ci.CacheUrl);
        }
        pCallback.OnQueryCache( 0,
                            Response,
                            CacheUrl,
                            Context,
                            null,
                            varContext);
      }

      // The content has expired. Call CompareContentInformation (for
      // open requests only).
      else
      {
        CacheProxyServer.CompareContentInformation(bstrOriginUrl,
                                                 Context,
                                                 pPresentationContext,
                                                 this,
                                                 null,
                                                 this,
                                                 (object)ci);
      }

    }

    // The request is not a GCI or open call. Determine what the
    // request is. 
    else
    {
      // Determine whether the call is for event propagation.
      int nCacheEvent = lQueryType & ((int)WMS_CACHE_QUERY_TYPE_FLAGS.WMS_CACHE_QUERY_CACHE_EVENT);
      int nLocalEvent = lQueryType & ((int)WMS_CACHE_QUERY_TYPE_FLAGS.WMS_CACHE_QUERY_LOCAL_EVENT);
      if((nCacheEvent | nLocalEvent)!=0)
      {
        // Declare a cache miss and ask the server to
        // forward the request.
        WMS_CACHE_QUERY_RESPONSE Response = WMS_CACHE_QUERY_RESPONSE.WMS_CACHE_QUERY_MISS;
        pCallback.OnQueryCache( 0,
                              Response,
                              bstrOriginUrl,
                              null,
                              null,
                              varContext);
      }
    }
  }

  catch(Exception e)
  {
    throw new COMException();
  }

  return;
}

Requirements

Reference: Add a reference to Microsoft.WindowsMediaServices.

Namespace: Microsoft.WindowsMediaServices.Interop.

Assembly: Microsoft.WindowsMediaServices.dll.

Library: WMSServerTypeLib.dll.

Platform: Windows Server 2003, Enterprise Edition; Windows Server 2003, Datacenter Edition; Windows Server 2008.

See Also

Previous Next