libfreerdp-utils: add ringbuffer debug output

This commit is contained in:
Marc-André Moreau
2015-01-14 13:15:57 -05:00
parent 580de466fc
commit 98cd697cad
2 changed files with 80 additions and 36 deletions

View File

@@ -876,7 +876,8 @@ BIO *findBufferedBio(BIO *front)
int tls_write_all(rdpTls* tls, const BYTE* data, int length)
{
int status, nchunks, commitedBytes;
int status, nchunks;
int committedBytes;
rdpTcp *tcp;
#ifdef HAVE_POLL_H
struct pollfd pollfds;
@@ -892,7 +893,7 @@ int tls_write_all(rdpTls* tls, const BYTE* data, int length)
if (!bufferedBio)
{
WLog_ERR(TAG, "error unable to retrieve the bufferedBio in the BIO chain");
WLog_ERR(TAG, "error unable to retrieve the bufferedBio in the BIO chain");
return -1;
}
@@ -907,6 +908,7 @@ int tls_write_all(rdpTls* tls, const BYTE* data, int length)
if (!BIO_should_retry(bio))
return -1;
#ifdef HAVE_POLL_H
pollfds.fd = tcp->sockfd;
pollfds.revents = 0;
@@ -922,7 +924,7 @@ int tls_write_all(rdpTls* tls, const BYTE* data, int length)
}
else
{
WLog_ERR(TAG, "weird we're blocked but the underlying is not read or write blocked !");
WLog_ERR(TAG, "weird we're blocked but the underlying is not read or write blocked !");
USleep(10);
continue;
}
@@ -950,7 +952,7 @@ int tls_write_all(rdpTls* tls, const BYTE* data, int length)
}
else
{
WLog_ERR(TAG, "weird we're blocked but the underlying is not read or write blocked !");
WLog_ERR(TAG, "weird we're blocked but the underlying is not read or write blocked !");
USleep(10);
continue;
}
@@ -966,7 +968,7 @@ int tls_write_all(rdpTls* tls, const BYTE* data, int length)
while (TRUE);
/* make sure the output buffer is empty */
commitedBytes = 0;
committedBytes = 0;
while ((nchunks = ringbuffer_peek(&tcp->xmitBuffer, chunks, ringbuffer_used(&tcp->xmitBuffer))))
{
int i;
@@ -981,7 +983,7 @@ int tls_write_all(rdpTls* tls, const BYTE* data, int length)
{
chunks[i].size -= status;
chunks[i].data += status;
commitedBytes += status;
committedBytes += status;
continue;
}
@@ -1013,11 +1015,11 @@ int tls_write_all(rdpTls* tls, const BYTE* data, int length)
}
}
ringbuffer_commit_read_bytes(&tcp->xmitBuffer, commitedBytes);
ringbuffer_commit_read_bytes(&tcp->xmitBuffer, committedBytes);
return length;
out_fail:
ringbuffer_commit_read_bytes(&tcp->xmitBuffer, commitedBytes);
ringbuffer_commit_read_bytes(&tcp->xmitBuffer, committedBytes);
return -1;
}

View File

