Сущность технологии СОМ. Библиотека программиста   ::   Бокс Дональд

Страница: 517 из 528

r/>
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(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(this);

else if (riid == IID_IChatSession)

*ppv = static_cast(this);

else

return (*ppv = 0), E_NOINTERFACE;

reinterpret_cast(*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(pNew);

return hr;

}

STDMETHODIMP

ChatSession::Unadvise(DWORD dwReg)

{

if (dwReg == 0)

return E_INVALIDARG;

HRESULT hr = S_OK;

LISTENER *pThisNode = reinterpret_cast(dwReg);

ALock();

if (pThisNode->pPrev)

pThisNode->pPrev->pNext = pThisNode->pNext;

else

m_pHeadListeners = pThisNode->pNext;

if (pThisNode->pNext)

pThisNode->

|< Пред. 515 516 517 518 519 След. >|

Java книги

Контакты: [email protected]