Commits

Ruslan Osmanov committed 4a12e65

Add: bufferevent_pair_new, bufferevent_write, bufferevent_write_buffer, bufferevent_read ,bufferevent_read_buffer

  • Participants
  • Parent commits 8440fb8

Comments (0)

Files changed (3)

 }
 /* }}} */
 
+/* {{{ proto array bufferevent_pair_new(resource base[, int options = 0]);
+ *
+ * options is one of EVENT_BEV_OPT_* constants, or 0.
+ *
+ * Returns array of two buffer event resources, each connected to the other.
+ * All the usual options are supported, except for EVENT_BEV_OPT_CLOSE_ON_FREE,
+ * which has no effect, and EVENT_BEV_OPT_DEFER_CALLBACKS, which is always on.
+ */
+PHP_FUNCTION(bufferevent_pair_new)
+{
+	zval               *zbase;
+	php_event_base_t   *base;
+	long                options        = 0;
+	php_event_bevent_t *b[2];
+	struct bufferevent *bevent_pair[2];
+	int                 i;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
+				&zbase, &options) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BASE(base, zbase);
+
+	if (bufferevent_pair_new(base, options, bevent_pair)) {
+		RETURN_FALSE;
+	}
+
+	array_init(return_value);
+
+	for (i = 0; i < 2; i++) {
+		b[i] = emalloc(sizeof(php_event_bevent_t));
+		memset(b[i], 0, sizeof(php_event_bevent_t));
+
+		b[i]->bevent    = bevent_pair[i];
+		b[i]->stream_id = -1;
+		b[i]->rsrc_id   = zend_list_insert(b, le_event_bevent TSRMLS_CC);
+
+		add_next_index_resource(return_value, b[i]->rsrc_id);
+	}
+}
+/* }}} */
+
 /* {{{ proto void bufferevent_free(resource bevent);
  * Free a buffer event resource. */
 PHP_FUNCTION(bufferevent_free)
 }
 /* }}} */
 
+/* {{{ proto bool bufferevent_write(resource bevent, string data);
+ * Adds `data' to a bufferevent's output buffer. */
+PHP_FUNCTION(bufferevent_write)
+{
+	php_event_bevent_t *bev;
+	zval               *zbevent;
+	zval               *zdata;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz",
+				&zbevent, &zdata) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
+
+	convert_to_string(zdata);
+
+	if (bufferevent_write(bev->bevent, Z_STRVAL_P(zdata), Z_STRLEN_P(zdata))) {
+		RETURN_FALSE;
+	}
+	RETVAL_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool bufferevent_write_buffer(resource bevent, resource buf);
+ * Adds contents of the entire buffer to a bufferevent's output buffer. */
+PHP_FUNCTION(bufferevent_write_buffer)
+{
+	php_event_bevent_t *bev;
+	zval               *zbevent;
+	php_event_buffer_t *b;
+	zval               *zbuf;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr",
+				&zbevent, &zbuf) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
+	PHP_EVENT_FETCH_BUFFER(b, zbuf);
+
+	if (bufferevent_write_buffer(bev->bevent, b->buf)) {
+		RETURN_FALSE;
+	}
+	RETVAL_TRUE;
+}
+/* }}} */
+
+/* {{{ proto int bufferevent_read(resource bevent, string &data, int size);
+ * Removes up to size bytes from the input buffer, storing them into the memory at data.
+ *
+ * Returns the number of bytes actually removed.  */
+PHP_FUNCTION(bufferevent_read)
+{
+	php_event_bevent_t *bev;
+	zval               *zbevent;
+	zval               *zdata;
+	long                size;
+	char               *data;
+	long                ret;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzl",
+				&zbevent, &zdata, &size) == FAILURE) {
+		return;
+	}
+
+	if (!Z_ISREF_P(zdata)) {
+		/* Was not passed by reference */
+		return;
+	}
+
+	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
+
+	data = safe_emalloc(size, sizeof(char), 1);
+
+	ret = bufferevent_read(bev->bevent, data, size);
+
+	if (ret > 0) {
+		convert_to_string(zdata);
+		zval_dtor(zdata);
+		Z_STRVAL_P(zdata) = estrndup(data, ret);
+		Z_STRLEN_P(zdata) = ret;
+	}
+
+	efree(data);
+
+	RETVAL_LONG(ret);
+}
+/* }}} */
+
+/* {{{ proto bool bufferevent_read_buffer(resource bevent, resource buf);
+ * Drains the entire contents of the input buffer and places them into buf; it returns 0 on success and -1 on failure. */
+PHP_FUNCTION(bufferevent_read_buffer)
+{
+	php_event_bevent_t *bev;
+	zval               *zbevent;
+	php_event_buffer_t *b;
+	zval               *zbuf;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr",
+				&zbevent, &zbuf) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
+	PHP_EVENT_FETCH_BUFFER(b, zbuf);
+
+	if (bufferevent_read_buffer(bev->bevent, b->buf)) {
+		RETURN_FALSE;
+	}
+	RETVAL_TRUE;
+}
+/* }}} */
+
+
+
 /* {{{ proto resource evbuffer_new(void);
  * Allocates storage for new event buffer and returns it's resource. */
 PHP_FUNCTION(evbuffer_new)
 }
 /* }}} */
 
