mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-04-13 09:08:06 -05:00
Implement freezing on Windows handle sockets.
That's been a FIXME in the code for ages, because it's difficult to get winhandl.c to stop an already-started read from a handle (since the read is a blocking system call running in a separate thread). But I now realise it isn't absolutely necessary to do so - you can just buffer one lot of data from winhandl and _then_ tell it to stop. [originally from svn r10067]
This commit is contained in:
parent
98a6a3553c
commit
9093071faa
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#define DEFINE_PLUG_METHOD_MACROS
|
#define DEFINE_PLUG_METHOD_MACROS
|
||||||
#include "tree234.h"
|
#include "tree234.h"
|
||||||
@ -20,6 +21,24 @@ struct Socket_handle_tag {
|
|||||||
HANDLE send_H, recv_H;
|
HANDLE send_H, recv_H;
|
||||||
struct handle *send_h, *recv_h;
|
struct handle *send_h, *recv_h;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Freezing one of these sockets is a slightly fiddly business,
|
||||||
|
* because the reads from the handle are happening in a separate
|
||||||
|
* thread as blocking system calls and so once one is in progress
|
||||||
|
* it can't sensibly be interrupted. Hence, after the user tries
|
||||||
|
* to freeze one of these sockets, it's unavoidable that we may
|
||||||
|
* receive one more load of data before we manage to get
|
||||||
|
* winhandl.c to stop reading.
|
||||||
|
*/
|
||||||
|
enum {
|
||||||
|
UNFROZEN, /* reading as normal */
|
||||||
|
FREEZING, /* have been set to frozen but winhandl is still reading */
|
||||||
|
FROZEN, /* really frozen - winhandl has been throttled */
|
||||||
|
THAWING /* we're gradually releasing our remaining data */
|
||||||
|
} frozen;
|
||||||
|
/* We buffer data here if we receive it from winhandl while frozen. */
|
||||||
|
bufchain inputdata;
|
||||||
|
|
||||||
char *error;
|
char *error;
|
||||||
|
|
||||||
Plug plug;
|
Plug plug;
|
||||||
@ -37,7 +56,24 @@ static int handle_gotdata(struct handle *h, void *data, int len)
|
|||||||
} else if (len == 0) {
|
} else if (len == 0) {
|
||||||
return plug_closing(ps->plug, NULL, 0, 0);
|
return plug_closing(ps->plug, NULL, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
return plug_receive(ps->plug, 0, data, len);
|
assert(ps->frozen != FREEZING && ps->frozen != THAWING);
|
||||||
|
if (ps->frozen == FREEZING) {
|
||||||
|
/*
|
||||||
|
* If we've received data while this socket is supposed to
|
||||||
|
* be frozen (because the read winhandl.c started before
|
||||||
|
* sk_set_frozen was called has now returned) then buffer
|
||||||
|
* the data for when we unfreeze.
|
||||||
|
*/
|
||||||
|
bufchain_add(&ps->inputdata, data, len);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* And return a very large backlog, to prevent further
|
||||||
|
* data arriving from winhandl until we unfreeze.
|
||||||
|
*/
|
||||||
|
return INT_MAX;
|
||||||
|
} else {
|
||||||
|
return plug_receive(ps->plug, 0, data, len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +102,7 @@ static void sk_handle_close(Socket s)
|
|||||||
CloseHandle(ps->send_H);
|
CloseHandle(ps->send_H);
|
||||||
if (ps->recv_H != ps->send_H)
|
if (ps->recv_H != ps->send_H)
|
||||||
CloseHandle(ps->recv_H);
|
CloseHandle(ps->recv_H);
|
||||||
|
bufchain_clear(&ps->inputdata);
|
||||||
|
|
||||||
sfree(ps);
|
sfree(ps);
|
||||||
}
|
}
|
||||||
@ -111,13 +148,98 @@ static void *sk_handle_get_private_ptr(Socket s)
|
|||||||
return ps->privptr;
|
return ps->privptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void handle_socket_unfreeze(void *psv)
|
||||||
|
{
|
||||||
|
Handle_Socket ps = (Handle_Socket) psv;
|
||||||
|
void *data;
|
||||||
|
int len, new_backlog;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If we've been put into a state other than THAWING since the
|
||||||
|
* last callback, then we're done.
|
||||||
|
*/
|
||||||
|
if (ps->frozen != THAWING)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get some of the data we've buffered.
|
||||||
|
*/
|
||||||
|
bufchain_prefix(&ps->inputdata, &data, &len);
|
||||||
|
assert(len > 0);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Hand it off to the plug.
|
||||||
|
*/
|
||||||
|
new_backlog = plug_receive(ps->plug, 0, data, len);
|
||||||
|
|
||||||
|
if (bufchain_size(&ps->inputdata) > 0) {
|
||||||
|
/*
|
||||||
|
* If there's still data in our buffer, stay in THAWING state,
|
||||||
|
* and reschedule ourself.
|
||||||
|
*/
|
||||||
|
queue_toplevel_callback(handle_socket_unfreeze, ps);
|
||||||
|
} else {
|
||||||
|
/*
|
||||||
|
* Otherwise, we've successfully thawed!
|
||||||
|
*/
|
||||||
|
ps->frozen = UNFROZEN;
|
||||||
|
handle_unthrottle(ps->recv_h, new_backlog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void sk_handle_set_frozen(Socket s, int is_frozen)
|
static void sk_handle_set_frozen(Socket s, int is_frozen)
|
||||||
{
|
{
|
||||||
Handle_Socket ps = (Handle_Socket) s;
|
Handle_Socket ps = (Handle_Socket) s;
|
||||||
|
|
||||||
/*
|
if (is_frozen) {
|
||||||
* FIXME
|
switch (ps->frozen) {
|
||||||
*/
|
case FREEZING:
|
||||||
|
case FROZEN:
|
||||||
|
return; /* nothing to do */
|
||||||
|
|
||||||
|
case THAWING:
|
||||||
|
/*
|
||||||
|
* We were in the middle of emptying our bufchain, and got
|
||||||
|
* frozen again. In that case, winhandl.c is already
|
||||||
|
* throttled, so just return to FROZEN state. The toplevel
|
||||||
|
* callback will notice and disable itself.
|
||||||
|
*/
|
||||||
|
ps->frozen = FROZEN;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case UNFROZEN:
|
||||||
|
/*
|
||||||
|
* The normal case. Go to FREEZING, and expect one more
|
||||||
|
* load of data from winhandl if we're unlucky.
|
||||||
|
*/
|
||||||
|
ps->frozen = FREEZING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (ps->frozen) {
|
||||||
|
case UNFROZEN:
|
||||||
|
case THAWING:
|
||||||
|
return; /* nothing to do */
|
||||||
|
|
||||||
|
case FREEZING:
|
||||||
|
/*
|
||||||
|
* If winhandl didn't send us any data throughout the time
|
||||||
|
* we were frozen, then we'll still be in this state and
|
||||||
|
* can just unfreeze in the trivial way.
|
||||||
|
*/
|
||||||
|
assert(bufchain_size(&ps->inputdata) == 0);
|
||||||
|
ps->frozen = UNFROZEN;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FROZEN:
|
||||||
|
/*
|
||||||
|
* If we have buffered data, go to THAWING and start
|
||||||
|
* releasing it in top-level callbacks.
|
||||||
|
*/
|
||||||
|
ps->frozen = THAWING;
|
||||||
|
queue_toplevel_callback(handle_socket_unfreeze, ps);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *sk_handle_socket_error(Socket s)
|
static const char *sk_handle_socket_error(Socket s)
|
||||||
@ -149,6 +271,8 @@ Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
|
|||||||
ret->fn = &socket_fn_table;
|
ret->fn = &socket_fn_table;
|
||||||
ret->plug = plug;
|
ret->plug = plug;
|
||||||
ret->error = NULL;
|
ret->error = NULL;
|
||||||
|
ret->frozen = UNFROZEN;
|
||||||
|
bufchain_init(&ret->inputdata);
|
||||||
|
|
||||||
ret->recv_H = recv_H;
|
ret->recv_H = recv_H;
|
||||||
ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
|
ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user