]> granicus.if.org Git - libevent/commitdiff
Refactor amount-to-read calculations in buffervent_ssl consider_reading()
authorMark Ellzey <mark.thomas@mandiant.com>
Thu, 17 Nov 2011 16:45:49 +0000 (11:45 -0500)
committerNick Mathewson <nickm@torproject.org>
Thu, 17 Nov 2011 16:45:49 +0000 (11:45 -0500)
Split up consider_reading()'s conditional checks into another function
can_read() for simplicity sake.

{Split into a separate patch by nickm}

bufferevent_openssl.c

index 86a8619b0c2196d5c5743026ed0ccc846b549056..7d822d9ef78e7c2b164c4cfb3ce3fbce45ccbdc5 100644 (file)
@@ -704,6 +704,38 @@ do_write(struct bufferevent_openssl *bev_ssl, int atmost)
 
 #define READ_DEFAULT 4096
 
+/* Try to figure out how many bytes to read; return 0 if we shouldn't be
+ * reading. */
+static int
+bytes_to_read(struct bufferevent_openssl *bev)
+{
+       struct evbuffer *input = bev->bev.bev.input;
+       struct event_watermark *wm = &bev->bev.bev.wm_read;
+
+       if (bev->write_blocked_on_read) {
+               return 0;
+       }
+
+       if (! (bev->bev.bev.enabled & EV_READ)) {
+               return 0;
+       }
+
+       if (bev->bev.read_suspended) {
+               return 0;
+       }
+
+       if (wm->high) {
+               if (evbuffer_get_length(input) >= wm->high) {
+                       return 0;
+               }
+
+               return wm->high - evbuffer_get_length(input);
+       }
+
+       return READ_DEFAULT;
+}
+
+
 /* Things look readable.  If write is blocked on read, write till it isn't.
  * Read from the underlying buffer until we block or we hit our high-water
  * mark.
@@ -712,8 +744,7 @@ static void
 consider_reading(struct bufferevent_openssl *bev_ssl)
 {
        int r;
-       struct evbuffer *input = bev_ssl->bev.bev.input;
-       struct event_watermark *wm = &bev_ssl->bev.bev.wm_read;
+       int n_to_read;
 
        while (bev_ssl->write_blocked_on_read) {
                r = do_write(bev_ssl, WRITE_FRAME);
@@ -722,12 +753,8 @@ consider_reading(struct bufferevent_openssl *bev_ssl)
        }
        if (bev_ssl->write_blocked_on_read)
                return;
-       while ((bev_ssl->bev.bev.enabled & EV_READ) &&
-           (! bev_ssl->bev.read_suspended) &&
-           (! wm->high || evbuffer_get_length(input) < wm->high)) {
-               int n_to_read =
-                   wm->high ? wm->high - evbuffer_get_length(input)
-                            : READ_DEFAULT;
+
+       while ((n_to_read = bytes_to_read(bev_ssl)) != 0) {
                r = do_read(bev_ssl, n_to_read);
                if (r <= 0)
                        break;