Mercurial > hg > nnchat
changeset 434:691400f1c9bb
Add function for dumping current network buffer.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 25 May 2012 20:18:24 +0300 |
parents | edd67b882271 |
children | 708a15fdf791 |
files | network.c network.h |
diffstat | 2 files changed, 41 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/network.c Fri May 25 20:16:05 2012 +0300 +++ b/network.c Fri May 25 20:18:24 2012 +0300 @@ -550,3 +550,41 @@ return nn_conn_send_msg(conn, user, tmp); } + + +void nn_conn_dump_buffer(FILE *f, nn_conn_t *conn) +{ + char *p; + size_t offs, left; + + fprintf(f, + "\n--------------------------------------------------------------\n" + "err=%d, status=%d, got_bytes=%d, total_bytes=%d\n" + "buf=0x%p, in_ptr=0x%04x, ptr=0x%04x\n", + conn->err, conn->status, conn->got_bytes, conn->total_bytes, + conn->buf, conn->in_ptr - conn->buf, conn->ptr - conn->buf); + + // Dump buffer contents as a hexdump + for (offs = 0, left = conn->total_bytes, p = conn->buf; p < conn->in_ptr;) + { + char buf[NN_DUMP_BYTES + 1]; + size_t bufoffs, amount = left < NN_DUMP_BYTES ? left : NN_DUMP_BYTES; + left -= amount; + + // Dump offs | xx xx xx xx | and fill string + fprintf(f, "%04x | ", offs); + for (bufoffs = 0; bufoffs < amount; offs++, bufoffs++, p++) + { + fprintf(f, "%02x ", *p); + buf[bufoffs] = th_isprint(*p) ? *p : '.'; + } + buf[bufoffs] = 0; + + // Add padding + for (; bufoffs < NN_DUMP_BYTES; bufoffs++) + fprintf(f, " "); + + // Print string + fprintf(f, "| %s\n", buf); + } +}
--- a/network.h Fri May 25 20:16:05 2012 +0300 +++ b/network.h Fri May 25 20:18:24 2012 +0300 @@ -29,6 +29,7 @@ #define NN_CONNBUF_SIZE (64 * 1024) #define NN_DELAY_USEC (15 * 1000) +#define NN_DUMP_BYTES 16 enum @@ -127,4 +128,6 @@ int nn_conn_buf_strcmp(nn_conn_t *conn, const char *str); char * nn_conn_buf_strstr(nn_conn_t *conn, const char *str); +void nn_conn_dump_buffer(FILE *f, nn_conn_t *conn); + #endif