@@ -23,47 +23,60 @@
#include <string.h>
#include <assert.h>
#include <winpr/crt.h>
#include <freerdp/log.h>
BOOL ringbuffer_init(RingBuffer *rb, size_t initialSize)
#define TAG FREERDP_TAG("utils.ringbuffer")
BOOL ringbuffer_init(RingBuffer* rb, size_t initialSize)
{
rb->buffer = malloc(initialSize);
if (!rb->buffer)
return FALSE;
rb->readPtr = rb->writePtr = 0;
rb->initialSize = rb->size = rb->freeSize = initialSize;
WLog_DBG(TAG, "ringbuffer_init(%p)", rb);
return TRUE;
}
size_t ringbuffer_used(const RingBuffer *ringbuffer)
size_t ringbuffer_used(const RingBuffer* rb)
{
return ringbuffer->size - ringbuffer->freeSize;
return rb->size - rb->freeSize;
}
size_t ringbuffer_capacity(const RingBuffer *ringbuffer)
size_t ringbuffer_capacity(const RingBuffer* rb)
{
return ringbuffer->size;
return rb->size;
}
void ringbuffer_destroy(RingBuffer *ringbuffer)
void ringbuffer_destroy(RingBuffer* rb)
{
free(ringbuffer->buffer);
ringbuffer->buffer = 0;
WLog_DBG(TAG, "ringbuffer_destroy(%p)", rb);
free(rb->buffer);
rb->buffer = NULL;
}
static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
static BOOL ringbuffer_realloc(RingBuffer* rb, size_t targetSize)
{
BYTE *newData;
BYTE* newData;
WLog_DBG(TAG, "ringbuffer_realloc(%p): targetSize: %d", rb, targetSize);
if (rb->writePtr == rb->readPtr)
{
/* when no size is used we can realloc() and set the heads at the
* beginning of the buffer
*/
newData = (BYTE *)realloc(rb->buffer, targetSize);
newData = (BYTE*) realloc(rb->buffer, targetSize);
if (!newData)
return FALSE;
rb->readPtr = rb->writePtr = 0;
rb->buffer = newData;
}
@@ -77,7 +90,8 @@ static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
* v v
* [............|XXXXXXXXXXXXXX|..........]
*/
newData = (BYTE *)realloc(rb->buffer, targetSize);
newData = (BYTE*) realloc(rb->buffer, targetSize);
if (!newData)
return FALSE;
@@ -88,9 +102,11 @@ static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
/* in case of malloc the read head is moved at the beginning of the new buffer
* and the write head is set accordingly
*/
newData = (BYTE *)malloc(targetSize);
newData = (BYTE*) malloc(targetSize);
if (!newData)
return FALSE;
if (rb->readPtr < rb->writePtr)
{
/* readPtr writePtr
@@ -107,12 +123,15 @@ static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
* v v
* [XXXXXXXXXXXX|..............|XXXXXXXXXX]
*/
BYTE *dst = newData;
BYTE* dst = newData;
memcpy(dst, rb->buffer + rb->readPtr, rb->size - rb->readPtr);
dst += (rb->size - rb->readPtr);
if (rb->writePtr)
memcpy(dst, rb->buffer, rb->writePtr);
}
rb->writePtr = rb->size - rb->freeSize;
rb->readPtr = 0;
free(rb->buffer);
@@ -121,6 +140,7 @@ static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
rb->freeSize += (targetSize - rb->size);
rb->size = targetSize;
return TRUE;
}
@@ -131,11 +151,13 @@ static BOOL ringbuffer_realloc(RingBuffer *rb, size_t targetSize)
* @param sz
* @return
*/
BOOL ringbuffer_write(RingBuffer *rb, const BYTE *ptr, size_t sz)
BOOL ringbuffer_write(RingBuffer* rb, const BYTE* ptr, size_t sz)
{
size_t toWrite;
size_t remaining;
WLog_DBG(TAG, "ringbuffer_write(%p): sz: %d", rb, sz);
if ((rb->freeSize <= sz) && !ringbuffer_realloc(rb, rb->size + sz))
return FALSE;
@@ -147,6 +169,7 @@ BOOL ringbuffer_write(RingBuffer *rb, const BYTE *ptr, size_t sz)
*/
toWrite = sz;
remaining = sz;
if (rb->size - rb->writePtr < sz)
toWrite = rb->size - rb->writePtr;
@@ -166,9 +189,10 @@ BOOL ringbuffer_write(RingBuffer *rb, const BYTE *ptr, size_t sz)
return TRUE;
}
BYTE *ringbuffer_ensure_linear_write(RingBuffer *rb, size_t sz)
BYTE* ringbuffer_ensure_linear_write(RingBuffer* rb, size_t sz)
{
WLog_DBG(TAG, "ringbuffer_ensure_linear_write(%p): sz: %d", rb, sz);
if (rb->freeSize < sz)
{
if (!ringbuffer_realloc(rb, rb->size + sz - rb->freeSize + 32))
@@ -177,7 +201,7 @@ BYTE *ringbuffer_ensure_linear_write(RingBuffer *rb, size_t sz)
if (rb->writePtr == rb->readPtr)
{
rb->writePtr = rb->readPtr = 0;
rb->writePtr = rb->readPtr = NULL;
}
if (rb->writePtr + sz < rb->size)
@@ -196,28 +220,40 @@ BYTE *ringbuffer_ensure_linear_write(RingBuffer *rb, size_t sz)
return rb->buffer + rb->writePtr;
}
BOOL ringbuffer_commit_written_bytes(RingBuffer *rb, size_t sz)
BOOL ringbuffer_commit_written_bytes(RingBuffer* rb, size_t sz)
{
WLog_DBG(TAG, "ringbuffer_commit_written_bytes(%p): sz: %d", rb, sz);
if (sz < 1)
return TRUE;
if (rb->writePtr + sz > rb->size)
return FALSE;
rb->writePtr = (rb->writePtr + sz) % rb->size;
rb->freeSize -= sz;
return TRUE;
}
int ringbuffer_peek(const RingBuffer *rb, DataChunk chunks[2], size_t sz)
int ringbuffer_peek(const RingBuffer* rb, DataChunk chunks[2], size_t sz)
{
size_t remaining = sz;
size_t toRead;
int chunkIndex = 0;
int ret = 0;
int status = 0;
WLog_DBG(TAG, "ringbuffer_peek(%p): sz: %d", rb, sz);
if (rb->size - rb->freeSize < sz)
if (sz < 1)
return 0;
if ((rb->size - rb->freeSize) < sz)
remaining = rb->size - rb->freeSize;
toRead = remaining;
if (rb->readPtr + remaining > rb->size)
if ((rb->readPtr + remaining) > rb->size)
toRead = rb->size - rb->readPtr;
if (toRead)
@@ -226,20 +262,26 @@ int ringbuffer_peek(const RingBuffer *rb, DataChunk chunks[2], size_t sz)
chunks[0].size = toRead;
remaining -= toRead;
chunkIndex++;
ret++;
status++;
}
if (remaining)
{
chunks[chunkIndex].data = rb->buffer;
chunks[chunkIndex].size = remaining;
ret++;
status++;
}
return ret;
return status;
}
void ringbuffer_commit_read_bytes(RingBuffer *rb, size_t sz)
void ringbuffer_commit_read_bytes(RingBuffer* rb, size_t sz)
{
WLog_DBG(TAG, "ringbuffer_commit_read_bytes(%p): sz: %d", rb, sz);
if (sz < 1)
return;
assert(rb->size - rb->freeSize >= sz);
rb->readPtr = (rb->readPtr + sz) % rb->size;