bool bAllowAnonymousAccess);
void Disconnect(void);
// IUnknown methods
STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
// IChatSession methods
STDMETHODIMP get_SessionName(OLECHAR **ppwsz);
STDMETHODIMP Say(const OLECHAR *pwszStatement);
STDMETHODIMP GetStatements(IEnumString **ppes);
STDMETHODIMP Advise(IChatSessionEvents *pEventSink,
DWORD *pdwReg);
STDMETHODIMP Unadvise(DWORD dwReg);
};
// this class enumerates the statements of a session
class StatementEnumerator : public IEnumString
{
LONG m_cRef;
ChatSession *m_pThis;
vector<wstring>::iterator m_cursor;
CRITICAL_SECTION m_csLock;
protected:
void Lock(void);
void Unlock(void);
virtual ~StatementEnumerator(void);
public:
StatementEnumerator(ChatSession *pThis);
// IUnknown methods
STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
// IEnumString methods
STDMETHODIMP Next(ULONG cElems, OLECHAR **rgElems,
ULONG *pcFetched);
STDMETHODIMP Skip(ULONG cElems);
STDMETHODIMP Reset(void);
STDMETHODIMP Clone(IEnumString **ppes);
};
// this class models the management of chat sessions
// and acts as the class object for CLSID_ChatSession
class ChatSessionClass : public IChatSessionManager,
public IExternalConnection
{
friend class SessionNamesEnumerator;
typedef map<wstring, ChatSession *> SESSIONMAP;
LONG m_cStrongLocks;
SESSIONMAP m_sessions;
CRITICAL_SECTION m_csSessionLock;
void Lock(void);
void Unlock(void);
bool CheckAccess(const OLECHAR *pwszUser);
public:
virtual ~ChatSessionClass(void);
ChatSessionClass(void);
// IUnknown methods
STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
// IExternalConnection methods
STDMETHODIMP_(DWORD) AddConnection(DWORD extconn, DWORD);
STDMETHODIMP_(DWORD) ReleaseConnection(DWORD extconn, DWORD,
BOOL bLastReleaseKillsStub);
// IChatSessionManager methods
STDMETHODIMP GetSessionNames(IEnumString **ppes);
STDMETHODIMP FindSession(const OLECHAR *pwszSessionName,
BOOL bDontCreate,
BOOL bAllowAnonymousAccess,
IChatSession **ppcs);
STDMETHODIMP DeleteSession(const OLECHAR *pwszSessionName);
};
// this class enumerates the session names of a server
class SessionNamesEnumerator : public IEnumString
{
LONG m_cRef;
vector<wstring> *m_pStrings;
SessionNamesEnumerator *m_pCloneSource;
vector<wstring>::iterator m_cursor;
CRITICAL_SECTION m_csLock;
protected:
vector<wstring>& Strings(void);
void Lock(void);
void Unlock(void);
virtual ~SessionNamesEnumerator(void);
public:
SessionNamesEnumerator(ChatSessionClass *pSessionClass);
SessionNamesEnumerator(SessionNamesEnumerator *pCloneSource);
// IUnknown methods
STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
// IEnumString methods
STDMETHODIMP Next(ULONG cElems, OLECHAR **rgElems,
ULONG *pcFetched);
STDMETHODIMP Skip(ULONG cElems);
STDMETHODIMP Reset(void);
STDMETHODIMP Clone(IEnumString **ppes);
};
#endif
/////////////////////////////////////////////////////
//
// ChatSession.cpp
//
// Copyright 1997, Don Box/Addison Wesley
//
// This code accompanies the book "The Component
// Object Model" from Addison Wesley. Blah blah blah
//
//
#include «ChatSession.h»
#include <iaccess.h>
// these routines are defined in svc.cpp to
// control server lifetime
extern void ModuleLock(void);
extern void ModuleUnlock(void);
// these access control objects are created
// in svc.cpp to control various privileged
// operations. Most operations in this class
// are non-privileged, so anyone can get in.
extern IAccessControl *g_pacUsers;
extern IAccessControl *g_pacAdmins;
// utility functions /////////////////////////
// duplicate an OLECHAR * using CoTaskMemAlloc
OLECHAR *OLESTRDUP(const OLECHAR *pwsz)
{
DWORD cb = sizeof(OLECHAR)*(wcslen(pwsz) + 1);
OLECHAR *pwszResult = (OLECHAR*)CoTaskMemAlloc(cb);
if (pwszResult)
wcscpy(pwszResult, pwsz);
return pwszResult;
}
// get the caller's username (or «anonymous» if
// no authentication was specified by the caller).
OLECHAR *GetCaller(void)
{
OLECHAR *pwsz = 0;
HRESULT hr = CoQueryClientBlanket(0,0,0,0,0,(void**)&pwsz,0);
if (SUCCEEDED(hr))
return OLESTRDUP(pwsz);
else
return OLESTRDUP(OLESTR(«anonymous»));
}
// class ChatSession ///////////////////////////////
ChatSession::ChatSession(const OLECHAR *pwszSessionName,
bool bAllowAnonymousAccess)
: m_cRef(0),
m_bAllowAnonymousAccess(bAllowAnonymousAccess),
m_pHeadListeners(0)
{
wcscpy(m_wszSessionName, pwszSessionName);
InitializeCriticalSection(&m_csStatementLock);
InitializeCriticalSection(&m_csAdviseLock);
}
ChatSession::~ChatSession(void)
{
DeleteCriticalSection(&m_csStatementLock);
DeleteCriticalSection(&m_csAdviseLock);
// tear down connected listeners
while (m_pHeadListeners)
{
LISTENER *pThisNode = m_pHeadListeners;
if (pThisNode->pItf)
pThisNode->pItf->Release();
if (pThisNode->pwszUser)
CoTaskMemFree(pThisNode->pwszUser);
m_pHeadListeners = pThisNode->pNext;
delete pThisNode;
}
}
// helper methods ///////////
void ChatSession::Disconnect(void)
{
CoDisconnectObject(this, 0);
// tear down connected listeners
ALock();
while (m_pHeadListeners)
{
LISTENER *pThisNode = m_pHeadListeners;
if (pThisNode->pItf)
pThisNode->pItf->Release();
if (pThisNode->pwszUser)
CoTaskMemFree(pThisNode->pwszUser);
m_pHeadListeners = pThisNode->pNext;
delete pThisNode;
}
AUnlock();
}
// send the OnNewStatement event to all listeners
void
ChatSession::Fire_OnNewStatement(const OLECHAR *pwszUser,
const OLECHAR *pwszStatement)
{
ALock();
for (LISTENER *pNode = m_pHeadListeners;
pNode != 0; pNode = pNode->pNext)
{
if (pNode->pItf)
pNode->pItf->OnNewStatement(pwszUser, pwszStatement);
}
AUnlock();
}
// send the OnNewUser event to all listeners
void
ChatSession::Fire_OnNewUser(const OLECHAR *pwszUser)
{
ALock();
for (LISTENER *pNode = m_pHeadListeners;
pNode != 0; pNode = pNode->pNext)
{
if (pNode->pItf)
pNode->pItf->OnNewUser(pwszUser);
}
AUnlock();
}
// send the OnUserLeft event to all listeners
void
ChatSession::Fire_OnUserLeft(const OLECHAR *pwszUser)
{
ALock();
for (LISTENER *pNode = m_pHeadListeners;
pNode != 0; pNode = pNode->pNext)
{
if (pNode->pItf)
pNode->pItf->OnUserLeft(pwszUser);
}
AUnlock();
}
// lock wrappers
void ChatSession::SLock(void)
{
EnterCriticalSection(&m_csStatementLock);
}
void ChatSession::SUnlock(void)
{
LeaveCriticalSection(&m_csStatementLock);
}
void ChatSession::ALock(void)
{
EnterCriticalSection(&m_csAdviseLock);
}
void ChatSession::AUnlock(void)
{
LeaveCriticalSection(&m_csAdviseLock);
}
// helper method to check access to Say method
bool
ChatSession::CheckAccess(const OLECHAR *pwszUser)
{
if (wcscmp(pwszUser, L"anonymous") == 0)
return m_bAllowAnonymousAccess;
// form trustee from caller and use Access Control
// object hardwired to COMChat Users group
TRUSTEEW trustee = {
0, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME,
TRUSTEE_IS_USER,
const_cast<OLECHAR*>(pwszUser)
};
BOOL bIsAllowed;
HRESULT hr = g_pacUsers->IsAccessAllowed(&trustee,0,
COM_RIGHTS_EXECUTE,
&bIsAllowed);
return SUCCEEDED(hr) && bIsAllowed != FALSE;
}
// IUnknown methods
STDMETHODIMP
ChatSession::QueryInterface(REFIID riid, void **ppv)
{
if (riid == IID_IUnknown)
*ppv = static_cast<IChatSession*>(this);
else if (riid == IID_IChatSession)
*ppv = static_cast<IChatSession*>(this);
else
return (*ppv = 0), E_NOINTERFACE;
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG)
ChatSession::AddRef(void)
{
ModuleLock();
return InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG)
ChatSession::Release(void)
{
LONG res = InterlockedDecrement(&m_cRef);
if (res == 0)
delete this;
ModuleUnlock();
return res;
}
// IChatSession methods
STDMETHODIMP
ChatSession::get_SessionName(OLECHAR **ppwsz)
{
if (!ppwsz)
return E_INVALIDARG;
else if ((*ppwsz = OLESTRDUP(m_wszSessionName)) == 0)
return E_OUTOFMEMORY;
return S_OK;
}
STDMETHODIMP
ChatSession::Say(const OLECHAR *pwszStatement)
{
HRESULT hr = S_OK;
// protect access to method
OLECHAR *pwszUser = GetCaller();
if (pwszUser && CheckAccess(pwszUser))
{
SLock();
try
{
wstring s = pwszUser;
s += L":";
s += pwszStatement;
m_statements.push_back(s);
}
catch(...)
{
hr = E_OUTOFMEMORY;
}
SUnlock();
if (SUCCEEDED(hr))
Fire_OnNewStatement(pwszUser, pwszStatement);
}
else
hr = E_ACCESSDENIED;
CoTaskMemFree(pwszUser);
return hr;
}
STDMETHODIMP
ChatSession::GetStatements(IEnumString **ppes)
{
if (ppes == 0)
return E_INVALIDARG;
*ppes = new StatementEnumerator(this);
if (*ppes == 0)
return E_OUTOFMEMORY;
(*ppes)->AddRef();
return S_OK;
}
STDMETHODIMP
ChatSession::Advise(IChatSessionEvents *pEventSink,
DWORD *pdwReg)
{
HRESULT hr = S_OK;
if (pEventSink == 0 || pdwReg == 0)
return E_INVALIDARG;
LISTENER *pNew = new LISTENER;
if (pNew == 0)
return E_OUTOFMEMORY;
OLECHAR *pwszUser = GetCaller();
if (pwszUser)
{
Fire_OnNewUser(pwszUser);
ALock();
pNew->pwszUser = pwszUser;
if (pNew->pItf = pEventSink)
pEventSink->AddRef();
pNew->pNext = m_pHeadListeners;
if (m_pHeadListeners)
m_pHeadListeners->pPrev = pNew;
pNew->pPrev = 0;
m_pHeadListeners = pNew;
AUnlock();
}
else
{
delete pNew;
return E_OUTOFMEMORY;
}
*pdwReg = reinterpret_cast<DWORD>(pNew);
return hr;
}
STDMETHODIMP
ChatSession::Unadvise(DWORD dwReg)
{
if (dwReg == 0)
return E_INVALIDARG;
HRESULT hr = S_OK;
LISTENER *pThisNode = reinterpret_cast<LISTENER *>(dwReg);
ALock();
if (pThisNode->pPrev)
pThisNode->pPrev->pNext = pThisNode->pNext;
else