Commits

Ruslan Osmanov committed ddbdebb

Add: some samples

  • Participants
  • Parent commits 4a12e65

Comments (0)

Files changed (3)

examples/buffer_proxy.php

+<?php
+/* TODO: Maybe use bufferevent pairs to complete example? */
+
+function read_callback_proxy($bev, $other_bev) {
+	/* One might use a function like this implementing
+	   	a simple proxy: it will take data from one connection (on
+	   	$bev), and write it to another, copying as little as
+	   	possible.
+	 */
+	bufferevent_read_buffer($bev,
+		bufferevent_get_output($other_bev));
+}
+
+?>

examples/fibonacci_buffer.php

+<?php
+/* TODO: Maybe use bufferevent pairs to complete example? */
+
+function write_callback_fibonacci($bev, $c) {
+	/* Here's a callback that adds some Fibonacci numbers to the
+	   	output buffer of $bev.  It stops once we have added 1k of
+	   	data; once this data is drained, we'll add more. */
+
+	echo __FUNCTION__, PHP_EOL;
+
+	$tmp = evbuffer_new();
+	while (evbuffer_get_length($tmp) < 1024) {
+		$next = $c[0] + $c[1];
+		$c[0] = $c[1];
+		$c[1] = $next;
+
+		evbuffer_add($tmp, $next);
+	}
+
+	/* Now we add the whole contents of tmp to bev. */
+	bufferevent_write_buffer($bev, $tmp);
+
+	/* We don't need tmp any longer. */
+	evbuffer_free($tmp);
+}
+?>

examples/uppercase_proxy.php

+<?php
+/* TODO: Maybe use bufferevent pairs to complete example? */
+
+function read_callback_uppercase($bev, $unused) {
+	/* This callback removes the data from $bev's input buffer 128
+		bytes at a time, uppercases it, and starts sending it
+		back.
+	 */
+
+	$tmp = NULL;
+
+	while (1) {
+		$n = bufferevent_read($bev, $tmp, 128);
+		($n > 0) or break;
+		bufferevent_write($bev, strtoupper($tmp), $n);
+	}
+}
+?>