First step to make the stomp.c/stomp.h more reusable

v1.1
Pierrick Charron 2009-12-28 01:01:07 +00:00
parent b986c891d8
commit 7c135b9664
3 changed files with 48 additions and 25 deletions

View File

@ -453,14 +453,21 @@ PHP_FUNCTION(stomp_connect)
}
}
stomp = stomp_init(url_parts->host, url_parts->port ? url_parts->port : 61613, STOMP_G(read_timeout_sec), STOMP_G(read_timeout_usec));
php_url_free(url_parts);
stomp = stomp_init();
#if HAVE_STOMP_SSL
stomp->use_ssl = use_ssl;
#endif
#endif
if ((stomp->status = stomp_connect(stomp TSRMLS_CC))) {
stomp->options.read_timeout_sec = STOMP_G(read_timeout_sec);
stomp->options.read_timeout_usec = STOMP_G(read_timeout_usec);
stomp->options.connect_timeout_sec = STOMP_G(connection_timeout_sec);
stomp->options.connect_timeout_usec = STOMP_G(connection_timeout_usec);
stomp->status = stomp_connect(stomp, url_parts->host, url_parts->port ? url_parts->port : 61613 TSRMLS_CC);
php_url_free(url_parts);
if (stomp->status) {
stomp_frame_t *res;
stomp_frame_t frame = {0};
@ -1030,8 +1037,8 @@ PHP_FUNCTION(stomp_set_read_timeout)
ZEND_FETCH_RESOURCE(stomp, stomp_t *, &arg, -1, PHP_STOMP_RES_NAME, le_stomp);
}
stomp->read_timeout_sec = sec;
stomp->read_timeout_usec = usec;
stomp->options.read_timeout_sec = sec;
stomp->options.read_timeout_usec = usec;
}
/* }}} */
@ -1053,8 +1060,8 @@ PHP_FUNCTION(stomp_get_read_timeout)
}
array_init(return_value);
add_assoc_long_ex(return_value, "sec", sizeof("sec"), stomp->read_timeout_sec);
add_assoc_long_ex(return_value, "usec", sizeof("usec"), stomp->read_timeout_usec);
add_assoc_long_ex(return_value, "sec", sizeof("sec"), stomp->options.read_timeout_sec);
add_assoc_long_ex(return_value, "usec", sizeof("usec"), stomp->options.read_timeout_usec);
}
/* }}} */

35
stomp.c
View File

@ -35,24 +35,23 @@ extern zend_class_entry *stomp_ce_exception;
/* {{{ stomp_init
*/
stomp_t *stomp_init(const char *host, unsigned short port, long read_timeout_sec, long read_timeout_usec)
stomp_t *stomp_init()
{
/* Memory allocation for the stomp */
stomp_t *stomp = (stomp_t *) emalloc(sizeof(stomp_t));
memset(stomp, 0, sizeof(*stomp));
/* Define all values */
stomp->host = (char *) emalloc(strlen(host) + 1);
memcpy(stomp->host, host, strlen(host));
stomp->host[strlen(host)] = '\0';
stomp->port = port;
stomp->host = NULL;
stomp->port = 0;
stomp->status = 0;
stomp->error = NULL;
stomp->errnum = 0;
stomp->read_timeout_sec = read_timeout_sec;
stomp->read_timeout_usec = read_timeout_usec;
stomp->session = NULL;
stomp->options.connect_timeout_sec = 2;
stomp->options.connect_timeout_usec = 0;
stomp->options.read_timeout_sec = 2;
stomp->options.read_timeout_usec = 2;
#if HAVE_STOMP_SSL
stomp->ssl_handle = NULL;
@ -79,15 +78,25 @@ void stomp_set_error(stomp_t *stomp, const char *error, int errnum)
/* {{{ stomp_connect
*/
int stomp_connect(stomp_t *stomp TSRMLS_DC)
int stomp_connect(stomp_t *stomp, const char *host, unsigned short port TSRMLS_DC)
{
char error[1024];
socklen_t size;
struct timeval tv;
fd_set rfds;
tv.tv_sec = STOMP_G(connection_timeout_sec);
tv.tv_usec = STOMP_G(connection_timeout_usec);
if (stomp->host != NULL)
{
efree(stomp->host);
}
stomp->host = (char *) emalloc(strlen(host) + 1);
memcpy(stomp->host, host, strlen(host));
stomp->host[strlen(host)] = '\0';
stomp->port = port;
tv.tv_sec = stomp->options.connect_timeout_sec;
tv.tv_usec = stomp->options.connect_timeout_usec;
stomp->fd = php_network_connect_socket_to_host(stomp->host, stomp->port, SOCK_STREAM, 0, &tv, NULL, NULL, NULL, 0 TSRMLS_CC);
if (stomp->fd == -1) {
@ -516,8 +525,8 @@ int stomp_select(stomp_t *stomp)
struct timeval tv;
fd_set rfds;
tv.tv_sec = stomp->read_timeout_sec;
tv.tv_usec = stomp->read_timeout_usec;
tv.tv_sec = stomp->options.read_timeout_sec;
tv.tv_usec = stomp->options.read_timeout_usec;
FD_ZERO(&rfds);
FD_SET(stomp->fd, &rfds);

15
stomp.h
View File

@ -35,16 +35,22 @@
ALLOC_HASHTABLE(f->headers); \
zend_hash_init(f->headers, 0, NULL, NULL, 0);
typedef struct _stomp_options {
long connect_timeout_sec;
long connect_timeout_usec;
long read_timeout_sec;
long read_timeout_usec;
} stomp_options_t;
typedef struct _stomp {
php_socket_t fd;
php_sockaddr_storage localaddr;
stomp_options_t options;
char *host;
unsigned short port;
int status;
char *error;
int errnum;
long read_timeout_sec;
long read_timeout_usec;
char *session;
#if HAVE_STOMP_SSL
SSL *ssl_handle;
@ -52,6 +58,7 @@ typedef struct _stomp {
#endif
} stomp_t;
typedef struct _stomp_frame {
char *command;
int command_length;
@ -60,8 +67,8 @@ typedef struct _stomp_frame {
int body_length;
} stomp_frame_t;
stomp_t *stomp_init(const char *host, unsigned short port, long read_timeout_sec, long read_timeout_usec);
int stomp_connect(stomp_t *stomp TSRMLS_DC);
stomp_t *stomp_init();
int stomp_connect(stomp_t *stomp, const char *host, unsigned short port TSRMLS_DC);
void stomp_close(stomp_t *stomp);
int stomp_send(stomp_t *connection, stomp_frame_t *frame TSRMLS_DC);
stomp_frame_t *stomp_read_frame(stomp_t *connection);