comparison libpurple/protocols/jabber/bosh.c @ 25997:c605e5f2fc99

Use a PurpleCircBuffer for the queued stanzas
author Paul Aurich <paul@darkrain42.org>
date Sat, 21 Mar 2009 03:03:18 +0000
parents 349e411da2ce
children 31bb0d6e7f7e
comparison
equal deleted inserted replaced
25996:349e411da2ce 25997:c605e5f2fc99
17 * along with this program; if not, write to the Free Software 17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 * 19 *
20 */ 20 */
21 #include "internal.h" 21 #include "internal.h"
22 #include "circbuffer.h"
22 #include "core.h" 23 #include "core.h"
23 #include "cipher.h" 24 #include "cipher.h"
24 #include "debug.h" 25 #include "debug.h"
25 #include "prpl.h" 26 #include "prpl.h"
26 #include "util.h" 27 #include "util.h"
61 62
62 unsigned int inactivity_timer; 63 unsigned int inactivity_timer;
63 int max_inactivity; 64 int max_inactivity;
64 int wait; 65 int wait;
65 66
66 GString *pending; 67 PurpleCircBuffer *pending;
67 int max_requests; 68 int max_requests;
68 int requests; 69 int requests;
69 70
70 PurpleBOSHConnectionConnectFunction connect_cb; 71 PurpleBOSHConnectionConnectFunction connect_cb;
71 PurpleBOSHConnectionReceiveFunction receive_cb; 72 PurpleBOSHConnectionReceiveFunction receive_cb;
175 176
176 conn->js = js; 177 conn->js = js;
177 /* FIXME: This doesn't seem very random */ 178 /* FIXME: This doesn't seem very random */
178 conn->rid = rand() % 100000 + 1728679472; 179 conn->rid = rand() % 100000 + 1728679472;
179 180
180 conn->pending = g_string_new(""); 181 conn->pending = purple_circ_buffer_new(0 /* default grow size */);
181 182
182 conn->ready = FALSE; 183 conn->ready = FALSE;
183 184
184 conn->connections[0] = jabber_bosh_http_connection_init(conn); 185 conn->connections[0] = jabber_bosh_http_connection_init(conn);
185 186
195 g_free(conn->path); 196 g_free(conn->path);
196 197
197 if (conn->inactivity_timer) 198 if (conn->inactivity_timer)
198 purple_timeout_remove(conn->inactivity_timer); 199 purple_timeout_remove(conn->inactivity_timer);
199 200
200 if (conn->pending) 201 purple_circ_buffer_destroy(conn->pending);
201 g_string_free(conn->pending, TRUE);
202 202
203 for (i = 0; i < MAX_HTTP_CONNECTIONS; ++i) { 203 for (i = 0; i < MAX_HTTP_CONNECTIONS; ++i) {
204 if (conn->connections[i]) 204 if (conn->connections[i])
205 jabber_bosh_http_connection_destroy(conn->connections[i]); 205 jabber_bosh_http_connection_destroy(conn->connections[i]);
206 } 206 }
460 xmlnode *node) 460 xmlnode *node)
461 { 461 {
462 PurpleHTTPConnection *chosen; 462 PurpleHTTPConnection *chosen;
463 GString *packet = NULL; 463 GString *packet = NULL;
464 char *buf = NULL; 464 char *buf = NULL;
465 int len;
465 466
466 chosen = find_available_http_connection(conn); 467 chosen = find_available_http_connection(conn);
467 468
468 if (type != PACKET_NORMAL && !chosen) { 469 if (type != PACKET_NORMAL && !chosen) {
469 /* 470 /*
476 purple_debug_warning("jabber", "First BOSH connection wasn't ready. Bad " 477 purple_debug_warning("jabber", "First BOSH connection wasn't ready. Bad "
477 "things may happen.\n"); 478 "things may happen.\n");
478 } 479 }
479 480
480 if (node) 481 if (node)
481 buf = xmlnode_to_str(node, NULL); 482 buf = xmlnode_to_str(node, &len);
482 483
483 if (type == PACKET_NORMAL && (!chosen || 484 if (type == PACKET_NORMAL && (!chosen ||
484 (conn->max_requests > 0 && conn->requests == conn->max_requests))) { 485 (conn->max_requests > 0 && conn->requests == conn->max_requests))) {
485 /* 486 /*
486 * For normal data, send up to max_requests requests at a time or there is no 487 * For normal data, send up to max_requests requests at a time or there is no
487 * connection ready (likely, we're currently opening a second connection and 488 * connection ready (likely, we're currently opening a second connection and
488 * will send these packets when connected). 489 * will send these packets when connected).
489 */ 490 */
490 if (buf) { 491 if (buf) {
491 conn->pending = g_string_append(conn->pending, buf); 492 purple_circ_buffer_append(conn->pending, buf, len);
492 g_free(buf); 493 g_free(buf);
493 } 494 }
494 return; 495 return;
495 } 496 }
496 497
508 conn->js->user->domain); 509 conn->js->user->domain);
509 510
510 if (type == PACKET_STREAM_RESTART) 511 if (type == PACKET_STREAM_RESTART)
511 packet = g_string_append(packet, " xmpp:restart='true'/>"); 512 packet = g_string_append(packet, " xmpp:restart='true'/>");
512 else { 513 else {
514 gsize read_amt;
513 if (type == PACKET_TERMINATE) 515 if (type == PACKET_TERMINATE)
514 packet = g_string_append(packet, " type='terminate'"); 516 packet = g_string_append(packet, " type='terminate'");
515 517
516 g_string_append_printf(packet, ">%s%s</body>", conn->pending->str, 518 packet = g_string_append_c(packet, '>');
517 buf ? buf : ""); 519
518 g_string_truncate(conn->pending, 0); 520 while ((read_amt = purple_circ_buffer_get_max_read(conn->pending)) > 0) {
521 packet = g_string_append_len(packet, conn->pending->outptr, read_amt);
522 purple_circ_buffer_mark_read(conn->pending, read_amt);
523 }
524
525 if (buf)
526 packet = g_string_append_len(packet, buf, len);
527 packet = g_string_append(packet, "</body>");
519 } 528 }
520 529
521 g_free(buf); 530 g_free(buf);
522 531
523 http_connection_send_request(chosen, packet); 532 http_connection_send_request(chosen, packet);
535 conn->headers_done = FALSE; 544 conn->headers_done = FALSE;
536 conn->handled_len = conn->body_len = 0; 545 conn->handled_len = conn->body_len = 0;
537 546
538 if (conn->bosh->ready) { 547 if (conn->bosh->ready) {
539 purple_debug_info("jabber", "BOSH session already exists. Trying to reuse it.\n"); 548 purple_debug_info("jabber", "BOSH session already exists. Trying to reuse it.\n");
540 if (conn->bosh->pending && conn->bosh->pending->len > 0) { 549 if (conn->bosh->pending->bufused > 0) {
541 /* Send the pending data */ 550 /* Send the pending data */
542 jabber_bosh_connection_send_native(conn->bosh, PACKET_NORMAL, NULL); 551 jabber_bosh_connection_send_native(conn->bosh, PACKET_NORMAL, NULL);
543 } 552 }
544 #if 0 553 #if 0
545 conn->bosh->receive_cb = jabber_bosh_connection_received; 554 conn->bosh->receive_cb = jabber_bosh_connection_received;
623 632
624 http_received_cb(conn->buf->str + conn->handled_len, conn->body_len, 633 http_received_cb(conn->buf->str + conn->handled_len, conn->body_len,
625 conn->bosh); 634 conn->bosh);
626 635
627 if (conn->bosh->ready && 636 if (conn->bosh->ready &&
628 (conn->bosh->requests == 0 || conn->bosh->pending->len)) { 637 (conn->bosh->requests == 0 || conn->bosh->pending->bufused > 0)) {
629 jabber_bosh_connection_send(conn->bosh, NULL); 638 jabber_bosh_connection_send(conn->bosh, NULL);
630 purple_debug_misc("jabber", "BOSH: Sending an empty request\n"); 639 purple_debug_misc("jabber", "BOSH: Sending an empty request\n");
631 } 640 }
632 641
633 g_string_free(conn->buf, TRUE); 642 g_string_free(conn->buf, TRUE);