Пример #1
0
void Connection::onDataAvailable(const SocketClient::SharedPtr&, Buffer&& buf)
{
    while (true) {
        if (!buf.isEmpty())
            mBuffers.push_back(std::move(buf));

        unsigned int available = bufferSize(mBuffers);
        if (!available)
            break;
        if (!mPendingRead) {
            if (available < static_cast<int>(sizeof(uint32_t)))
                break;
            char buf[sizeof(uint32_t)];
            const int read = bufferRead(mBuffers, buf, 4);
            assert(read == 4);
            Deserializer strm(buf, read);
            strm >> mPendingRead;
            assert(mPendingRead > 0);
            available -= 4;
        }
        assert(mPendingRead >= 0);
        if (available < static_cast<unsigned int>(mPendingRead))
            break;
        char buf[1024];
        char *buffer = buf;
        if (mPendingRead > static_cast<int>(sizeof(buf))) {
            buffer = new char[mPendingRead];
        }
        const int read = bufferRead(mBuffers, buffer, mPendingRead);
        assert(read == mPendingRead);
        mPendingRead = 0;
        std::shared_ptr<Message> message = Message::create(mVersion, buffer, read);
        if (message) {
            auto that = shared_from_this();
            if (message->messageId() == FinishMessage::MessageId) {
                mFinishStatus = std::static_pointer_cast<FinishMessage>(message)->status();
                mFinished(that, mFinishStatus);
            } else if (message->messageId() == ConnectMessage::MessageId) {
                mIsConnected = true;
            } else {
                newMessage()(message, that);
            }
        }
        if (buffer != buf)
            delete[] buffer;
        if (!message)
            mSocketClient->close();
    // mClient->dataAvailable().disconnect(this, &Connection::dataAvailable);
    }
}
Пример #2
0
uint16_t Framebuffer::bgColorAt(int16_t x, int16_t y) {
    if (x < 0 || y < 0 || x >= _width || y >= _height) {
        return 0;
    }
    uint32_t pos = y * _width + x;
    return palette[bufferRead(pos)];
}
Пример #3
0
void Connection::onDataAvailable(const SocketClient::SharedPtr&, Buffer&& buf)
{
    while (true) {
        if (!buf.isEmpty())
            mBuffers.push_back(std::move(buf));

        unsigned int available = bufferSize(mBuffers);
        if (!available)
            break;
        if (!mPendingRead) {
            if (available < static_cast<int>(sizeof(uint32_t)))
                break;
            union {
                char buf[sizeof(uint32_t)];
                int pending;
            };
            const int read = bufferRead(mBuffers, buf, 4);
            assert(read == 4);
            mPendingRead = pending;
            assert(mPendingRead > 0);
            available -= read;
        }
        assert(mPendingRead >= 0);
        if (available < static_cast<unsigned int>(mPendingRead))
            break;

        StackBuffer<1024 * 16> buffer(mPendingRead);
        const int read = bufferRead(mBuffers, buffer, mPendingRead);
        assert(read == mPendingRead);
        mPendingRead = 0;
        std::shared_ptr<Message> message = Message::create(mVersion, buffer, read);
        if (message) {
            auto that = shared_from_this();
            if (message->messageId() == FinishMessage::MessageId) {
                mFinishStatus = std::static_pointer_cast<FinishMessage>(message)->status();
                mFinished(that, mFinishStatus);
            } else {
                newMessage()(message, that);
            }
        } else {
            ::error() << "Unable to create message from data" << read;
        }
        if (!message)
            mSocketClient->close();
    // mClient->dataAvailable().disconnect(this, &Connection::dataAvailable);
    }
}
Пример #4
0
uint16_t Framebuffer::colorAt(int16_t x, int16_t y) {
    if (x < 0 || y < 0 || x >= _width || y >= _height) {
        return 0;
    }
    struct sprite *s = spriteAt(x, y);
    if (s) {
        uint32_t offset = s->width * s->height * s->currentframe;
        uint8_t color = s->data[offset + (y - s->ypos) * s->width + (x - s->xpos)];
        return palette[color];
    }

    uint32_t pos = y * _width + x;
    return palette[bufferRead(pos)];
}
Пример #5
0
int fcgi_accept(int argc, char* argv[])
{
//	fprintf(stderr, "count = %d\n", ++count);
	
	if(!is_init)
		fcgi_init(argc, argv);

	close(STDIN_FILENO);
	close(STDOUT_FILENO);

	int connfd = tcpAccept(NEW_FCGI_LISTEN_FD);
//	fprintf(stderr, "connfd = %d\n", connfd);
	assert(connfd >= 0);
	setNonBlock(connfd, false);

	struct Buffer* input = newBuffer();

//	fprintf(stderr, "before read\n");
	int n = bufferRead(input, connfd);
	fprintf(stderr, "n = %d\n", n);


//	fprintf(stderr, "after read\n");
	char* line;

//init environment
	while( (line = readLine(input)) != NULL && line[0] != '\0')
	{
		if(strlen(line) > 0)
		{
//			fprintf(stderr, "%s\n", line);
			putenv(line);
		}
	}
	
	freeBuffer(input);

	dup2(connfd, STDIN_FILENO);
	dup2(connfd, STDOUT_FILENO);

	setbuf(stdin, NULL);
	setbuf(stdout, NULL);
//	fprintf(stderr, "fcgi accept over. \n");

	return 0;
}
Пример #6
0
/**
 * Buffers input sent by peer. May be called multiple times until the entire
 * buffer is filled. Returns true when the buffer is full.
 */
