Source code of Windows XP (NT5)
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.

135 lines
4.0 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. sharesdo.h
  7. defines classes for sharing SdoServer among property pages for different users and snapins
  8. FILE HISTORY:
  9. */
  10. //////////////////////////////////////////////////////////////////////
  11. #if !defined(__SHARE_SDO_H__)
  12. #define __SHARE_SDO_H__
  13. #if _MSC_VER >= 1000
  14. #pragma once
  15. #endif // _MSC_VER >= 1000
  16. #include <list>
  17. // this class is used to build a map of the connected SDO servers being used
  18. // the consumer of the class may NOT call ISdoMachine::Connect directly, should use Connect function
  19. // defined in this class
  20. class CSharedSdoServerPool;
  21. class CSharedSdoServerImp;
  22. class CSharedSdoServer;
  23. // implementation class of shared server
  24. // used by CSdoServerPool and CMarshalSdoServer
  25. class CSharedSdoServerImp
  26. {
  27. friend class CSdoServerPool;
  28. friend class CMarshalSdoServer;
  29. protected: // only be used by friends and derived ones
  30. CSharedSdoServerImp(LPCTSTR machine, LPCTSTR user, LPCTSTR passwd);
  31. ~CSharedSdoServerImp()
  32. {
  33. // no longer check this, this could be different
  34. // ASSERT(threadId == GetCurrentThreadId());
  35. spServer.Release();
  36. };
  37. // to make this class element of collection, provide following member functions
  38. bool IsFor(LPCTSTR machine, LPCTSTR user, LPCTSTR passwd) const;
  39. // CoCreate SdoServer object
  40. HRESULT CreateServer();
  41. // get marshal stream, can specify, if immediate connection is required.
  42. HRESULT GetMarshalStream(LPSTREAM *ppStream, bool* pbConnect /* both input and output */);
  43. // Connect the server to the a machine
  44. HRESULT Connect(ISdoMachine* pMarshaledServer /* NULL, when calling from the same thread */);
  45. // Used by different thread, to retrived marshaled interface out of the stream
  46. static HRESULT GetServerNReleaseStream(LPSTREAM pStream, ISdoMachine** ppServer);
  47. private:
  48. CString strMachine; // name of the serve to connect to
  49. CString strUser; // user id used to connect
  50. CString strPasswd; // user's passwd
  51. CComPtr<ISdoMachine> spServer; // ISdoInterface, created(not yet connected), or connected
  52. bool bConnected;
  53. CCriticalSection cs;
  54. DWORD threadId; // the thread ID of the creating thread
  55. };
  56. // used between thread which managed the SdoServerPool and consumer of the pool
  57. class CMarshalSdoServer
  58. {
  59. friend CSdoServerPool;
  60. public:
  61. CMarshalSdoServer();
  62. ~CMarshalSdoServer()
  63. {
  64. spServer.Release();
  65. spStm.Release();
  66. pImp = NULL;
  67. };
  68. // if connection is needed, should call the connec of CSharedSdoServer, rather than ISdoMachine::Connect
  69. // this should be used by a different thread to get marshaled interface
  70. HRESULT GetServer(ISdoMachine** ppServer);
  71. // connect the sdo server
  72. HRESULT Connect();
  73. // release the data members
  74. void Release();
  75. protected:
  76. void SetInfo(IStream* pStream, CSharedSdoServerImp* pImp1)
  77. {
  78. spStm.Release();
  79. spStm = pStream;
  80. pImp = pImp1;
  81. };
  82. private:
  83. CComPtr<ISdoMachine> spServer;
  84. CComPtr<IStream> spStm; //
  85. CSharedSdoServerImp* pImp; // the pointer is kept in global list, no need to free it
  86. };
  87. // class used to manage a shared SdoServerPool
  88. class CSdoServerPool
  89. {
  90. public:
  91. // find a server in the pool, if there isn't, create an entry in the pool
  92. // this need bo called in the main thread
  93. HRESULT GetMarshalServer(LPCTSTR machineName, LPCTSTR userName, LPCTSTR passwd, bool* pbConnect, CMarshalSdoServer* pServer);
  94. ~CSdoServerPool();
  95. private:
  96. std::list<CSharedSdoServerImp*> listServers;
  97. CCriticalSection cs;
  98. };
  99. // the server pool pointer used to share SdoServer among pages and snapins
  100. extern CSdoServerPool* g_pSdoServerPool;
  101. HRESULT ConnectToSdoServer(BSTR machineName, BSTR userName, BSTR passwd, ISdoMachine** ppServer);
  102. HRESULT GetSharedSdoServer(LPCTSTR machine, LPCTSTR user, LPCTSTR passwd, bool* pbConnect, CMarshalSdoServer* pServer);
  103. #endif // !defined(__SHARE_SDO_H__)