cvs: php4 /ext/ircg ircg.c

From: Date: Mon, 28 Apr 2003 15:18:39 +0000
Subject: cvs: php4 /ext/ircg ircg.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-20539@lists.php.net to get a copy of this message
sas Mon Apr 28 11:18:39 2003 EDT Modified files: /php4/ext/ircg ircg.c Log: # disk going bad, committing changes as I go add handling of incoming data (e.g. for duplex Flash XMLServer support) fix output file support in multi-process environment

Index: php4/ext/ircg/ircg.c diff -u php4/ext/ircg/ircg.c:1.176 php4/ext/ircg/ircg.c:1.177 --- php4/ext/ircg/ircg.c:1.176 Wed Apr 23 00:02:10 2003 +++ php4/ext/ircg/ircg.c Mon Apr 28 11:18:39 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: ircg.c,v 1.176 2003/04/23 04:02:10 sas Exp $ */ +/* $Id: ircg.c,v 1.177 2003/04/28 15:18:39 sas Exp $ */ /* {{{ includes */ @@ -90,6 +90,7 @@ function_entry ircg_functions[] = { #ifdef IRCG_PENDING_URL PHP_FE(ircg_set_on_die, NULL) + PHP_FE(ircg_set_on_read_data, NULL) #endif PHP_FE(ircg_pconnect, NULL) PHP_FE(ircg_set_current, NULL) @@ -159,10 +160,12 @@ size_t od_len; struct in_addr od_ip; short od_port; + + struct in_addr read_data_ip; + short read_data_port; + smart_str read_data_req; #endif - int file_fd; - char bailout_on_trivial; /* Whether to handle trivial errors as fatal */ char *ident; /* NOT available outside of ircg_pconnect or register_hooks */ @@ -351,31 +354,26 @@ php_irconn_t *conn = dummy; php_ircg->irc_quit_handlers++; - if (conn->fd > -1) { -#ifdef USE_FD2IRCONN - ircg_hash_index_del(&h_fd2irconn, conn->fd); - if (is_my_conn(conn)) - shutdown(conn->fd, 2); + +#ifdef USE_IRCONN_MANAGEMENT + ircg_hash_index_del(&h_irconn, conn->irconn_id); #endif - if (conn->file_fd == -1) - irc_write_buf_del(&conn->wb); - } - if (conn->file_fd != -1) { + + if (conn->fd > -1) { smart_str m = {0}; FORMAT_MSG(conn, FMT_MSG_DISCONNECTED, NULL, NULL, NULL, NULL, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); - close(conn->file_fd); - conn->file_fd = -1; - } - - conn->fd = -2; - -#ifdef USE_IRCONN_MANAGEMENT - ircg_hash_index_del(&h_irconn, conn->irconn_id); +#ifdef USE_FD2IRCONN + ircg_hash_index_del(&h_fd2irconn, conn->fd); + if (is_my_conn(conn)) + shutdown(conn->fd, 2); #endif + conn->fd = -1; + irc_write_buf_del(&conn->wb); + } if (conn->fmt_msgs->rc > 0) { fmt_msgs_drop_ref(conn->fmt_msgs); @@ -386,7 +384,8 @@ #ifdef IRCG_PENDING_URL if (conn->od_port) { - irc_add_pending_url(conn->od_ip, conn->od_port, conn->od_data, conn->od_len); + irc_add_pending_url(conn->od_ip, conn->od_port, conn->od_data, + conn->od_len); IRCG_SHARED_FREE(conn->od_data); } #endif @@ -447,14 +446,7 @@ if (msg->c == 0) return; - if (conn->file_fd != -1) { - write(conn->file_fd, msg->c, msg->len); - goto done; - } - switch (conn->fd) { - case -2: /* Connection was finished */ - goto done; case -1: /* No message window yet. Buffer */ if ((php_ircg_now() - conn->login) > WINDOW_TIMEOUT) { irc_disconnect(&conn->conn, timeout_message); @@ -496,7 +488,6 @@ return; #else irc_write_buf_append(&conn->wb, msg); - goto done; #endif break; } @@ -509,7 +500,7 @@ static void msg_send(php_irconn_t *conn, smart_str *msg) { msg_accum_send(conn, msg); - if (conn->fd >= 0 && conn->file_fd == -1) + if (conn->fd >= 0) irc_write_buf_flush(&conn->wb); } @@ -895,8 +886,7 @@ smart_str_setl(&tmp, " ", 2); irc_write_buf_append_ex(&conn->wb, &tmp, 1); irc_write_buf_flush(&conn->wb); - } else if (conn->file_fd < 0 - && (php_ircg_now() - conn->login) > WINDOW_TIMEOUT) { + } else if ((php_ircg_now() - conn->login) > WINDOW_TIMEOUT) { char buf[1024]; sprintf(buf, "timeout after %d seconds (%d, %d)", @@ -945,6 +935,39 @@ #endif /* }}} */ +/* {{{ proto bool ircg_set_on_read_data(int connection, string host, int port, string data) + Set action to be executed when data is received from a HTTP client */ +PHP_FUNCTION(ircg_set_on_read_data) +{ + zval **p1, **p2, **p3, **p4; + php_irconn_t *conn; + + if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &p1, &p2, &p3, &p4) == FAILURE) + WRONG_PARAM_COUNT; + + convert_to_long_ex(p1); + + conn = lookup_irconn(Z_LVAL_PP(p1)); + + if (!conn) RETURN_FALSE; + + convert_to_string_ex(p2); + convert_to_long_ex(p3); + convert_to_string_ex(p4); + + if (inet_aton(Z_STRVAL_PP(p2), &conn->read_data_ip) != 0) { + conn->read_data_port = Z_LVAL_PP(p3); + + smart_str_appendl(&conn->read_data_req, Z_STRVAL_PP(p4), Z_STRLEN_PP(p4)); + RETVAL_TRUE; + } else { + RETVAL_FALSE; + } + + put_irconn(conn); +} +/* }}} */ + /* {{{ proto bool ircg_set_on_die(int connection, string host, int port, string data) Sets hostaction to be executed when connection dies */ #ifdef IRCG_PENDING_URL @@ -1009,6 +1032,7 @@ { zval **p1, **p2; php_irconn_t *conn; + int do_put = 1; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &p1, &p2) == FAILURE) WRONG_PARAM_COUNT; @@ -1018,33 +1042,42 @@ conn = lookup_irconn(Z_LVAL_PP(p1)); - if (!conn) RETURN_FALSE; + RETVAL_FALSE; + + if (!conn) return; if (conn->fd != -1) { - php_error(E_WARNING, "Called after a call to ircg_set_current(). You must set the output filename before opening the persistent HTTP connection which is kept alive."); - RETVAL_FALSE; - } else { - if (conn->file_fd != -1) - close(conn->file_fd); + php_error(E_WARNING, "ircg_set_file cannot be called after " + "ircg_set_current()."); + goto out; + } + + conn->fd = open(Z_STRVAL_PP(p2), O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0644); + if (conn->fd == -1) { + goto out; + } + + if (fcntl(conn->fd, F_SETFD, 1)) { + close(conn->fd); + conn->fd = -1; + goto out; + } - conn->file_fd = open(Z_STRVAL_PP(p2), O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0644); - if (conn->file_fd == -1) { - RETVAL_FALSE; - } else { -#ifdef F_SETFD - if (fcntl(conn->file_fd, F_SETFD, 1)) { - close(conn->file_fd); - conn->file_fd = -1; - RETVAL_FALSE; - } else -#endif - { - msg_replay_buffer(conn); - RETVAL_TRUE; - } - } + if (irc_write_buf_add_ex(&conn->wb, conn->fd, wbuf_hooks, conn) == 0) { + msg_replay_buffer(conn); + + IRCGG(flush_data) = conn; + do_put = 0; + RETVAL_TRUE; + } else { + close(conn->fd); + conn->fd = -1; } - put_irconn(conn); + +out: + + if (do_put) + put_irconn(conn); } /* }}} */ @@ -1059,18 +1092,53 @@ put_irconn(data); } +static void wbuf_read_data(irc_write_buf *p, void *data, smart_str *s) +{ + php_irconn_t *conn = (php_irconn_t *) data; + smart_str enc = {0}; + smart_str req = {0}; + + enc.c = php_url_encode(s->c, s->len, &enc.len); + + smart_str_append(&req, &conn->read_data_req); + smart_str_appends(&req, "Connection: close\r\n" + "Content-Type: " + "application/x-www-form-urlencoded\r\n" + "Content-Length: "); + smart_str_append_long(&req, enc.len + 5); + smart_str_appends(&req, "\r\n\r\ndata="); + smart_str_append(&req, &enc); + + smart_str_free(&enc); + + irc_add_pending_url(conn->read_data_ip, conn->read_data_port, + req.c, req.len); + + smart_str_free(&req); +} + IRCG_CB_MAP_START(php_ircg_cb_wbuf) IRCG_CB_MAP_ENTRY(wbuf_closed_conn) IRCG_CB_MAP_ENTRY(wbuf_add_complete) + IRCG_CB_MAP_ENTRY(wbuf_read_data) IRCG_CB_MAP_END() static void wbuf_hooks(irc_write_buf *p, void *data) { + php_irconn_t *conn = (php_irconn_t *) data; + irc_write_buf_reg(p, WB_MAP_HANDLE, (irc_hook) php_ircg_cb_wbuf_handle); irc_write_buf_reg(p, WB_CLOSED_CONN, wbuf_closed_conn); irc_write_buf_reg(p, WB_ADD_COMPLETE, wbuf_add_complete); + + if (conn->read_data_port) + irc_write_buf_reg(p, WB_READ_DATA, wbuf_read_data); } +#ifndef USE_FD2IRCONN +#define php_ircg_register_closed_connection(a) (1) +#endif + /* {{{ proto bool ircg_set_current(int connection) Sets current connection for output */ PHP_FUNCTION(ircg_set_current) @@ -1078,6 +1146,7 @@ zval **p1; php_irconn_t *conn; int do_put = 1; + int n; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &p1) == FAILURE) WRONG_PARAM_COUNT; @@ -1086,53 +1155,59 @@ conn = lookup_irconn(Z_LVAL_PP(p1)); - if (!conn) RETURN_FALSE; + RETVAL_FALSE; + + if (!conn) return; if (conn->fd >= 0) { #ifdef USE_FD2IRCONN /* There is a HTTP connection alive, kill it */ ircg_hash_index_del(&h_fd2irconn, conn->fd); #endif - if (conn->file_fd == -1) - irc_write_buf_del(&conn->wb); + irc_write_buf_del(&conn->wb); conn->fd = -1; } if (php_ircg_register_with_sapi(&conn->fd TSRMLS_CC) -#ifdef USE_FD2IRCONN || php_ircg_register_closed_connection(http_closed_connection) -#endif || fcntl(conn->fd, F_GETFL) == -1) { #ifdef USE_IRCONN_MANAGEMENT ircg_hash_index_del(&h_irconn, Z_LVAL_PP(p1)); #endif - php_error(E_WARNING, "current fd is not valid"); - RETVAL_FALSE; - } else { + php_error(E_WARNING, "Failed to obtain the socket of the current " + "HTTP connection"); + goto out; + } + + if (conn->fd == -1) + goto out; + #ifdef USE_FD2IRCONN - ircg_hash_index_update(&h_fd2irconn, conn->fd, &Z_LVAL_PP(p1), - sizeof(int), NULL); + ircg_hash_index_update(&h_fd2irconn, conn->fd, &Z_LVAL_PP(p1), + sizeof(int), NULL); #endif - if (conn->file_fd == -1 && conn->fd >= 0) { + #if IRCG_API_VERSION < 20021127 - irc_write_buf_add(&conn->wb, conn->fd); - if (1) { +#define ircg_resource_get(a) do {} while (0) + irc_write_buf_add(&conn->wb, conn->fd); + n = 0; #else - if (irc_write_buf_add_ex(&conn->wb, conn->fd, wbuf_hooks, conn) == 0) { - ircg_resource_get(conn->irconn_id); + n = irc_write_buf_add_ex(&conn->wb, conn->fd, wbuf_hooks, conn); #endif - php_ircg->irc_set_currents++; - IRCGG(flush_data) = conn; - /* We maintain a reference to conn until rshutdown */ - do_put = 0; - RETVAL_TRUE; - } else { - php_ircg->failed_set_currents++; - conn->fd = -1; - RETVAL_FALSE; - } - } + + if (n == 0) { + ircg_resource_get(conn->irconn_id); + php_ircg->irc_set_currents++; + IRCGG(flush_data) = conn; + /* We maintain a reference to conn until rshutdown */ + do_put = 0; + RETVAL_TRUE; + } else { + php_ircg->failed_set_currents++; + conn->fd = -1; } + +out: if (do_put) put_irconn(conn); } @@ -1866,7 +1941,7 @@ */ conn = IRCG_SHARED_ALLOC(sizeof *conn); memset(conn, 0, sizeof *conn); - conn->fd = conn->file_fd = -1; + conn->fd = -1; if (ZEND_NUM_ARGS() > 5 && Z_TYPE_PP(p6) == IS_ARRAY) { zval **val;
« previous php.cvs (#20539) next »