Skip to content

Commit

Permalink
read buffer implementation on top of stomp_recv
Browse files Browse the repository at this point in the history
  • Loading branch information
adoy committed Nov 21, 2012
1 parent 9e0839f commit 19f47cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
41 changes: 39 additions & 2 deletions stomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ stomp_t *stomp_init()
#endif

stomp->frame_stack = NULL;
stomp->read_buffer.size = 0;
return stomp;
}
/* }}} */
Expand Down Expand Up @@ -336,10 +337,12 @@ int stomp_send(stomp_t *stomp, stomp_frame_t *frame TSRMLS_DC)

/* {{{ stomp_recv
*/
int stomp_recv(stomp_t *stomp, char *msg, size_t length)
static int stomp_recv_no_buffer(stomp_t *stomp, char *msg, const size_t length)
{
int len;

stomp_select(stomp);

#if HAVE_STOMP_SSL
if(stomp->options.use_ssl) {
len = SSL_read(stomp->ssl_handle, msg, length);
Expand All @@ -357,6 +360,40 @@ int stomp_recv(stomp_t *stomp, char *msg, size_t length)
}
return len;
}

int stomp_recv(stomp_t *stomp, char *msg, const size_t length)
{
if (stomp->read_buffer.size == 0) {
if (length >= STOMP_BUFSIZE) {
return stomp_recv_no_buffer(stomp, msg, length);
} else {
int recv_size = stomp_recv_no_buffer(stomp, stomp->read_buffer.buf, STOMP_BUFSIZE);
if (recv_size <= length) {
memcpy(msg, stomp->read_buffer.buf, recv_size);
return recv_size;
} else {
memcpy(msg, stomp->read_buffer.buf, length);
stomp->read_buffer.pos = stomp->read_buffer.buf + length;
stomp->read_buffer.size = recv_size - length;
return length;
}
}
} else if (stomp->read_buffer.size >= length) {
memcpy(msg, stomp->read_buffer.pos, length);
stomp->read_buffer.pos += length;
stomp->read_buffer.size -= length;
return length;
} else {
int len = stomp->read_buffer.size;
memcpy(msg, stomp->read_buffer.pos, stomp->read_buffer.size);
stomp->read_buffer.size = 0;
if (stomp_select_ex(stomp, 0, 0)) {
return len + stomp_recv(stomp, msg + len, length - len);
} else {
return len;
}
}
}
/* }}} */

/* {{{ stomp_read_buffer
Expand Down Expand Up @@ -639,7 +676,7 @@ int stomp_select_ex(stomp_t *stomp, const long int sec, const long int usec)
int n;
struct timeval tv;

if (stomp->frame_stack) {
if (stomp->read_buffer.size || stomp->frame_stack) {
return 1;
}
tv.tv_sec = sec;
Expand Down
5 changes: 5 additions & 0 deletions stomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ typedef struct _stomp {
SSL *ssl_handle;
#endif
stomp_frame_stack_t *frame_stack;
struct {
size_t size;
char buf[STOMP_BUFSIZE];
char *pos;
} read_buffer;
} stomp_t;

stomp_t *stomp_init();
Expand Down

0 comments on commit 19f47cd

Please sign in to comment.