工具项目

SSLClient.h 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : http://www.jessma.org
  6. * Project : https://github.com/ldcsaa
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 75375912, 44636872
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "TcpClient.h"
  25. #include "SSLHelper.h"
  26. #ifdef _SSL_SUPPORT
  27. class CSSLClient : public CTcpClient
  28. {
  29. public:
  30. virtual BOOL IsSecure() {return TRUE;}
  31. virtual BOOL SendPackets(const WSABUF pBuffers[], int iCount);
  32. virtual BOOL SetupSSLContext(int iVerifyMode = SSL_VM_NONE, LPCTSTR lpszPemCertFile = nullptr, LPCTSTR lpszPemKeyFile = nullptr, LPCTSTR lpszKeyPasswod = nullptr, LPCTSTR lpszCAPemCertFileOrPath = nullptr)
  33. {return m_sslCtx.Initialize(SSL_SM_CLIENT, iVerifyMode, lpszPemCertFile, lpszPemKeyFile, lpszKeyPasswod, lpszCAPemCertFileOrPath, nullptr);}
  34. virtual void CleanupSSLContext()
  35. {m_sslCtx.Cleanup();}
  36. protected:
  37. virtual EnHandleResult FireConnect();
  38. virtual EnHandleResult FireReceive(const BYTE* pData, int iLength);
  39. virtual BOOL CheckParams();
  40. virtual void PrepareStart();
  41. virtual void Reset();
  42. virtual void OnWorkerThreadEnd(DWORD dwThreadID);
  43. private:
  44. friend EnHandleResult ProcessHandShake<>(CSSLClient* pThis, CSSLClient* pSocketObj, CSSLSession* pSession);
  45. friend EnHandleResult ProcessReceive<>(CSSLClient* pThis, CSSLClient* pSocketObj, CSSLSession* pSession, const BYTE* pData, int iLength);
  46. friend BOOL ProcessSend<>(CSSLClient* pThis, CSSLClient* pSocketObj, CSSLSession* pSession, const WSABUF * pBuffers, int iCount);
  47. public:
  48. CSSLClient(ITcpClientListener* pListener)
  49. : CTcpClient(pListener)
  50. , m_sslSession (m_itPool)
  51. , m_dwMainThreadID (0)
  52. {
  53. }
  54. virtual ~CSSLClient()
  55. {
  56. Stop();
  57. }
  58. private:
  59. CSSLContext m_sslCtx;
  60. CSSLSession m_sslSession;
  61. DWORD m_dwMainThreadID;
  62. };
  63. #endif