1 // This is a part of the Active Template Library.
2 // Copyright (C) Microsoft Corporation
3 // All rights reserved.
5 // This source code is only intended as a supplement to the
6 // Active Template Library Reference and related
7 // electronic documentation provided with the library.
8 // See these sources for detailed information regarding the
9 // Active Template Library product.
11 #ifndef __ATLSHAREDSVC_H__
12 #define __ATLSHAREDSVC_H__
18 #pragma pack(push,_ATL_PACKING)
21 #ifndef ATL_SHAREDBLOBCACHE_TIMEOUT
22 #define ATL_SHAREDBLOBCACHE_TIMEOUT 36000000000 // in 100 nano second intervals
23 // each entry will be free'd if
24 // no access in 1 hour.
27 // Interface used by to access the shared blob cache.
28 [ uuid("AB4AF9CD-8DB1-4974-A617-CF0449578FB9"), object
]
29 __interface ISharedBlobCache
31 [id(0)] STDMETHOD(AddItem
)([in
] BSTR szItemName
, [in
] BSTR szData
);
32 [id(1)] STDMETHOD(GetItem
)([in
] BSTR szItemName
, [out
,retval
] BSTR
*szData
);
36 public CBlobCache
<CWorkerThread
<>, CStdStatClass
>,
37 public IMemoryCacheClient
,
38 public ISharedBlobCache
40 typedef CBlobCache
<CWorkerThread
<>, CStdStatClass
> basecache
;
43 // IMemoryCacheClient method, frees data in the memory cache.
44 STDMETHOD( Free
)(const void *pvData
)
48 ::SysFreeString((BSTR
)pvData
);
54 STDMETHODIMP
AddItem(BSTR szItemName
, BSTR szData
)
57 HRESULT hr
= E_UNEXPECTED
;
59 // We make a copy of the BSTR and stick it in the cache.
60 // The BSTR will be freed in our IMemoryCacheClient::Free
61 // implementation above.
62 BSTR szEntry
= SysAllocString(szData
);
66 // create a time span and for the entry
67 CFileTime tm
= CFileTime::GetCurrentTime();
69 span
.SetTimeSpan(ATL_SHAREDBLOBCACHE_TIMEOUT
);
72 hr
= basecache::Add(OLE2A_EX(szItemName
, _ATL_SAFE_ALLOCA_DEF_THRESHOLD
), szEntry
, sizeof(BSTR
),
73 &tm
, _AtlBaseModule
.m_hInst
, &h
, static_cast<IMemoryCacheClient
*>(this));
77 // On successful add, we have to release our
78 // reference on the entry.
79 basecache::ReleaseEntry(h
);
85 STDMETHODIMP
GetItem(BSTR szItemName
, BSTR
*szData
)
88 HRESULT hr
= E_UNEXPECTED
;
89 HCACHEITEM hEntry
= NULL
;
91 if (!szItemName
|| !szData
)
94 hr
= basecache::LookupEntry(OLE2A_EX(szItemName
, _ATL_SAFE_ALLOCA_DEF_THRESHOLD
), &hEntry
);
99 hr
= basecache::GetData(hEntry
, &pData
, &dwSize
);
102 // make a copy of the string
103 *szData
= ::SysAllocString((BSTR
)pData
);
105 basecache::ReleaseEntry(hEntry
);
111 STDMETHODIMP
QueryInterface(REFIID riid
, void **ppv
)
113 HRESULT hr
= E_NOINTERFACE
;
114 if (InlineIsEqualGUID(__uuidof(IMemoryCacheClient
), riid
)||
115 InlineIsEqualGUID(__uuidof(IUnknown
), riid
))
117 *ppv
= static_cast<void*>(static_cast<IMemoryCacheClient
*>(this));
120 else if( InlineIsEqualGUID(__uuidof(ISharedBlobCache
), riid
))
122 *ppv
= static_cast<void*>(static_cast<ISharedBlobCache
*>(this));
127 ULONG STDMETHODCALLTYPE
AddRef()
131 ULONG STDMETHODCALLTYPE
Release()
138 // This class implements the SOAP interface for the shared blob cache.
141 name
="SharedBlobCache",
142 namespace="http://www.microsoft.com/vc/atlserver/soap/SharedBlobCache",
146 name
="SharedBlobCache",
147 sdl
="GenSharedBlobCacheWSDL"
150 class CSharedCacheHandler
:
151 public ISharedBlobCache
155 STDMETHOD(AddItem
)(BSTR szItemName
, BSTR szData
)
159 return m_spMemCache
->AddItem(szItemName
, szData
);
163 STDMETHOD(GetItem
)(BSTR szItemName
, BSTR
*szData
)
167 return m_spMemCache
->GetItem(szItemName
, szData
);
170 HTTP_CODE
Initialize(IServiceProvider
*pProvider
)
172 ATLASSERT(pProvider
); // should never be NULL
174 return HTTP_ERROR(500, ISE_SUBERR_UNEXPECTED
);
177 return HTTP_SUCCESS
; // already initialized
179 pProvider
->QueryService(__uuidof(ISharedBlobCache
), &m_spMemCache
);
180 return m_spMemCache
? HTTP_SUCCESS
: HTTP_ERROR(500, ISE_SUBERR_UNEXPECTED
);
183 // override HandleRequest to Initialize our m_spServiceProvider
184 // and to handle authorizing the client.
185 HTTP_CODE
HandleRequest(AtlServerRequest
*pRequestInfo
, IServiceProvider
*pProvider
)
187 HTTP_CODE dwErr
= Initialize(pProvider
);
188 if (dwErr
!= HTTP_SUCCESS
)
191 dwErr
= CSoapHandler
<CSharedCacheHandler
>::HandleRequest(pRequestInfo
,
195 CComPtr
<ISharedBlobCache
> m_spMemCache
;
202 #endif // __ATLSHAREDSVC_H__