Commits

Ruslan Osmanov committed cb036d7

Add: callbacks and the callback arg are now passed to EventBufferEvent::__construct as optional arguments

Comments (0)

Files changed (3)

classes/buffer_event.c

 /* Private }}} */
 
 
-/* {{{ proto EventBufferEvent EventBufferEvent::__construct(EventBase base[, mixed socket = NULL[, int options = 0]]);
+/* {{{ proto EventBufferEvent EventBufferEvent::__construct(EventBase base[, mixed socket = NULL[, int options = 0[, callable readcb[, callable writecb[, callable eventcb[, mixed arg = NULL]]]]]]);
  *
  * Create a socket-based buffer event.
  * options is one of EVENT_BEV_OPT_* constants, or 0.
  * Returns buffer event resource optionally associated with socket resource. */
 PHP_METHOD(EventBufferEvent, __construct)
 {
-	zval                *zself   = getThis();
-	zval                *zbase;
-	php_event_base_t    *base;
-	zval               **ppzfd   = NULL;
-	evutil_socket_t      fd;
-	long                 options = 0;
-	php_event_bevent_t  *bev;
-	struct bufferevent  *bevent;
-
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|Z!l",
-				&zbase, php_event_base_ce, &ppzfd, &options) == FAILURE) {
+	zval                   *zself     = getThis();
+	zval                   *zbase;
+	php_event_base_t       *base;
+	zval                  **ppzfd     = NULL;
+	evutil_socket_t         fd;
+	long                    options   = 0;
+	php_event_bevent_t     *bev;
+	struct bufferevent     *bevent;
+	zend_fcall_info         fci_read  = empty_fcall_info;
+	zend_fcall_info_cache   fcc_read  = empty_fcall_info_cache;
+	zend_fcall_info         fci_write = empty_fcall_info;
+	zend_fcall_info_cache   fcc_write = empty_fcall_info_cache;
+	zend_fcall_info         fci_event = empty_fcall_info;
+	zend_fcall_info_cache   fcc_event = empty_fcall_info_cache;
+	zval                   *zarg      = NULL;
+	bufferevent_data_cb     read_cb;
+	bufferevent_data_cb     write_cb;
+	bufferevent_event_cb    event_cb;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|Z!lf!f!f!z!",
+				&zbase, php_event_base_ce, &ppzfd, &options,
+				&fci_read, &fcc_read,
+				&fci_write, &fcc_write,
+				&fci_event, &fcc_event,
+				&zarg) == FAILURE) {
 		return;
 	}
 
 	 * within a callback.
 	 * Z_ADDREF_P(zself);
 	 */
+
+	if (ZEND_FCI_INITIALIZED(fci_read)) {
+		read_cb = bevent_read_cb;
+		PHP_EVENT_FREE_FCALL_INFO(bev->fci_read, bev->fcc_read);
+		PHP_EVENT_COPY_FCALL_INFO(bev->fci_read, bev->fcc_read, &fci_read, &fcc_read);
+	} else {
+		read_cb = NULL;
+	}
+
+	if (ZEND_FCI_INITIALIZED(fci_write)) {
+		write_cb = bevent_write_cb;
+		PHP_EVENT_FREE_FCALL_INFO(bev->fci_write, bev->fcc_write);
+		PHP_EVENT_COPY_FCALL_INFO(bev->fci_write, bev->fcc_write, &fci_write, &fcc_write);
+	} else {
+		write_cb = NULL;
+	}
+
+	if (ZEND_FCI_INITIALIZED(fci_event)) {
+		event_cb = bevent_event_cb;
+		PHP_EVENT_FREE_FCALL_INFO(bev->fci_event, bev->fcc_event);
+		PHP_EVENT_COPY_FCALL_INFO(bev->fci_event, bev->fcc_event, &fci_event, &fcc_event);
+	} else {
+		event_cb = NULL;
+	}
+
+	if (zarg) {
+		Z_ADDREF_P(zarg);
+		bev->data = zarg;
+	}
+
+	TSRMLS_SET_CTX(bev->thread_ctx);
+
+	if (read_cb || write_cb || event_cb || zarg) {
+		bufferevent_setcb(bev->bevent, read_cb, write_cb, event_cb, (void *) bev);
+	}
 }
 /* }}} */
 
 {
 	zval                  *zbevent   = getThis();
 	php_event_bevent_t    *bev;
-	zend_fcall_info        fci_read;
-	zend_fcall_info_cache  fcc_read;
-	zend_fcall_info        fci_write;
-	zend_fcall_info_cache  fcc_write;
-	zend_fcall_info        fci_event;
-	zend_fcall_info_cache  fcc_event;
+	zend_fcall_info        fci_read  = empty_fcall_info;
+	zend_fcall_info_cache  fcc_read  = empty_fcall_info_cache;
+	zend_fcall_info        fci_write = empty_fcall_info;
+	zend_fcall_info_cache  fcc_write = empty_fcall_info_cache;
+	zend_fcall_info        fci_event = empty_fcall_info;
+	zend_fcall_info_cache  fcc_event = empty_fcall_info_cache;
 	zval                  *zarg      = NULL;
 	bufferevent_data_cb    read_cb;
 	bufferevent_data_cb    write_cb;

examples/httpv0client.php

 Trivial HTTP 0.x client
 Syntax: php {$argv[0]} [hostname] [resource]
 Example: php {$argv[0]} www.google.com /
+
 EOS;
 	exit();
 }
 }
 
 $bev = new EventBufferEvent($base, /* use internal socket */ NULL,
-	EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS);
+	EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS,
+	"readcb", /* writecb */ NULL, "eventcb", $base
+);
 if (!$bev) {
 	exit("Failed creating bufferevent socket\n");
 }
 
-$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
+//$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
 $bev->enable(Event::READ | Event::WRITE);
 
 $output = $bev->output; //$bev->getOutput();
   Fix: EventListener cached file descriptor for all connections
   Change: EventBufferEvent enable/disable methods return bool now
   Change: instead of stream pass numeric file descriptor to EventListener's accept-connection callback
+  Add: callbacks and the callback arg are now passed to EventBufferEvent::__construct as optional arguments
   ]]></notes>
   <!--}}}-->
   <!--{{{ Contents -->
   Fix: EventListener cached file descriptor for all connections
   Change: EventBufferEvent enable/disable methods return bool now
   Change: instead of stream pass numeric file descriptor to EventListener's accept-connection callback
+  Add: callbacks and the callback arg are now passed to EventBufferEvent::__construct as optional arguments
   ]]></notes>
     <!--}}}-->
     <!--{{{ 1.2.4-alpha -->
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.