handle-io.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * handle-io.c: Module to give Windows front ends the general
  3. * ability to deal with consoles, pipes, serial ports, or any other
  4. * type of data stream accessed through a Windows API HANDLE rather
  5. * than a WinSock SOCKET.
  6. *
  7. * We do this by spawning a subthread to continuously try to read
  8. * from the handle. Every time a read successfully returns some
  9. * data, the subthread sets an event object which is picked up by
  10. * the main thread, and the main thread then sets an event in
  11. * return to instruct the subthread to resume reading.
  12. *
  13. * Output works precisely the other way round, in a second
  14. * subthread. The output subthread should not be attempting to
  15. * write all the time, because it hasn't always got data _to_
  16. * write; so the output thread waits for an event object notifying
  17. * it to _attempt_ a write, and then it sets an event in return
  18. * when one completes.
  19. *
  20. * (It's terribly annoying having to spawn a subthread for each
  21. * direction of each handle. Technically it isn't necessary for
  22. * serial ports, since we could use overlapped I/O within the main
  23. * thread and wait directly on the event objects in the OVERLAPPED
  24. * structures. However, we can't use this trick for some types of
  25. * file handle at all - for some reason Windows restricts use of
  26. * OVERLAPPED to files which were opened with the overlapped flag -
  27. * and so we must use threads for those. This being the case, it's
  28. * simplest just to use threads for everything rather than trying
  29. * to keep track of multiple completely separate mechanisms.)
  30. */
  31. #include <assert.h>
  32. #include "putty.h"
  33. /* ----------------------------------------------------------------------
  34. * Generic definitions.
  35. */
  36. #ifndef WINSCP
  37. // Moved to putty.h
  38. typedef struct handle_list_node handle_list_node;
  39. struct handle_list_node {
  40. handle_list_node *next, *prev;
  41. };
  42. #endif
  43. static void add_to_ready_list(struct handle_generic *ctx); // WINSCP
  44. /*
  45. * Maximum amount of backlog we will allow to build up on an input
  46. * handle before we stop reading from it.
  47. */
  48. #define MAX_BACKLOG 32768
  49. struct handle_generic {
  50. /*
  51. * Initial fields common to both handle_input and handle_output
  52. * structures.
  53. *
  54. * The three HANDLEs are set up at initialisation time and are
  55. * thereafter read-only to both main thread and subthread.
  56. * `moribund' is only used by the main thread; `done' is
  57. * written by the main thread before signalling to the
  58. * subthread. `defunct' and `busy' are used only by the main
  59. * thread.
  60. */
  61. HANDLE h; /* the handle itself */
  62. handle_list_node ready_node; /* for linking on to the ready list */
  63. HANDLE ev_from_main; /* event used to signal back to us */
  64. bool moribund; /* are we going to kill this soon? */
  65. bool done; /* request subthread to terminate */
  66. bool defunct; /* has the subthread already gone? */
  67. bool busy; /* operation currently in progress? */
  68. void *privdata; /* for client to remember who they are */
  69. struct callback_set * callback_set; // WINSCP
  70. };
  71. typedef enum { HT_INPUT, HT_OUTPUT } HandleType;
  72. /* ----------------------------------------------------------------------
  73. * Input threads.
  74. */
  75. /*
  76. * Data required by an input thread.
  77. */
  78. struct handle_input {
  79. /*
  80. * Copy of the handle_generic structure.
  81. */
  82. HANDLE h; /* the handle itself */
  83. handle_list_node ready_node; /* for linking on to the ready list */
  84. HANDLE ev_from_main; /* event used to signal back to us */
  85. bool moribund; /* are we going to kill this soon? */
  86. bool done; /* request subthread to terminate */
  87. bool defunct; /* has the subthread already gone? */
  88. bool busy; /* operation currently in progress? */
  89. void *privdata; /* for client to remember who they are */
  90. struct callback_set * callback_set; // WINSCP
  91. /*
  92. * Data set at initialisation and then read-only.
  93. */
  94. int flags;
  95. /*
  96. * Data set by the input thread before marking the handle ready,
  97. * and read by the main thread after receiving that signal.
  98. */
  99. char buffer[4096]; /* the data read from the handle */
  100. DWORD len; /* how much data that was */
  101. int readerr; /* lets us know about read errors */
  102. /*
  103. * Callback function called by this module when data arrives on
  104. * an input handle.
  105. */
  106. handle_inputfn_t gotdata;
  107. };
  108. /*
  109. * The actual thread procedure for an input thread.
  110. */
  111. static DWORD WINAPI handle_input_threadfunc(void *param)
  112. {
  113. struct handle_input *ctx = (struct handle_input *) param;
  114. OVERLAPPED ovl, *povl;
  115. HANDLE oev;
  116. bool readret, finished;
  117. int readlen;
  118. if (ctx->flags & HANDLE_FLAG_OVERLAPPED) {
  119. povl = &ovl;
  120. oev = CreateEvent(NULL, true, false, NULL);
  121. } else {
  122. povl = NULL;
  123. }
  124. if (ctx->flags & HANDLE_FLAG_UNITBUFFER)
  125. readlen = 1;
  126. else
  127. readlen = sizeof(ctx->buffer);
  128. while (1) {
  129. if (povl) {
  130. memset(povl, 0, sizeof(OVERLAPPED));
  131. povl->hEvent = oev;
  132. }
  133. readret = ReadFile(ctx->h, ctx->buffer,readlen, &ctx->len, povl);
  134. if (!readret)
  135. ctx->readerr = GetLastError();
  136. else
  137. ctx->readerr = 0;
  138. if (povl && !readret && ctx->readerr == ERROR_IO_PENDING) {
  139. WaitForSingleObject(povl->hEvent, INFINITE);
  140. readret = GetOverlappedResult(ctx->h, povl, &ctx->len, false);
  141. if (!readret)
  142. ctx->readerr = GetLastError();
  143. else
  144. ctx->readerr = 0;
  145. }
  146. if (!readret) {
  147. /*
  148. * Windows apparently sends ERROR_BROKEN_PIPE when a
  149. * pipe we're reading from is closed normally from the
  150. * writing end. This is ludicrous; if that situation
  151. * isn't a natural EOF, _nothing_ is. So if we get that
  152. * particular error, we pretend it's EOF.
  153. */
  154. if (ctx->readerr == ERROR_BROKEN_PIPE)
  155. ctx->readerr = 0;
  156. ctx->len = 0;
  157. }
  158. if (readret && ctx->len == 0 &&
  159. (ctx->flags & HANDLE_FLAG_IGNOREEOF))
  160. continue;
  161. /*
  162. * If we just set ctx->len to 0, that means the read operation
  163. * has returned end-of-file. Telling that to the main thread
  164. * will cause it to set its 'defunct' flag and dispose of the
  165. * handle structure at the next opportunity, in which case we
  166. * mustn't touch ctx at all after the SetEvent. (Hence we do
  167. * even _this_ check before the SetEvent.)
  168. */
  169. finished = (ctx->len == 0);
  170. add_to_ready_list(ctx); // WINSCP
  171. if (finished)
  172. break;
  173. WaitForSingleObject(ctx->ev_from_main, INFINITE);
  174. if (ctx->done) {
  175. /*
  176. * The main thread has asked us to shut down. Send back an
  177. * event indicating that we've done so. Hereafter we must
  178. * not touch ctx at all, because the main thread might
  179. * have freed it.
  180. */
  181. add_to_ready_list(ctx); // WINSCP
  182. break;
  183. }
  184. }
  185. if (povl)
  186. CloseHandle(oev);
  187. return 0;
  188. }
  189. /*
  190. * This is called after a successful read, or from the
  191. * `unthrottle' function. It decides whether or not to begin a new
  192. * read operation.
  193. */
  194. static void handle_throttle(struct handle_input *ctx, int backlog)
  195. {
  196. if (ctx->defunct)
  197. return;
  198. /*
  199. * If there's a read operation already in progress, do nothing:
  200. * when that completes, we'll come back here and be in a
  201. * position to make a better decision.
  202. */
  203. if (ctx->busy)
  204. return;
  205. /*
  206. * Otherwise, we must decide whether to start a new read based
  207. * on the size of the backlog.
  208. */
  209. if (backlog < MAX_BACKLOG) {
  210. SetEvent(ctx->ev_from_main);
  211. ctx->busy = true;
  212. }
  213. }
  214. /* ----------------------------------------------------------------------
  215. * Output threads.
  216. */
  217. /*
  218. * Data required by an output thread.
  219. */
  220. struct handle_output {
  221. /*
  222. * Copy of the handle_generic structure.
  223. */
  224. HANDLE h; /* the handle itself */
  225. handle_list_node ready_node; /* for linking on to the ready list */
  226. HANDLE ev_from_main; /* event used to signal back to us */
  227. bool moribund; /* are we going to kill this soon? */
  228. bool done; /* request subthread to terminate */
  229. bool defunct; /* has the subthread already gone? */
  230. bool busy; /* operation currently in progress? */
  231. void *privdata; /* for client to remember who they are */
  232. struct callback_set * callback_set; // WINSCP
  233. /*
  234. * Data set at initialisation and then read-only.
  235. */
  236. int flags;
  237. /*
  238. * Data set by the main thread before signalling ev_from_main,
  239. * and read by the input thread after receiving that signal.
  240. */
  241. const char *buffer; /* the data to write */
  242. DWORD len; /* how much data there is */
  243. /*
  244. * Data set by the input thread before marking this handle as
  245. * ready, and read by the main thread after receiving that signal.
  246. */
  247. DWORD lenwritten; /* how much data we actually wrote */
  248. int writeerr; /* return value from WriteFile */
  249. /*
  250. * Data only ever read or written by the main thread.
  251. */
  252. bufchain queued_data; /* data still waiting to be written */
  253. enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
  254. /*
  255. * Callback function called when the backlog in the bufchain
  256. * drops.
  257. */
  258. handle_outputfn_t sentdata;
  259. struct handle *sentdata_param;
  260. };
  261. static DWORD WINAPI handle_output_threadfunc(void *param)
  262. {
  263. struct handle_output *ctx = (struct handle_output *) param;
  264. OVERLAPPED ovl, *povl;
  265. HANDLE oev;
  266. bool writeret;
  267. if (ctx->flags & HANDLE_FLAG_OVERLAPPED) {
  268. povl = &ovl;
  269. oev = CreateEvent(NULL, true, false, NULL);
  270. } else {
  271. povl = NULL;
  272. }
  273. while (1) {
  274. WaitForSingleObject(ctx->ev_from_main, INFINITE);
  275. if (ctx->done) {
  276. /*
  277. * The main thread has asked us to shut down. Send back an
  278. * event indicating that we've done so. Hereafter we must
  279. * not touch ctx at all, because the main thread might
  280. * have freed it.
  281. */
  282. add_to_ready_list(ctx); // WINSCP
  283. break;
  284. }
  285. if (povl) {
  286. memset(povl, 0, sizeof(OVERLAPPED));
  287. povl->hEvent = oev;
  288. }
  289. writeret = WriteFile(ctx->h, ctx->buffer, ctx->len,
  290. &ctx->lenwritten, povl);
  291. if (!writeret)
  292. ctx->writeerr = GetLastError();
  293. else
  294. ctx->writeerr = 0;
  295. if (povl && !writeret && GetLastError() == ERROR_IO_PENDING) {
  296. writeret = GetOverlappedResult(ctx->h, povl,
  297. &ctx->lenwritten, true);
  298. if (!writeret)
  299. ctx->writeerr = GetLastError();
  300. else
  301. ctx->writeerr = 0;
  302. }
  303. add_to_ready_list(ctx); // WINSCP
  304. if (!writeret) {
  305. /*
  306. * The write operation has suffered an error. Telling that
  307. * to the main thread will cause it to set its 'defunct'
  308. * flag and dispose of the handle structure at the next
  309. * opportunity, so we must not touch ctx at all after
  310. * this.
  311. */
  312. break;
  313. }
  314. }
  315. if (povl)
  316. CloseHandle(oev);
  317. return 0;
  318. }
  319. static void handle_try_output(struct handle_output *ctx)
  320. {
  321. if (!ctx->busy && bufchain_size(&ctx->queued_data)) {
  322. ptrlen data = bufchain_prefix(&ctx->queued_data);
  323. ctx->buffer = data.ptr;
  324. ctx->len = min(data.len, ~(DWORD)0);
  325. SetEvent(ctx->ev_from_main);
  326. ctx->busy = true;
  327. } else if (!ctx->busy && bufchain_size(&ctx->queued_data) == 0 &&
  328. ctx->outgoingeof == EOF_PENDING) {
  329. ctx->sentdata(ctx->sentdata_param, 0, 0, true);
  330. ctx->h = INVALID_HANDLE_VALUE;
  331. ctx->outgoingeof = EOF_SENT;
  332. }
  333. }
  334. /* ----------------------------------------------------------------------
  335. * Unified code handling both input and output threads.
  336. */
  337. struct handle {
  338. HandleType type;
  339. union {
  340. struct handle_generic g;
  341. struct handle_input i;
  342. struct handle_output o;
  343. } u;
  344. };
  345. #ifndef WINSCP
  346. /*
  347. * Linked list storing the current list of handles ready to have
  348. * something done to them by the main thread.
  349. */
  350. static handle_list_node ready_head[1];
  351. static CRITICAL_SECTION ready_critsec[1];
  352. /*
  353. * Event object used by all subthreads to signal that they've just put
  354. * something on the ready list, i.e. that the ready list is non-empty.
  355. */
  356. static HANDLE ready_event = INVALID_HANDLE_VALUE;
  357. #endif
  358. static void add_to_ready_list(struct handle_generic *ctx) // WINSCP
  359. {
  360. handle_list_node *node = &ctx->ready_node; // WINSCP
  361. struct callback_set * callback_set = ctx->callback_set;
  362. /*
  363. * Called from subthreads, when their handle has done something
  364. * that they need the main thread to respond to. We append the
  365. * given list node to the end of the ready list, and set
  366. * ready_event to signal to the main thread that the ready list is
  367. * now non-empty.
  368. */
  369. EnterCriticalSection(callback_set->ready_critsec);
  370. node->next = callback_set->ready_head;
  371. node->prev = callback_set->ready_head->prev;
  372. node->next->prev = node->prev->next = node;
  373. SetEvent(callback_set->ready_event);
  374. LeaveCriticalSection(callback_set->ready_critsec);
  375. }
  376. static void remove_from_ready_list(struct handle_generic *ctx) // WINSCP
  377. {
  378. handle_list_node *node = &ctx->ready_node; // WINSCP
  379. struct callback_set * callback_set = ctx->callback_set;
  380. /*
  381. * Called from the main thread, just before destroying a 'struct
  382. * handle' completely: as a precaution, we make absolutely sure
  383. * it's not linked on the ready list, just in case somehow it
  384. * still was.
  385. */
  386. EnterCriticalSection(callback_set->ready_critsec);
  387. node->next->prev = node->prev;
  388. node->prev->next = node->next;
  389. node->next = node->prev = node;
  390. LeaveCriticalSection(callback_set->ready_critsec);
  391. }
  392. static bool handle_ready(struct handle *h); /* process one handle (below) */ // WINSCP
  393. static bool handle_ready_callback(struct callback_set * callback_set, void *vctx) // WINSCP
  394. {
  395. /*
  396. * Called when the main thread detects ready_event, indicating
  397. * that at least one handle is on the ready list. We empty the
  398. * whole list and process the handles one by one.
  399. *
  400. * It's possible that other handles may be destroyed, and hence
  401. * taken _off_ the ready list, during this processing. That
  402. * shouldn't cause a deadlock, because according to the API docs,
  403. * it's safe to call EnterCriticalSection twice in the same thread
  404. * - the second call will return immediately because that thread
  405. * already owns the critsec. (And then it takes two calls to
  406. * LeaveCriticalSection to release it again, which is just what we
  407. * want here.)
  408. */
  409. bool result = false;
  410. EnterCriticalSection(callback_set->ready_critsec);
  411. while (callback_set->ready_head->next != callback_set->ready_head) {
  412. handle_list_node *node = callback_set->ready_head->next;
  413. node->prev->next = node->next;
  414. node->next->prev = node->prev;
  415. node->next = node->prev = node;
  416. if (handle_ready(container_of(node, struct handle, u.g.ready_node))) // WINSCP
  417. {
  418. result = true; // WINSCP
  419. }
  420. }
  421. LeaveCriticalSection(callback_set->ready_critsec);
  422. return result;
  423. }
  424. static inline void ensure_ready_event_setup(struct callback_set * callback_set) // WINSCP
  425. {
  426. if (callback_set->ready_event == INVALID_HANDLE_VALUE) {
  427. callback_set->ready_head->prev = callback_set->ready_head->next = callback_set->ready_head;
  428. InitializeCriticalSection(callback_set->ready_critsec);
  429. callback_set->ready_event = CreateEvent(NULL, false, false, NULL);
  430. add_handle_wait(callback_set, callback_set->ready_event, handle_ready_callback, NULL);
  431. }
  432. }
  433. struct handle *handle_input_new(struct callback_set * callback_set, HANDLE handle, handle_inputfn_t gotdata,
  434. void *privdata, int flags)
  435. {
  436. struct handle *h = snew(struct handle);
  437. DWORD in_threadid; /* required for Win9x */
  438. h->type = HT_INPUT;
  439. h->u.i.h = handle;
  440. h->u.i.ev_from_main = CreateEvent(NULL, false, false, NULL);
  441. h->u.i.gotdata = gotdata;
  442. h->u.i.defunct = false;
  443. h->u.i.moribund = false;
  444. h->u.i.done = false;
  445. h->u.i.privdata = privdata;
  446. h->u.i.flags = flags;
  447. h->u.i.callback_set = callback_set; // WINSCP
  448. ensure_ready_event_setup(callback_set);
  449. { // WINSCP
  450. HANDLE hThread = CreateThread(NULL, 0, handle_input_threadfunc,
  451. &h->u.i, 0, &in_threadid);
  452. if (hThread)
  453. CloseHandle(hThread); /* we don't need the thread handle */
  454. h->u.i.busy = true;
  455. return h;
  456. } // WINSCP
  457. }
  458. struct handle *handle_output_new(struct callback_set * callback_set, HANDLE handle, handle_outputfn_t sentdata, // WINSCP
  459. void *privdata, int flags)
  460. {
  461. struct handle *h = snew(struct handle);
  462. DWORD out_threadid; /* required for Win9x */
  463. h->type = HT_OUTPUT;
  464. h->u.o.h = handle;
  465. h->u.o.ev_from_main = CreateEvent(NULL, false, false, NULL);
  466. h->u.o.busy = false;
  467. h->u.o.defunct = false;
  468. h->u.o.moribund = false;
  469. h->u.o.done = false;
  470. h->u.o.privdata = privdata;
  471. bufchain_init(&h->u.o.queued_data);
  472. h->u.o.outgoingeof = EOF_NO;
  473. h->u.o.sentdata = sentdata;
  474. h->u.o.sentdata_param = h;
  475. h->u.o.flags = flags;
  476. h->u.o.callback_set = callback_set; // WINSCP
  477. ensure_ready_event_setup(callback_set);
  478. { // WINSCP
  479. HANDLE hThread = CreateThread(NULL, 0, handle_output_threadfunc,
  480. &h->u.o, 0, &out_threadid);
  481. if (hThread)
  482. CloseHandle(hThread); /* we don't need the thread handle */
  483. return h;
  484. } // WINSCP
  485. }
  486. size_t handle_write(struct handle *h, const void *data, size_t len)
  487. {
  488. assert(h->type == HT_OUTPUT);
  489. assert(h->u.o.outgoingeof == EOF_NO);
  490. bufchain_add(&h->u.o.queued_data, data, len);
  491. handle_try_output(&h->u.o);
  492. return bufchain_size(&h->u.o.queued_data);
  493. }
  494. void handle_write_eof(struct handle *h)
  495. {
  496. /*
  497. * This function is called when we want to proactively send an
  498. * end-of-file notification on the handle. We can only do this by
  499. * actually closing the handle - so never call this on a
  500. * bidirectional handle if we're still interested in its incoming
  501. * direction!
  502. */
  503. assert(h->type == HT_OUTPUT);
  504. if (h->u.o.outgoingeof == EOF_NO) {
  505. h->u.o.outgoingeof = EOF_PENDING;
  506. handle_try_output(&h->u.o);
  507. }
  508. }
  509. static void handle_destroy(struct handle *h)
  510. {
  511. if (h->type == HT_OUTPUT)
  512. bufchain_clear(&h->u.o.queued_data);
  513. CloseHandle(h->u.g.ev_from_main);
  514. remove_from_ready_list(&h->u.g); // WINSCP
  515. sfree(h);
  516. }
  517. void handle_free(struct handle *h)
  518. {
  519. assert(h && !h->u.g.moribund);
  520. if (h->u.g.busy) {
  521. /*
  522. * If the handle is currently busy, we cannot immediately free
  523. * it, because its subthread is in the middle of something.
  524. * (Exception: foreign handles don't have a subthread.)
  525. *
  526. * Instead we must wait until it's finished its current
  527. * operation, because otherwise the subthread will write to
  528. * invalid memory after we free its context from under it. So
  529. * we set the moribund flag, which will be noticed next time
  530. * an operation completes.
  531. */
  532. h->u.g.moribund = true;
  533. } else if (h->u.g.defunct) {
  534. /*
  535. * There isn't even a subthread; we can go straight to
  536. * handle_destroy.
  537. */
  538. handle_destroy(h); // WINSCP
  539. } else {
  540. /*
  541. * The subthread is alive but not busy, so we now signal it
  542. * to die. Set the moribund flag to indicate that it will
  543. * want destroying after that.
  544. */
  545. h->u.g.moribund = true;
  546. h->u.g.done = true;
  547. h->u.g.busy = true;
  548. SetEvent(h->u.g.ev_from_main);
  549. }
  550. }
  551. static bool handle_ready(struct handle *h) // WINSCP
  552. {
  553. bool result = false; // WINSCP
  554. if (h->u.g.moribund) {
  555. /*
  556. * A moribund handle is one which we have either already
  557. * signalled to die, or are waiting until its current I/O op
  558. * completes to do so. Either way, it's treated as already
  559. * dead from the external user's point of view, so we ignore
  560. * the actual I/O result. We just signal the thread to die if
  561. * we haven't yet done so, or destroy the handle if not.
  562. */
  563. if (h->u.g.done) {
  564. handle_destroy(h); // WINSCP
  565. } else {
  566. h->u.g.done = true;
  567. h->u.g.busy = true;
  568. SetEvent(h->u.g.ev_from_main);
  569. }
  570. return result;
  571. }
  572. switch (h->type) {
  573. int backlog;
  574. case HT_INPUT:
  575. h->u.i.busy = false;
  576. /*
  577. * A signal on an input handle means data has arrived.
  578. */
  579. if (h->u.i.len == 0) {
  580. /*
  581. * EOF, or (nearly equivalently) read error.
  582. */
  583. h->u.i.defunct = true;
  584. h->u.i.gotdata(h, NULL, 0, h->u.i.readerr);
  585. } else {
  586. backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len, 0);
  587. handle_throttle(&h->u.i, backlog);
  588. }
  589. result = true;
  590. break;
  591. case HT_OUTPUT:
  592. h->u.o.busy = false;
  593. /*
  594. * A signal on an output handle means we have completed a
  595. * write. Call the callback to indicate that the output
  596. * buffer size has decreased, or to indicate an error.
  597. */
  598. if (h->u.o.writeerr) {
  599. /*
  600. * Write error. Send a negative value to the callback,
  601. * and mark the thread as defunct (because the output
  602. * thread is terminating by now).
  603. */
  604. h->u.o.defunct = true;
  605. h->u.o.sentdata(h, 0, h->u.o.writeerr, false);
  606. } else {
  607. bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten);
  608. noise_ultralight(NOISE_SOURCE_IOLEN, h->u.o.lenwritten);
  609. h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data), 0, false);
  610. handle_try_output(&h->u.o);
  611. }
  612. break;
  613. }
  614. return result;
  615. }
  616. void handle_unthrottle(struct handle *h, size_t backlog)
  617. {
  618. assert(h->type == HT_INPUT);
  619. handle_throttle(&h->u.i, backlog);
  620. }
  621. size_t handle_backlog(struct handle *h)
  622. {
  623. assert(h->type == HT_OUTPUT);
  624. return bufchain_size(&h->u.o.queued_data);
  625. }
  626. void *handle_get_privdata(struct handle *h)
  627. {
  628. return h->u.g.privdata;
  629. }
  630. static void handle_sink_write(BinarySink *bs, const void *data, size_t len)
  631. {
  632. handle_sink *sink = BinarySink_DOWNCAST(bs, handle_sink);
  633. handle_write(sink->h, data, len);
  634. }
  635. void handle_sink_init(handle_sink *sink, struct handle *h)
  636. {
  637. sink->h = h;
  638. BinarySink_INIT(sink, handle_sink_write);
  639. }