SSLAgent.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 3.6.1
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #include "stdafx.h"
  25. #include "SSLAgent.h"
  26. #include "SSLHelper.h"
  27. BOOL CSSLAgent::CheckParams()
  28. {
  29. if(!g_SSL.IsValid())
  30. {
  31. SetLastError(SE_SSL_ENV_NOT_READY, __FUNCTION__, ERROR_NOT_READY);
  32. return FALSE;
  33. }
  34. return __super::CheckParams();
  35. }
  36. void CSSLAgent::PrepareStart()
  37. {
  38. __super::PrepareStart();
  39. m_sslPool.SetItemCapacity (GetSocketBufferSize());
  40. m_sslPool.SetItemPoolSize (GetFreeBufferObjPool());
  41. m_sslPool.SetItemPoolHold (GetFreeBufferObjHold());
  42. m_sslPool.SetSessionLockTime(GetFreeSocketObjLockTime());
  43. m_sslPool.SetSessionPoolSize(GetFreeSocketObjPool());
  44. m_sslPool.SetSessionPoolHold(GetFreeSocketObjHold());
  45. m_sslPool.Prepare();
  46. }
  47. void CSSLAgent::Reset()
  48. {
  49. g_SSL.RemoveThreadLocalState();
  50. __super::Reset();
  51. }
  52. void CSSLAgent::OnWorkerThreadEnd(DWORD dwThreadID)
  53. {
  54. g_SSL.RemoveThreadLocalState();
  55. __super::OnWorkerThreadEnd(dwThreadID);
  56. }
  57. BOOL CSSLAgent::SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  58. {
  59. ASSERT(pBuffers && iCount > 0);
  60. TSocketObj* pSocketObj = FindSocketObj(dwConnID);
  61. if(!TSocketObj::IsValid(pSocketObj))
  62. {
  63. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  64. return FALSE;
  65. }
  66. CSSLSession* pSession = nullptr;
  67. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  68. return ::ProcessSend(this, pSocketObj, pSession, pBuffers, iCount);
  69. }
  70. EnHandleResult CSSLAgent::FireConnect(TSocketObj* pSocketObj)
  71. {
  72. EnHandleResult result = DoFireConnect(pSocketObj);
  73. if(result != HR_ERROR)
  74. {
  75. CSSLSession* pSession = m_sslPool.PickFreeSession();
  76. VERIFY(SetConnectionReserved2(pSocketObj, pSession));
  77. VERIFY(::ProcessHandShake(this, pSocketObj, pSession) == HR_OK);
  78. }
  79. return result;
  80. }
  81. EnHandleResult CSSLAgent::FireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  82. {
  83. CSSLSession* pSession = nullptr;
  84. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  85. ASSERT(pSession);
  86. return ::ProcessReceive(this, pSocketObj, pSession, pData, iLength);
  87. }
  88. EnHandleResult CSSLAgent::FireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  89. {
  90. EnHandleResult result = DoFireClose(pSocketObj, enOperation, iErrorCode);
  91. CSSLSession* pSession = nullptr;
  92. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  93. if(pSession != nullptr)
  94. m_sslPool.PutFreeSession(pSession);
  95. return result;
  96. }
  97. EnHandleResult CSSLAgent::FireShutdown()
  98. {
  99. EnHandleResult result = DoFireShutdown();
  100. m_sslPool.Clear();
  101. return result;
  102. }