mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-06-30 19:12:48 -05:00
Allow creating FdSocket/HandleSocket before the fds/handles.
Previously, a setup function returning one of these socket types (such as platform_new_connection) had to do all its setup synchronously, because if it was going to call make_fd_socket or make_handle_socket, it had to have the actual fds or HANDLEs ready-made. If some kind of asynchronous operation were needed before those fds become available, there would be no way the function could achieve it, except by becoming a whole extra permanent Socket wrapper layer. Now there is, because you can make an FdSocket when you don't yet have the fds, or a HandleSocket without the HANDLEs. Instead, you provide an instance of the new trait 'DeferredSocketOpener', which is responsible for setting in motion whatever asynchronous setup procedure it needs, and when that finishes, calling back to setup_fd_socket / setup_handle_socket to provide the missing pieces. In the meantime, the FdSocket or HandleSocket will sit there inertly, buffering any data the client might eagerly hand it via sk_write(), and waiting for its setup to finish. When it does finish, buffered data will be released. In FdSocket, this is easy enough, because we were doing our own buffering anyway - we called the uxsel system to find out when the fds were readable/writable, and then wrote to them from our own bufchain. So more or less all I had to do was make the try_send function do nothing if the setup phase wasn't finished yet. In HandleSocket, on the other hand, we're passing all our data to the underlying handle-io.c system, and making _that_ deferrable in the same way would be much more painful, because that's the place where the scary threads live. So instead I've arranged it by replacing the whole vtable, so that a deferred HandleSocket and a normal HandleSocket are effectively separate trait implementations that can share their state structure. And in fact that state struct itself now contains a big anonymous union, containing one branch to go with each vtable. Nothing yet uses this system, but the next commit will do so.
This commit is contained in:
@ -14,7 +14,8 @@
|
||||
#include "network.h"
|
||||
|
||||
typedef struct FdSocket {
|
||||
int outfd, infd, inerrfd;
|
||||
int outfd, infd, inerrfd; /* >= 0 if socket is open */
|
||||
DeferredSocketOpener *opener; /* non-NULL if not opened yet */
|
||||
|
||||
bufchain pending_output_data;
|
||||
bufchain pending_input_data;
|
||||
@ -115,6 +116,9 @@ static void fdsocket_close(Socket *s)
|
||||
{
|
||||
FdSocket *fds = container_of(s, FdSocket, sock);
|
||||
|
||||
if (fds->opener)
|
||||
deferred_socket_opener_free(fds->opener);
|
||||
|
||||
if (fds->outfd >= 0) {
|
||||
del234(fdsocket_by_outfd, fds);
|
||||
uxsel_del(fds->outfd);
|
||||
@ -165,6 +169,9 @@ static int fdsocket_try_send(FdSocket *fds)
|
||||
{
|
||||
int sent = 0;
|
||||
|
||||
if (fds->opener)
|
||||
return sent;
|
||||
|
||||
while (bufchain_size(&fds->pending_output_data) > 0) {
|
||||
ssize_t ret;
|
||||
|
||||
@ -326,27 +333,20 @@ static void fdsocket_connect_success_callback(void *ctx)
|
||||
NULL, 0);
|
||||
}
|
||||
|
||||
Socket *make_fd_socket(int infd, int outfd, int inerrfd,
|
||||
SockAddr *addr, int port, Plug *plug)
|
||||
void setup_fd_socket(Socket *s, int infd, int outfd, int inerrfd)
|
||||
{
|
||||
FdSocket *fds;
|
||||
FdSocket *fds = container_of(s, FdSocket, sock);
|
||||
assert(fds->sock.vt == &FdSocket_sockvt);
|
||||
|
||||
fds = snew(FdSocket);
|
||||
fds->sock.vt = &FdSocket_sockvt;
|
||||
fds->addr = addr;
|
||||
fds->port = port;
|
||||
fds->plug = plug;
|
||||
fds->outgoingeof = EOF_NO;
|
||||
fds->pending_error = 0;
|
||||
if (fds->opener) {
|
||||
deferred_socket_opener_free(fds->opener);
|
||||
fds->opener = NULL;
|
||||
}
|
||||
|
||||
fds->infd = infd;
|
||||
fds->outfd = outfd;
|
||||
fds->inerrfd = inerrfd;
|
||||
|
||||
bufchain_init(&fds->pending_input_data);
|
||||
bufchain_init(&fds->pending_output_data);
|
||||
psb_init(&fds->psb);
|
||||
|
||||
if (fds->outfd >= 0) {
|
||||
if (!fdsocket_by_outfd)
|
||||
fdsocket_by_outfd = newtree234(fdsocket_outfd_cmp);
|
||||
@ -369,6 +369,42 @@ Socket *make_fd_socket(int infd, int outfd, int inerrfd,
|
||||
}
|
||||
|
||||
queue_toplevel_callback(fdsocket_connect_success_callback, fds);
|
||||
}
|
||||
|
||||
static FdSocket *make_fd_socket_internal(SockAddr *addr, int port, Plug *plug)
|
||||
{
|
||||
FdSocket *fds;
|
||||
|
||||
fds = snew(FdSocket);
|
||||
fds->sock.vt = &FdSocket_sockvt;
|
||||
fds->addr = addr;
|
||||
fds->port = port;
|
||||
fds->plug = plug;
|
||||
fds->outgoingeof = EOF_NO;
|
||||
fds->pending_error = 0;
|
||||
|
||||
fds->opener = NULL;
|
||||
fds->infd = fds->outfd = fds->inerrfd = -1;
|
||||
|
||||
bufchain_init(&fds->pending_input_data);
|
||||
bufchain_init(&fds->pending_output_data);
|
||||
psb_init(&fds->psb);
|
||||
|
||||
return fds;
|
||||
}
|
||||
|
||||
Socket *make_fd_socket(int infd, int outfd, int inerrfd,
|
||||
SockAddr *addr, int port, Plug *plug)
|
||||
{
|
||||
FdSocket *fds = make_fd_socket_internal(addr, port, plug);
|
||||
setup_fd_socket(&fds->sock, infd, outfd, inerrfd);
|
||||
return &fds->sock;
|
||||
}
|
||||
|
||||
Socket *make_deferred_fd_socket(DeferredSocketOpener *opener,
|
||||
SockAddr *addr, int port, Plug *plug)
|
||||
{
|
||||
FdSocket *fds = make_fd_socket_internal(addr, port, plug);
|
||||
fds->opener = opener;
|
||||
return &fds->sock;
|
||||
}
|
||||
|
@ -382,6 +382,9 @@ bool so_peercred(int fd, int *pid, int *uid, int *gid);
|
||||
*/
|
||||
Socket *make_fd_socket(int infd, int outfd, int inerrfd,
|
||||
SockAddr *addr, int port, Plug *plug);
|
||||
Socket *make_deferred_fd_socket(DeferredSocketOpener *opener,
|
||||
SockAddr *addr, int port, Plug *plug);
|
||||
void setup_fd_socket(Socket *s, int infd, int outfd, int inerrfd);
|
||||
|
||||
/*
|
||||
* Default font setting, which can vary depending on NOT_X_WINDOWS.
|
||||
|
Reference in New Issue
Block a user