NCDRequestClient.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /**
  2. * @file NCDRequestClient.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. #include <limits.h>
  33. #include <inttypes.h>
  34. #include <string.h>
  35. #include <misc/byteorder.h>
  36. #include <misc/expstring.h>
  37. #include <misc/offset.h>
  38. #include <misc/compare.h>
  39. #include <protocol/packetproto.h>
  40. #include <protocol/requestproto.h>
  41. #include <base/BLog.h>
  42. #include "NCDRequestClient.h"
  43. #include <generated/blog_channel_NCDRequestClient.h>
  44. #define SEND_PAYLOAD_MTU 32768
  45. #define RECV_PAYLOAD_MTU 32768
  46. #define SEND_MTU (SEND_PAYLOAD_MTU + sizeof(struct requestproto_header))
  47. #define RECV_MTU (RECV_PAYLOAD_MTU + sizeof(struct requestproto_header))
  48. #define CSTATE_CONNECTING 1
  49. #define CSTATE_CONNECTED 2
  50. #define RSTATE_SENDING_REQUEST 1
  51. #define RSTATE_READY 2
  52. #define RSTATE_SENDING_REQUEST_ABORT 3
  53. #define RSTATE_SENDING_ABORT 4
  54. #define RSTATE_WAITING_END 5
  55. #define RSTATE_DEAD_SENDING 6
  56. static int uint32_comparator (void *unused, void *vv1, void *vv2);
  57. static void report_error (NCDRequestClient *o);
  58. static void request_report_finished (NCDRequestClientRequest *o, int is_error);
  59. static void connector_handler (NCDRequestClient *o, int is_error);
  60. static void connection_handler (NCDRequestClient *o, int event);
  61. static void decoder_handler_error (NCDRequestClient *o);
  62. static void recv_if_handler_send (NCDRequestClient *o, uint8_t *data, int data_len);
  63. static struct NCDRequestClient_req * find_req (NCDRequestClient *o, uint32_t request_id);
  64. static int get_free_request_id (NCDRequestClient *o, uint32_t *out);
  65. static int build_requestproto_packet (uint32_t request_id, uint32_t type, NCDValRef payload_value, uint8_t **out_data, int *out_len);
  66. static void build_nodata_packet (uint32_t request_id, uint32_t type, uint8_t *data, int *out_len);
  67. static int req_is_aborted (struct NCDRequestClient_req *req);
  68. static void req_abort (struct NCDRequestClient_req *req);
  69. static void req_free (struct NCDRequestClient_req *req);
  70. static void req_send_abort (struct NCDRequestClient_req *req);
  71. static void req_qflow_send_iface_handler_done (struct NCDRequestClient_req *req);
  72. static int uint32_comparator (void *unused, void *vv1, void *vv2)
  73. {
  74. uint32_t *v1 = vv1;
  75. uint32_t *v2 = vv2;
  76. return B_COMPARE(*v1, *v2);
  77. }
  78. static void report_error (NCDRequestClient *o)
  79. {
  80. ASSERT(!o->is_error)
  81. o->is_error = 1;
  82. DEBUGERROR(&o->d_err, o->handler_error(o->user))
  83. }
  84. static void request_report_finished (NCDRequestClientRequest *o, int is_error)
  85. {
  86. o->req = NULL;
  87. DEBUGERROR(&o->d_err, o->handler_finished(o->user, is_error))
  88. }
  89. static void connector_handler (NCDRequestClient *o, int is_error)
  90. {
  91. DebugObject_Access(&o->d_obj);
  92. DebugError_AssertNoError(&o->d_err);
  93. ASSERT(o->state == CSTATE_CONNECTING)
  94. // check error
  95. if (is_error) {
  96. BLog(BLOG_ERROR, "failed to connect to socket");
  97. goto fail0;
  98. }
  99. BPendingGroup *pg = BReactor_PendingGroup(o->reactor);
  100. // init connection
  101. if (!BConnection_Init(&o->con, BConnection_source_connector(&o->connector), o->reactor, o, (BConnection_handler)connection_handler)) {
  102. BLog(BLOG_ERROR, "BConnection_Init failed");
  103. goto fail0;
  104. }
  105. // init connection interfaces
  106. BConnection_SendAsync_Init(&o->con);
  107. BConnection_RecvAsync_Init(&o->con);
  108. StreamPassInterface *con_send_if = BConnection_SendAsync_GetIf(&o->con);
  109. StreamRecvInterface *con_recv_if = BConnection_RecvAsync_GetIf(&o->con);
  110. // init receive interface
  111. PacketPassInterface_Init(&o->recv_if, RECV_MTU, (PacketPassInterface_handler_send)recv_if_handler_send, o, pg);
  112. // init receive decoder
  113. if (!PacketProtoDecoder_Init(&o->recv_decoder, con_recv_if, &o->recv_if, pg, o, (PacketProtoDecoder_handler_error)decoder_handler_error)) {
  114. BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
  115. goto fail1;
  116. }
  117. // init send sender
  118. PacketStreamSender_Init(&o->send_sender, con_send_if, PACKETPROTO_ENCLEN(SEND_MTU), pg);
  119. // init send queue
  120. PacketPassFifoQueue_Init(&o->send_queue, PacketStreamSender_GetInput(&o->send_sender), pg);
  121. // set state connected
  122. o->state = CSTATE_CONNECTED;
  123. // call connected handler
  124. o->handler_connected(o->user);
  125. return;
  126. fail1:
  127. PacketPassInterface_Free(&o->recv_if);
  128. BConnection_RecvAsync_Free(&o->con);
  129. BConnection_SendAsync_Free(&o->con);
  130. BConnection_Free(&o->con);
  131. fail0:
  132. report_error(o);
  133. }
  134. static void connection_handler (NCDRequestClient *o, int event)
  135. {
  136. DebugObject_Access(&o->d_obj);
  137. DebugError_AssertNoError(&o->d_err);
  138. ASSERT(o->state == CSTATE_CONNECTED)
  139. BLog(BLOG_ERROR, "connection error");
  140. report_error(o);
  141. }
  142. static void decoder_handler_error (NCDRequestClient *o)
  143. {
  144. DebugObject_Access(&o->d_obj);
  145. DebugError_AssertNoError(&o->d_err);
  146. ASSERT(o->state == CSTATE_CONNECTED)
  147. BLog(BLOG_ERROR, "decoder error");
  148. report_error(o);
  149. }
  150. static void recv_if_handler_send (NCDRequestClient *o, uint8_t *data, int data_len)
  151. {
  152. DebugObject_Access(&o->d_obj);
  153. DebugError_AssertNoError(&o->d_err);
  154. ASSERT(o->state == CSTATE_CONNECTED)
  155. ASSERT(data_len >= 0)
  156. ASSERT(data_len <= RECV_MTU)
  157. // accept packet
  158. PacketPassInterface_Done(&o->recv_if);
  159. if (data_len < sizeof(struct requestproto_header)) {
  160. BLog(BLOG_ERROR, "missing requestproto header");
  161. goto fail;
  162. }
  163. struct requestproto_header header;
  164. memcpy(&header, data, sizeof(header));
  165. uint32_t request_id = ltoh32(header.request_id);
  166. uint32_t type = ltoh32(header.type);
  167. uint8_t *payload = data + sizeof(header);
  168. int payload_len = data_len - sizeof(header);
  169. // find request
  170. struct NCDRequestClient_req *req = find_req(o, request_id);
  171. if (!req) {
  172. BLog(BLOG_ERROR, "received packet with unknown request ID");
  173. goto fail;
  174. }
  175. switch (type) {
  176. case REQUESTPROTO_TYPE_SERVER_REPLY: {
  177. switch (o->state) {
  178. case RSTATE_READY: {
  179. // init memory
  180. NCDValMem mem;
  181. NCDValMem_Init(&mem);
  182. // parse payload
  183. NCDValRef payload_value;
  184. if (!NCDValParser_Parse(MemRef_Make((char *)payload, payload_len), &mem, &payload_value)) {
  185. BLog(BLOG_ERROR, "failed to parse reply payload");
  186. NCDValMem_Free(&mem);
  187. goto fail;
  188. }
  189. // call reply handler
  190. req->creq->handler_reply(req->creq->user, mem, payload_value);
  191. return;
  192. } break;
  193. case RSTATE_SENDING_ABORT:
  194. case RSTATE_WAITING_END:
  195. return;
  196. default:
  197. BLog(BLOG_ERROR, "received unexpected reply");
  198. goto fail;
  199. }
  200. } break;
  201. case REQUESTPROTO_TYPE_SERVER_FINISHED:
  202. case REQUESTPROTO_TYPE_SERVER_ERROR: {
  203. if (payload_len != 0) {
  204. BLog(BLOG_ERROR, "finshed/aborted message has non-empty payload");
  205. goto fail;
  206. }
  207. NCDRequestClientRequest *creq = req->creq;
  208. req->creq = NULL;
  209. switch (req->state) {
  210. case RSTATE_SENDING_ABORT: {
  211. // set state dying send
  212. req->state = RSTATE_DEAD_SENDING;
  213. } break;
  214. case RSTATE_WAITING_END:
  215. case RSTATE_READY: {
  216. // free req
  217. req_free(req);
  218. } break;
  219. default:
  220. BLog(BLOG_ERROR, "received unexpected finished/aborted");
  221. goto fail;
  222. }
  223. // report finished
  224. if (creq) {
  225. request_report_finished(creq, type == REQUESTPROTO_TYPE_SERVER_ERROR);
  226. }
  227. return;
  228. } break;
  229. default:
  230. BLog(BLOG_ERROR, "received invalid message type");
  231. goto fail;
  232. }
  233. ASSERT(0)
  234. fail:
  235. report_error(o);
  236. }
  237. static struct NCDRequestClient_req * find_req (NCDRequestClient *o, uint32_t request_id)
  238. {
  239. BAVLNode *tn = BAVL_LookupExact(&o->reqs_tree, &request_id);
  240. if (!tn) {
  241. return NULL;
  242. }
  243. struct NCDRequestClient_req *req = UPPER_OBJECT(tn, struct NCDRequestClient_req, reqs_tree_node);
  244. ASSERT(req->request_id == request_id)
  245. return req;
  246. }
  247. static int get_free_request_id (NCDRequestClient *o, uint32_t *out)
  248. {
  249. uint32_t first = o->next_request_id;
  250. do {
  251. if (!find_req(o, o->next_request_id)) {
  252. *out = o->next_request_id;
  253. return 1;
  254. }
  255. o->next_request_id++;
  256. } while (o->next_request_id != first);
  257. return 0;
  258. }
  259. static int build_requestproto_packet (uint32_t request_id, uint32_t type, NCDValRef payload_value, uint8_t **out_data, int *out_len)
  260. {
  261. ExpString str;
  262. if (!ExpString_Init(&str)) {
  263. BLog(BLOG_ERROR, "ExpString_Init failed");
  264. goto fail0;
  265. }
  266. if (!ExpString_AppendZeros(&str, sizeof(struct packetproto_header) + sizeof(struct requestproto_header))) {
  267. BLog(BLOG_ERROR, "ExpString_AppendBinary failed");
  268. goto fail1;
  269. }
  270. if (!NCDVal_IsInvalid(payload_value) && !NCDValGenerator_AppendGenerate(payload_value, &str)) {
  271. BLog(BLOG_ERROR, "NCDValGenerator_AppendGenerate failed");
  272. goto fail1;
  273. }
  274. size_t len = ExpString_Length(&str);
  275. if (len > INT_MAX || len > PACKETPROTO_ENCLEN(SEND_MTU) || len - sizeof(struct packetproto_header) > UINT16_MAX) {
  276. BLog(BLOG_ERROR, "reply is too long");
  277. goto fail1;
  278. }
  279. uint8_t *packet = (uint8_t *)ExpString_Get(&str);
  280. struct packetproto_header pp;
  281. pp.len = htol16(len - sizeof(struct packetproto_header));
  282. struct requestproto_header rp;
  283. rp.request_id = htol32(request_id);
  284. rp.type = htol32(type);
  285. memcpy(packet, &pp, sizeof(pp));
  286. memcpy(packet + sizeof(pp), &rp, sizeof(rp));
  287. *out_data = packet;
  288. *out_len = len;
  289. return 1;
  290. fail1:
  291. ExpString_Free(&str);
  292. fail0:
  293. return 0;
  294. }
  295. static void build_nodata_packet (uint32_t request_id, uint32_t type, uint8_t *data, int *out_len)
  296. {
  297. struct packetproto_header pp;
  298. pp.len = htol16(sizeof(struct requestproto_header));
  299. struct requestproto_header rp;
  300. rp.request_id = htol32(request_id);
  301. rp.type = htol32(type);
  302. memcpy(data, &pp, sizeof(pp));
  303. memcpy(data + sizeof(pp), &rp, sizeof(rp));
  304. *out_len = sizeof(pp) + sizeof(rp);
  305. }
  306. static int req_is_aborted (struct NCDRequestClient_req *req)
  307. {
  308. switch (req->state) {
  309. case RSTATE_SENDING_REQUEST:
  310. case RSTATE_READY:
  311. return 0;
  312. default:
  313. return 1;
  314. }
  315. }
  316. static void req_abort (struct NCDRequestClient_req *req)
  317. {
  318. ASSERT(!req_is_aborted(req))
  319. ASSERT(!req->client->is_error)
  320. switch (req->state) {
  321. case RSTATE_SENDING_REQUEST: {
  322. req->state = RSTATE_SENDING_REQUEST_ABORT;
  323. } break;
  324. case RSTATE_READY: {
  325. req_send_abort(req);
  326. } break;
  327. default: ASSERT(0);
  328. }
  329. }
  330. static void req_free (struct NCDRequestClient_req *req)
  331. {
  332. NCDRequestClient *client = req->client;
  333. PacketPassFifoQueueFlow_AssertFree(&req->send_qflow);
  334. ASSERT(!req->creq)
  335. // free queue flow
  336. PacketPassFifoQueueFlow_Free(&req->send_qflow);
  337. // free request data
  338. free(req->request_data);
  339. // remove from reqs tree
  340. BAVL_Remove(&client->reqs_tree, &req->reqs_tree_node);
  341. // free structure
  342. free(req);
  343. }
  344. static void req_send_abort (struct NCDRequestClient_req *req)
  345. {
  346. // build packet
  347. build_nodata_packet(req->request_id, REQUESTPROTO_TYPE_CLIENT_ABORT, req->request_data, &req->request_len);
  348. // start sending
  349. PacketPassInterface_Sender_Send(req->send_qflow_iface, req->request_data, req->request_len);
  350. // set state sending abort
  351. req->state = RSTATE_SENDING_ABORT;
  352. }
  353. static void req_qflow_send_iface_handler_done (struct NCDRequestClient_req *req)
  354. {
  355. switch (req->state) {
  356. case RSTATE_SENDING_REQUEST: {
  357. // set state ready
  358. req->state = RSTATE_READY;
  359. // call sent handler
  360. req->creq->handler_sent(req->creq->user);
  361. return;
  362. } break;
  363. case RSTATE_SENDING_REQUEST_ABORT: {
  364. // send abort
  365. req_send_abort(req);
  366. } break;
  367. case RSTATE_SENDING_ABORT: {
  368. // set state waiting end
  369. req->state = RSTATE_WAITING_END;
  370. } break;
  371. case RSTATE_DEAD_SENDING: {
  372. // free req
  373. req_free(req);
  374. } break;
  375. default: ASSERT(0);
  376. }
  377. }
  378. int NCDRequestClient_Init (NCDRequestClient *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  379. NCDRequestClient_handler_error handler_error,
  380. NCDRequestClient_handler_connected handler_connected)
  381. {
  382. ASSERT(handler_error)
  383. ASSERT(handler_connected)
  384. // init arguments
  385. o->reactor = reactor;
  386. o->user = user;
  387. o->handler_error = handler_error;
  388. o->handler_connected = handler_connected;
  389. // init connector
  390. if (!BConnector_InitGeneric(&o->connector, addr, reactor, o, (BConnector_handler)connector_handler)) {
  391. BLog(BLOG_ERROR, "BConnector_InitGeneric failed");
  392. goto fail0;
  393. }
  394. // init reqs tree
  395. BAVL_Init(&o->reqs_tree, OFFSET_DIFF(struct NCDRequestClient_req, request_id, reqs_tree_node), uint32_comparator, NULL);
  396. // set next request ID
  397. o->next_request_id = 0;
  398. // set state connecting
  399. o->state = CSTATE_CONNECTING;
  400. // set is not error
  401. o->is_error = 0;
  402. DebugCounter_Init(&o->d_reqests_ctr);
  403. DebugError_Init(&o->d_err, BReactor_PendingGroup(reactor));
  404. DebugObject_Init(&o->d_obj);
  405. return 1;
  406. fail0:
  407. return 0;
  408. }
  409. void NCDRequestClient_Free (NCDRequestClient *o)
  410. {
  411. DebugObject_Free(&o->d_obj);
  412. DebugError_Free(&o->d_err);
  413. DebugCounter_Free(&o->d_reqests_ctr);
  414. if (o->state == CSTATE_CONNECTED) {
  415. // allow freeing queue flow
  416. PacketPassFifoQueue_PrepareFree(&o->send_queue);
  417. // free remaining reqs
  418. BAVLNode *tn;
  419. while (tn = BAVL_GetFirst(&o->reqs_tree)) {
  420. struct NCDRequestClient_req *req = UPPER_OBJECT(tn, struct NCDRequestClient_req, reqs_tree_node);
  421. ASSERT(!req->creq)
  422. req_free(req);
  423. }
  424. // free connection stuff
  425. PacketPassFifoQueue_Free(&o->send_queue);
  426. PacketStreamSender_Free(&o->send_sender);
  427. PacketProtoDecoder_Free(&o->recv_decoder);
  428. PacketPassInterface_Free(&o->recv_if);
  429. BConnection_RecvAsync_Free(&o->con);
  430. BConnection_SendAsync_Free(&o->con);
  431. BConnection_Free(&o->con);
  432. }
  433. // free connector
  434. BConnector_Free(&o->connector);
  435. }
  436. int NCDRequestClientRequest_Init (NCDRequestClientRequest *o, NCDRequestClient *client, NCDValRef payload_value, void *user,
  437. NCDRequestClientRequest_handler_sent handler_sent,
  438. NCDRequestClientRequest_handler_reply handler_reply,
  439. NCDRequestClientRequest_handler_finished handler_finished)
  440. {
  441. ASSERT(client->state == CSTATE_CONNECTED)
  442. DebugError_AssertNoError(&client->d_err);
  443. ASSERT(!NCDVal_IsInvalid(payload_value))
  444. ASSERT(handler_sent)
  445. ASSERT(handler_reply)
  446. ASSERT(handler_finished)
  447. // init arguments
  448. o->client = client;
  449. o->user = user;
  450. o->handler_sent = handler_sent;
  451. o->handler_reply = handler_reply;
  452. o->handler_finished = handler_finished;
  453. // allocate req structure
  454. struct NCDRequestClient_req *req = malloc(sizeof(*req));
  455. if (!req) {
  456. BLog(BLOG_ERROR, "malloc failed");
  457. goto fail0;
  458. }
  459. // allocate request ID
  460. if (!get_free_request_id(client, &req->request_id)) {
  461. BLog(BLOG_ERROR, "failed to allocate request ID");
  462. goto fail1;
  463. }
  464. // insert to reqs tree
  465. int res = BAVL_Insert(&client->reqs_tree, &req->reqs_tree_node, NULL);
  466. ASSERT_EXECUTE(res)
  467. // set pointers
  468. o->req = req;
  469. req->creq = o;
  470. req->client = client;
  471. // build request
  472. if (!build_requestproto_packet(req->request_id, REQUESTPROTO_TYPE_CLIENT_REQUEST, payload_value, &req->request_data, &req->request_len)) {
  473. BLog(BLOG_ERROR, "failed to build request");
  474. goto fail2;
  475. }
  476. // init queue flow
  477. PacketPassFifoQueueFlow_Init(&req->send_qflow, &client->send_queue);
  478. // init send interface
  479. req->send_qflow_iface = PacketPassFifoQueueFlow_GetInput(&req->send_qflow);
  480. PacketPassInterface_Sender_Init(req->send_qflow_iface, (PacketPassInterface_handler_done)req_qflow_send_iface_handler_done, req);
  481. // start sending request
  482. PacketPassInterface_Sender_Send(req->send_qflow_iface, req->request_data, req->request_len);
  483. // set state sending request
  484. req->state = RSTATE_SENDING_REQUEST;
  485. DebugCounter_Increment(&client->d_reqests_ctr);
  486. DebugError_Init(&o->d_err, BReactor_PendingGroup(client->reactor));
  487. DebugObject_Init(&o->d_obj);
  488. return 1;
  489. fail2:
  490. BAVL_Remove(&client->reqs_tree, &req->reqs_tree_node);
  491. fail1:
  492. free(req);
  493. fail0:
  494. return 0;
  495. }
  496. void NCDRequestClientRequest_Free (NCDRequestClientRequest *o)
  497. {
  498. struct NCDRequestClient_req *req = o->req;
  499. DebugObject_Free(&o->d_obj);
  500. DebugError_Free(&o->d_err);
  501. DebugCounter_Decrement(&o->client->d_reqests_ctr);
  502. if (req) {
  503. ASSERT(req->creq == o)
  504. // remove reference to us
  505. req->creq = NULL;
  506. // abort req if not already
  507. if (!req->client->is_error && !req_is_aborted(req)) {
  508. req_abort(req);
  509. }
  510. }
  511. }
  512. void NCDRequestClientRequest_Abort (NCDRequestClientRequest *o)
  513. {
  514. struct NCDRequestClient_req *req = o->req;
  515. DebugObject_Access(&o->d_obj);
  516. DebugError_AssertNoError(&o->d_err);
  517. DebugError_AssertNoError(&o->client->d_err);
  518. ASSERT(req)
  519. ASSERT(req->creq == o)
  520. ASSERT(!req_is_aborted(req))
  521. // abort req
  522. req_abort(req);
  523. }