Commits

Ruslan Osmanov  committed 463e7a4

Fix: double check
Translated some samples according to the new oop api

  • Participants
  • Parent commits 6abe96c

Comments (0)

Files changed (4)

File classes/buffer_event.c

 
 	if (ppzfd) {
 #ifdef PHP_EVENT_SOCKETS_SUPPORT 
-		if (ppzfd) {
-			/* php_event_zval_to_fd reports error
-	 	 	 * in case if it is not a valid socket resource */
-			/*fd = (evutil_socket_t) php_event_zval_to_fd(ppzfd TSRMLS_CC);*/
-			fd = php_event_zval_to_fd(ppzfd TSRMLS_CC);
-
-		}
+		/* php_event_zval_to_fd reports error
+	 	 * in case if it is not a valid socket resource */
+		/*fd = (evutil_socket_t) php_event_zval_to_fd(ppzfd TSRMLS_CC);*/
+		fd = php_event_zval_to_fd(ppzfd TSRMLS_CC);
 
 		if (fd < 0) {
 			RETURN_FALSE;
 	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
 
 	PHP_EVENT_INIT_CLASS_OBJECT(return_value, php_event_buffer_ce);
-
-
 	PHP_EVENT_FETCH_BUFFER(b, return_value);
 
 	b->buf = bufferevent_get_input(bev->bevent);

File examples/eio.php

 
 echo "step 1\n";
 
-$base = event_base_new();
+$base = new EventBase();
 
 echo "step 2\n";
 
 
 eio_mkdir($dir, 0750, EIO_PRI_DEFAULT, "my_nop_cb");
 
-$event = event_new($base, eio_get_event_stream(),
-	EVENT_READ | EVENT_PERSIST, function ($fd, $events, $base) {
+$event = new Event($base, eio_get_event_stream(),
+	Event::READ | Event::PERSIST, function ($fd, $events, $base) {
 	echo "step 5\n";
 
 	while (eio_nreqs()) {
 		eio_poll();
 	}
 
-	event_base_loopbreak($base);
+	$base->stop();
 }, $base);
 
 echo "step 3\n";
 
-event_add($event);
+$event->add();
 
 echo "step 4\n";
 
-event_base_dispatch($base);
+$base->dispatch();
 
 echo "Done\n";

File examples/httpv0client.php

 <?php
+
 /* Read callback */
 function readcb($bev, $base) {
-	$input = bufferevent_get_input($bev);
+	echo __FUNCTION__, PHP_EOL;
+	$input = $bev->getInput();
 
-	while (($n = evbuffer_remove($input, $buf, 1024)) > 0) {
+	while (($n = $input->remove($buf, 1024)) > 0) {
 		echo $buf;
 	}
 }
 
 /* Event callback */
 function eventcb($bev, $events, $base) {
-	if ($events & EVENT_BEV_EVENT_CONNECTED) {
+	if ($events & EventBufferEvent::CONNECTED) {
 		echo "Connected.\n";
-	} elseif ($events & (EVENT_BEV_EVENT_ERROR | EVENT_BEV_EVENT_EOF)) {
-		if ($events & EVENT_BEV_EVENT_ERROR) {
-			echo "DNS error: ", bufferevent_socket_get_dns_error($bev), PHP_EOL;
+	} elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
+		if ($events & EventBufferEvent::ERROR) {
+			echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL;
 		}
 
 		echo "Closing\n";
-		bufferevent_free($bev);
-		event_base_loopexit($base);
+		$base->exit();
 		exit("Done\n");
 	}
 }
 	exit();
 }
 
-$base = event_base_new();
+$base = new EventBase();
 
-$dns_base = evdns_base_new($base, TRUE); // We'll use async DNS resolving
+$dns_base = new EventDnsBase($base, TRUE); // We'll use async DNS resolving
 if (!$dns_base) {
 	exit("Failed to init DNS Base\n");
 }
 
-$bev = bufferevent_socket_new($base, /* use internal socket */ NULL,
-	EVENT_BEV_OPT_CLOSE_ON_FREE | EVENT_BEV_OPT_DEFER_CALLBACKS);
+$bev = new EventBufferEvent($base, /* use internal socket */ NULL,
+	EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS);
 if (!$bev) {
 	exit("Failed creating bufferevent socket\n");
 }
 
-bufferevent_setcb($bev, "readcb", /* writecb */ NULL, "eventcb", $base);
-bufferevent_enable($bev, EVENT_READ | EVENT_WRITE);
+$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
+$bev->enable(Event::READ | Event::WRITE);
 
-$output = bufferevent_get_output($bev);
+$output = $bev->getOutput();
 
-if (!evbuffer_add($output,
-	"GET {$argv[2]} HTTP/1.1\r\n".
+if (!$output->add(
+	"GET {$argv[2]} HTTP/1.0\r\n".
 	"Host: {$argv[1]}\r\n".
 	"Connection: Close\r\n\r\n"
 )) {
 	exit("Failed adding request to output buffer\n");
 }
 
-if (!bufferevent_socket_connect_hostname($bev, $dns_base, $argv[1], 80, EVENT_AF_UNSPEC)) {
+if (!$bev->connectHost($dns_base, $argv[1], 80, EventUtil::AF_UNSPEC)) {
 	exit("Can't connect to host {$argv[1]}\n");
 }
 
-event_base_dispatch($base);
+$base->dispatch();

File examples/httpv0client2.php

 <?php
 /*
  * 1. Connect to 127.0.0.1 at port 80
- * by means of bufferevent_socket_connect().
+ * by means of EventBufferEvent::connect().
  *
  * 2. Request /index.cphp via HTTP/1.0
  * using the output buffer.
 
 /* Read callback */
 function readcb($bev, $base) {
-	$input = bufferevent_get_input($bev);
+	echo __FUNCTION__, PHP_EOL;
+	$input = $bev->getInput();
 
-	while (($n = evbuffer_remove($input, $buf, 1024)) > 0) {
+	while (($n = $input->remove($buf, 1024)) > 0) {
 		echo $buf;
 	}
 }
 
 /* Event callback */
 function eventcb($bev, $events, $base) {
-	if ($events & EVENT_BEV_EVENT_CONNECTED) {
+	if ($events & EventBufferEvent::CONNECTED) {
 		echo "Connected.\n";
-	} elseif ($events & (EVENT_BEV_EVENT_ERROR | EVENT_BEV_EVENT_EOF)) {
-		if ($events & EVENT_BEV_EVENT_ERROR) {
-			echo "DNS error: ", bufferevent_socket_get_dns_error($bev), PHP_EOL;
+	} elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
+		if ($events & EventBufferEvent::ERROR) {
+			echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL;
 		}
 
 		echo "Closing\n";
-		bufferevent_free($bev);
-		event_base_loopexit($base);
+		$base->exit();
 		exit("Done\n");
 	}
 }
 
-$base = event_base_new();
+$base = new EventBase();
 
-$bev = bufferevent_socket_new($base, /* use internal socket */ NULL,
-	EVENT_BEV_OPT_CLOSE_ON_FREE | EVENT_BEV_OPT_DEFER_CALLBACKS);
+echo "step 1\n";
+$bev = new EventBufferEvent($base, /* use internal socket */ NULL,
+	EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS);
 if (!$bev) {
 	exit("Failed creating bufferevent socket\n");
 }
 
-bufferevent_setcb($bev, "readcb", /* writecb */ NULL, "eventcb", $base);
-bufferevent_enable($bev, EVENT_READ | EVENT_WRITE);
+echo "step 2\n";
+$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
+$bev->enable(Event::READ | Event::WRITE);
 
+echo "step 3\n";
 /* Send request */
-$output = bufferevent_get_output($bev);
-if (!evbuffer_add($output,
+$output = $bev->getOutput();
+if (!$output->add(
 	"GET /index.cphp HTTP/1.0\r\n".
 	"Connection: Close\r\n\r\n"
 )) {
 
 /* Connect to the host syncronously.
  * We know the IP, and don't need to resolve DNS. */
-if (!bufferevent_socket_connect($bev, "127.0.0.1:80")) {
+if (!$bev->connect("127.0.0.1:80")) {
 	exit("Can't connect to host\n");
 }
 
 /* Dispatch pending events */
-event_base_dispatch($base);
+$base->dispatch();