예제 #1
0
int lb_readfd(struct LineBuffer* lb, int fd) {
	int n = read(fd, lb->line + lb->head, size_left(lb));
	if (n == 0)
		return EOF;
	if (n < 0)
		return errno;
	return update_r(lb, n);
}
예제 #2
0
int lb_putline(struct LineBuffer* lb, char *buf) {
	int len = strlen(buf);
	int mustadd = (len == 0 || buf[len-1] == '\n') ? 0 : 1;
	if(len + mustadd > size_left(lb))
		return -1;
	lb_readstr(lb, &buf, len);
	char* c = &EOL;
	if(mustadd) lb_readstr(lb, &c, 1);
	return len;
}
    static size_t onRead(char *buffer, size_t size, size_t nitems, void *instream)
    {
        WebRequestInternalState *is_(reinterpret_cast<WebRequestInternalState*>(instream));
        is_->state = HTTP_OPEN;
        if (is_->isAborted)
        {
            is_->state = HTTP_CLOSED;
            return CURL_READFUNC_ABORT;
        }

        // Find the size in bytes.
        size_t real_size(size * nitems);

        // Read as much as we can from the upload buffer queue.
        Deserializer* upload(dynamic_cast<Deserializer*>(is_->upload.Get()));
        size_t size_queued(upload->GetSize());
        size_t size_left(real_size);
        if ((size_left > 0) && (size_queued > 0))
        {
            size_t read_size(std::min(size_queued, size_left));
            upload->Read(buffer, (unsigned int)read_size);
            size_left -= read_size;
        }

        // If we still have bytes to fill, then emit a "upload_chunk" event.
        if (size_left > 0)
        {
            VariantMap eventData;
            eventData.Insert(MakePair(StringHash("upload"), Variant(is_->upload)));
            eventData.Insert(MakePair(StringHash("size"), Variant((unsigned int)size_left)));
            is_->es.SendEvent("upload_chunk", eventData);
        }

        // Read as much as we can from the upload buffer queue (again).
        size_queued = upload->GetSize();
        size_left = real_size;
        if ((size_left > 0) && (size_queued > 0))
        {
            size_t read_size(std::min(size_queued, size_left));
            upload->Read(buffer, (unsigned int)read_size);
            size_left -= read_size;
        }

        // If we still have bytes to fill, then something went wrong, so we should abort.
        if (size_left > 0)
        {
            is_->isAborted = true;
            return CURL_READFUNC_ABORT;
        }

        return real_size;
    }
예제 #4
0
int lb_readstr(struct LineBuffer* lb, char** buf, int len) {
	int n = read_buf(*buf, len, lb->line + lb->head, size_left(lb));
	*buf += n;
	return update_r(lb, n);
}