-
 /* Extra API functions END }}} */
 #endif
 
 	ZEND_ARG_INFO(0, highmark)
 ZEND_END_ARG_INFO();
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_bufferevent_write, 0, 0, 2)
+	ZEND_ARG_INFO(0, bevent)
+	ZEND_ARG_INFO(0, data)
+ZEND_END_ARG_INFO();
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_bufferevent_write_buffer, 0, 0, 2)
+	ZEND_ARG_INFO(0, bevent)
+	ZEND_ARG_INFO(0, buf)
+ZEND_END_ARG_INFO();
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_bufferevent_read, 0, 0, 3)
+	ZEND_ARG_INFO(0, bevent)
+	ZEND_ARG_INFO(1, data)
+	ZEND_ARG_INFO(0, size)
+ZEND_END_ARG_INFO();
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_bufferevent_pair_new, 0, 0, 2)
+	ZEND_ARG_INFO(0, base)
+	ZEND_ARG_INFO(0, events)
+ZEND_END_ARG_INFO();
+
+
 ZEND_BEGIN_ARG_INFO_EX(arginfo_evbuffer_1, 0, 0, 1)
 	ZEND_ARG_INFO(0, buf)
 ZEND_END_ARG_INFO();
 	PHP_FE(bufferevent_get_input,               arginfo_bufferevent_1)
 	PHP_FE(bufferevent_get_output,              arginfo_bufferevent_1)
 	PHP_FE(bufferevent_set_watermark,           arginfo_bufferevent_set_watermark)
+	PHP_FE(bufferevent_write,                   arginfo_bufferevent_write)
+	PHP_FE(bufferevent_write_buffer,            arginfo_bufferevent_write_buffer)
+	PHP_FE(bufferevent_read,                    arginfo_bufferevent_read)
+	PHP_FE(bufferevent_read_buffer,             arginfo_bufferevent_write_buffer)
+	PHP_FE(bufferevent_pair_new,                arginfo_bufferevent_pair_new)
 
 	PHP_FE(evbuffer_new,            arginfo_event__void)
 	PHP_FE(evbuffer_free,           arginfo_evbuffer_1)
 	PHP_FALIAS(event_buffer_enable,        bufferevent_enable,        arginfo_bufferevent__events)
 	PHP_FALIAS(event_buffer_disable,       bufferevent_disable,       arginfo_bufferevent__events)
 	PHP_FALIAS(event_buffer_watermark_set, bufferevent_set_watermark, arginfo_bufferevent_set_watermark)
+	PHP_FALIAS(event_buffer_write,         bufferevent_write,         arginfo_bufferevent_write)
 
 
 #if HAVE_EVENT_EXTRA_LIB
 PHP_FUNCTION(bufferevent_get_output);
 PHP_FUNCTION(bufferevent_set_watermark);
 PHP_FUNCTION(bufferevent_socket_get_dns_error);
+PHP_FUNCTION(bufferevent_write);
+PHP_FUNCTION(bufferevent_write_buffer);
+PHP_FUNCTION(bufferevent_read);
+PHP_FUNCTION(bufferevent_read_buffer);
+PHP_FUNCTION(bufferevent_pair_new);
 
 PHP_FUNCTION(evbuffer_new);
 PHP_FUNCTION(evbuffer_free);