Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

425 lines
11 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. CTSRDPServerChannelMgr.h
  5. Abstract:
  6. This module contains the TSRDP server-side subclass of
  7. CRemoteDesktopChannelMgr. Classes in this hierarchy are used to multiplex
  8. a single data channel into multiple client channels.
  9. CRemoteDesktopChannelMgr handles most of the details of multiplexing
  10. the data. Subclasses are responsible for implementing the details of
  11. interfacing with the transport for the underlying single data channel.
  12. The CTSRDPServerChannelMgr class creates a named pipe that
  13. can be connected to by the TSRDP Assistant SessionVC Add-In. The TSRDP
  14. Assistant Session VC Add-In acts as a proxy for virtual channel data
  15. from the client-side Remote Desktop Host ActiveX Control. A background
  16. thread in this class handles the movement of data between an instance
  17. of this class and the proxy.
  18. Author:
  19. Tad Brockway 02/00
  20. Revision History:
  21. --*/
  22. #ifndef __CTSRDPSERVERDATACHANNELMGR_H__
  23. #define __CTSRDPSERVERDATACHANNELMGR_H__
  24. #include <DataChannelMgr.h>
  25. #include <atlbase.h>
  26. #include <aclapi.h>
  27. #include <RemoteDesktopChannels.h>
  28. #include <rdshost.h>
  29. #include <resource.h>
  30. #include <ServerDataChannelMgrP.h>
  31. ///////////////////////////////////////////////////////
  32. //
  33. // CTSRDPServerDataChannel
  34. //
  35. // TSRDP Server-Specific Subclass of CRemoteDesktopDataChannel.
  36. //
  37. class ATL_NO_VTABLE CTSRDPServerDataChannel :
  38. public CRemoteDesktopDataChannel,
  39. public CComObjectRootEx<CComSingleThreadModel>,
  40. public CComCoClass<CTSRDPServerDataChannel, &CLSID_TSRDPServerDataChannel>,
  41. public IConnectionPointContainerImpl<CTSRDPServerDataChannel>,
  42. public IDispatchImpl<ISAFRemoteDesktopDataChannel, &IID_ISAFRemoteDesktopDataChannel, &LIBID_RDSSERVERHOSTLib>,
  43. public IProvideClassInfo2Impl<&CLSID_TSRDPServerDataChannelMgr, NULL, &LIBID_RDSSERVERHOSTLib>,
  44. public CProxy_ISAFRemoteDesktopDataChannelEvents< CTSRDPServerDataChannel >
  45. {
  46. protected:
  47. //
  48. // Scriptable Event Callback Object
  49. //
  50. CComPtr<IDispatch> m_OnChannelDataReady;
  51. //
  52. // Back pointer to the channel manager.
  53. //
  54. CRemoteDesktopChannelMgr *m_ChannelMgr;
  55. public:
  56. //
  57. // Constructor/Destructor
  58. //
  59. CTSRDPServerDataChannel();
  60. virtual ~CTSRDPServerDataChannel();
  61. //
  62. // Initialize an instance of this class.
  63. //
  64. virtual void Initialize(
  65. CRemoteDesktopChannelMgr *mgr,
  66. BSTR channelName
  67. )
  68. {
  69. m_ChannelMgr = mgr;
  70. m_ChannelName = channelName;
  71. }
  72. DECLARE_REGISTRY_RESOURCEID(IDR_TSRDPSERVERDATACHANNEL)
  73. DECLARE_PROTECT_FINAL_CONSTRUCT()
  74. BEGIN_COM_MAP(CTSRDPServerDataChannel)
  75. COM_INTERFACE_ENTRY(ISAFRemoteDesktopDataChannel)
  76. COM_INTERFACE_ENTRY2(IDispatch, ISAFRemoteDesktopDataChannel)
  77. COM_INTERFACE_ENTRY(IProvideClassInfo)
  78. COM_INTERFACE_ENTRY(IProvideClassInfo2)
  79. COM_INTERFACE_ENTRY(IConnectionPointContainer)
  80. END_COM_MAP()
  81. BEGIN_CONNECTION_POINT_MAP(CTSRDPServerDataChannel)
  82. CONNECTION_POINT_ENTRY(DIID__ISAFRemoteDesktopDataChannelEvents)
  83. END_CONNECTION_POINT_MAP()
  84. //
  85. // ISAFRemoteDesktopDataChannel Methods
  86. //
  87. // The parent class handles the details of these methods.
  88. //
  89. STDMETHOD(ReceiveChannelData)(/*[out, retval]*/BSTR *data);
  90. STDMETHOD(SendChannelData)(BSTR data);
  91. STDMETHOD(put_OnChannelDataReady)(/*[in]*/ IDispatch * newVal);
  92. STDMETHOD(get_ChannelName)(/*[out, retval]*/ BSTR *pVal);
  93. //
  94. // Called to return our ISAFRemoteDesktopDataChannel interface.
  95. //
  96. virtual HRESULT GetISAFRemoteDesktopDataChannel(
  97. ISAFRemoteDesktopDataChannel **channel
  98. ) {
  99. HRESULT hr;
  100. hr = this->QueryInterface(
  101. IID_ISAFRemoteDesktopDataChannel, (PVOID*)channel
  102. );
  103. return hr;
  104. }
  105. //
  106. // Called by the data channel manager when data is ready on our channel.
  107. //
  108. virtual VOID DataReady();
  109. //
  110. // Return this class name.
  111. //
  112. virtual const LPTSTR ClassName() { return TEXT("CTSRDPServerDataChannel"); }
  113. };
  114. ///////////////////////////////////////////////////////
  115. //
  116. // CTSRDPServerChannelMgr
  117. //
  118. class CTSRDPRemoteDesktopSession;
  119. class CTSRDPServerChannelMgr : public CRemoteDesktopChannelMgr,
  120. public CComObjectRootEx<CComSingleThreadModel>,
  121. public CComCoClass<CTSRDPServerChannelMgr, &CLSID_TSRDPServerDataChannelMgr>,
  122. public IDispatchImpl<ISAFRemoteDesktopChannelMgr, &IID_ISAFRemoteDesktopChannelMgr, &LIBID_RDSSERVERHOSTLib>,
  123. public IDispatchImpl<IRDSThreadBridge, &IID_IRDSThreadBridge, &LIBID_RDSSERVERHOSTLib>
  124. {
  125. private:
  126. //
  127. // Named pipe connection to TSRDP Assistant Session VC Add-In
  128. //
  129. HANDLE m_VCAddInPipe;
  130. BOOL m_Connected;
  131. BOOL m_Initialized;
  132. //
  133. // Management of Bridge between Background Thread and STA
  134. // for this component.
  135. //
  136. LPSTREAM m_IOThreadBridgeStream;
  137. DWORD m_IOThreadBridgeThreadID;
  138. IRDSThreadBridge *m_IOThreadBridge;
  139. //
  140. // Back Pointer to the TSRDP Session Object
  141. //
  142. CTSRDPRemoteDesktopSession *m_RDPSessionObject;
  143. //
  144. // Incoming buffer and size.
  145. //
  146. BSTR m_IncomingBuffer;
  147. DWORD m_IncomingBufferSize;
  148. //
  149. // Handle to background thread and related events.
  150. //
  151. HANDLE m_IOThreadHndl;
  152. DWORD m_IOThreadID;
  153. HANDLE m_ReadIOCompleteEvent;
  154. HANDLE m_WriteIOCompleteEvent;
  155. HANDLE m_PipeCreateEvent;
  156. //
  157. // Machine Assistant Account Name
  158. //
  159. CComBSTR m_AssistAccount;
  160. //
  161. // Help session ID for the help session associated with this
  162. // instance of the channel manager.
  163. //
  164. CComBSTR m_HelpSessionID;
  165. //
  166. // Shutdown flag.
  167. //
  168. BOOL m_ThreadShutdownFlag;
  169. //
  170. // ThreadLock
  171. //
  172. CRITICAL_SECTION m_cs;
  173. #if DBG
  174. LONG m_LockCount;
  175. #endif
  176. //
  177. // ThreadLock/ThreadUnlock an instance of this class.
  178. //
  179. VOID ThreadLock();
  180. VOID ThreadUnlock();
  181. //
  182. // Background Thread Managing Named Pipe Connection to the
  183. // TSRDP Assistant SessionVC Add-In.
  184. //
  185. DWORD IOThread();
  186. static DWORD _IOThread(CTSRDPServerChannelMgr *instance);
  187. //
  188. // Process messages on the named pipe until it disconnects or
  189. // until the shutdown flag is set.
  190. //
  191. VOID ProcessPipeMessagesUntilDisconnect();
  192. //
  193. // Get the SID for a particular user.
  194. //
  195. PSID GetUserSid(HANDLE userToken);
  196. //
  197. // Release security attribs allocated via a call to
  198. // GetPipeSecurityAttribs
  199. //
  200. VOID FreePipeSecurityAttribs(PSECURITY_ATTRIBUTES attribs);
  201. //
  202. // Returns the security attribs for the named pipe.
  203. //
  204. PSECURITY_ATTRIBUTES GetPipeSecurityAttribs(LPTSTR assistantUserName);
  205. //
  206. // Close the named pipe.
  207. //
  208. VOID ClosePipe();
  209. //
  210. // Called on Init/Shutdown of IO Background Thread.
  211. //
  212. DWORD IOThreadInit();
  213. DWORD IOThreadShutdown(HANDLE shutDownEvent);
  214. //
  215. // Help the parent class out by opening the right channel object
  216. // for the platform.
  217. //
  218. virtual CRemoteDesktopDataChannel *OpenPlatformSpecificDataChannel(
  219. BSTR channelName,
  220. ISAFRemoteDesktopDataChannel **channel
  221. )
  222. {
  223. CComObject<CTSRDPServerDataChannel> *obj;
  224. obj = new CComObject<CTSRDPServerDataChannel>();
  225. if (obj != NULL) {
  226. obj->Initialize(this, channelName);
  227. obj->QueryInterface(
  228. __uuidof(ISAFRemoteDesktopDataChannel),
  229. (PVOID *)channel
  230. );
  231. // AV if mgr object goes away before datachannel object
  232. this->AddRef();
  233. }
  234. return obj;
  235. }
  236. protected:
  237. //
  238. // Send Function Invoked by Parent Class
  239. //
  240. virtual HRESULT SendData(PREMOTEDESKTOP_CHANNELBUFHEADER msg);
  241. //
  242. // Read the next message from the pipe. This function will
  243. // return, immediately, if the shutdown event is signaled.
  244. //
  245. DWORD ReadNextPipeMessage(DWORD bytesToRead, DWORD *bytesRead, PBYTE buf);
  246. public:
  247. //
  248. // Release the ref. counter we add to ourself after creating a data channel
  249. //
  250. virtual HRESULT RemoveChannel(BSTR channel) {
  251. HRESULT hr;
  252. hr = CRemoteDesktopChannelMgr::RemoveChannel(channel);
  253. if( SUCCEEDED(hr) ) {
  254. this->Release();
  255. }
  256. return hr;
  257. }
  258. //
  259. // Constructor/Destructor
  260. //
  261. CTSRDPServerChannelMgr();
  262. ~CTSRDPServerChannelMgr();
  263. //
  264. // Start/stop listening for channel data.
  265. //
  266. virtual HRESULT StartListening(BSTR assistAccount);
  267. virtual HRESULT StopListening();
  268. DECLARE_REGISTRY_RESOURCEID(IDR_TSRDPSERVERCHANNELMGR)
  269. DECLARE_PROTECT_FINAL_CONSTRUCT()
  270. BEGIN_COM_MAP(CTSRDPServerChannelMgr)
  271. COM_INTERFACE_ENTRY(ISAFRemoteDesktopChannelMgr)
  272. COM_INTERFACE_ENTRY(IDispatch)
  273. COM_INTERFACE_ENTRY(IRDSThreadBridge)
  274. END_COM_MAP()
  275. //
  276. // ISAFRemoteDesktopChannelMgr Methods
  277. //
  278. STDMETHOD(OpenDataChannel)(BSTR name, ISAFRemoteDesktopDataChannel **channel)
  279. {
  280. //
  281. // Let the parent handle it.
  282. //
  283. return OpenDataChannel_(name, channel);
  284. }
  285. //
  286. // Force a disconnect of the currently connected client.
  287. //
  288. VOID Disconnect() {
  289. StopListening();
  290. }
  291. //
  292. // IRDSThreadBridge Functions
  293. //
  294. // These functions are used to bridge functions that get called,
  295. // asynchronously, from a thread other than the STA thread associated
  296. // with an instance of this class.
  297. //
  298. STDMETHOD(ClientConnectedNotify)();
  299. STDMETHOD(ClientDisconnectedNotify)();
  300. STDMETHOD(DataReadyNotify)(BSTR data);
  301. //
  302. // Initialize an instance of this class.
  303. //
  304. virtual HRESULT Initialize(
  305. CTSRDPRemoteDesktopSession *sessionObject,
  306. BSTR helpSessionID
  307. );
  308. //
  309. // Return this class name.
  310. //
  311. virtual const LPTSTR ClassName()
  312. { return TEXT("CTSRDPServerChannelMgr"); }
  313. };
  314. ///////////////////////////////////////////////////////
  315. //
  316. // Inline Members
  317. //
  318. //
  319. // TODO: If nothing is using these functions,
  320. //
  321. inline VOID CTSRDPServerChannelMgr::ThreadLock()
  322. {
  323. DC_BEGIN_FN("CTSRDPServerChannelMgr::ThreadLock");
  324. #if DBG
  325. m_LockCount++;
  326. //TRC_NRM((TB, TEXT("ThreadLock count is now %ld."), m_LockCount));
  327. #endif
  328. EnterCriticalSection(&m_cs);
  329. DC_END_FN();
  330. }
  331. inline VOID CTSRDPServerChannelMgr::ThreadUnlock()
  332. {
  333. DC_BEGIN_FN("CTSRDPServerChannelMgr::ThreadUnlock");
  334. #if DBG
  335. m_LockCount--;
  336. //TRC_NRM((TB, TEXT("ThreadLock count is now %ld."), m_LockCount));
  337. ASSERT(m_LockCount >= 0);
  338. #endif
  339. LeaveCriticalSection(&m_cs);
  340. DC_END_FN();
  341. }
  342. #endif //__CTSRDPSERVERDATACHANNELMGR_H__