/qpid/cpp/bindings/qpid/dotnet/src/Session.cpp

https://github.com/ncdc/qpid · C++ · 728 lines · 565 code · 124 blank · 39 comment · 75 complexity · 40ccfdb67dd4cbdae4be0e3ec76272a2 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include <windows.h>
  20. #include <msclr\lock.h>
  21. #include <oletx2xa.h>
  22. #include <string>
  23. #include <limits>
  24. #include "qpid/messaging/Session.h"
  25. #include "qpid/messaging/exceptions.h"
  26. #include "QpidMarshal.h"
  27. #include "Address.h"
  28. #include "Session.h"
  29. #include "Connection.h"
  30. #include "Duration.h"
  31. #include "Receiver.h"
  32. #include "Sender.h"
  33. #include "Message.h"
  34. #include "QpidException.h"
  35. namespace Org {
  36. namespace Apache {
  37. namespace Qpid {
  38. namespace Messaging {
  39. /// <summary>
  40. /// Session is a managed wrapper for a ::qpid::messaging::Session
  41. /// </summary>
  42. // Disallow access if object has been destroyed.
  43. void Session::ThrowIfDisposed()
  44. {
  45. if (IsDisposed)
  46. throw gcnew ObjectDisposedException (GetType()->FullName);
  47. }
  48. // unmanaged clone
  49. Session::Session(const ::qpid::messaging::Session & session,
  50. Org::Apache::Qpid::Messaging::Connection ^ connRef) :
  51. parentConnectionp(connRef)
  52. {
  53. System::Exception ^ newException = nullptr;
  54. try
  55. {
  56. privateLock = gcnew System::Object();
  57. nativeObjPtr = new ::qpid::messaging::Session (session);
  58. }
  59. catch (const ::qpid::types::Exception & error)
  60. {
  61. String ^ errmsg = gcnew String(error.what());
  62. newException = gcnew QpidException(errmsg);
  63. }
  64. if (newException != nullptr)
  65. {
  66. throw newException;
  67. }
  68. }
  69. // Destructor
  70. Session::~Session()
  71. {
  72. this->!Session();
  73. }
  74. // Finalizer
  75. Session::!Session()
  76. {
  77. if (NULL != nativeObjPtr)
  78. {
  79. msclr::lock lk(privateLock);
  80. if (NULL != nativeObjPtr)
  81. {
  82. delete nativeObjPtr;
  83. nativeObjPtr = NULL;
  84. }
  85. }
  86. }
  87. // Copy constructor look-alike (C#)
  88. Session::Session(const Session ^ session)
  89. : parentConnectionp(session->parentConnectionp)
  90. {
  91. System::Exception ^ newException = nullptr;
  92. try
  93. {
  94. privateLock = gcnew System::Object();
  95. nativeObjPtr = new ::qpid::messaging::Session(
  96. *(const_cast<Session ^>(session)->NativeSession));
  97. }
  98. catch (const ::qpid::types::Exception & error)
  99. {
  100. String ^ errmsg = gcnew String(error.what());
  101. newException = gcnew QpidException(errmsg);
  102. }
  103. if (newException != nullptr)
  104. {
  105. throw newException;
  106. }
  107. }
  108. // Copy constructor implicitly dereferenced (C++)
  109. Session::Session(const Session % session)
  110. : parentConnectionp(session.parentConnectionp)
  111. {
  112. System::Exception ^ newException = nullptr;
  113. try
  114. {
  115. privateLock = gcnew System::Object();
  116. nativeObjPtr = new ::qpid::messaging::Session(
  117. *(const_cast<Session %>(session).NativeSession));
  118. }
  119. catch (const ::qpid::types::Exception & error)
  120. {
  121. String ^ errmsg = gcnew String(error.what());
  122. newException = gcnew QpidException(errmsg);
  123. }
  124. if (newException != nullptr)
  125. {
  126. throw newException;
  127. }
  128. }
  129. void Session::Close()
  130. {
  131. msclr::lock lk(privateLock);
  132. ThrowIfDisposed();
  133. System::Exception ^ newException = nullptr;
  134. try
  135. {
  136. nativeObjPtr->close();
  137. }
  138. catch (const ::qpid::types::Exception & error)
  139. {
  140. String ^ errmsg = gcnew String(error.what());
  141. newException = gcnew QpidException(errmsg);
  142. }
  143. if (newException != nullptr)
  144. {
  145. throw newException;
  146. }
  147. }
  148. void Session::Commit()
  149. {
  150. msclr::lock lk(privateLock);
  151. ThrowIfDisposed();
  152. System::Exception ^ newException = nullptr;
  153. try
  154. {
  155. nativeObjPtr->commit();
  156. }
  157. catch (const ::qpid::types::Exception & error)
  158. {
  159. String ^ errmsg = gcnew String(error.what());
  160. newException = gcnew QpidException(errmsg);
  161. }
  162. if (newException != nullptr)
  163. {
  164. throw newException;
  165. }
  166. }
  167. void Session::Rollback()
  168. {
  169. msclr::lock lk(privateLock);
  170. ThrowIfDisposed();
  171. System::Exception ^ newException = nullptr;
  172. try
  173. {
  174. nativeObjPtr->rollback();
  175. }
  176. catch (const ::qpid::types::Exception & error)
  177. {
  178. String ^ errmsg = gcnew String(error.what());
  179. newException = gcnew QpidException(errmsg);
  180. }
  181. if (newException != nullptr)
  182. {
  183. throw newException;
  184. }
  185. }
  186. void Session::Acknowledge()
  187. {
  188. Acknowledge(false);
  189. }
  190. void Session::Acknowledge(bool sync)
  191. {
  192. msclr::lock lk(privateLock);
  193. ThrowIfDisposed();
  194. System::Exception ^ newException = nullptr;
  195. try
  196. {
  197. nativeObjPtr->acknowledge(sync);
  198. }
  199. catch (const ::qpid::types::Exception & error)
  200. {
  201. String ^ errmsg = gcnew String(error.what());
  202. newException = gcnew QpidException(errmsg);
  203. }
  204. if (newException != nullptr)
  205. {
  206. throw newException;
  207. }
  208. }
  209. void Session::Acknowledge(Message ^ message)
  210. {
  211. Acknowledge(message, false);
  212. }
  213. void Session::Acknowledge(Message ^ message, bool sync)
  214. {
  215. msclr::lock lk(privateLock);
  216. ThrowIfDisposed();
  217. System::Exception ^ newException = nullptr;
  218. try
  219. {
  220. nativeObjPtr->acknowledge(*(message->NativeMessage), sync);
  221. }
  222. catch (const ::qpid::types::Exception & error)
  223. {
  224. String ^ errmsg = gcnew String(error.what());
  225. newException = gcnew QpidException(errmsg);
  226. }
  227. if (newException != nullptr)
  228. {
  229. throw newException;
  230. }
  231. }
  232. void Session::AcknowledgeUpTo(Message ^ message)
  233. {
  234. AcknowledgeUpTo(message, false);
  235. }
  236. void Session::AcknowledgeUpTo(Message ^ message, bool sync)
  237. {
  238. msclr::lock lk(privateLock);
  239. ThrowIfDisposed();
  240. System::Exception ^ newException = nullptr;
  241. try
  242. {
  243. nativeObjPtr->acknowledgeUpTo(*(message->NativeMessage), sync);
  244. }
  245. catch (const ::qpid::types::Exception & error)
  246. {
  247. String ^ errmsg = gcnew String(error.what());
  248. newException = gcnew QpidException(errmsg);
  249. }
  250. if (newException != nullptr)
  251. {
  252. throw newException;
  253. }
  254. }
  255. void Session::Reject(Message ^ message)
  256. {
  257. msclr::lock lk(privateLock);
  258. ThrowIfDisposed();
  259. System::Exception ^ newException = nullptr;
  260. try
  261. {
  262. nativeObjPtr->::qpid::messaging::Session::reject(*(message->NativeMessage));
  263. }
  264. catch (const ::qpid::types::Exception & error)
  265. {
  266. String ^ errmsg = gcnew String(error.what());
  267. newException = gcnew QpidException(errmsg);
  268. }
  269. if (newException != nullptr)
  270. {
  271. throw newException;
  272. }
  273. }
  274. void Session::Release(Message ^ message)
  275. {
  276. msclr::lock lk(privateLock);
  277. ThrowIfDisposed();
  278. System::Exception ^ newException = nullptr;
  279. try
  280. {
  281. nativeObjPtr->::qpid::messaging::Session::release(*(message->NativeMessage));
  282. }
  283. catch (const ::qpid::types::Exception & error)
  284. {
  285. String ^ errmsg = gcnew String(error.what());
  286. newException = gcnew QpidException(errmsg);
  287. }
  288. if (newException != nullptr)
  289. {
  290. throw newException;
  291. }
  292. }
  293. void Session::Sync()
  294. {
  295. Sync(true);
  296. }
  297. void Session::Sync(bool block)
  298. {
  299. msclr::lock lk(privateLock);
  300. ThrowIfDisposed();
  301. System::Exception ^ newException = nullptr;
  302. try
  303. {
  304. nativeObjPtr->sync(block);
  305. }
  306. catch (const ::qpid::types::Exception & error)
  307. {
  308. String ^ errmsg = gcnew String(error.what());
  309. newException = gcnew QpidException(errmsg);
  310. }
  311. if (newException != nullptr)
  312. {
  313. throw newException;
  314. }
  315. }
  316. // next(receiver)
  317. bool Session::NextReceiver(Receiver ^ rcvr)
  318. {
  319. return NextReceiver(rcvr, DurationConstants::FORVER);
  320. }
  321. bool Session::NextReceiver(Receiver ^ rcvr, Duration ^ timeout)
  322. {
  323. msclr::lock lk(privateLock);
  324. ThrowIfDisposed();
  325. System::Exception ^ newException = nullptr;
  326. try
  327. {
  328. // create a duration object
  329. ::qpid::messaging::Duration dur(timeout->Milliseconds);
  330. // wait for the next received message
  331. return nativeObjPtr->nextReceiver(*(rcvr->NativeReceiver), dur);
  332. }
  333. catch (const ::qpid::types::Exception & error)
  334. {
  335. String ^ errmsg = gcnew String(error.what());
  336. if ("No message to fetch" == errmsg){
  337. return false;
  338. }
  339. newException = gcnew QpidException(errmsg);
  340. }
  341. if (newException != nullptr)
  342. {
  343. throw newException;
  344. }
  345. return true;
  346. }
  347. // receiver = next()
  348. Receiver ^ Session::NextReceiver()
  349. {
  350. return NextReceiver(DurationConstants::FORVER);
  351. }
  352. Receiver ^ Session::NextReceiver(Duration ^ timeout)
  353. {
  354. msclr::lock lk(privateLock);
  355. ThrowIfDisposed();
  356. System::Exception ^ newException = nullptr;
  357. try
  358. {
  359. ::qpid::messaging::Duration dur(timeout->Milliseconds);
  360. ::qpid::messaging::Receiver receiver = nativeObjPtr->::qpid::messaging::Session::nextReceiver(dur);
  361. Receiver ^ newRcvr = gcnew Receiver(receiver, this);
  362. return newRcvr;
  363. }
  364. catch (const ::qpid::types::Exception & error)
  365. {
  366. String ^ errmsg = gcnew String(error.what());
  367. if ("No message to fetch" == errmsg)
  368. {
  369. return nullptr;
  370. }
  371. newException = gcnew QpidException(errmsg);
  372. }
  373. if (newException != nullptr)
  374. {
  375. throw newException;
  376. }
  377. return nullptr;
  378. }
  379. Sender ^ Session::CreateSender (System::String ^ address)
  380. {
  381. msclr::lock lk(privateLock);
  382. ThrowIfDisposed();
  383. System::Exception ^ newException = nullptr;
  384. Sender ^ newSender = nullptr;
  385. try
  386. {
  387. // create the sender
  388. ::qpid::messaging::Sender sender =
  389. nativeObjPtr->::qpid::messaging::Session::createSender(QpidMarshal::ToNative(address));
  390. // create a managed sender
  391. newSender = gcnew Sender(sender, this);
  392. }
  393. catch (const ::qpid::types::Exception & error)
  394. {
  395. String ^ errmsg = gcnew String(error.what());
  396. newException = gcnew QpidException(errmsg);
  397. }
  398. finally
  399. {
  400. if (newException != nullptr)
  401. {
  402. if (newSender != nullptr)
  403. {
  404. delete newSender;
  405. }
  406. }
  407. }
  408. if (newException != nullptr)
  409. {
  410. throw newException;
  411. }
  412. return newSender;
  413. }
  414. Sender ^ Session::CreateSender (Address ^ address)
  415. {
  416. msclr::lock lk(privateLock);
  417. ThrowIfDisposed();
  418. System::Exception ^ newException = nullptr;
  419. Sender ^ newSender = nullptr;
  420. try
  421. {
  422. // allocate a native sender
  423. ::qpid::messaging::Sender sender =
  424. nativeObjPtr->::qpid::messaging::Session::createSender(*(address->NativeAddress));
  425. // create a managed sender
  426. newSender = gcnew Sender(sender, this);
  427. }
  428. catch (const ::qpid::types::Exception & error)
  429. {
  430. String ^ errmsg = gcnew String(error.what());
  431. newException = gcnew QpidException(errmsg);
  432. }
  433. finally
  434. {
  435. if (newException != nullptr)
  436. {
  437. if (newSender != nullptr)
  438. {
  439. delete newSender;
  440. }
  441. }
  442. }
  443. if (newException != nullptr)
  444. {
  445. throw newException;
  446. }
  447. return newSender;
  448. }
  449. Receiver ^ Session::CreateReceiver(System::String ^ address)
  450. {
  451. msclr::lock lk(privateLock);
  452. ThrowIfDisposed();
  453. System::Exception ^ newException = nullptr;
  454. Receiver ^ newReceiver = nullptr;
  455. try
  456. {
  457. // create the receiver
  458. ::qpid::messaging::Receiver receiver =
  459. nativeObjPtr->createReceiver(QpidMarshal::ToNative(address));
  460. // create a managed receiver
  461. newReceiver = gcnew Receiver(receiver, this);
  462. }
  463. catch (const ::qpid::types::Exception & error)
  464. {
  465. String ^ errmsg = gcnew String(error.what());
  466. newException = gcnew QpidException(errmsg);
  467. }
  468. finally
  469. {
  470. if (newException != nullptr)
  471. {
  472. if (newReceiver != nullptr)
  473. {
  474. delete newReceiver;
  475. }
  476. }
  477. }
  478. if (newException != nullptr)
  479. {
  480. throw newException;
  481. }
  482. return newReceiver;
  483. }
  484. Receiver ^ Session::CreateReceiver(Address ^ address)
  485. {
  486. msclr::lock lk(privateLock);
  487. ThrowIfDisposed();
  488. System::Exception ^ newException = nullptr;
  489. Receiver ^ newReceiver = nullptr;
  490. try
  491. {
  492. // create the receiver
  493. ::qpid::messaging::Receiver receiver =
  494. nativeObjPtr->createReceiver(*(address->NativeAddress));
  495. // create a managed receiver
  496. newReceiver = gcnew Receiver(receiver, this);
  497. }
  498. catch (const ::qpid::types::Exception & error)
  499. {
  500. String ^ errmsg = gcnew String(error.what());
  501. newException = gcnew QpidException(errmsg);
  502. }
  503. finally
  504. {
  505. if (newException != nullptr)
  506. {
  507. if (newReceiver != nullptr)
  508. {
  509. delete newReceiver;
  510. }
  511. }
  512. }
  513. if (newException != nullptr)
  514. {
  515. throw newException;
  516. }
  517. return newReceiver;
  518. }
  519. Sender ^ Session::GetSender(System::String ^ name)
  520. {
  521. msclr::lock lk(privateLock);
  522. ThrowIfDisposed();
  523. System::Exception ^ newException = nullptr;
  524. Sender ^ newSender = nullptr;
  525. try
  526. {
  527. ::qpid::messaging::Sender senderp =
  528. nativeObjPtr->::qpid::messaging::Session::getSender(QpidMarshal::ToNative(name));
  529. newSender = gcnew Sender(senderp, this);
  530. }
  531. catch (const ::qpid::types::Exception & error)
  532. {
  533. String ^ errmsg = gcnew String(error.what());
  534. newException = gcnew QpidException(errmsg);
  535. }
  536. finally
  537. {
  538. if (newException != nullptr)
  539. {
  540. if (newSender != nullptr)
  541. {
  542. delete newSender;
  543. }
  544. }
  545. }
  546. if (newException != nullptr)
  547. {
  548. throw newException;
  549. }
  550. return newSender;
  551. }
  552. Receiver ^ Session::GetReceiver(System::String ^ name)
  553. {
  554. msclr::lock lk(privateLock);
  555. ThrowIfDisposed();
  556. System::Exception ^ newException = nullptr;
  557. Receiver ^ newReceiver = nullptr;
  558. try
  559. {
  560. ::qpid::messaging::Receiver receiver =
  561. nativeObjPtr->::qpid::messaging::Session::getReceiver(QpidMarshal::ToNative(name));
  562. newReceiver = gcnew Receiver(receiver, this);
  563. }
  564. catch (const ::qpid::types::Exception & error)
  565. {
  566. String ^ errmsg = gcnew String(error.what());
  567. newException = gcnew QpidException(errmsg);
  568. }
  569. finally
  570. {
  571. if (newException != nullptr)
  572. {
  573. if (newReceiver != nullptr)
  574. {
  575. delete newReceiver;
  576. }
  577. }
  578. }
  579. if (newException != nullptr)
  580. {
  581. throw newException;
  582. }
  583. return newReceiver;
  584. }
  585. void Session::CheckError()
  586. {
  587. msclr::lock lk(privateLock);
  588. ThrowIfDisposed();
  589. System::Exception ^ newException = nullptr;
  590. try
  591. {
  592. nativeObjPtr->checkError();
  593. }
  594. catch (const ::qpid::types::Exception & error)
  595. {
  596. String ^ errmsg = gcnew String(error.what());
  597. newException = gcnew QpidException(errmsg);
  598. }
  599. if (newException != nullptr)
  600. {
  601. throw newException;
  602. }
  603. }
  604. }}}}