| |
1 /** |
| |
2 * @file sendqueue.c |
| |
3 * |
| |
4 * gaim |
| |
5 * |
| |
6 * Gaim is the legal property of its developers, whose names are too numerous |
| |
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
| |
8 * source distribution. |
| |
9 * |
| |
10 * This program is free software; you can redistribute it and/or modify |
| |
11 * it under the terms of the GNU General Public License as published by |
| |
12 * the Free Software Foundation; either version 2 of the License, or |
| |
13 * (at your option) any later version. |
| |
14 * |
| |
15 * This program is distributed in the hope that it will be useful, |
| |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
18 * GNU General Public License for more details. |
| |
19 * |
| |
20 * You should have received a copy of the GNU General Public License |
| |
21 * along with this program; if not, write to the Free Software |
| |
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| |
23 */ |
| |
24 |
| |
25 #include "connection.h" |
| |
26 #include "debug.h" |
| |
27 #include "internal.h" |
| |
28 #include "notify.h" |
| |
29 #include "prefs.h" |
| |
30 #include "request.h" |
| |
31 |
| |
32 #include "header_info.h" |
| |
33 #include "qq_proxy.h" |
| |
34 #include "sendqueue.h" |
| |
35 |
| |
36 #define QQ_RESEND_MAX 8 /* max resend per packet */ |
| |
37 |
| |
38 typedef struct _gc_and_packet gc_and_packet; |
| |
39 |
| |
40 struct _gc_and_packet { |
| |
41 GaimConnection *gc; |
| |
42 qq_sendpacket *packet; |
| |
43 }; |
| |
44 |
| |
45 /* Remove a packet with send_seq from sendqueue */ |
| |
46 void qq_sendqueue_remove(qq_data *qd, guint16 send_seq) |
| |
47 { |
| |
48 GList *list; |
| |
49 qq_sendpacket *p; |
| |
50 |
| |
51 list = qd->sendqueue; |
| |
52 while (list != NULL) { |
| |
53 p = (qq_sendpacket *) (list->data); |
| |
54 if (p->send_seq == send_seq) { |
| |
55 qd->sendqueue = g_list_remove(qd->sendqueue, p); |
| |
56 g_free(p->buf); |
| |
57 g_free(p); |
| |
58 break; |
| |
59 } |
| |
60 list = list->next; |
| |
61 } |
| |
62 } |
| |
63 |
| |
64 /* clean up sendqueue and free all contents */ |
| |
65 void qq_sendqueue_free(qq_data *qd) |
| |
66 { |
| |
67 qq_sendpacket *p; |
| |
68 gint i; |
| |
69 |
| |
70 i = 0; |
| |
71 while (qd->sendqueue != NULL) { |
| |
72 p = (qq_sendpacket *) (qd->sendqueue->data); |
| |
73 qd->sendqueue = g_list_remove(qd->sendqueue, p); |
| |
74 g_free(p->buf); |
| |
75 g_free(p); |
| |
76 i++; |
| |
77 } |
| |
78 gaim_debug(GAIM_DEBUG_INFO, "QQ", "%d packets in sendqueue are freed!\n", i); |
| |
79 } |
| |
80 |
| |
81 /* FIXME We shouldn't be dropping packets, but for now we have to because |
| |
82 * somewhere we're generating invalid packets that the server won't ack. |
| |
83 * Given enough time, a buildup of those packets would crash the client. */ |
| |
84 gboolean qq_sendqueue_timeout_callback(gpointer data) |
| |
85 { |
| |
86 GaimConnection *gc; |
| |
87 qq_data *qd; |
| |
88 GList *list; |
| |
89 qq_sendpacket *p; |
| |
90 time_t now; |
| |
91 gint wait_time; |
| |
92 |
| |
93 gc = (GaimConnection *) data; |
| |
94 qd = (qq_data *) gc->proto_data; |
| |
95 now = time(NULL); |
| |
96 list = qd->sendqueue; |
| |
97 |
| |
98 /* empty queue, return TRUE so that timeout continues functioning */ |
| |
99 if (qd->sendqueue == NULL) |
| |
100 return TRUE; |
| |
101 |
| |
102 while (list != NULL) { /* remove all packet whose resend_times == -1 */ |
| |
103 p = (qq_sendpacket *) list->data; |
| |
104 if (p->resend_times == -1) { /* to remove */ |
| |
105 qd->sendqueue = g_list_remove(qd->sendqueue, p); |
| |
106 g_free(p->buf); |
| |
107 g_free(p); |
| |
108 list = qd->sendqueue; |
| |
109 } else { |
| |
110 list = list->next; |
| |
111 } |
| |
112 } |
| |
113 |
| |
114 list = qd->sendqueue; |
| |
115 while (list != NULL) { |
| |
116 p = (qq_sendpacket *) list->data; |
| |
117 if (p->resend_times == QQ_RESEND_MAX) { /* reach max */ |
| |
118 switch (p->cmd) { |
| |
119 case QQ_CMD_KEEP_ALIVE: |
| |
120 if (qd->logged_in) { |
| |
121 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Connection lost!\n"); |
| |
122 gaim_connection_error(gc, _("Connection lost")); |
| |
123 qd->logged_in = FALSE; |
| |
124 } |
| |
125 p->resend_times = -1; |
| |
126 break; |
| |
127 case QQ_CMD_LOGIN: |
| |
128 case QQ_CMD_REQUEST_LOGIN_TOKEN: |
| |
129 if (!qd->logged_in) /* cancel login progress */ |
| |
130 gaim_connection_error(gc, _("Login failed, no reply")); |
| |
131 p->resend_times = -1; |
| |
132 break; |
| |
133 default:{ |
| |
134 gaim_debug(GAIM_DEBUG_WARNING, "QQ", |
| |
135 "%s packet sent %d times but not acked. Not resending it.\n", |
| |
136 qq_get_cmd_desc(p->cmd), QQ_RESEND_MAX); |
| |
137 } |
| |
138 p->resend_times = -1; |
| |
139 } |
| |
140 } else { /* resend_times < QQ_RESEND_MAX, so sent it again */ |
| |
141 wait_time = (gint) (QQ_SENDQUEUE_TIMEOUT / 1000); |
| |
142 if (difftime(now, p->sendtime) > (wait_time * (p->resend_times + 1))) { |
| |
143 qq_proxy_write(qd, p->buf, p->len); |
| |
144 p->resend_times++; |
| |
145 gaim_debug(GAIM_DEBUG_INFO, |
| |
146 "QQ", "<<< [%05d] send again for %d times!\n", |
| |
147 p->send_seq, p->resend_times); |
| |
148 } |
| |
149 } |
| |
150 list = list->next; |
| |
151 } |
| |
152 return TRUE; /* if we return FALSE, the timeout callback stops functioning */ |
| |
153 } |