A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
tcp/tcpbuf.cpp
- Committer:
- andrewboyson
- Date:
- 2017-11-13
- Revision:
- 54:84ef2b29cf7e
File content as of revision 54:84ef2b29cf7e:
#include "mbed.h" #include "http.h" #include "io.h" static int currentPositionInMessage; static int bufferPositionInMessage; static int bufferLength; static char* pBuffer; static char* p; static bool currentPositionIsInBuffer() { return currentPositionInMessage >= bufferPositionInMessage && currentPositionInMessage < bufferPositionInMessage + bufferLength; } void TcpBufStart(int position, int mss, char *pData) { currentPositionInMessage = 0; bufferPositionInMessage = position; bufferLength = mss; pBuffer = pData; p = pData; } int TcpBufLength() { return p - pBuffer; } void TcpBufAddChar(char c) { if (currentPositionIsInBuffer()) *p++ = c; currentPositionInMessage++; } void TcpBufFillChar(char c, int length) { while (length > 0) { if (currentPositionIsInBuffer()) *p++ = c; currentPositionInMessage++; length--; } } int TcpBufAddText(const char* text) { const char* start = text; while (*text) { if (currentPositionIsInBuffer()) *p++ = *text; text++; currentPositionInMessage++; } return text - start; } int TcpBufAddV(char *fmt, va_list argptr) { int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null vsprintf(text, fmt, argptr); //Fill the new buffer return TcpBufAddText(text); //Add the text } int TcpBufAddF(char *fmt, ...) { va_list argptr; va_start(argptr, fmt); int size = TcpBufAddV(fmt, argptr); va_end(argptr); return size; } void TcpBufAddData(const char* data, int length) { while (length > 0) { if (currentPositionIsInBuffer()) *p++ = *data; data++; currentPositionInMessage++; length--; } } void TcpBufAddStream(void (*startFunction)(void), int (*enumerateFunction)(void)) { startFunction(); while (true) { int c = enumerateFunction(); if (c == EOF) break; if (currentPositionIsInBuffer()) *p++ = c; currentPositionInMessage++; } }