winhandl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 successful 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. #ifndef WINSCP
  360. static tree234 *handles_by_evtomain;
  361. #endif
  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(tree234 * handles_by_evtomain, 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. #ifndef WINSCP
  400. if (!handles_by_evtomain)
  401. handles_by_evtomain = newtree234(handle_cmp_evtomain);
  402. #endif
  403. add234(handles_by_evtomain, h);
  404. CreateThread(NULL, 0, handle_input_threadfunc,
  405. &h->u.i, 0, &in_threadid);
  406. h->u.i.busy = true;
  407. return h;
  408. }
  409. struct handle *handle_output_new(tree234 * handles_by_evtomain, HANDLE handle, handle_outputfn_t sentdata, // WINSCP
  410. void *privdata, int flags)
  411. {
  412. struct handle *h = snew(struct handle);
  413. DWORD out_threadid; /* required for Win9x */
  414. h->type = HT_OUTPUT;
  415. h->u.o.h = handle;
  416. h->u.o.ev_to_main = CreateEvent(NULL, false, false, NULL);
  417. h->u.o.ev_from_main = CreateEvent(NULL, false, false, NULL);
  418. h->u.o.busy = false;
  419. h->u.o.defunct = false;
  420. h->u.o.moribund = false;
  421. h->u.o.done = false;
  422. h->u.o.privdata = privdata;
  423. bufchain_init(&h->u.o.queued_data);
  424. h->u.o.outgoingeof = EOF_NO;
  425. h->u.o.sentdata = sentdata;
  426. h->u.o.flags = flags;
  427. #ifndef WINSCP
  428. if (!handles_by_evtomain)
  429. handles_by_evtomain = newtree234(handle_cmp_evtomain);
  430. #endif
  431. add234(handles_by_evtomain, h);
  432. CreateThread(NULL, 0, handle_output_threadfunc,
  433. &h->u.o, 0, &out_threadid);
  434. return h;
  435. }
  436. #ifndef WINSCP
  437. struct handle *handle_add_foreign_event(HANDLE event,
  438. void (*callback)(void *), void *ctx)
  439. {
  440. struct handle *h = snew(struct handle);
  441. h->type = HT_FOREIGN;
  442. h->u.f.h = INVALID_HANDLE_VALUE;
  443. h->u.f.ev_to_main = event;
  444. h->u.f.ev_from_main = INVALID_HANDLE_VALUE;
  445. h->u.f.defunct = true; /* we have no thread in the first place */
  446. h->u.f.moribund = false;
  447. h->u.f.done = false;
  448. h->u.f.privdata = NULL;
  449. h->u.f.callback = callback;
  450. h->u.f.ctx = ctx;
  451. h->u.f.busy = true;
  452. if (!handles_by_evtomain)
  453. handles_by_evtomain = newtree234(handle_cmp_evtomain);
  454. add234(handles_by_evtomain, h);
  455. return h;
  456. }
  457. #endif
  458. size_t handle_write(struct handle *h, const void *data, size_t len)
  459. {
  460. assert(h->type == HT_OUTPUT);
  461. assert(h->u.o.outgoingeof == EOF_NO);
  462. bufchain_add(&h->u.o.queued_data, data, len);
  463. handle_try_output(&h->u.o);
  464. return bufchain_size(&h->u.o.queued_data);
  465. }
  466. void handle_write_eof(struct handle *h)
  467. {
  468. /*
  469. * This function is called when we want to proactively send an
  470. * end-of-file notification on the handle. We can only do this by
  471. * actually closing the handle - so never call this on a
  472. * bidirectional handle if we're still interested in its incoming
  473. * direction!
  474. */
  475. assert(h->type == HT_OUTPUT);
  476. if (h->u.o.outgoingeof == EOF_NO) {
  477. h->u.o.outgoingeof = EOF_PENDING;
  478. handle_try_output(&h->u.o);
  479. }
  480. }
  481. HANDLE *handle_get_events(tree234 * handles_by_evtomain, int *nevents) // WINSCP
  482. {
  483. HANDLE *ret;
  484. struct handle *h;
  485. int i;
  486. size_t n, size;
  487. /*
  488. * Go through our tree counting the handle objects currently
  489. * engaged in useful activity.
  490. */
  491. ret = NULL;
  492. n = size = 0;
  493. if (handles_by_evtomain) {
  494. for (i = 0; (h = index234(handles_by_evtomain, i)) != NULL; i++) {
  495. if (h->u.g.busy) {
  496. sgrowarray(ret, size, n);
  497. ret[n++] = h->u.g.ev_to_main;
  498. }
  499. }
  500. }
  501. *nevents = n;
  502. return ret;
  503. }
  504. static void handle_destroy(tree234 * handles_by_evtomain, struct handle *h) // WINSCP
  505. {
  506. if (h->type == HT_OUTPUT)
  507. bufchain_clear(&h->u.o.queued_data);
  508. CloseHandle(h->u.g.ev_from_main);
  509. CloseHandle(h->u.g.ev_to_main);
  510. del234(handles_by_evtomain, h);
  511. sfree(h);
  512. }
  513. void handle_free(tree234 * handles_by_evtomain, struct handle *h) // WINSCP
  514. {
  515. assert(h && !h->u.g.moribund);
  516. if (h->u.g.busy && h->type != HT_FOREIGN) {
  517. /*
  518. * If the handle is currently busy, we cannot immediately free
  519. * it, because its subthread is in the middle of something.
  520. * (Exception: foreign handles don't have a subthread.)
  521. *
  522. * Instead we must wait until it's finished its current
  523. * operation, because otherwise the subthread will write to
  524. * invalid memory after we free its context from under it. So
  525. * we set the moribund flag, which will be noticed next time
  526. * an operation completes.
  527. */
  528. h->u.g.moribund = true;
  529. } else if (h->u.g.defunct) {
  530. /*
  531. * There isn't even a subthread; we can go straight to
  532. * handle_destroy.
  533. */
  534. handle_destroy(handles_by_evtomain, h); // WINSCP
  535. } else {
  536. /*
  537. * The subthread is alive but not busy, so we now signal it
  538. * to die. Set the moribund flag to indicate that it will
  539. * want destroying after that.
  540. */
  541. h->u.g.moribund = true;
  542. h->u.g.done = true;
  543. h->u.g.busy = true;
  544. SetEvent(h->u.g.ev_from_main);
  545. }
  546. }
  547. int handle_got_event(tree234 * handles_by_evtomain, HANDLE event) // WINSCP
  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. return 0; // WINSCP
  562. }
  563. if (h->u.g.moribund) {
  564. /*
  565. * A moribund handle is one which we have either already
  566. * signalled to die, or are waiting until its current I/O op
  567. * completes to do so. Either way, it's treated as already
  568. * dead from the external user's point of view, so we ignore
  569. * the actual I/O result. We just signal the thread to die if
  570. * we haven't yet done so, or destroy the handle if not.
  571. */
  572. if (h->u.g.done) {
  573. handle_destroy(handles_by_evtomain, h); // WINSCP
  574. } else {
  575. h->u.g.done = true;
  576. h->u.g.busy = true;
  577. SetEvent(h->u.g.ev_from_main);
  578. }
  579. return 0; // WINSCP
  580. }
  581. switch (h->type) {
  582. int backlog;
  583. case HT_INPUT:
  584. h->u.i.busy = false;
  585. /*
  586. * A signal on an input handle means data has arrived.
  587. */
  588. if (h->u.i.len == 0) {
  589. /*
  590. * EOF, or (nearly equivalently) read error.
  591. */
  592. h->u.i.defunct = true;
  593. h->u.i.gotdata(h, NULL, 0, h->u.i.readerr);
  594. } else {
  595. backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len, 0);
  596. handle_throttle(&h->u.i, backlog);
  597. }
  598. return 1; // WINSCP
  599. case HT_OUTPUT:
  600. h->u.o.busy = false;
  601. /*
  602. * A signal on an output handle means we have completed a
  603. * write. Call the callback to indicate that the output
  604. * buffer size has decreased, or to indicate an error.
  605. */
  606. if (h->u.o.writeerr) {
  607. /*
  608. * Write error. Send a negative value to the callback,
  609. * and mark the thread as defunct (because the output
  610. * thread is terminating by now).
  611. */
  612. h->u.o.defunct = true;
  613. h->u.o.sentdata(h, 0, h->u.o.writeerr);
  614. } else {
  615. bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten);
  616. noise_ultralight(NOISE_SOURCE_IOLEN, h->u.o.lenwritten);
  617. h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data), 0);
  618. handle_try_output(&h->u.o);
  619. }
  620. return 0; // WINSCP
  621. case HT_FOREIGN:
  622. /* Just call the callback. */
  623. h->u.f.callback(h->u.f.ctx);
  624. return 0; // WINSCP
  625. }
  626. }
  627. void handle_unthrottle(struct handle *h, size_t backlog)
  628. {
  629. assert(h->type == HT_INPUT);
  630. handle_throttle(&h->u.i, backlog);
  631. }
  632. size_t handle_backlog(struct handle *h)
  633. {
  634. assert(h->type == HT_OUTPUT);
  635. return bufchain_size(&h->u.o.queued_data);
  636. }
  637. void *handle_get_privdata(struct handle *h)
  638. {
  639. return h->u.g.privdata;
  640. }
  641. static void handle_sink_write(BinarySink *bs, const void *data, size_t len)
  642. {
  643. handle_sink *sink = BinarySink_DOWNCAST(bs, handle_sink);
  644. handle_write(sink->h, data, len);
  645. }
  646. void handle_sink_init(handle_sink *sink, struct handle *h)
  647. {
  648. sink->h = h;
  649. BinarySink_INIT(sink, handle_sink_write);
  650. }
  651. // WINSCP
  652. tree234 *new_handles_by_evtomain()
  653. {
  654. return newtree234(handle_cmp_evtomain);
  655. }