1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * soup-session-async.c
4 *
5 * Copyright (C) 2000-2003, Ximian, Inc.
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include "soup-session-async.h"
13 #include "soup.h"
14 #include "soup-session-private.h"
15 #include "soup-message-private.h"
16 #include "soup-message-queue.h"
17 #include "soup-misc-private.h"
18
19 /**
20 * SECTION:soup-session-async
21 * @short_description: SoupSession for asynchronous (main-loop-based) I/O
22 * (deprecated).
23 *
24 * #SoupSessionAsync is an implementation of #SoupSession that uses
25 * non-blocking I/O via the glib main loop for all I/O.
26 *
27 * Deprecated: 2.42: Use the #SoupSession class (which uses both asynchronous
28 * and synchronous I/O, depending on the API used). See the
29 * <link linkend="libsoup-session-porting">porting guide</link>.
30 **/
31
32 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
33
G_DEFINE_TYPE(SoupSessionAsync,soup_session_async,SOUP_TYPE_SESSION)34 G_DEFINE_TYPE (SoupSessionAsync, soup_session_async, SOUP_TYPE_SESSION)
35
36 static void
37 soup_session_async_init (SoupSessionAsync *sa)
38 {
39 }
40
41 /**
42 * soup_session_async_new:
43 *
44 * Creates an asynchronous #SoupSession with the default options.
45 *
46 * Return value: the new session.
47 *
48 * Deprecated: #SoupSessionAsync is deprecated; use a plain
49 * #SoupSession, created with soup_session_new(). See the <link
50 * linkend="libsoup-session-porting">porting guide</link>.
51 **/
52 SoupSession *
soup_session_async_new(void)53 soup_session_async_new (void)
54 {
55 return g_object_new (SOUP_TYPE_SESSION_ASYNC, NULL);
56 }
57
58 /**
59 * soup_session_async_new_with_options:
60 * @optname1: name of first property to set
61 * @...: value of @optname1, followed by additional property/value pairs
62 *
63 * Creates an asynchronous #SoupSession with the specified options.
64 *
65 * Return value: the new session.
66 *
67 * Deprecated: #SoupSessionAsync is deprecated; use a plain
68 * #SoupSession, created with soup_session_new_with_options(). See the
69 * <link linkend="libsoup-session-porting">porting guide</link>.
70 **/
71 SoupSession *
soup_session_async_new_with_options(const char * optname1,...)72 soup_session_async_new_with_options (const char *optname1, ...)
73 {
74 SoupSession *session;
75 va_list ap;
76
77 va_start (ap, optname1);
78 session = (SoupSession *)g_object_new_valist (SOUP_TYPE_SESSION_ASYNC,
79 optname1, ap);
80 va_end (ap);
81
82 return session;
83 }
84
85 static guint
soup_session_async_send_message(SoupSession * session,SoupMessage * msg)86 soup_session_async_send_message (SoupSession *session, SoupMessage *msg)
87 {
88 SoupMessageQueueItem *item;
89 GMainContext *async_context =
90 soup_session_get_async_context (session);
91
92 item = soup_session_append_queue_item (session, msg, TRUE, FALSE,
93 NULL, NULL);
94 soup_session_kick_queue (session);
95
96 while (item->state != SOUP_MESSAGE_FINISHED)
97 g_main_context_iteration (async_context, TRUE);
98
99 soup_message_queue_item_unref (item);
100
101 return msg->status_code;
102 }
103
104 static void
soup_session_async_cancel_message(SoupSession * session,SoupMessage * msg,guint status_code)105 soup_session_async_cancel_message (SoupSession *session, SoupMessage *msg,
106 guint status_code)
107 {
108 SoupMessageQueue *queue;
109 SoupMessageQueueItem *item;
110
111 SOUP_SESSION_CLASS (soup_session_async_parent_class)->
112 cancel_message (session, msg, status_code);
113
114 queue = soup_session_get_queue (session);
115 item = soup_message_queue_lookup (queue, msg);
116 if (!item)
117 return;
118
119 /* Force it to finish immediately, so that
120 * soup_session_abort (session); g_object_unref (session);
121 * will work. (The soup_session_cancel_message() docs
122 * point out that the callback will be invoked from
123 * within the cancel call.)
124 */
125 if (soup_message_io_in_progress (msg))
126 soup_message_io_finished (msg);
127 else if (item->state != SOUP_MESSAGE_FINISHED)
128 item->state = SOUP_MESSAGE_FINISHING;
129
130 if (item->state != SOUP_MESSAGE_FINISHED)
131 soup_session_process_queue_item (session, item, NULL, FALSE);
132
133 soup_message_queue_item_unref (item);
134 }
135
136 static void
soup_session_async_class_init(SoupSessionAsyncClass * soup_session_async_class)137 soup_session_async_class_init (SoupSessionAsyncClass *soup_session_async_class)
138 {
139 SoupSessionClass *session_class = SOUP_SESSION_CLASS (soup_session_async_class);
140
141 /* virtual method override */
142 session_class->send_message = soup_session_async_send_message;
143 session_class->cancel_message = soup_session_async_cancel_message;
144 }
145
146 G_GNUC_END_IGNORE_DEPRECATIONS;
147