WSMessageStreamExporter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <memory>
  19. #include <new>
  20. #include <kopano/mapi_ptr.h>
  21. #include "WSMessageStreamExporter.h"
  22. #include "WSSerializedMessage.h"
  23. #include "WSTransport.h"
  24. #include <kopano/charset/convert.h>
  25. #include "WSUtil.h"
  26. /**
  27. * Create a WSMessageStreamExporter instance.
  28. * @param[in] ulOffset The offset that should be used to index the streams. The server returns [0:B-A), while the client would
  29. * expect [A-B). This offset makes sure GetSerializedObject can be called with an index from the expected
  30. * range.
  31. * @param[in] ulCount The number message streams that should be handled. The actual amount of streams returned from the server
  32. * could be less if those messages didn't exist anymore on the server. This makes sure the client can still
  33. * request those streams and an appropriate error can be returned.
  34. * @param[in] streams The streams (or actually the information about the streams).
  35. * @param[in] lpTransport Pointer to the parent transport. Used to get the streams from the network. This transport MUST be used
  36. * exclusively by this WSMessageStreamExporter only.
  37. * @param[out] lppStreamExporter The new instance.
  38. */
  39. HRESULT WSMessageStreamExporter::Create(ULONG ulOffset, ULONG ulCount, const messageStreamArray &streams, WSTransport *lpTransport, WSMessageStreamExporter **lppStreamExporter)
  40. {
  41. HRESULT hr = hrSuccess;
  42. convert_context converter;
  43. WSMessageStreamExporterPtr ptrStreamExporter(new(std::nothrow) WSMessageStreamExporter);
  44. if (ptrStreamExporter == nullptr)
  45. return MAPI_E_INVALID_PARAMETER;
  46. for (gsoap_size_t i = 0; i < streams.__size; ++i) {
  47. std::unique_ptr<StreamInfo> lpsi(new(std::nothrow) StreamInfo);
  48. if (lpsi == nullptr)
  49. return MAPI_E_NOT_ENOUGH_MEMORY;
  50. lpsi->id.assign(streams.__ptr[i].sStreamData.xop__Include.id);
  51. hr = MAPIAllocateBuffer(streams.__ptr[i].sPropVals.__size * sizeof(SPropValue), &~lpsi->ptrPropVals);
  52. if (hr != hrSuccess)
  53. return hr;
  54. for (gsoap_size_t j = 0; j < streams.__ptr[i].sPropVals.__size; ++j) {
  55. hr = CopySOAPPropValToMAPIPropVal(&lpsi->ptrPropVals[j], &streams.__ptr[i].sPropVals.__ptr[j], lpsi->ptrPropVals, &converter);
  56. if (hr != hrSuccess)
  57. return hr;
  58. }
  59. lpsi->cbPropVals = streams.__ptr[i].sPropVals.__size;
  60. ptrStreamExporter->m_mapStreamInfo[streams.__ptr[i].ulStep + ulOffset] = lpsi.release();
  61. }
  62. ptrStreamExporter->m_ulExpectedIndex = ulOffset;
  63. ptrStreamExporter->m_ulMaxIndex = ulOffset + ulCount;
  64. ptrStreamExporter->m_ptrTransport.reset(lpTransport);
  65. *lppStreamExporter = ptrStreamExporter.release();
  66. return hrSuccess;
  67. }
  68. /**
  69. * Check if any more streams are available from the exporter.
  70. */
  71. bool WSMessageStreamExporter::IsDone() const
  72. {
  73. assert(m_ulExpectedIndex <= m_ulMaxIndex);
  74. return m_ulExpectedIndex == m_ulMaxIndex;
  75. }
  76. /**
  77. * Request a serialized messages.
  78. * @param[in] ulIndex The index of the requested messages stream. The first time this must equal the ulOffset used during
  79. * construction. At each consecutive call this must be one higher than the previous call.
  80. * @param[out] lppSerializedMessage The requested stream.
  81. *
  82. * @retval MAPI_E_INVALID_PARAMETER ulIndex was not as expected or lppSerializedMessage is NULL
  83. * @retval SYNC_E_OBJECT_DELETED The message was deleted on the server.
  84. */
  85. HRESULT WSMessageStreamExporter::GetSerializedMessage(ULONG ulIndex, WSSerializedMessage **lppSerializedMessage)
  86. {
  87. StreamInfoMap::const_iterator iStreamInfo;
  88. if (ulIndex != m_ulExpectedIndex || lppSerializedMessage == NULL)
  89. return MAPI_E_INVALID_PARAMETER;
  90. iStreamInfo = m_mapStreamInfo.find(ulIndex);
  91. if (iStreamInfo == m_mapStreamInfo.cend()) {
  92. ++m_ulExpectedIndex;
  93. return SYNC_E_OBJECT_DELETED;
  94. }
  95. WSSerializedMessagePtr ptrMessage(new(std::nothrow) WSSerializedMessage(m_ptrTransport->m_lpCmd->soap, iStreamInfo->second->id, iStreamInfo->second->cbPropVals, iStreamInfo->second->ptrPropVals.get()));
  96. if (ptrMessage == nullptr)
  97. return MAPI_E_NOT_ENOUGH_MEMORY;
  98. AddChild(ptrMessage);
  99. ++m_ulExpectedIndex;
  100. *lppSerializedMessage = ptrMessage.release();
  101. return hrSuccess;
  102. }
  103. WSMessageStreamExporter::~WSMessageStreamExporter()
  104. {
  105. if (m_ulMaxIndex != m_ulExpectedIndex &&
  106. m_ptrTransport->m_lpCmd != nullptr)
  107. // We are halfway through a sync batch, so there is data waiting for us. Since we have our
  108. // own transport, we just drop the connection now, instead of letting the server output up to 254
  109. // messages that we'd just discard. Probably we will need to reconnect very soon after this call, to LogOff()
  110. // the transport's session, but that's better than receiving unwanted data.
  111. m_ptrTransport->m_lpCmd->soap->fshutdownsocket(m_ptrTransport->m_lpCmd->soap, m_ptrTransport->m_lpCmd->soap->socket, 0);
  112. for (const auto &i : m_mapStreamInfo)
  113. delete i.second;
  114. }