static bool peerProxyBufferInput(PeerProxy* peerProxy) {
    Buffer* in = peerProxy->inputBuffer;
    ssize_t size = bufferRead(in, peerProxy->fd->fd);
    if (size < 0) {
        peerProxyHandleError(peerProxy, "read");
        return false;
    } else if (size == 0) {
        // EOF.
    	ALOGI("EOF");
        peerProxyKill(peerProxy, false);
        return false;
    } else if (bufferReadComplete(in)) {
        // We're done!
        return true;
    } else {
        // Continue reading.
        return false;
    }
}
Пример #7
0
void Framebuffer::copyRect(int16_t dx, int16_t dy, int16_t sx, int16_t sy, uint16_t w, uint16_t h) {
    uint32_t dpos;
    uint32_t spos;
    translateCoordinates(&dx, &dy);
    for (int y = 0; y < w; y++) {
        for (int x = 0; x < h; x++) {
            if (sx > dx && sy > dy) {
                dpos = (y + dy) * _width + (x + dx);
                spos = (y + sy) * _width + (x + sx);
            } else if(sx <= dx && sy > dy) {
                dpos = (y + dy) * _width + ((w - x) + dx);
                spos = (y + sy) * _width + ((w - x) + sx);
            } else if(sx > dx && sy <= dy) {
                dpos = ((h - y) + dy) * _width + (x + dx);
                spos = ((h - y) + sy) * _width + (x + sx);
            } else if(sx <= dx && sy <= dy) {
                dpos = ((h - y) + dy) * _width + ((w - x) + dx);
                spos = ((h - y) + sy) * _width + ((w - x) + sx);
            }
            bufferWrite(dpos, bufferRead(spos));
        }
    }
}
int main(void) {

	initUSART();
	_delay_ms(200);

	uint8_t i;
	uint8_t tempCharStorage;

	while (1) {

		dumpBuffer();
		printString("  Demo: adding characters to buffer\n");
		uint8_t coolString[] = "Howdy";
		i = 0;
		while(i < sizeof(coolString) - 1){
			bufferWrite(coolString[i]);
			++i;
			dumpBuffer();
		}

		printString("  Demo: reading out the first three characters\n");
		for (i = 0; i<3 ; i++){	
			bufferRead(&tempCharStorage);
			transmitByte(tempCharStorage);	
			printString("\n");
			dumpBuffer();
		}

		printString("  Demo: adding more characters to buffer\n");
		uint8_t anotherString[] = "Hello";	
		i = 0;
		while(i < sizeof(anotherString) - 1){
			bufferWrite(anotherString[i]);
			++i;
			dumpBuffer();
		}

		// And read it back out using return code
		printString("  Demo: reading everything back out\n");
		while (bufferRead(&tempCharStorage) == BUFFER_OK){
			transmitByte(tempCharStorage);	
		}
		printString("\n");
		
		dumpBuffer();
		printString("  Demo: empty! (newest = oldest)\n");

		// Fill up buffer, using return code
		i = 0;
		while(bufferWrite('A'+i) == BUFFER_OK){
			++i;
			dumpBuffer();
		}

		printString("  Note: never fills up whole buffer\n");
		printString("  it's full when first index equals last\n");

		return 0;

	}                                                  /* End event loop */
	return 0;                              /* This line is never reached */
}
Пример #9
0
static void *processingThreadFun(void *param)
{
    (void) param;

    while(!end_flag) {
        pthread_mutex_lock(&state_mx);
        while((!new_frame_flag) && (!end_flag)) {
            struct timeval now;
            gettimeofday(&now, NULL);
            struct timespec absTime;
            absTime.tv_sec = now.tv_sec + 3;
            absTime.tv_nsec = now.tv_usec * 1000;
            pthread_cond_timedwait(&state_cv, &state_mx, &absTime);
        }
        new_frame_flag = false;
        pthread_mutex_unlock(&state_mx);
//    printf("Processing frame!\n");
        if(isEmpty(reader)) {
//      printf("No new buffer!\n");
        } else {
//      printf("Processing buffer %d @ %p\n", reader, getCurrentBuffer(reader));
            image_t img = {
                .bitmap = getCurrentBuffer(reader),
                .w = width,
                .h = height,
                .ratio = 1.0f
            };
            struct blob_type blobs_array[MAX_BLOBS] = {{0.0f, 0.0f, 0}};
            struct bloblist_type bloblist = {
                .num_blobs = MAX_BLOBS,
                .blobs = blobs_array
            };

            bool res;
#ifdef OPENCV
            if(facetrack) {
                ltr_int_face_detect(&img, &bloblist);
                res = (bloblist.num_blobs == 1);
            } else {
#endif
                ltr_int_to_stripes(&img);
                res = (ltr_int_stripes_to_blobs(MAX_BLOBS, &bloblist, ltr_int_getMinBlob(mmm), ltr_int_getMaxBlob(mmm), &img) == 0);
#ifdef OPENCV
            }
#endif
            if(res) {
                ltr_int_setBlobs(mmm, blobs_array, bloblist.num_blobs);
                if(!ltr_int_getFrameFlag(mmm)) {
//	  printf("Copying buffer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
                    memcpy(ltr_int_getFramePtr(mmm), img.bitmap, width * height);
                    ltr_int_setFrameFlag(mmm);
                }
            }
            bufferRead(&reader);
        }
    }
#ifdef OPENCV
    if(facetrack) {
        ltr_int_stop_face_detect();
    }
    ltr_int_cleanup_after_processing();
#endif
    return NULL;
}

bool startProcessing(int w, int h, int buffers, struct mmap_s *mmm_p)
{
    mmm = mmm_p;
    new_frame_flag = 0;
    end_flag = false;
    width = w;
    height = h;
    if(!createBuffers(buffers, w * h)) {
//    printf("Problem creating buffers!\n");
        return false;
    }
    ltr_int_prepare_for_processing(w, h);
#ifdef OPENCV
    facetrack = doFacetrack();
    if(facetrack && (!ltr_int_init_face_detect())) {
        return false;
    }
#endif
//  printf("Starting processing thread!\n");
    return 0 == pthread_create(&processing_thread, NULL, processingThreadFun, NULL);
}