工具项目

SSLClient.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "stdafx.h"
  24. #include "SSLClient.h"
  25. #include "SSLHelper.h"
  26. #ifdef _SSL_SUPPORT
  27. BOOL CSSLClient::CheckParams()
  28. {
  29. if(!m_sslCtx.IsValid())
  30. {
  31. SetLastError(SE_SSL_ENV_NOT_READY, __FUNCTION__, ERROR_NOT_READY);
  32. return FALSE;
  33. }
  34. return __super::CheckParams();
  35. }
  36. void CSSLClient::PrepareStart()
  37. {
  38. m_dwMainThreadID = ::GetCurrentThreadId();
  39. __super::PrepareStart();
  40. }
  41. void CSSLClient::Reset()
  42. {
  43. m_sslSession.Reset();
  44. if(m_dwMainThreadID != 0)
  45. {
  46. m_sslCtx.RemoveThreadLocalState(m_dwMainThreadID);
  47. m_dwMainThreadID = 0;
  48. }
  49. __super::Reset();
  50. }
  51. void CSSLClient::OnWorkerThreadEnd(DWORD dwThreadID)
  52. {
  53. m_sslCtx.RemoveThreadLocalState();
  54. __super::OnWorkerThreadEnd(dwThreadID);
  55. }
  56. BOOL CSSLClient::SendPackets(const WSABUF pBuffers[], int iCount)
  57. {
  58. ASSERT(pBuffers && iCount > 0);
  59. return ::ProcessSend(this, this, &m_sslSession, pBuffers, iCount);
  60. }
  61. EnHandleResult CSSLClient::FireConnect()
  62. {
  63. EnHandleResult result = DoFireConnect(this);
  64. if(result != HR_ERROR)
  65. {
  66. m_sslSession.Renew(m_sslCtx, m_strHost);
  67. VERIFY(::ProcessHandShake(this, this, &m_sslSession) == HR_OK);
  68. }
  69. return result;
  70. }
  71. EnHandleResult CSSLClient::FireReceive(const BYTE* pData, int iLength)
  72. {
  73. return ::ProcessReceive(this, this, &m_sslSession, pData, iLength);
  74. }
  75. #endif