NCDRequestClient.c 20 KB

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