Esempio n. 1
0
int testNormalizer() {
	printf("\n\nNormalizer test:\n\n");
	nInit(0, 10, 5, -1);

	nAddValue(1);
	nAddValue(2);
	nAddValue(3);
	printf("normalize: %f\n", nNormalize());

	nAddValue(12);
	printf("normalize: %f\n", nNormalize());

	nAddValue(7);
	printf("normalize: %f\n", nNormalize());

	nFree();
	return 0;
}
Esempio n. 2
0
int udpRead(u_int ud, void* buf, long len)
{
    unsigned char* d;
    int rtn;
    NBuf* b;
    UDP_QUEUE* q;
    struct in_addr fromAddr;
    UBYTE err;

//    TRACE("udpRead(%d,%p,%ld)\n",ud,buf,len);

    if (!(udps[ud].flags & FUDP_OPEN)) return -1;
    d = (unsigned char*)buf;
    rtn = 0;
    OSSemPend(udps[ud].sem, 0, &err);
    if (udps[ud].head == NULL) {
        return -1;
    }
    fromAddr = udps[ud].theirAddr = udps[ud].head->srcAddr;
    udps[ud].theirPort = udps[ud].head->srcPort;

    UDPDEBUG(("fromAddr.s_addr = %08X,   udps[ud].head->srcAddr.s_addr = %08X\n", fromAddr.s_addr, udps[ud].head->srcAddr.s_addr));

    while ((udps[ud].head) && (len) && (fromAddr.s_addr == udps[ud].head->srcAddr.s_addr)) {
        // Get next nBuf
        b = udps[ud].head->nBuf;
        // While we have more buffer to copy out to AND
        // We have a queued nBuf
        while ((len) && (b)) {
        // While we have more buffer space to copy to AND
        // nBuf is not empty
//            while ((len) && (b->data != &b->body[b->len])) {
            while ( (len) && (b->len) )	{
		// Copy one byte
                *d++ = *b->data++;
                b->len--; 
                len--;
                rtn++;
            }
	    // If nBuf is empty
//            if (b->data == &b->body[b->len]) {
            if (!b->len) {
                OS_ENTER_CRITICAL();
                b = udps[ud].head->nBuf = nFree(b);
                OS_EXIT_CRITICAL();
            }
        }
        // If nBuf was freed (we need another one if any)
        if (b == NULL) {
            // Get next nBuf in queue
            q = udps[ud].head;
            OS_ENTER_CRITICAL();
            udps[ud].head = q->next;
            OS_EXIT_CRITICAL();
            free_udp_q(q);
            #if ONETASK_SUPPORT > 0
            #else
            if (udps[ud].head)
                OSSemPend(udps[ud].sem, 0, &err);
            #endif
        } else {
            #if ONETASK_SUPPORT > 0
            // nBuf was not freed but we have filled our buffer (buf) 
            return rtn;
            #else
            OSSemPost(udps[ud].sem);
            #endif
        }
    }
    if (udps[ud].head == NULL)
        udps[ud].tail = NULL;
    return rtn;
}