|
Server 0.0.1
HTTP/REST server implementation
|
#include <ctype.h>#include <string.h>#include <sys/types.h>#include "class.h"#include "stream.h"#include "commons.h"
Go to the source code of this file.
Data Structures | |
| struct | Cbuf |
Defines | |
| #define | ECBUFOVFL 100 |
Functions | |
| ssize_t | cbufRead (Cbuf, Stream) |
| ssize_t | cbufWrite (Cbuf, Stream) |
| char * | cbufGetLine (Cbuf, char **) |
| char * | cbufGetData (Cbuf, size_t) |
| char * | cbufSetData (Cbuf, const void *, size_t) |
| void | cbufEmpty (Cbuf) |
| char * | cbufGetRead (Cbuf this) |
| char * | cbufGetWrite (Cbuf this) |
| char * | cbufMemchr (Cbuf this, int c) |
| size_t | cbufAddrIndex (Cbuf this, const void *c) |
| void | cbufIncRead (Cbuf this, size_t n) |
| void | cbufIncWrite (Cbuf this, size_t n) |
| size_t | cbufGetFree (Cbuf this) |
| char | cbufIsEmpty (Cbuf this) |
| void | cbufSkipNonAlpha (Cbuf this) |
| Bool | cbufIsLocked (Cbuf this) |
| void | cbufLock (Cbuf this) |
| void | cbufRelease (Cbuf this) |
My implementation of a ringbuffer. It maps a shared memory object twice directly following thus make it possible to read and write from any position within the buffer without the nasty wrap calculations. This is achived because the same memory region is mapped at the two addresses.
Copyright © 2012 Georg Hopp
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
Definition in file cbuf.h.
| size_t cbufAddrIndex | ( | Cbuf | this, |
| const void * | c | ||
| ) |
Definition at line 28 of file addr_index.c.
{
return c - (const void *)cbufGetRead(this);
}


| void cbufEmpty | ( | Cbuf | ) |
| char* cbufGetData | ( | Cbuf | , |
| size_t | |||
| ) |
Definition at line 29 of file get_data.c.
{
char * ret = cbufGetRead(this);
if (n > this->bused) {
return (char *)-1;
}
cbufIncRead(this, n);
return ret;
}


| size_t cbufGetFree | ( | Cbuf | this | ) |
Definition at line 28 of file get_free.c.
{
return this->bsize - this->bused;
}

| char* cbufGetLine | ( | Cbuf | , |
| char ** | |||
| ) |
Definition at line 30 of file get_line.c.
{
char * nl = cbufMemchr(this, '\n');
char * ret = NULL;
if (NULL != nl) {
size_t len = cbufAddrIndex(this, nl) + 1;
*line_end = nl - 1;
*nl = 0;
*(nl-1) = ('\r' == *(nl-1))? 0 : *(nl-1);
ret = cbufGetRead(this);
cbufIncRead(this, len);
}
return ret;
}


| char* cbufGetRead | ( | Cbuf | this | ) |
Definition at line 26 of file get_read.c.
{
return this->data + this->read;
}

| char* cbufGetWrite | ( | Cbuf | this | ) |
Definition at line 26 of file get_write.c.
{
return this->data + this->write;
}

| void cbufIncRead | ( | Cbuf | this, |
| size_t | n | ||
| ) |
Definition at line 28 of file inc_read.c.
{
this->read += n;
this->read = (this->read >= this->bsize)?
this->read - this->bsize : this->read;
this->bused -= n;
}

| void cbufIncWrite | ( | Cbuf | this, |
| size_t | n | ||
| ) |
Definition at line 28 of file inc_write.c.
{
this->write += n;
this->write = (this->write >= this->bsize)?
this->write - this->bsize : this->write;
this->bused += n;
}

| char cbufIsEmpty | ( | Cbuf | this | ) |
Definition at line 26 of file is_empty.c.
{
return (0 == this->bused);
}

| Bool cbufIsLocked | ( | Cbuf | this | ) |
Definition at line 28 of file is_locked.c.
{
return this->lock;
}

| void cbufLock | ( | Cbuf | this | ) |
| char* cbufMemchr | ( | Cbuf | this, |
| int | c | ||
| ) |
Definition at line 28 of file memchr.c.
{
return memchr(cbufGetRead(this), c, this->bused);
}


Definition at line 32 of file read.c.
{
ssize_t rrsize = 0;
size_t rsize = cbufGetFree(this);
if (0 == rsize) {
errno = ECBUFOVFL;
return -1;
}
rrsize = streamRead(st, cbufGetWrite(this), rsize);
switch (rrsize) {
case 0:
rrsize = -2;
// DROP THROUGH
case -1:
break;
default:
cbufIncWrite(this, rrsize);
break;
}
return rrsize;
}


| void cbufRelease | ( | Cbuf | this | ) |
| char* cbufSetData | ( | Cbuf | , |
| const void * | , | ||
| size_t | |||
| ) |
Definition at line 30 of file set_data.c.
{
char * addr;
if (n > cbufGetFree(this)) {
errno = ECBUFOVFL;
return (char *)-1;
}
addr = memcpy(cbufGetWrite(this), src, n);
cbufIncWrite(this, n);
return addr;
}


| void cbufSkipNonAlpha | ( | Cbuf | this | ) |
Definition at line 28 of file skip_non_alpha.c.
{
while(0 < this->bused && !isalpha(*cbufGetRead(this)))
cbufIncRead(this, 1);
}


Definition at line 31 of file write.c.
{
ssize_t wwsize = 0;
size_t wsize = this->bused;
if (0 == wsize) return 0;
wwsize = streamWrite(st, cbufGetRead(this), wsize);
switch (wwsize) {
case -1:
break;
default:
cbufIncRead(this, wwsize);
break;
}
return wwsize;
}

