NCDRequestClient.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /**
  2. * @file NCDRequestClient.c
  3. * @author Ambroz Bizjak <[email protected]>
  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 = (struct requestproto_header *)data;
  164. uint32_t request_id = ltoh32(header->request_id);
  165. uint32_t type = ltoh32(header->type);
  166. uint8_t *payload = data + sizeof(*header);
  167. int payload_len = data_len - sizeof(*header);
  168. // find request
  169. struct NCDRequestClient_req *req = find_req(o, request_id);
  170. if (!req) {
  171. BLog(BLOG_ERROR, "received packet with unknown request ID");
  172. goto fail;
  173. }
  174. switch (type) {
  175. case REQUESTPROTO_TYPE_SERVER_REPLY: {
  176. switch (o->state) {
  177. case RSTATE_READY: {
  178. // init memory
  179. NCDValMem mem;
  180. NCDValMem_Init(&mem);
  181. // parse payload
  182. NCDValRef payload_value;
  183. if (!NCDValParser_Parse((char *)payload, payload_len, &mem, &payload_value)) {
  184. BLog(BLOG_ERROR, "failed to parse reply payload");
  185. NCDValMem_Free(&mem);
  186. goto fail;
  187. }
  188. // call reply handler
  189. req->creq->handler_reply(req->creq->user, mem, payload_value);
  190. return;
  191. } break;
  192. case RSTATE_SENDING_ABORT:
  193. case RSTATE_WAITING_END:
  194. return;
  195. default:
  196. BLog(BLOG_ERROR, "received unexpected reply");
  197. goto fail;
  198. }
  199. } break;
  200. case REQUESTPROTO_TYPE_SERVER_FINISHED:
  201. case REQUESTPROTO_TYPE_SERVER_ERROR: {
  202. if (payload_len != 0) {
  203. BLog(BLOG_ERROR, "finshed/aborted message has non-empty payload");
  204. goto fail;
  205. }
  206. NCDRequestClientRequest *creq = req->creq;
  207. req->creq = NULL;
  208. switch (req->state) {
  209. case RSTATE_SENDING_ABORT: {
  210. // set state dying send
  211. req->state = RSTATE_DEAD_SENDING;
  212. } break;
  213. case RSTATE_WAITING_END:
  214. case RSTATE_READY: {
  215. // free req
  216. req_free(req);
  217. } break;
  218. default:
  219. BLog(BLOG_ERROR, "received unexpected finished/aborted");
  220. goto fail;
  221. }
  222. // report finished
  223. if (creq) {
  224. request_report_finished(creq, type == REQUESTPROTO_TYPE_SERVER_ERROR);
  225. }
  226. return;
  227. } break;
  228. default:
  229. BLog(BLOG_ERROR, "received invalid message type");
  230. goto fail;
  231. }
  232. ASSERT(0)
  233. fail:
  234. report_error(o);
  235. }
  236. static struct NCDRequestClient_req * find_req (NCDRequestClient *o, uint32_t request_id)
  237. {
  238. BAVLNode *tn = BAVL_LookupExact(&o->reqs_tree, &request_id);
  239. if (!tn) {
  240. return NULL;
  241. }
  242. struct NCDRequestClient_req *req = UPPER_OBJECT(tn, struct NCDRequestClient_req, reqs_tree_node);
  243. ASSERT(req->request_id == request_id)
  244. return req;
  245. }
  246. static int get_free_request_id (NCDRequestClient *o, uint32_t *out)
  247. {
  248. uint32_t first = o->next_request_id;
  249. do {
  250. if (!find_req(o, o->next_request_id)) {
  251. *out = o->next_request_id;
  252. return 1;
  253. }
  254. o->next_request_id++;
  255. } while (o->next_request_id != first);
  256. return 0;
  257. }
  258. static int build_requestproto_packet (uint32_t request_id, uint32_t type, NCDValRef payload_value, uint8_t **out_data, int *out_len)
  259. {
  260. ExpString str;
  261. if (!ExpString_Init(&str)) {
  262. BLog(BLOG_ERROR, "ExpString_Init failed");
  263. goto fail0;
  264. }
  265. if (!ExpString_AppendZeros(&str, sizeof(struct packetproto_header) + sizeof(struct requestproto_header))) {
  266. BLog(BLOG_ERROR, "ExpString_AppendBinary failed");
  267. goto fail1;
  268. }
  269. if (!NCDVal_IsInvalid(payload_value) && !NCDValGenerator_AppendGenerate(payload_value, &str)) {
  270. BLog(BLOG_ERROR, "NCDValGenerator_AppendGenerate failed");
  271. goto fail1;
  272. }
  273. size_t len = ExpString_Length(&str);
  274. if (len > INT_MAX || len > PACKETPROTO_ENCLEN(SEND_MTU) || len - sizeof(struct packetproto_header) > UINT16_MAX) {
  275. BLog(BLOG_ERROR, "reply is too long");
  276. goto fail1;
  277. }
  278. uint8_t *packet = (uint8_t *)ExpString_Get(&str);
  279. struct packetproto_header pp;
  280. pp.len = htol16(len - sizeof(struct packetproto_header));
  281. struct requestproto_header rp;
  282. rp.request_id = htol32(request_id);
  283. rp.type = htol32(type);
  284. memcpy(packet, &pp, sizeof(pp));
  285. memcpy(packet + sizeof(pp), &rp, sizeof(rp));
  286. *out_data = packet;
  287. *out_len = len;
  288. return 1;
  289. fail1:
  290. ExpString_Free(&str);
  291. fail0:
  292. return 0;
  293. }
  294. static void build_nodata_packet (uint32_t request_id, uint32_t type, uint8_t *data, int *out_len)
  295. {
  296. struct packetproto_header pp;
  297. pp.len = htol16(sizeof(struct requestproto_header));
  298. struct requestproto_header rp;
  299. rp.request_id = htol32(request_id);
  300. rp.type = htol32(type);
  301. memcpy(data, &pp, sizeof(pp));
  302. memcpy(data + sizeof(pp), &rp, sizeof(rp));
  303. *out_len = sizeof(pp) + sizeof(rp);
  304. }
  305. static int req_is_aborted (struct NCDRequestClient_req *req)
  306. {
  307. switch (req->state) {
  308. case RSTATE_SENDING_REQUEST:
  309. case RSTATE_READY:
  310. return 0;
  311. default:
  312. return 1;
  313. }
  314. }
  315. static void req_abort (struct NCDRequestClient_req *req)
  316. {
  317. ASSERT(!req_is_aborted(req))
  318. ASSERT(!req->client->is_error)
  319. switch (req->state) {
  320. case RSTATE_SENDING_REQUEST: {
  321. req->state = RSTATE_SENDING_REQUEST_ABORT;
  322. } break;
  323. case RSTATE_READY: {
  324. req_send_abort(req);
  325. } break;
  326. default: ASSERT(0);
  327. }
  328. }
  329. static void req_free (struct NCDRequestClient_req *req)
  330. {
  331. NCDRequestClient *client = req->client;
  332. PacketPassFifoQueueFlow_AssertFree(&req->send_qflow);
  333. ASSERT(!req->creq)
  334. // free queue flow
  335. PacketPassFifoQueueFlow_Free(&req->send_qflow);
  336. // free request data
  337. free(req->request_data);
  338. // remove from reqs tree
  339. BAVL_Remove(&client->reqs_tree, &req->reqs_tree_node);
  340. // free structure
  341. free(req);
  342. }
  343. static void req_send_abort (struct NCDRequestClient_req *req)
  344. {
  345. // build packet
  346. build_nodata_packet(req->request_id, REQUESTPROTO_TYPE_CLIENT_ABORT, req->request_data, &req->request_len);
  347. // start sending
  348. PacketPassInterface_Sender_Send(req->send_qflow_iface, req->request_data, req->request_len);
  349. // set state sending abort
  350. req->state = RSTATE_SENDING_ABORT;
  351. }
  352. static void req_qflow_send_iface_handler_done (struct NCDRequestClient_req *req)
  353. {
  354. switch (req->state) {
  355. case RSTATE_SENDING_REQUEST: {
  356. // set state ready
  357. req->state = RSTATE_READY;
  358. // call sent handler
  359. req->creq->handler_sent(req->creq->user);
  360. return;
  361. } break;
  362. case RSTATE_SENDING_REQUEST_ABORT: {
  363. // send abort
  364. req_send_abort(req);
  365. } break;
  366. case RSTATE_SENDING_ABORT: {
  367. // set state waiting end
  368. req->state = RSTATE_WAITING_END;
  369. } break;
  370. case RSTATE_DEAD_SENDING: {
  371. // free req
  372. req_free(req);
  373. } break;
  374. default: ASSERT(0);
  375. }
  376. }
  377. int NCDRequestClient_Init (NCDRequestClient *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  378. NCDRequestClient_handler_error handler_error,
  379. NCDRequestClient_handler_connected handler_connected)
  380. {
  381. ASSERT(handler_error)
  382. ASSERT(handler_connected)
  383. // init arguments
  384. o->reactor = reactor;
  385. o->user = user;
  386. o->handler_error = handler_error;
  387. o->handler_connected = handler_connected;
  388. // init connector
  389. if (!BConnector_InitGeneric(&o->connector, addr, reactor, o, (BConnector_handler)connector_handler)) {
  390. BLog(BLOG_ERROR, "BConnector_InitGeneric failed");
  391. goto fail0;
  392. }
  393. // init reqs tree
  394. BAVL_Init(&o->reqs_tree, OFFSET_DIFF(struct NCDRequestClient_req, request_id, reqs_tree_node), uint32_comparator, NULL);
  395. // set next request ID
  396. o->next_request_id = 0;
  397. // set state connecting
  398. o->state = CSTATE_CONNECTING;
  399. // set is not error
  400. o->is_error = 0;
  401. DebugCounter_Init(&o->d_reqests_ctr);
  402. DebugError_Init(&o->d_err, BReactor_PendingGroup(reactor));
  403. DebugObject_Init(&o->d_obj);
  404. return 1;
  405. fail0:
  406. return 0;
  407. }
  408. void NCDRequestClient_Free (NCDRequestClient *o)
  409. {
  410. DebugObject_Free(&o->d_obj);
  411. DebugError_Free(&o->d_err);
  412. DebugCounter_Free(&o->d_reqests_ctr);
  413. if (o->state == CSTATE_CONNECTED) {
  414. // allow freeing queue flow
  415. PacketPassFifoQueue_PrepareFree(&o->send_queue);
  416. // free remaining reqs
  417. BAVLNode *tn;
  418. while (tn = BAVL_GetFirst(&o->reqs_tree)) {
  419. struct NCDRequestClient_req *req = UPPER_OBJECT(tn, struct NCDRequestClient_req, reqs_tree_node);
  420. ASSERT(!req->creq)
  421. req_free(req);
  422. }
  423. // free connection stuff
  424. PacketPassFifoQueue_Free(&o->send_queue);
  425. PacketStreamSender_Free(&o->send_sender);
  426. PacketProtoDecoder_Free(&o->recv_decoder);
  427. PacketPassInterface_Free(&o->recv_if);
  428. BConnection_RecvAsync_Free(&o->con);
  429. BConnection_SendAsync_Free(&o->con);
  430. BConnection_Free(&o->con);
  431. }
  432. // free connector
  433. BConnector_Free(&o->connector);
  434. }
  435. int NCDRequestClientRequest_Init (NCDRequestClientRequest *o, NCDRequestClient *client, NCDValRef payload_value, void *user,
  436. NCDRequestClientRequest_handler_sent handler_sent,
  437. NCDRequestClientRequest_handler_reply handler_reply,
  438. NCDRequestClientRequest_handler_finished handler_finished)
  439. {
  440. ASSERT(client->state == CSTATE_CONNECTED)
  441. DebugError_AssertNoError(&client->d_err);
  442. ASSERT(!NCDVal_IsInvalid(payload_value))
  443. ASSERT(handler_sent)
  444. ASSERT(handler_reply)
  445. ASSERT(handler_finished)
  446. // init arguments
  447. o->client = client;
  448. o->user = user;
  449. o->handler_sent = handler_sent;
  450. o->handler_reply = handler_reply;
  451. o->handler_finished = handler_finished;
  452. // allocate req structure
  453. struct NCDRequestClient_req *req = malloc(sizeof(*req));
  454. if (!req) {
  455. BLog(BLOG_ERROR, "malloc failed");
  456. goto fail0;
  457. }
  458. // allocate request ID
  459. if (!get_free_request_id(client, &req->request_id)) {
  460. BLog(BLOG_ERROR, "failed to allocate request ID");
  461. goto fail1;
  462. }
  463. // insert to reqs tree
  464. int res = BAVL_Insert(&client->reqs_tree, &req->reqs_tree_node, NULL);
  465. ASSERT_EXECUTE(res)
  466. // set pointers
  467. o->req = req;
  468. req->creq = o;
  469. req->client = client;
  470. // build request
  471. if (!build_requestproto_packet(req->request_id, REQUESTPROTO_TYPE_CLIENT_REQUEST, payload_value, &req->request_data, &req->request_len)) {
  472. BLog(BLOG_ERROR, "failed to build request");
  473. goto fail2;
  474. }
  475. // init queue flow
  476. PacketPassFifoQueueFlow_Init(&req->send_qflow, &client->send_queue);
  477. // init send interface
  478. req->send_qflow_iface = PacketPassFifoQueueFlow_GetInput(&req->send_qflow);
  479. PacketPassInterface_Sender_Init(req->send_qflow_iface, (PacketPassInterface_handler_done)req_qflow_send_iface_handler_done, req);
  480. // start sending request
  481. PacketPassInterface_Sender_Send(req->send_qflow_iface, req->request_data, req->request_len);
  482. // set state sending request
  483. req->state = RSTATE_SENDING_REQUEST;
  484. DebugCounter_Increment(&client->d_reqests_ctr);
  485. DebugError_Init(&o->d_err, BReactor_PendingGroup(client->reactor));
  486. DebugObject_Init(&o->d_obj);
  487. return 1;
  488. fail2:
  489. BAVL_Remove(&client->reqs_tree, &req->reqs_tree_node);
  490. fail1:
  491. free(req);
  492. fail0:
  493. return 0;
  494. }
  495. void NCDRequestClientRequest_Free (NCDRequestClientRequest *o)
  496. {
  497. struct NCDRequestClient_req *req = o->req;
  498. DebugObject_Free(&o->d_obj);
  499. DebugError_Free(&o->d_err);
  500. DebugCounter_Decrement(&o->client->d_reqests_ctr);
  501. if (req) {
  502. ASSERT(req->creq == o)
  503. // remove reference to us
  504. req->creq = NULL;
  505. // abort req if not already
  506. if (!req->client->is_error && !req_is_aborted(req)) {
  507. req_abort(req);
  508. }
  509. }
  510. }
  511. void NCDRequestClientRequest_Abort (NCDRequestClientRequest *o)
  512. {
  513. struct NCDRequestClient_req *req = o->req;
  514. DebugObject_Access(&o->d_obj);
  515. DebugError_AssertNoError(&o->d_err);
  516. DebugError_AssertNoError(&o->client->d_err);
  517. ASSERT(req)
  518. ASSERT(req->creq == o)
  519. ASSERT(!req_is_aborted(req))
  520. // abort req
  521. req_abort(req);
  522. }