Mercurial > pidgin
annotate libpurple/protocols/qq/sendqueue.c @ 23050:51dbe83ebbd3
patch-04-tcp-pending
| author | SHiNE CsyFeK <csyfek@gmail.com> |
|---|---|
| date | Tue, 24 Jun 2008 12:22:40 +0000 |
| parents | ba41f2a60253 |
| children |
| rev | line source |
|---|---|
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
1 /** |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
2 * @file sendqueue.c |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
3 * |
| 15822 | 4 * purple |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
5 * |
| 15822 | 6 * Purple is the legal property of its developers, whose names are too numerous |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
8 * source distribution. |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
9 * |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
10 * This program is free software; you can redistribute it and/or modify |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
11 * it under the terms of the GNU General Public License as published by |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
12 * the Free Software Foundation; either version 2 of the License, or |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
13 * (at your option) any later version. |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
14 * |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
15 * This program is distributed in the hope that it will be useful, |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
18 * GNU General Public License for more details. |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
19 * |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
20 * You should have received a copy of the GNU General Public License |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
21 * along with this program; if not, write to the Free Software |
|
19681
44b4e8bd759b
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
18210
diff
changeset
|
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
23 */ |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
24 |
|
18210
b8572b937c09
#include reorganizations to allow compiling with glib < 2.8 using the
Stu Tomlinson <stu@nosnilmot.com>
parents:
15822
diff
changeset
|
25 #include "internal.h" |
|
b8572b937c09
#include reorganizations to allow compiling with glib < 2.8 using the
Stu Tomlinson <stu@nosnilmot.com>
parents:
15822
diff
changeset
|
26 |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
27 #include "connection.h" |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
28 #include "debug.h" |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
29 #include "notify.h" |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
30 #include "prefs.h" |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
31 #include "request.h" |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
32 |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
33 #include "header_info.h" |
| 23050 | 34 #include "qq_network.h" |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
35 #include "sendqueue.h" |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
36 |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
37 #define QQ_RESEND_MAX 8 /* max resend per packet */ |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
38 |
| 23050 | 39 typedef struct _transaction { |
| 40 guint16 seq; | |
| 41 guint16 cmd; | |
| 42 guint8 *buf; | |
| 43 gint buf_len; | |
| 44 | |
| 45 gint fd; | |
| 46 gint retries; | |
| 47 time_t create_time; | |
| 48 } transaction; | |
| 49 | |
| 50 void qq_trans_append(qq_data *qd, guint8 *buf, gint buf_len, guint16 cmd, guint16 seq) | |
| 51 { | |
| 52 transaction *trans = g_new0(transaction, 1); | |
| 53 | |
| 54 g_return_if_fail(trans != NULL); | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
55 |
| 23050 | 56 trans->fd = qd->fd; |
| 57 trans->cmd = cmd; | |
| 58 trans->seq = seq; | |
| 59 trans->retries = QQ_RESEND_MAX; | |
| 60 trans->create_time = time(NULL); | |
| 61 trans->buf = g_memdup(buf, buf_len); /* don't use g_strdup, may have 0x00 */ | |
| 62 trans->buf_len = buf_len; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
63 |
| 23050 | 64 purple_debug(PURPLE_DEBUG_ERROR, "QQ", |
| 65 "Add to transaction, seq = %d, buf = %lu, len = %d\n", | |
| 66 trans->seq, trans->buf, trans->buf_len); | |
| 67 qd->transactions = g_list_append(qd->transactions, trans); | |
| 68 } | |
| 69 | |
| 70 /* Remove a packet with seq from sendqueue */ | |
| 71 void qq_trans_remove(qq_data *qd, gpointer data) | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
72 { |
| 23050 | 73 transaction *trans = (transaction *)data; |
| 74 | |
| 75 g_return_if_fail(qd != NULL && data != NULL); | |
| 76 | |
| 77 purple_debug(PURPLE_DEBUG_INFO, "QQ", | |
| 78 "ack [%05d] %s, remove from sendqueue\n", | |
| 79 trans->seq, qq_get_cmd_desc(trans->cmd)); | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
80 |
| 23050 | 81 if (trans->buf) g_free(trans->buf); |
| 82 qd->transactions = g_list_remove(qd->transactions, trans); | |
| 83 g_free(trans); | |
| 84 } | |
| 85 | |
| 86 gpointer qq_trans_find(qq_data *qd, guint16 seq) | |
| 87 { | |
| 88 GList *curr; | |
| 89 GList *next; | |
| 90 transaction *trans; | |
| 91 | |
| 92 curr = qd->transactions; | |
| 93 while(curr) { | |
| 94 next = curr->next; | |
| 95 trans = (transaction *) (curr->data); | |
| 96 if(trans->seq == seq) { | |
| 97 return trans; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
98 } |
| 23050 | 99 curr = next; |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
100 } |
| 23050 | 101 |
| 102 return NULL; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
103 } |
| 23050 | 104 |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
105 /* clean up sendqueue and free all contents */ |
| 23050 | 106 void qq_trans_remove_all(qq_data *qd) |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
107 { |
| 23050 | 108 GList *curr; |
| 109 GList *next; | |
| 110 transaction *trans; | |
| 111 gint count = 0; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
112 |
| 23050 | 113 curr = qd->transactions; |
| 114 while(curr) { | |
| 115 next = curr->next; | |
| 116 | |
| 117 trans = (transaction *) (curr->data); | |
| 118 /* | |
| 119 purple_debug(PURPLE_DEBUG_ERROR, "QQ", | |
| 120 "Remove to transaction, seq = %d, buf = %lu, len = %d\n", | |
| 121 trans->seq, trans->buf, trans->len); | |
| 122 */ | |
| 123 qq_trans_remove(qd, trans); | |
| 124 | |
| 125 count++; | |
| 126 curr = next; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
127 } |
| 23050 | 128 g_list_free(qd->transactions); |
| 129 | |
| 130 purple_debug(PURPLE_DEBUG_INFO, "QQ", "%d packets in sendqueue are freed!\n", count); | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
131 } |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
132 |
| 23050 | 133 gint qq_trans_scan(qq_data *qd, gint *start, |
| 134 guint8 *buf, gint maxlen, guint16 *cmd, gint *retries) | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
135 { |
| 23050 | 136 GList *curr; |
| 137 GList *next = NULL; | |
| 138 transaction *trans; | |
| 139 gint copylen; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
140 |
| 23050 | 141 g_return_val_if_fail(qd != NULL && *start >= 0 && maxlen > 0, -1); |
| 142 | |
| 143 //purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Scan from %d\n", *start); | |
| 144 curr = g_list_nth(qd->transactions, *start); | |
| 145 while(curr) { | |
| 146 next = curr->next; | |
| 147 *start = g_list_position(qd->transactions, next); | |
| 148 | |
| 149 trans = (transaction *) (curr->data); | |
| 150 if (trans->buf == NULL || trans->buf_len <= 0) { | |
| 151 qq_trans_remove(qd, trans); | |
| 152 curr = next; | |
| 153 continue; | |
| 154 } | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
155 |
| 23050 | 156 if (trans->retries < 0) { |
| 157 purple_debug(PURPLE_DEBUG_ERROR, "QQ", | |
| 158 "Remove transaction, seq %d, buf %lu, len %d, retries %d, next %d\n", | |
| 159 trans->seq, trans->buf, trans->buf_len, trans->retries, *start); | |
| 160 qq_trans_remove(qd, trans); | |
| 161 curr = next; | |
| 162 continue; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
163 } |
| 23050 | 164 |
| 165 purple_debug(PURPLE_DEBUG_ERROR, "QQ", | |
| 166 "Resend transaction, seq %d, buf %lu, len %d, retries %d, next %d\n", | |
| 167 trans->seq, trans->buf, trans->buf_len, trans->retries, *start); | |
| 168 copylen = MIN(trans->buf_len, maxlen); | |
| 169 g_memmove(buf, trans->buf, copylen); | |
| 170 | |
| 171 *cmd = trans->cmd; | |
| 172 *retries = trans->retries; | |
| 173 trans->retries--; | |
| 174 return copylen; | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
175 } |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
176 |
| 23050 | 177 // purple_debug(PURPLE_DEBUG_INFO, "QQ", "Scan finished\n"); |
| 178 return -1; | |
| 179 } | |
| 180 | |
| 181 void qq_packet_push(qq_data *qd, guint16 cmd, guint16 seq, guint8 *data, gint data_len) | |
| 182 { | |
| 183 transaction *trans = g_new0(transaction, 1); | |
| 184 | |
| 185 g_return_if_fail(data != NULL && data_len > 0); | |
| 186 g_return_if_fail(trans != NULL); | |
| 187 | |
| 188 trans->cmd = cmd; | |
| 189 trans->seq = seq; | |
| 190 trans->buf = g_memdup(data, data_len); | |
| 191 trans->buf_len = data_len; | |
| 192 trans->create_time = time(NULL); | |
| 193 | |
| 194 if (qd->rcv_trans == NULL) | |
| 195 qd->rcv_trans = g_queue_new(); | |
| 196 | |
| 197 g_queue_push_head(qd->rcv_trans, trans); | |
| 198 } | |
| 199 | |
| 200 gint qq_packet_pop(qq_data *qd, guint16 *cmd, guint16 *seq, guint8 *data, gint max_len) | |
| 201 { | |
| 202 transaction *trans = NULL; | |
| 203 gint copy_len; | |
| 204 | |
| 205 g_return_val_if_fail(data != NULL && max_len > 0, -1); | |
| 206 | |
| 207 if (g_queue_is_empty(qd->rcv_trans)) { | |
| 208 return -1; | |
| 209 } | |
| 210 trans = (transaction *) g_queue_pop_head(qd->rcv_trans); | |
| 211 if (trans == NULL) { | |
| 212 return 0; | |
| 213 } | |
| 214 if (trans->buf == NULL || trans->buf_len <= 0) { | |
| 215 return 0; | |
| 216 } | |
| 217 | |
| 218 copy_len = MIN(max_len, trans->buf_len); | |
| 219 g_memmove(data, trans->buf, copy_len); | |
| 220 *cmd = trans->cmd; | |
| 221 *seq = trans->seq; | |
| 222 | |
| 223 g_free(trans->buf); | |
| 224 g_free(trans); | |
| 225 return copy_len; | |
| 226 } | |
| 227 | |
| 228 /* clean up the packets before login */ | |
| 229 void qq_packet_remove_all(qq_data *qd) | |
| 230 { | |
| 231 transaction *trans = NULL; | |
| 232 | |
| 233 g_return_if_fail(qd != NULL); | |
| 234 | |
| 235 /* now clean up my own data structures */ | |
| 236 if (qd->rcv_trans != NULL) { | |
| 237 while (NULL != (trans = g_queue_pop_tail(qd->rcv_trans))) { | |
| 238 g_free(trans->buf); | |
| 239 g_free(trans); | |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
240 } |
| 23050 | 241 g_queue_free(qd->rcv_trans); |
|
15373
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
242 } |
|
5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
Sean Egan <seanegan@gmail.com>
parents:
diff
changeset
|
243 } |
