1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-session-async.c
*
* Copyright (C) 2000-2003, Ximian, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "soup-session-async.h"
#include "soup-session-private.h"
#include "soup-message-private.h"
#include "soup-misc.h"
/**
* SECTION:soup-session-async
* @short_description: Soup session for asynchronous (main-loop-based) I/O.
*
* #SoupSessionAsync is an implementation of #SoupSession that uses
* non-blocking I/O via the glib main loop. It is intended for use in
* single-threaded programs.
**/
static gboolean run_queue (SoupSessionAsync *sa, gboolean try_pruning);
static void queue_message (SoupSession *session, SoupMessage *req,
SoupSessionCallback callback, gpointer user_data);
static guint send_message (SoupSession *session, SoupMessage *req);
G_DEFINE_TYPE (SoupSessionAsync, soup_session_async, SOUP_TYPE_SESSION)
static void
soup_session_async_init (SoupSessionAsync *sa)
{
}
static void
soup_session_async_class_init (SoupSessionAsyncClass *soup_session_async_class)
{
SoupSessionClass *session_class = SOUP_SESSION_CLASS (soup_session_async_class);
/* virtual method override */
session_class->queue_message = queue_message;
session_class->send_message = send_message;
}
/**
* soup_session_async_new:
*
* Creates an asynchronous #SoupSession with the default options.
*
* Return value: the new session.
**/
SoupSession *
soup_session_async_new (void)
{
return g_object_new (SOUP_TYPE_SESSION_ASYNC, NULL);
}
/**
* soup_session_async_new_with_options:
* @optname1: name of first property to set
* @...: value of @optname1, followed by additional property/value pairs
*
* Creates an asynchronous #SoupSession with the specified options.
*
* Return value: the new session.
**/
SoupSession *
soup_session_async_new_with_options (const char *optname1, ...)
{
SoupSession *session;
va_list ap;
va_start (ap, optname1);
session = (SoupSession *)g_object_new_valist (SOUP_TYPE_SESSION_ASYNC,
optname1, ap);
va_end (ap);
return session;
}
static void
connection_closed (SoupConnection *conn, SoupSessionAsync *sa)
{
/* Run the queue in case anyone was waiting for a connection
* to be closed.
*/
run_queue (sa, FALSE);
}
static void
got_connection (SoupConnection *conn, guint status, gpointer user_data)
{
SoupSessionAsync *sa = user_data;
if (status != SOUP_STATUS_OK) {
/* The connection attempt failed, and thus @conn was
* closed and the open connection count for the
* session has been decremented. (If the failure was
* fatal, then SoupSession itself will have dealt
* with cancelling any pending messages for that
* host, so we don't need to worry about that here.)
* However, there may be other messages in the
* queue that were waiting for the connection count
* to go down, so run the queue now.
*/
run_queue (sa, FALSE);
return;
}
g_signal_connect (conn, "disconnected",
G_CALLBACK (connection_closed), sa);
/* @conn has been marked reserved by SoupSession, but we don't
* actually have any specific message in mind for it. (In
* particular, the message we were originally planning to
* queue on it may have already been queued on some other
* connection that became available while we were waiting for
* this one to connect.) So we release the connection into the
* idle pool and then just run the queue and see what happens.
*/
soup_connection_release (conn);
run_queue (sa, FALSE);
}
static gboolean
run_queue (SoupSessionAsync *sa, gboolean try_pruning)
{
SoupSession *session = SOUP_SESSION (sa);
SoupMessageQueue *queue = soup_session_get_queue (session);
SoupMessageQueueIter iter;
SoupMessage *msg;
SoupConnection *conn;
gboolean should_prune = FALSE, started_any = FALSE, is_new;
/* FIXME: prefer CONNECTING messages */
try_again:
for (msg = soup_message_queue_first (queue, &iter);
msg;
msg = soup_message_queue_next (queue, &iter)) {
if (!SOUP_MESSAGE_IS_STARTING (msg) ||
soup_message_io_in_progress (msg))
continue;
conn = soup_session_get_connection (session, msg,
&should_prune, &is_new);
if (!conn)
continue;
if (is_new) {
soup_connection_connect_async (conn, got_connection,
session);
} else
soup_connection_send_request (conn, msg);
started_any = TRUE;
}
if (try_pruning && should_prune && !started_any) {
/* We didn't manage to start any message, but there is
* at least one message in the queue that could be
* sent if we pruned an idle connection from some
* other server.
*/
if (soup_session_try_prune_connection (session)) {
try_pruning = FALSE;
goto try_again;
}
}
return started_any;
}
static void
request_restarted (SoupMessage *req, gpointer sa)
{
run_queue (sa, FALSE);
}
typedef struct {
SoupSessionAsync *sa;
SoupSessionCallback callback;
gpointer callback_data;
} SoupSessionAsyncQueueData;
static void
final_finished (SoupMessage *req, gpointer user_data)
{
SoupSessionAsyncQueueData *saqd = user_data;
SoupSessionAsync *sa = saqd->sa;
if (!SOUP_MESSAGE_IS_STARTING (req)) {
g_signal_handlers_disconnect_by_func (req, final_finished, saqd);
if (saqd->callback) {
saqd->callback ((SoupSession *)sa, req,
saqd->callback_data);
}
g_object_unref (req);
g_slice_free (SoupSessionAsyncQueueData, saqd);
}
run_queue (sa, FALSE);
}
static gboolean
idle_run_queue (gpointer user_data)
{
SoupSessionAsync *sa = user_data;
g_object_add_weak_pointer (G_OBJECT (sa), (gpointer)&sa);
g_object_unref (sa);
if (sa) {
g_object_remove_weak_pointer (G_OBJECT (sa), (gpointer)&sa);
run_queue (sa, TRUE);
}
return FALSE;
}
static void
queue_message (SoupSession *session, SoupMessage *req,
SoupSessionCallback callback, gpointer user_data)
{
SoupSessionAsync *sa = SOUP_SESSION_ASYNC (session);
SoupSessionAsyncQueueData *saqd;
g_signal_connect (req, "restarted",
G_CALLBACK (request_restarted), sa);
saqd = g_slice_new (SoupSessionAsyncQueueData);
saqd->sa = sa;
saqd->callback = callback;
saqd->callback_data = user_data;
g_signal_connect_after (req, "finished",
G_CALLBACK (final_finished), saqd);
SOUP_SESSION_CLASS (soup_session_async_parent_class)->queue_message (session, req, callback, user_data);
g_object_ref (sa);
soup_add_idle (soup_session_get_async_context (session),
idle_run_queue, sa);
}
static guint
send_message (SoupSession *session, SoupMessage *req)
{
GMainContext *async_context =
soup_session_get_async_context (session);
/* Balance out the unref that final_finished will do */
g_object_ref (req);
queue_message (session, req, NULL, NULL);
while (soup_message_get_io_status (req) != SOUP_MESSAGE_IO_STATUS_FINISHED &&
!SOUP_STATUS_IS_TRANSPORT_ERROR (req->status_code))
g_main_context_iteration (async_context, TRUE);
return req->status_code;
}
|