winhandl.c 21 KB

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