winhandl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. const 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. if (!ctx->busy && bufchain_size(&ctx->queued_data)) {
  310. ptrlen data = bufchain_prefix(&ctx->queued_data);
  311. ctx->buffer = data.ptr;
  312. ctx->len = min(data.len, ~(DWORD)0);
  313. SetEvent(ctx->ev_from_main);
  314. ctx->busy = true;
  315. } else if (!ctx->busy && bufchain_size(&ctx->queued_data) == 0 &&
  316. ctx->outgoingeof == EOF_PENDING) {
  317. CloseHandle(ctx->h);
  318. ctx->h = INVALID_HANDLE_VALUE;
  319. ctx->outgoingeof = EOF_SENT;
  320. }
  321. }
  322. /* ----------------------------------------------------------------------
  323. * 'Foreign events'. These are handle structures which just contain a
  324. * single event object passed to us by another module such as
  325. * winnps.c, so that they can make use of our handle_get_events /
  326. * handle_got_event mechanism for communicating with application main
  327. * loops.
  328. */
  329. struct handle_foreign {
  330. /*
  331. * Copy of the handle_generic structure.
  332. */
  333. HANDLE h; /* the handle itself */
  334. HANDLE ev_to_main; /* event used to signal main thread */
  335. HANDLE ev_from_main; /* event used to signal back to us */
  336. bool moribund; /* are we going to kill this soon? */
  337. bool done; /* request subthread to terminate */
  338. bool defunct; /* has the subthread already gone? */
  339. bool busy; /* operation currently in progress? */
  340. void *privdata; /* for client to remember who they are */
  341. /*
  342. * Our own data, just consisting of knowledge of who to call back.
  343. */
  344. void (*callback)(void *);
  345. void *ctx;
  346. };
  347. /* ----------------------------------------------------------------------
  348. * Unified code handling both input and output threads.
  349. */
  350. struct handle {
  351. HandleType type;
  352. union {
  353. struct handle_generic g;
  354. struct handle_input i;
  355. struct handle_output o;
  356. struct handle_foreign f;
  357. } u;
  358. };
  359. static tree234 *handles_by_evtomain;
  360. static int handle_cmp_evtomain(void *av, void *bv)
  361. {
  362. struct handle *a = (struct handle *)av;
  363. struct handle *b = (struct handle *)bv;
  364. if ((uintptr_t)a->u.g.ev_to_main < (uintptr_t)b->u.g.ev_to_main)
  365. return -1;
  366. else if ((uintptr_t)a->u.g.ev_to_main > (uintptr_t)b->u.g.ev_to_main)
  367. return +1;
  368. else
  369. return 0;
  370. }
  371. static int handle_find_evtomain(void *av, void *bv)
  372. {
  373. HANDLE *a = (HANDLE *)av;
  374. struct handle *b = (struct handle *)bv;
  375. if ((uintptr_t)*a < (uintptr_t)b->u.g.ev_to_main)
  376. return -1;
  377. else if ((uintptr_t)*a > (uintptr_t)b->u.g.ev_to_main)
  378. return +1;
  379. else
  380. return 0;
  381. }
  382. struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata,
  383. void *privdata, int flags)
  384. {
  385. struct handle *h = snew(struct handle);
  386. DWORD in_threadid; /* required for Win9x */
  387. h->type = HT_INPUT;
  388. h->u.i.h = handle;
  389. h->u.i.ev_to_main = CreateEvent(NULL, false, false, NULL);
  390. h->u.i.ev_from_main = CreateEvent(NULL, false, false, NULL);
  391. h->u.i.gotdata = gotdata;
  392. h->u.i.defunct = false;
  393. h->u.i.moribund = false;
  394. h->u.i.done = false;
  395. h->u.i.privdata = privdata;
  396. h->u.i.flags = flags;
  397. if (!handles_by_evtomain)
  398. handles_by_evtomain = newtree234(handle_cmp_evtomain);
  399. add234(handles_by_evtomain, h);
  400. CreateThread(NULL, 0, handle_input_threadfunc,
  401. &h->u.i, 0, &in_threadid);
  402. h->u.i.busy = true;
  403. return h;
  404. }
  405. struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,
  406. void *privdata, int flags)
  407. {
  408. struct handle *h = snew(struct handle);
  409. DWORD out_threadid; /* required for Win9x */
  410. h->type = HT_OUTPUT;
  411. h->u.o.h = handle;
  412. h->u.o.ev_to_main = CreateEvent(NULL, false, false, NULL);
  413. h->u.o.ev_from_main = CreateEvent(NULL, false, false, NULL);
  414. h->u.o.busy = false;
  415. h->u.o.defunct = false;
  416. h->u.o.moribund = false;
  417. h->u.o.done = false;
  418. h->u.o.privdata = privdata;
  419. bufchain_init(&h->u.o.queued_data);
  420. h->u.o.outgoingeof = EOF_NO;
  421. h->u.o.sentdata = sentdata;
  422. h->u.o.flags = flags;
  423. if (!handles_by_evtomain)
  424. handles_by_evtomain = newtree234(handle_cmp_evtomain);
  425. add234(handles_by_evtomain, h);
  426. CreateThread(NULL, 0, handle_output_threadfunc,
  427. &h->u.o, 0, &out_threadid);
  428. return h;
  429. }
  430. struct handle *handle_add_foreign_event(HANDLE event,
  431. void (*callback)(void *), void *ctx)
  432. {
  433. struct handle *h = snew(struct handle);
  434. h->type = HT_FOREIGN;
  435. h->u.f.h = INVALID_HANDLE_VALUE;
  436. h->u.f.ev_to_main = event;
  437. h->u.f.ev_from_main = INVALID_HANDLE_VALUE;
  438. h->u.f.defunct = true; /* we have no thread in the first place */
  439. h->u.f.moribund = false;
  440. h->u.f.done = false;
  441. h->u.f.privdata = NULL;
  442. h->u.f.callback = callback;
  443. h->u.f.ctx = ctx;
  444. h->u.f.busy = true;
  445. if (!handles_by_evtomain)
  446. handles_by_evtomain = newtree234(handle_cmp_evtomain);
  447. add234(handles_by_evtomain, h);
  448. return h;
  449. }
  450. size_t handle_write(struct handle *h, const void *data, size_t len)
  451. {
  452. assert(h->type == HT_OUTPUT);
  453. assert(h->u.o.outgoingeof == EOF_NO);
  454. bufchain_add(&h->u.o.queued_data, data, len);
  455. handle_try_output(&h->u.o);
  456. return bufchain_size(&h->u.o.queued_data);
  457. }
  458. void handle_write_eof(struct handle *h)
  459. {
  460. /*
  461. * This function is called when we want to proactively send an
  462. * end-of-file notification on the handle. We can only do this by
  463. * actually closing the handle - so never call this on a
  464. * bidirectional handle if we're still interested in its incoming
  465. * direction!
  466. */
  467. assert(h->type == HT_OUTPUT);
  468. if (h->u.o.outgoingeof == EOF_NO) {
  469. h->u.o.outgoingeof = EOF_PENDING;
  470. handle_try_output(&h->u.o);
  471. }
  472. }
  473. HANDLE *handle_get_events(int *nevents)
  474. {
  475. HANDLE *ret;
  476. struct handle *h;
  477. int i;
  478. size_t 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. sgrowarray(ret, size, n);
  489. ret[n++] = h->u.g.ev_to_main;
  490. }
  491. }
  492. }
  493. *nevents = n;
  494. return ret;
  495. }
  496. static void handle_destroy(struct handle *h)
  497. {
  498. if (h->type == HT_OUTPUT)
  499. bufchain_clear(&h->u.o.queued_data);
  500. CloseHandle(h->u.g.ev_from_main);
  501. CloseHandle(h->u.g.ev_to_main);
  502. del234(handles_by_evtomain, h);
  503. sfree(h);
  504. }
  505. void handle_free(struct handle *h)
  506. {
  507. assert(h && !h->u.g.moribund);
  508. if (h->u.g.busy && h->type != HT_FOREIGN) {
  509. /*
  510. * If the handle is currently busy, we cannot immediately free
  511. * it, because its subthread is in the middle of something.
  512. * (Exception: foreign handles don't have a subthread.)
  513. *
  514. * Instead we must wait until it's finished its current
  515. * operation, because otherwise the subthread will write to
  516. * invalid memory after we free its context from under it. So
  517. * we set the moribund flag, which will be noticed next time
  518. * an operation completes.
  519. */
  520. h->u.g.moribund = true;
  521. } else if (h->u.g.defunct) {
  522. /*
  523. * There isn't even a subthread; we can go straight to
  524. * handle_destroy.
  525. */
  526. handle_destroy(h);
  527. } else {
  528. /*
  529. * The subthread is alive but not busy, so we now signal it
  530. * to die. Set the moribund flag to indicate that it will
  531. * want destroying after that.
  532. */
  533. h->u.g.moribund = true;
  534. h->u.g.done = true;
  535. h->u.g.busy = true;
  536. SetEvent(h->u.g.ev_from_main);
  537. }
  538. }
  539. int handle_got_event(HANDLE event) // WINSCP
  540. {
  541. struct handle *h;
  542. assert(handles_by_evtomain);
  543. h = find234(handles_by_evtomain, &event, handle_find_evtomain);
  544. if (!h) {
  545. /*
  546. * This isn't an error condition. If two or more event
  547. * objects were signalled during the same select operation,
  548. * and processing of the first caused the second handle to
  549. * be closed, then it will sometimes happen that we receive
  550. * an event notification here for a handle which is already
  551. * deceased. In that situation we simply do nothing.
  552. */
  553. return 0; // WINSCP
  554. }
  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);
  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 0; // WINSCP
  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. return 1; // WINSCP
  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);
  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);
  610. handle_try_output(&h->u.o);
  611. }
  612. return 0; // WINSCP
  613. case HT_FOREIGN:
  614. /* Just call the callback. */
  615. h->u.f.callback(h->u.f.ctx);
  616. return 0; // WINSCP
  617. }
  618. }
  619. void handle_unthrottle(struct handle *h, size_t backlog)
  620. {
  621. assert(h->type == HT_INPUT);
  622. handle_throttle(&h->u.i, backlog);
  623. }
  624. size_t handle_backlog(struct handle *h)
  625. {
  626. assert(h->type == HT_OUTPUT);
  627. return bufchain_size(&h->u.o.queued_data);
  628. }
  629. void *handle_get_privdata(struct handle *h)
  630. {
  631. return h->u.g.privdata;
  632. }
  633. static void handle_sink_write(BinarySink *bs, const void *data, size_t len)
  634. {
  635. handle_sink *sink = BinarySink_DOWNCAST(bs, handle_sink);
  636. handle_write(sink->h, data, len);
  637. }
  638. void handle_sink_init(handle_sink *sink, struct handle *h)
  639. {
  640. sink->h = h;
  641. BinarySink_INIT(sink, handle_sink_write);
  642. }