|
/////////////////////////////////////////////////////////////////
// //
// CServerAccessPoint //
//-------------------------------------------------------------//
// By Eugene Khodakovsky //
// Nov.,2001 //
// ekhodakovsky@yahoo.com //
// Last Update: Nov., 2001 //
/////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ServerAccessPoint.h"
#include "../StreamingServer/StreamingServer_i.c"
#include "SLUtil.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CServerAccessPoint
CServerAccessPoint* CServerAccessPoint::m_pServer = NULL;
IMPLEMENT_DYNCREATE(CServerAccessPoint, CCmdTarget)
BEGIN_INTERFACE_MAP(CServerAccessPoint, CCmdTarget)
INTERFACE_PART(CServerAccessPoint, IID_IConnectionSink, ConnectionSink)
END_INTERFACE_MAP()
CServerAccessPoint::CServerAccessPoint():
m_pDcomServer(NULL),
m_dwSinkCookie(NULL),
m_pListener(NULL)
{
ASSERT(m_pServer == NULL);
m_pServer = this;
}
CServerAccessPoint::~CServerAccessPoint()
{
}
BEGIN_MESSAGE_MAP(CServerAccessPoint, CCmdTarget)
//{{AFX_MSG_MAP(CServerAccessPoint)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CServerAccessPoint Attributes
CServerAccessPoint* CServerAccessPoint::GetServer()
{
ATLASSERT(m_pServer);
return m_pServer;
}
bool CServerAccessPoint::IsValidServer()
{
bool bOk = false;
CComPtr<IUnknown> spUnknown;
if( m_pDcomServer != NULL &&
SUCCEEDED(m_pDcomServer->QueryInterface(&spUnknown))
)
{
bOk = true;
}
return bOk;
}
bool CServerAccessPoint::IsConnected()
{
return IsValidServer();
}
void CServerAccessPoint::ShowError(CString text)
{
MessageBox(NULL,text,"Server Error",MB_ICONSTOP | MB_OK);
}
void CServerAccessPoint::ShowServerError()
{
CString lpszUnknownError = "Unknown Server Error";
if(!IsValidServer())
{
ShowError("Connection is missing");
return;
}
CComPtr<ISupportErrorInfo> spSupportErrorInfo;
HRESULT hr = m_pDcomServer->QueryInterface(&spSupportErrorInfo);
if (FAILED(hr))
{
CString str; str.Format("Server Error # %d",hr);
ShowError(str);
return;
}
hr = spSupportErrorInfo->InterfaceSupportsErrorInfo(IID_SERVER);
if (FAILED(hr))
{
CString str; str.Format("Server Error # %d",hr);
ShowError(str);
return;
}
CComPtr<IErrorInfo> spErrorInfo;
if (spSupportErrorInfo != NULL)
{
hr = GetErrorInfo(NULL,&spErrorInfo);
if (FAILED(hr))
{
ShowError(lpszUnknownError);
return;
}
}
else
{
ShowError("Not supported error");
return;
}
CString strDescr,strSource;
if(spErrorInfo)
{
BSTR bstrSource = NULL;
BSTR bstrDescription = NULL;
spErrorInfo->GetDescription(&bstrDescription);
spErrorInfo->GetSource(&bstrSource);
if(bstrDescription)
{
strDescr = bstrDescription;
CoTaskMemFree(bstrDescription);
}
if(bstrSource)
{
strSource = bstrSource;
CoTaskMemFree(bstrSource);
}
ShowError(strDescr);
}
else
{
ShowError(lpszUnknownError);
}
}
/////////////////////////////////////////////////////////////////////////////
// CServerAccessPoint Operations
bool CServerAccessPoint::ServerConnect(CConnection& connObj)
{
if(IsConnected())
{
ATLASSERT(0);
return true;
}
HRESULT hr = S_OK;
CComBSTR bstrServer(connObj.GetServerName());
const ServerType = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER;
COSERVERINFO csi;
memset(&csi, 0, sizeof(COSERVERINFO));
csi.pwszName = bstrServer;
MULTI_QI mq;
mq.pIID = &IID_SERVER;
mq.pItf = NULL;
mq.hr = S_OK;
hr = CoCreateInstanceEx(CLSID_SERVER, NULL, ServerType, &csi, 1, &mq);
if (FAILED(hr))
{
m_pDcomServer = NULL;
CString str(_T("Could not create instance of DCOM object"));
ShowError(str);
return false;
}
m_pDcomServer = (I_SERVER*)mq.pItf;
hr = m_xConnectionSink.QueryInterface(IID_IUnknown, (void**) &m_pUnkSink);
if(FAILED(hr))
{
ShowError(_T("Failed QueryInterface for sink"));
m_pDcomServer->Release();
m_pDcomServer = NULL;
return false;
}
hr = AtlAdvise(m_pDcomServer,m_pUnkSink,IID_IConnectionSink,&m_dwSinkCookie);
if(FAILED(hr))
{
ShowError(_T("Could not advise connection point "));
ShowServerError();
m_dwSinkCookie = NULL;
m_pDcomServer->Release();
m_pDcomServer = NULL;
return false;
}
CComSafeArray conn,connResult;
m_connection = connObj;
m_connection.SetComputerName(GetComputerName());
m_connection.Write(conn);
hr = m_pDcomServer->Connect(&conn,&connResult);
if (FAILED(hr))
{
ShowServerError();
if(m_dwSinkCookie != NULL)
{
hr = AtlUnadvise(m_pDcomServer,IID_IConnectionSink,m_dwSinkCookie);
ATLASSERT(SUCCEEDED(hr));
}
m_connection.Clear();
m_pUnkSink = NULL;
m_dwSinkCookie = NULL;
m_pDcomServer->Release();
m_pDcomServer = NULL;
return false;
}
m_connection.Read(connResult);
return true;
}
void CServerAccessPoint::ServerDisconnect()
{
if(IsValidServer())
{
HRESULT hr = S_OK;
if(m_dwSinkCookie != NULL)
{
hr = AtlUnadvise(m_pDcomServer,IID_IConnectionSink,m_dwSinkCookie);
ATLASSERT(SUCCEEDED(hr));
}
CComSafeArray conn;
m_connection.Write(conn);
hr = m_pDcomServer->Disconnect(conn);
if(FAILED(hr))
{
ShowServerError();
}
m_pDcomServer->Release();
}
m_connection.Clear();
m_pUnkSink = NULL;
m_dwSinkCookie = NULL;
m_pDcomServer = NULL;
}
void CServerAccessPoint::OnConnectionsChanged(VARIANT* pNewConnections)
{
m_connections.CSLArray::RemoveAndDestroy();
m_connections.Read(pNewConnections);
if(m_pListener)
{
m_pListener->OnServerConnectionChanged(&m_connections);
}
}
void CServerAccessPoint::PumpMessage()
{
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage(&msg, NULL, 0, 0))
return;
DispatchMessage(&msg);
}
}
///////////////////////////////////////////////////////////////////////////////////
// Connection Sink Implementation
STDMETHODIMP_(ULONG) CServerAccessPoint::XConnectionSink::AddRef()
{
METHOD_PROLOGUE_EX(CServerAccessPoint, ConnectionSink)
TRACE(_T("AddRef\n"));
return (ULONG)pThis->ExternalAddRef();
}
STDMETHODIMP_(ULONG) CServerAccessPoint::XConnectionSink::Release()
{
METHOD_PROLOGUE_EX(CServerAccessPoint, ConnectionSink)
TRACE(_T("Release\n"));
return (ULONG)pThis->ExternalRelease();
}
STDMETHODIMP CServerAccessPoint::XConnectionSink::QueryInterface(
REFIID iid, LPVOID far * ppvObj)
{
METHOD_PROLOGUE_EX(CServerAccessPoint, ConnectionSink)
TRACE(_T("QueryInterface\n"));
return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}
STDMETHODIMP CServerAccessPoint::XConnectionSink::OnEvent(VARIANT* pEvent)
{
METHOD_PROLOGUE_EX(CServerAccessPoint, ConnectionSink)
TRACE(_T("OnComPlusEvent\n"));
pThis->OnConnectionsChanged(pEvent);
return S_OK;
}
STDMETHODIMP CServerAccessPoint::XConnectionSink::OnPropertyChanged(VARIANT*pProperty)
{
METHOD_PROLOGUE_EX(CServerAccessPoint, ConnectionSink)
TRACE(_T("OnPropertyChanged\n"));
return S_OK;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.