handle-io.c 24 KB

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