Added HTTP API to C027_Support library.
Fork of C027_Support by
Diff: Pipe.h
- Revision:
- 9:e7a5959ffae1
- Parent:
- 8:2435cdff8015
- Child:
- 13:e2446fcdc246
--- a/Pipe.h Sat Nov 09 13:31:49 2013 +0000 +++ b/Pipe.h Sun Nov 10 16:39:42 2013 +0000 @@ -7,126 +7,150 @@ inline int _inc(int i, int n = 1) { i += n; - if (i >= s) - i -= s; + if (i >= _s) + i -= _s; return i; } public: - Pipe(int n, T* p = NULL) + Pipe(int n, T* b = NULL) { - a = p ? NULL : new T[n]; - r = 0; - w = 0; - b = p ? p : a; - s = n; + _a = b ? NULL : new T[n]; + _r = 0; + _w = 0; + _b = b ? b : _a; + _s = n; } virtual ~Pipe() { - if (a) - delete [] a; + if (_a) + delete [] _a; } // writing thread bool writeable(void) // = not full { - int i = _inc(w); - return (i = r); + return free() > 0; } int free(void) // number of elements that can be added { - int t = r - w; - if (t <= 0) - t += s; - return t - 1; + int s = _r - _w; + if (s <= 0) + s += _s; + return s - 1; } void putc(T c) { - int i = w; + int i = _w; int j = i; i = _inc(i); - //assert(i != r); - b[j] = c; - w = i; + while (i == _r) // = !writeable() + /*wait for space*/; + _b[j] = c; + _w = i; } - int put(T* p, int n) + int put(const T* p, int n, bool t = false) { - int f = free(); - if (f < n) - n = f; - if (n) + int c = n; + while (c) { - int m = s - w; - if (m > n) - m = n; - memcpy(&b[w], &p[0], m); - int t = n - m; - if (t) + int f; + for (;;) // wait for space { - memcpy(&b[0], &p[m], t); - w = t; + f = free(); + if (f) break; // data avail + if (!t) return n - c; // no more space and not blocking } - else - w = _inc(w,m); + // check free space + if (c < f) f = c; + int w = _w; + int m = _s - w; + // check wrap + if (f > m) f = m; + memcpy(&_b[w], p, f); + _w = _inc(w, f); + c -= f; + p += f; } - return n; + return n - c; } // reading thread // -------------------------------------------------------- //! check if there are any values available bool readable(void) // = not empty { - return (r != w); + return (_r != _w); } //! get the number of values avialable in the buffer virtual int size(void) { - int t = w - r; - if (t < 0) - t += s; - return t; + int s = _w - _r; + if (s < 0) + s += _s; + return s; } //! get a value from buffer (this function will block if no values available) T getc(void) { - //while (r == w) - // /* just wait until w changes*/; - T t = b[r]; - r = _inc(r); + int r = _r; + while (r == _w) // = !readable() + /*wait for data*/; + T t = _b[r]; + _r = _inc(r); return t; } - // get values from buffer (if the buffer has less values, only the values avilable are returned) - virtual int get(T* p, int n) + //! get values from buffer + virtual int get(T* p, int n, bool t = false) { - int f = size(); - if (f < n) - n = f; - if (n) + int c = n; + while (c) { - int m = s - r; - if (m > n) - m = n; - memcpy(&p[0], &b[r], m); - int t = n - m; - if (t) + int f; + for (;;) // wait for data { - memcpy(&p[m] ,&b[0], t); - r = t; + f = size(); + if (f) break; // free space + if (!t) return n - c; // no space and not blocking } - else - r = _inc(r,m); + // check available data + if (c < f) f = c; + int r = _r; + int m = _s - r; + // check wrap + if (f > m) f = m; + memcpy(p, &_b[r], f); + _r = _inc(r, f); + c -= f; + p += f; } - return n; + return n - c; } + // the following functions are useful if you like to inspect or parse the buffer - virtual int start(void) { o = r; return size(); } // reset the parsing index and return the number of available elments - virtual T next(void) { T t = b[o]; o = _inc(o); return t; } // get the next element and increment - virtual void done(void) { r = o; } // commit the index + + //! reset the parsing index and return the number of available elments + virtual int start(void) + { + _o = _r; + return size(); + } + //! get the next element and increment + virtual T next(void) + { + int o = _o; + T t = _b[o]; + _o = _inc(o); + return t; + } + //! commit the index + virtual void done(void) + { + _r = _o; + } private: - // buffer - T* b; //!< buffer - T* a; //!< allocated buffer - int s; //!< size of buffer (s - 1) elements can be stored - volatile int w; //! write index - volatile int r; //! read index - int o; //! offest index used by parsing functions + T* _b; //!< buffer + T* _a; //!< allocated buffer + int _s; //!< size of buffer (s - 1) elements can be stored + volatile int _w; //!< write index + volatile int _r; //!< read index + int _o; //!< offest index used by parsing functions };