const _tstring assembly = _T("ManagedHost, Version=1.0.0.0, PublicKeyToken=24e328fd91cb8ee6"); const _tstring type = _T("EchoServerCLR.ManagedHost"); const _tstring welcomeMessage = commandLine.NoWelcomeMessage() ? _T("") : _T("Welcome to echo server\r\n"); CManagedHost managedHost( socketAllocator, welcomeMessage, assembly, type); managedHost.Start(); const CFullAddress address1( commandLine.Server(), commandLine.Port()); CSocketServer server1( managedHost, false, address1, listenBacklog, pool, socketAllocator, bufferAllocator, connectionLimiter); const CFullAddress address2( commandLine.Server(), commandLine.Port() + 1); CSocketServer server2( managedHost, true, address2, listenBacklog, pool, socketAllocator, bufferAllocator, connectionLimiter);
CSocketServer::CSocketServer( CManagedHost &managedHost, const bool createPerConnectionApplicationDomain, const IFullAddress &address, const ListenBacklog listenBacklog, IIOPool &pool, IAllocateStreamSockets &socketAllocator, IAllocateBuffers &bufferAllocator, ILimitConnections &connectionLimiter) : CStreamSocketServer(address, listenBacklog, *this, pool, socketAllocator, bufferAllocator, NoZeroByteRead, connectionLimiter), m_managedHost(managedHost), m_createPerConnectionApplicationDomain(createPerConnectionApplicationDomain) { } CSocketServer::~CSocketServer() { WaitForShutdownToComplete(); } void CSocketServer::OnConnectionEstablished( IStreamSocket &socket, const IAddress & /*address*/) { Output(_T("OnConnectionEstablished")); m_managedHost.OnConnectionEstablished(m_createPerConnectionApplicationDomain, socket, socket); } void CSocketServer::OnConnectionClientClose( IStreamSocket &socket) { Output(_T("OnConnectionClientClose")); m_managedHost.OnConnectionClientClose(socket); } void CSocketServer::OnReadCompleted( IStreamSocket &socket, IBuffer &buffer) { m_managedHost.ReadCompleted(socket, buffer); } void CSocketServer::OnSocketReleased( IIndexedOpaqueUserData &userData) { m_managedHost.OnSocketReleased(userData); }
IHostControl
and passes itself to its JetByteTools::CLRHosting::CCLRHost member variable when it starts the CLR. CCLRHost
is responsible for loading and managing the CLR. void CManagedHost::OnConnectionEstablished( const bool createPerConnectionApplicationDomain, IIndexedOpaqueUserData &userData, IStreamSocket &socket) { IManagedHost *pDefaultManager = GetDefaultManagedHost(); IManagedHost *pManager = pDefaultManager; int id = 0; if (createPerConnectionApplicationDomain) { _bstr_t name(L"Name"); HRESULT hr = pDefaultManager->CreateAppDomain(name, &id); JetByteTools::COM::CException::ThrowOnFailure(_T("CManagedHost::OnConnectionEstablished()"), hr); pManager = GetManagedHost(id); } userData.SetUserPointer(m_managerIndex, pManager); userData.SetUserData(m_appDomainIDIndex, id); CSocket *pSocket = new CSocket(socket); userData.SetUserPointer(m_managedSocketIndex, pSocket); SAFEARRAY *pArray = 0; HRESULT hr = SafeArrayAllocDescriptorEx(VT_UI1, 1, &pArray); JetByteTools::COM::CException::ThrowOnFailure(_T("CManagedHost::OnConnectionEstablished() - SafeArrayAllocDescriptorEx"), hr); pArray->pvData = 0; pArray->rgsabound[0].lLbound = 0; pArray->rgsabound[0].cElements = 0; userData.SetUserPointer(m_managedDataIndex, pArray); pManager->OnConnectionEstablished(pSocket); }
void IManagedHost.OnConnectionEstablished( ISocket socket) { socket.WriteString("Welcome to CLR echo server\r\n"); socket.Read(); }
void CManagedHost::ReadCompleted( IIndexedOpaqueUserData &userData, const IBufferBase &buffer) { try { IManagedHost *pManager = GetManagedHost(userData); CSocket *pSocket = GetManagedSocket(userData); SAFEARRAY *pArray = GetManagedData(userData, buffer); HRESULT hr = pManager->OnReadCompleted( pArray, pSocket); if (FAILED(hr)) { Output(_T("CManagedHost::ReadCompleted() - Managed call failed: ") + GetLastErrorMessage(hr)); } } catch(const CException &e) { Output(_T("CManagedHost::ReadCompleted() - Exception: ") + e.GetDetails()); } catch(...) { Output(_T("CManagedHost::ReadCompleted() - Unexpected exception")); } }
void IManagedHost.OnReadCompleted(
byte[] data,
ISocket socket)
{
socket.Write(data);
socket.Read();
}