| |
1 /** |
| |
2 * The QQ2003C protocol plugin |
| |
3 * |
| |
4 * for gaim |
| |
5 * |
| |
6 * Copyright (C) 2004 Puzzlebird |
| |
7 * |
| |
8 * This program is free software; you can redistribute it and/or modify |
| |
9 * it under the terms of the GNU General Public License as published by |
| |
10 * the Free Software Foundation; either version 2 of the License, or |
| |
11 * (at your option) any later version. |
| |
12 * |
| |
13 * This program is distributed in the hope that it will be useful, |
| |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
16 * GNU General Public License for more details. |
| |
17 * |
| |
18 * You should have received a copy of the GNU General Public License |
| |
19 * along with this program; if not, write to the Free Software |
| |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| |
21 */ |
| |
22 |
| |
23 // START OF FILE |
| |
24 /*****************************************************************************/ |
| |
25 #include "connection.h" // GaimConnection |
| |
26 #include "debug.h" // gaim_debug |
| |
27 #include "internal.h" // _("get_text") |
| |
28 #include "notify.h" // gaim_notify |
| |
29 #include "prefs.h" // gaim_prefs_get_bool |
| |
30 #include "request.h" // gaim_request_action |
| |
31 |
| |
32 #include "header_info.h" // cmd alias |
| |
33 #include "qq_proxy.h" // qq_proxy_write |
| |
34 #include "sendqueue.h" |
| |
35 |
| |
36 #define QQ_RESEND_MAX 5 // 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 /*****************************************************************************/ |
| |
46 // Remove a packet with send_seq from sendqueue |
| |
47 void qq_sendqueue_remove(qq_data * qd, guint16 send_seq) |
| |
48 { |
| |
49 GList *list; |
| |
50 qq_sendpacket *p; |
| |
51 |
| |
52 g_return_if_fail(qd != NULL); |
| |
53 |
| |
54 list = qd->sendqueue; |
| |
55 while (list != NULL) { |
| |
56 p = (qq_sendpacket *) (list->data); |
| |
57 if (p->send_seq == send_seq) { |
| |
58 qd->sendqueue = g_list_remove(qd->sendqueue, p); |
| |
59 g_free(p->buf); |
| |
60 g_free(p); |
| |
61 break; |
| |
62 } |
| |
63 list = list->next; |
| |
64 } // while |
| |
65 } // qq_sendqueue_remove |
| |
66 |
| |
67 /*****************************************************************************/ |
| |
68 // clean up sendqueue and free all contents |
| |
69 void qq_sendqueue_free(qq_data * qd) |
| |
70 { |
| |
71 qq_sendpacket *p; |
| |
72 gint i; |
| |
73 |
| |
74 i = 0; |
| |
75 while (qd->sendqueue != NULL) { |
| |
76 p = (qq_sendpacket *) (qd->sendqueue->data); |
| |
77 qd->sendqueue = g_list_remove(qd->sendqueue, p); |
| |
78 g_free(p->buf); |
| |
79 g_free(p); |
| |
80 i++; |
| |
81 } |
| |
82 gaim_debug(GAIM_DEBUG_INFO, "QQ", "%d packets in sendqueue are freed!\n", i); |
| |
83 } // qq_sendqueue_free |
| |
84 |
| |
85 /*****************************************************************************/ |
| |
86 // packet lost, agree to send again, (and will NOT prompt again) |
| |
87 // it is removed only when ack-ed by server |
| |
88 static void _qq_send_again(gc_and_packet * gp) |
| |
89 { |
| |
90 GaimConnection *gc; |
| |
91 qq_data *qd; |
| |
92 qq_sendpacket *packet; |
| |
93 GList *list; |
| |
94 |
| |
95 g_return_if_fail(gp != NULL && gp->gc != NULL && gp->packet != NULL); |
| |
96 g_return_if_fail(gp->gc->proto_data != NULL); |
| |
97 |
| |
98 gc = gp->gc; |
| |
99 packet = gp->packet; |
| |
100 qd = (qq_data *) gc->proto_data; |
| |
101 |
| |
102 list = g_list_find(qd->sendqueue, packet); |
| |
103 if (list != NULL) { |
| |
104 packet->resend_times = 0; |
| |
105 packet->sendtime = time(NULL); |
| |
106 qq_proxy_write(qd, packet->buf, packet->len); |
| |
107 } |
| |
108 g_free(gp); |
| |
109 } // _qq_send_again |
| |
110 |
| |
111 /*****************************************************************************/ |
| |
112 // packet lost, do not send again |
| |
113 static void _qq_send_cancel(gc_and_packet * gp) |
| |
114 { |
| |
115 GaimConnection *gc; |
| |
116 qq_data *qd; |
| |
117 qq_sendpacket *packet; |
| |
118 GList *list; |
| |
119 |
| |
120 g_return_if_fail(gp != NULL && gp->gc != NULL && gp->packet != NULL); |
| |
121 g_return_if_fail(gp->gc->proto_data != NULL); |
| |
122 |
| |
123 gc = gp->gc; |
| |
124 packet = gp->packet; |
| |
125 qd = (qq_data *) gc->proto_data; |
| |
126 |
| |
127 list = g_list_find(qd->sendqueue, packet); |
| |
128 if (list != NULL) |
| |
129 qq_sendqueue_remove(qd, packet->send_seq); |
| |
130 |
| |
131 g_free(gp); |
| |
132 } // _qq_send_cancel |
| |
133 |
| |
134 /*****************************************************************************/ |
| |
135 gboolean qq_sendqueue_timeout_callback(gpointer data) |
| |
136 { |
| |
137 GaimConnection *gc; |
| |
138 qq_data *qd; |
| |
139 GList *list; |
| |
140 qq_sendpacket *p; |
| |
141 gc_and_packet *gp; |
| |
142 time_t now; |
| |
143 gint wait_time; |
| |
144 gboolean need_action; |
| |
145 |
| |
146 gc = (GaimConnection *) data; |
| |
147 qd = (qq_data *) gc->proto_data; |
| |
148 now = time(NULL); |
| |
149 list = qd->sendqueue; |
| |
150 |
| |
151 // empty queue, return TRUE so that timeout continues functioning |
| |
152 if (qd->sendqueue == NULL) |
| |
153 return TRUE; |
| |
154 |
| |
155 while (list != NULL) { // remove all packet whose resend_times == -1 |
| |
156 p = (qq_sendpacket *) list->data; |
| |
157 if (p->resend_times == -1) { // to remove |
| |
158 qd->sendqueue = g_list_remove(qd->sendqueue, p); |
| |
159 g_free(p->buf); |
| |
160 g_free(p); |
| |
161 list = qd->sendqueue; |
| |
162 } else |
| |
163 list = list->next; |
| |
164 } // while list |
| |
165 |
| |
166 list = qd->sendqueue; |
| |
167 while (list != NULL) { |
| |
168 p = (qq_sendpacket *) list->data; |
| |
169 if (p->resend_times >= QQ_RESEND_MAX) { |
| |
170 if (p->resend_times == QQ_RESEND_MAX) { // reach max |
| |
171 switch (p->cmd) { |
| |
172 case QQ_CMD_KEEP_ALIVE: |
| |
173 if (qd->logged_in) { |
| |
174 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Connection lost!\n"); |
| |
175 gaim_connection_error(gc, _("Connection lost!")); |
| |
176 qd->logged_in = FALSE; |
| |
177 } // if logged_in |
| |
178 p->resend_times = -1; |
| |
179 break; |
| |
180 case QQ_CMD_LOGIN: |
| |
181 if (!qd->logged_in) // cancel logging progress |
| |
182 gaim_connection_error(gc, _("Login failed, no reply!")); |
| |
183 p->resend_times = -1; |
| |
184 break; |
| |
185 case QQ_CMD_UPDATE_INFO: |
| |
186 gaim_notify_error(gc, NULL, |
| |
187 _("Connection timeout!"), _("User info is not updated")); |
| |
188 p->resend_times = -1; |
| |
189 break; |
| |
190 default:{ |
| |
191 need_action = |
| |
192 gaim_prefs_get_bool("/plugins/prpl/qq/prompt_for_missing_packet"); |
| |
193 if (!need_action) |
| |
194 p->resend_times = -1; // it will be removed next time |
| |
195 else { // prompt for action |
| |
196 gp = g_new0(gc_and_packet, 1); |
| |
197 gp->gc = gc; |
| |
198 gp->packet = p; |
| |
199 gaim_request_action |
| |
200 (gc, NULL, |
| |
201 _ |
| |
202 ("Send packet"), |
| |
203 _ |
| |
204 ("Packets lost, send again?"), |
| |
205 0, gp, 2, |
| |
206 _("Send"), |
| |
207 G_CALLBACK |
| |
208 (_qq_send_again), |
| |
209 _("Cancel"), G_CALLBACK(_qq_send_cancel)); |
| |
210 p->resend_times++; // will send once more, but only once |
| |
211 } // if !need_action |
| |
212 } // default |
| |
213 } // switch |
| |
214 } // resend_times == QQ_RESEND_MAX |
| |
215 } else { // resend_times < QQ_RESEND_MAX, so sent it again |
| |
216 wait_time = (gint) (QQ_SENDQUEUE_TIMEOUT / 1000); |
| |
217 if (difftime(now, p->sendtime) > (wait_time * (p->resend_times + 1))) { |
| |
218 qq_proxy_write(qd, p->buf, p->len); |
| |
219 p->resend_times++; |
| |
220 gaim_debug(GAIM_DEBUG_INFO, |
| |
221 "QQ", "<<< [%05d] send again for %d times!\n", p->send_seq, p->resend_times); |
| |
222 } // if difftime |
| |
223 } // if resend_times >= QQ_RESEND_MAX |
| |
224 list = list->next; |
| |
225 } // whiile list |
| |
226 return TRUE; // if we return FALSE, the timeout callback stops functioning |
| |
227 } // qq_sendqueue_timeout_callback |
| |
228 |
| |
229 /*****************************************************************************/ |
| |
230 // END OF FILE |