int testBuffer() { printf("Ring buffer test:\n\n"); RingBuffer_t rb; rbInit(&rb, 5); printf("Is empty: %d\n", rbIsEmpty(&rb)); int i; for(i = 1; i <= rb.size; i++) { rbWrite(&rb, i); } printf("Is empty: %d\n", rbIsEmpty(&rb)); for(i = 0; i < rb.size; i++) { printf("read value %d:%d\n", i, rbRead(&rb, i)); } printf("\n"); rbWrite(&rb, rb.size+1); for(i = 0; i < rb.size; i++) { printf("read value %d:%d\n", i, rbRead(&rb, i)); } printf("\nfree\n"); rbFree(&rb); printf("Is empty: %d\n", rbIsEmpty(&rb)); return 0; }
static DWORD WINAPI pipeDataFrSe(LPVOID pVClient) { char *buf = malloc(PIPE_BUF_SIZE); ClientElem_t *pClient = (ClientElem_t *)pVClient; while (pClient->running) { int nBytesRead; if (!readSerial(pClient, &buf[0], PIPE_BUF_SIZE, &nBytesRead, 1000)) { DBG_PRINT("ERROR uartread pipe %ld, hdl %ld", GetLastError(), pClient->serialHdl); pClient->running = 0; } else if (nBytesRead > 0) { int wrIx = 0; int wrLen = 0; DBG_PRINT("SE-> put %i bytes in buf", nBytesRead); while (nBytesRead > 0 && wrLen >= 0 && pClient->running) { wrLen = rbWrite(&pClient->rbSeSo, &buf[wrIx], nBytesRead, 1000); nBytesRead -= wrLen; wrIx += wrLen; } if (wrLen < 0) { DBG_PRINT("ERROR uartwrbuf pipe"); pClient->running = 0; } } } free(buf); DBG_PRINT("SE->X"); return 0; }
static void pipeDataFrSo(ClientElem_t *pClient) { struct timeval time; fd_set set; char *buf = malloc(PIPE_BUF_SIZE); while (pClient->running) { FD_ZERO(&set); FD_SET(pClient->sockfd, &set); time.tv_sec = 1; time.tv_usec = 0; if (select(0, &set, NULL, NULL, &time) > 0) { int nBytesRead = 0; nBytesRead = recv(pClient->sockfd, &buf[0], PIPE_BUF_SIZE, 0); if (nBytesRead == SOCKET_ERROR) { DBG_PRINT("ERROR sockread pipe"); pClient->running = 0; } else if (nBytesRead > 0) { int wrIx = 0; int wrLen = 0; DBG_PRINT("SO-> put %i bytes in buf", nBytesRead); while (nBytesRead > 0 && wrLen >= 0 && pClient->running) { wrLen = rbWrite(&pClient->rbSoSe, &buf[wrIx], nBytesRead, 1000); nBytesRead -= wrLen; wrIx += wrLen; } if (wrLen < 0) { DBG_PRINT("ERROR sockwrbuf pipe"); pClient->running = 0; } } } } DBG_PRINT("SO->X"); free(buf); }