winhandl.c 22 KB

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