Permalink
Browse files

Refactor amount-to-read calculations in buffervent_ssl consider_readi…

…ng()

Split up consider_reading()'s conditional checks into another function
can_read() for simplicity sake.

{Split into a separate patch by nickm}
  • Loading branch information...
1 parent 6660c9a commit a186e7320054ed8f508a18876adc9f770c9c4e2e Mark Ellzey committed with Nick Mathewson Nov 17, 2011
Showing with 35 additions and 8 deletions.
  1. +35 −8 bufferevent_openssl.c
View
@@ -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;

0 comments on commit a186e73

Please sign in to comment.