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 pipeDataToSo(LPVOID pVClient) { char *buf = malloc(PIPE_BUF_SIZE); ClientElem_t *pClient = (ClientElem_t *)pVClient; while (pClient->running) { int nBytesRead = rbRead(&pClient->rbSeSo, buf, PIPE_BUF_SIZE, 1000); if (nBytesRead > 0) DBG_PRINT("->SO got %i bytes from buf", nBytesRead); else DBG_PRINT("->SO"); if (nBytesRead < 0) { DBG_PRINT("ERROR uartrdbuf pipe"); pClient->running = 0; } else if (nBytesRead > 0) { int wrIx = 0; int wrLen = 0; while (nBytesRead > 0 && wrLen >= 0 && pClient->running) { wrLen = send(pClient->sockfd, (const char*)&buf[wrIx], nBytesRead, 0); if (wrLen == SOCKET_ERROR) { DBG_PRINT("ERROR sockwrite pipe"); pClient->running = 0; } else { wrIx += wrLen; nBytesRead -= wrLen; } } } } free(buf); DBG_PRINT("X->SO"); return 0; }
static DWORD WINAPI pipeDataToSe(LPVOID pVClient) { char *buf = malloc(PIPE_BUF_SIZE); ClientElem_t *pClient = (ClientElem_t *)pVClient; while (pClient->running) { int nBytesRead = rbRead(&pClient->rbSoSe, buf, PIPE_BUF_SIZE, 1000); if (nBytesRead > 0) DBG_PRINT("->SE got %i bytes from buf", nBytesRead); else DBG_PRINT("->SE"); if (nBytesRead < 0) { DBG_PRINT("ERROR uartrdbuf pipe"); pClient->running = 0; } else if (nBytesRead > 0) { int wrIx = 0; int wrLen = 0; while (nBytesRead > 0 && wrLen >= 0 && pClient->running) { if (!writeSerial(pClient, (const char*)&buf[wrIx], nBytesRead, &wrLen, 1000)) { DBG_PRINT("ERROR uartwrite pipe %ld, hdl %ld", GetLastError(), pClient->serialHdl); pClient->running = 0; } else { wrIx += wrLen; nBytesRead -= wrLen; } } } } free(buf); DBG_PRINT("X->SE"); return 0; }