NCDRequestClient.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 <misc/byteorder.h>
  35. #include <misc/expstring.h>
  36. #include <misc/offset.h>
  37. #include <protocol/packetproto.h>
  38. #include <protocol/requestproto.h>
  39. #include <base/BLog.h>
  40. #include "NCDRequestClient.h"
  41. #include <generated/blog_channel_NCDRequestClient.h>
  42. #define SEND_PAYLOAD_MTU 32768
  43. #define RECV_PAYLOAD_MTU 32768
  44. #define SEND_MTU (SEND_PAYLOAD_MTU + sizeof(struct requestproto_header))
  45. #define RECV_MTU (RECV_PAYLOAD_MTU + sizeof(struct requestproto_header))
  46. #define CSTATE_CONNECTING 1
  47. #define CSTATE_CONNECTED 2
  48. #define RSTATE_SENDING_REQUEST 1
  49. #define RSTATE_READY 2
  50. #define RSTATE_SENDING_REQUEST_ABORT 3
  51. #define RSTATE_SENDING_ABORT 4
  52. #define RSTATE_WAITING_END 5
  53. #define RSTATE_DEAD_SENDING 6
  54. static int uint32_comparator (void *unused, void *vv1, void *vv2);
  55. static void report_error (NCDRequestClient *o);
  56. static void request_report_finished (NCDRequestClientRequest *o, int is_error);
  57. static void connector_handler (NCDRequestClient *o, int is_error);
  58. static void connection_handler (NCDRequestClient *o, int event);
  59. static void decoder_handler_error (NCDRequestClient *o);
  60. static void recv_if_handler_send (NCDRequestClient *o, uint8_t *data, int data_len);
  61. static struct NCDRequestClient_req * find_req (NCDRequestClient *o, uint32_t request_id);
  62. static int get_free_request_id (NCDRequestClient *o, uint32_t *out);
  63. static int build_requestproto_packet (uint32_t request_id, uint32_t type, NCDValue *payload_value, uint8_t **out_data, int *out_len);
  64. static void build_nodata_packet (uint32_t request_id, uint32_t type, uint8_t *data, int *out_len);
  65. static int req_is_aborted (struct NCDRequestClient_req *req);
  66. static void req_abort (struct NCDRequestClient_req *req);
  67. static void req_free (struct NCDRequestClient_req *req);
  68. static void req_send_abort (struct NCDRequestClient_req *req);
  69. static void req_qflow_send_iface_handler_done (struct NCDRequestClient_req *req);
  70. static int uint32_comparator (void *unused, void *vv1, void *vv2)
  71. {
  72. uint32_t *v1 = vv1;
  73. uint32_t *v2 = vv2;
  74. return (*v1 > *v2) - (*v1 < *v2);
  75. }
  76. static void report_error (NCDRequestClient *o)
  77. {
  78. DEBUGERROR(&o->d_err, o->handler_error(o->user))
  79. }
  80. static void request_report_finished (NCDRequestClientRequest *o, int is_error)
  81. {
  82. o->req = NULL;
  83. DEBUGERROR(&o->d_err, o->handler_finished(o->user, is_error))
  84. }
  85. static void connector_handler (NCDRequestClient *o, int is_error)
  86. {
  87. DebugObject_Access(&o->d_obj);
  88. ASSERT(o->state == CSTATE_CONNECTING)
  89. // check error
  90. if (is_error) {
  91. BLog(BLOG_ERROR, "failed to connect to socket");
  92. goto fail0;
  93. }
  94. BPendingGroup *pg = BReactor_PendingGroup(o->reactor);
  95. // init connection
  96. if (!BConnection_Init(&o->con, BCONNECTION_SOURCE_CONNECTOR(&o->connector), o->reactor, o, (BConnection_handler)connection_handler)) {
  97. BLog(BLOG_ERROR, "BConnection_Init failed");
  98. goto fail0;
  99. }
  100. // init connection interfaces
  101. BConnection_SendAsync_Init(&o->con);
  102. BConnection_RecvAsync_Init(&o->con);
  103. StreamPassInterface *con_send_if = BConnection_SendAsync_GetIf(&o->con);
  104. StreamRecvInterface *con_recv_if = BConnection_RecvAsync_GetIf(&o->con);
  105. // init receive interface
  106. PacketPassInterface_Init(&o->recv_if, RECV_MTU, (PacketPassInterface_handler_send)recv_if_handler_send, o, pg);
  107. // init receive decoder
  108. if (!PacketProtoDecoder_Init(&o->recv_decoder, con_recv_if, &o->recv_if, pg, o, (PacketProtoDecoder_handler_error)decoder_handler_error)) {
  109. BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
  110. goto fail1;
  111. }
  112. // init send sender
  113. PacketStreamSender_Init(&o->send_sender, con_send_if, PACKETPROTO_ENCLEN(SEND_MTU), pg);
  114. // init send queue
  115. PacketPassFifoQueue_Init(&o->send_queue, PacketStreamSender_GetInput(&o->send_sender), pg);
  116. // set state connected
  117. o->state = CSTATE_CONNECTED;
  118. // call connected handler
  119. o->handler_connected(o->user);
  120. return;
  121. fail1:
  122. PacketPassInterface_Free(&o->recv_if);
  123. BConnection_RecvAsync_Free(&o->con);
  124. BConnection_SendAsync_Free(&o->con);
  125. BConnection_Free(&o->con);
  126. fail0:
  127. report_error(o);
  128. }
  129. static void connection_handler (NCDRequestClient *o, int event)
  130. {
  131. DebugObject_Access(&o->d_obj);
  132. ASSERT(o->state == CSTATE_CONNECTED)
  133. BLog(BLOG_ERROR, "connection error");
  134. report_error(o);
  135. }
  136. static void decoder_handler_error (NCDRequestClient *o)
  137. {
  138. DebugObject_Access(&o->d_obj);
  139. ASSERT(o->state == CSTATE_CONNECTED)
  140. BLog(BLOG_ERROR, "decoder error");
  141. report_error(o);
  142. }
  143. static void recv_if_handler_send (NCDRequestClient *o, uint8_t *data, int data_len)
  144. {
  145. DebugObject_Access(&o->d_obj);
  146. ASSERT(o->state == CSTATE_CONNECTED)
  147. ASSERT(data_len >= 0)
  148. ASSERT(data_len <= RECV_MTU)
  149. // accept packet
  150. PacketPassInterface_Done(&o->recv_if);
  151. if (data_len < sizeof(struct requestproto_header)) {
  152. BLog(BLOG_ERROR, "missing requestproto header");
  153. goto fail;
  154. }
  155. struct requestproto_header *header = (struct requestproto_header *)data;
  156. uint32_t request_id = ltoh32(header->request_id);
  157. uint32_t type = ltoh32(header->type);
  158. uint8_t *payload = data + sizeof(*header);
  159. int payload_len = data_len - sizeof(*header);
  160. // find request
  161. struct NCDRequestClient_req *req = find_req(o, request_id);
  162. if (!req) {
  163. BLog(BLOG_ERROR, "received packet with unknown request ID");
  164. goto fail;
  165. }
  166. switch (type) {
  167. case REQUESTPROTO_TYPE_SERVER_REPLY: {
  168. switch (o->state) {
  169. case RSTATE_READY: {
  170. // parse payload
  171. NCDValue payload_value;
  172. if (!NCDValueParser_Parse((char *)payload, payload_len, &payload_value)) {
  173. BLog(BLOG_ERROR, "failed to parse reply payload");
  174. goto fail;
  175. }
  176. // call reply handler
  177. req->creq->handler_reply(req->creq->user, payload_value);
  178. return;
  179. } break;
  180. case RSTATE_SENDING_ABORT:
  181. case RSTATE_WAITING_END:
  182. return;
  183. default:
  184. BLog(BLOG_ERROR, "received unexpected reply");
  185. goto fail;
  186. }
  187. } break;
  188. case REQUESTPROTO_TYPE_SERVER_FINISHED:
  189. case REQUESTPROTO_TYPE_SERVER_ERROR: {
  190. if (payload_len != 0) {
  191. BLog(BLOG_ERROR, "finshed/aborted message has non-empty payload");
  192. goto fail;
  193. }
  194. NCDRequestClientRequest *creq = req->creq;
  195. req->creq = NULL;
  196. switch (req->state) {
  197. case RSTATE_SENDING_ABORT: {
  198. // set state dying send
  199. req->state = RSTATE_DEAD_SENDING;
  200. } break;
  201. case RSTATE_WAITING_END:
  202. case RSTATE_READY: {
  203. // free req
  204. req_free(req);
  205. } break;
  206. default:
  207. BLog(BLOG_ERROR, "received unexpected finished/aborted");
  208. goto fail;
  209. }
  210. // report finished
  211. if (creq) {
  212. request_report_finished(creq, type == REQUESTPROTO_TYPE_SERVER_ERROR);
  213. }
  214. return;
  215. } break;
  216. default:
  217. BLog(BLOG_ERROR, "received invalid message type");
  218. goto fail;
  219. }
  220. ASSERT(0)
  221. fail:
  222. report_error(o);
  223. }
  224. static struct NCDRequestClient_req * find_req (NCDRequestClient *o, uint32_t request_id)
  225. {
  226. BAVLNode *tn = BAVL_LookupExact(&o->reqs_tree, &request_id);
  227. if (!tn) {
  228. return NULL;
  229. }
  230. struct NCDRequestClient_req *req = UPPER_OBJECT(tn, struct NCDRequestClient_req, reqs_tree_node);
  231. ASSERT(req->request_id == request_id)
  232. return req;
  233. }
  234. static int get_free_request_id (NCDRequestClient *o, uint32_t *out)
  235. {
  236. uint32_t first = o->next_request_id;
  237. do {
  238. if (!find_req(o, o->next_request_id)) {
  239. *out = o->next_request_id;
  240. return 1;
  241. }
  242. o->next_request_id++;
  243. } while (o->next_request_id != first);
  244. return 0;
  245. }
  246. static int build_requestproto_packet (uint32_t request_id, uint32_t type, NCDValue *payload_value, uint8_t **out_data, int *out_len)
  247. {
  248. struct header {
  249. struct packetproto_header pp;
  250. struct requestproto_header rp;
  251. } __attribute__((packed));
  252. ExpString str;
  253. if (!ExpString_Init(&str)) {
  254. BLog(BLOG_ERROR, "ExpString_Init failed");
  255. goto fail0;
  256. }
  257. if (!ExpString_AppendZeros(&str, sizeof(struct header))) {
  258. BLog(BLOG_ERROR, "ExpString_AppendBinary failed");
  259. goto fail1;
  260. }
  261. if (payload_value && !NCDValueGenerator_AppendGenerate(payload_value, &str)) {
  262. BLog(BLOG_ERROR, "NCDValueGenerator_AppendGenerate failed");
  263. goto fail1;
  264. }
  265. size_t len = ExpString_Length(&str);
  266. if (len > INT_MAX || len > PACKETPROTO_ENCLEN(SEND_MTU) || len - sizeof(struct packetproto_header) > UINT16_MAX) {
  267. BLog(BLOG_ERROR, "reply is too long");
  268. goto fail1;
  269. }
  270. uint8_t *packet = (uint8_t *)ExpString_Get(&str);
  271. struct header *header = (void *)packet;
  272. header->pp.len = htol16(len - sizeof(struct packetproto_header));
  273. header->rp.request_id = htol32(request_id);
  274. header->rp.type = htol32(type);
  275. *out_data = packet;
  276. *out_len = len;
  277. return 1;
  278. fail1:
  279. ExpString_Free(&str);
  280. fail0:
  281. return 0;
  282. }
  283. static void build_nodata_packet (uint32_t request_id, uint32_t type, uint8_t *data, int *out_len)
  284. {
  285. struct header {
  286. struct packetproto_header pp;
  287. struct requestproto_header rp;
  288. } __attribute__((packed));
  289. struct header *header = (void *)data;
  290. header->pp.len = htol16(sizeof(header->rp));
  291. header->rp.request_id = htol32(request_id);
  292. header->rp.type = htol32(type);
  293. *out_len = sizeof(*header);
  294. }
  295. static int req_is_aborted (struct NCDRequestClient_req *req)
  296. {
  297. switch (req->state) {
  298. case RSTATE_SENDING_REQUEST:
  299. case RSTATE_READY:
  300. return 0;
  301. default:
  302. return 1;
  303. }
  304. }
  305. static void req_abort (struct NCDRequestClient_req *req)
  306. {
  307. ASSERT(!req_is_aborted(req))
  308. switch (req->state) {
  309. case RSTATE_SENDING_REQUEST: {
  310. req->state = RSTATE_SENDING_REQUEST_ABORT;
  311. } break;
  312. case RSTATE_READY: {
  313. req_send_abort(req);
  314. } break;
  315. default: ASSERT(0);
  316. }
  317. }
  318. static void req_free (struct NCDRequestClient_req *req)
  319. {
  320. NCDRequestClient *client = req->client;
  321. PacketPassFifoQueueFlow_AssertFree(&req->send_qflow);
  322. ASSERT(!req->creq)
  323. // free queue flow
  324. PacketPassFifoQueueFlow_Free(&req->send_qflow);
  325. // free request data
  326. free(req->request_data);
  327. // remove from reqs tree
  328. BAVL_Remove(&client->reqs_tree, &req->reqs_tree_node);
  329. // free structure
  330. free(req);
  331. }
  332. static void req_send_abort (struct NCDRequestClient_req *req)
  333. {
  334. // build packet
  335. build_nodata_packet(req->request_id, REQUESTPROTO_TYPE_CLIENT_ABORT, req->request_data, &req->request_len);
  336. // start sending
  337. PacketPassInterface_Sender_Send(req->send_qflow_iface, req->request_data, req->request_len);
  338. // set state sending abort
  339. req->state = RSTATE_SENDING_ABORT;
  340. }
  341. static void req_qflow_send_iface_handler_done (struct NCDRequestClient_req *req)
  342. {
  343. switch (req->state) {
  344. case RSTATE_SENDING_REQUEST: {
  345. // set state ready
  346. req->state = RSTATE_READY;
  347. // call sent handler
  348. req->creq->handler_sent(req->creq->user);
  349. return;
  350. } break;
  351. case RSTATE_SENDING_REQUEST_ABORT: {
  352. // send abort
  353. req_send_abort(req);
  354. } break;
  355. case RSTATE_SENDING_ABORT: {
  356. // set state waiting end
  357. req->state = RSTATE_WAITING_END;
  358. } break;
  359. case RSTATE_DEAD_SENDING: {
  360. // free req
  361. req_free(req);
  362. } break;
  363. default: ASSERT(0);
  364. }
  365. }
  366. int NCDRequestClient_Init (NCDRequestClient *o, struct NCDRequestClient_addr addr, BReactor *reactor, void *user,
  367. NCDRequestClient_handler_error handler_error,
  368. NCDRequestClient_handler_connected handler_connected)
  369. {
  370. ASSERT(handler_error)
  371. ASSERT(handler_connected)
  372. // init arguments
  373. o->reactor = reactor;
  374. o->user = user;
  375. o->handler_error = handler_error;
  376. o->handler_connected = handler_connected;
  377. // init connector
  378. switch (addr.type) {
  379. case NCDREQUESTCLIENT_ADDR_TYPE_UNIX: {
  380. ASSERT(addr.u.unix_socket_path)
  381. if (!BConnector_InitUnix(&o->connector, addr.u.unix_socket_path, reactor, o, (BConnector_handler)connector_handler)) {
  382. BLog(BLOG_ERROR, "BConnector_InitUnix failed");
  383. goto fail0;
  384. }
  385. } break;
  386. case NCDREQUESTCLIENT_ADDR_TYPE_TCP: {
  387. BAddr_Assert(&addr.u.tcp_baddr);
  388. if (!BConnector_Init(&o->connector, addr.u.tcp_baddr, reactor, o, (BConnector_handler)connector_handler)) {
  389. BLog(BLOG_ERROR, "BConnector_InitUnix failed");
  390. goto fail0;
  391. }
  392. } break;
  393. default: ASSERT(0);
  394. }
  395. // init reqs tree
  396. BAVL_Init(&o->reqs_tree, OFFSET_DIFF(struct NCDRequestClient_req, request_id, reqs_tree_node), uint32_comparator, NULL);
  397. // set next request ID
  398. o->next_request_id = 0;
  399. // set state connecting
  400. o->state = CSTATE_CONNECTING;
  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. ASSERT(req->state != RSTATE_SENDING_REQUEST)
  422. ASSERT(req->state != RSTATE_READY)
  423. req_free(req);
  424. }
  425. // free connection stuff
  426. PacketPassFifoQueue_Free(&o->send_queue);
  427. PacketStreamSender_Free(&o->send_sender);
  428. PacketProtoDecoder_Free(&o->recv_decoder);
  429. PacketPassInterface_Free(&o->recv_if);
  430. BConnection_RecvAsync_Free(&o->con);
  431. BConnection_SendAsync_Free(&o->con);
  432. BConnection_Free(&o->con);
  433. }
  434. // free connector
  435. BConnector_Free(&o->connector);
  436. }
  437. int NCDRequestClientRequest_Init (NCDRequestClientRequest *o, NCDRequestClient *client, NCDValue *payload_value, void *user,
  438. NCDRequestClientRequest_handler_sent handler_sent,
  439. NCDRequestClientRequest_handler_reply handler_reply,
  440. NCDRequestClientRequest_handler_finished handler_finished)
  441. {
  442. ASSERT(client->state == CSTATE_CONNECTED)
  443. DebugError_AssertNoError(&client->d_err);
  444. ASSERT(payload_value)
  445. ASSERT(handler_sent)
  446. ASSERT(handler_reply)
  447. ASSERT(handler_finished)
  448. // init arguments
  449. o->client = client;
  450. o->user = user;
  451. o->handler_sent = handler_sent;
  452. o->handler_reply = handler_reply;
  453. o->handler_finished = handler_finished;
  454. // allocate req structure
  455. struct NCDRequestClient_req *req = malloc(sizeof(*req));
  456. if (!req) {
  457. BLog(BLOG_ERROR, "malloc failed");
  458. goto fail0;
  459. }
  460. // allocate request ID
  461. if (!get_free_request_id(client, &req->request_id)) {
  462. BLog(BLOG_ERROR, "failed to allocate request ID");
  463. goto fail1;
  464. }
  465. // insert to reqs tree
  466. int res = BAVL_Insert(&client->reqs_tree, &req->reqs_tree_node, NULL);
  467. ASSERT(res)
  468. // set pointers
  469. o->req = req;
  470. req->creq = o;
  471. req->client = client;
  472. // build request
  473. if (!build_requestproto_packet(req->request_id, REQUESTPROTO_TYPE_CLIENT_REQUEST, payload_value, &req->request_data, &req->request_len)) {
  474. BLog(BLOG_ERROR, "failed to build request");
  475. goto fail2;
  476. }
  477. // init queue flow
  478. PacketPassFifoQueueFlow_Init(&req->send_qflow, &client->send_queue);
  479. // init send interface
  480. req->send_qflow_iface = PacketPassFifoQueueFlow_GetInput(&req->send_qflow);
  481. PacketPassInterface_Sender_Init(req->send_qflow_iface, (PacketPassInterface_handler_done)req_qflow_send_iface_handler_done, req);
  482. // start sending request
  483. PacketPassInterface_Sender_Send(req->send_qflow_iface, req->request_data, req->request_len);
  484. // set state sending request
  485. req->state = RSTATE_SENDING_REQUEST;
  486. DebugCounter_Increment(&client->d_reqests_ctr);
  487. DebugError_Init(&o->d_err, BReactor_PendingGroup(client->reactor));
  488. DebugObject_Init(&o->d_obj);
  489. return 1;
  490. fail2:
  491. BAVL_Remove(&client->reqs_tree, &req->reqs_tree_node);
  492. fail1:
  493. free(req);
  494. fail0:
  495. return 0;
  496. }
  497. void NCDRequestClientRequest_Free (NCDRequestClientRequest *o)
  498. {
  499. struct NCDRequestClient_req *req = o->req;
  500. DebugObject_Free(&o->d_obj);
  501. DebugError_Free(&o->d_err);
  502. DebugCounter_Decrement(&o->client->d_reqests_ctr);
  503. if (req) {
  504. ASSERT(req->creq == o)
  505. // remove reference to us
  506. req->creq = NULL;
  507. // abort req if not already
  508. if (!req_is_aborted(req)) {
  509. req_abort(req);
  510. }
  511. }
  512. }
  513. void NCDRequestClientRequest_Abort (NCDRequestClientRequest *o)
  514. {
  515. struct NCDRequestClient_req *req = o->req;
  516. DebugObject_Access(&o->d_obj);
  517. DebugError_AssertNoError(&o->d_err);
  518. ASSERT(req)
  519. ASSERT(req->creq == o)
  520. ASSERT(!req_is_aborted(req))
  521. // abort req
  522. req_abort(req);
  523. }