NCDRequestClient.c 20 KB

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