Exemple #1
0
void do_packet_capture(void)
{
//    int 				ret;
    static uint8_t 		*bottom = NULL;
    static uint8_t 		*top = NULL;
    ULONG total_read, 	buffer_len, read_size, diff;

    /*
     * The main Dag capture loop, wait for data and deliver.
     */
    while( loop_continue )
    {
#if 0
        top = dag_advance_stream(dag_fd, dag_devnum, &bottom);
        if(top == NULL) {
            dagutil_panic("dag_advance_stream %s:%u: %s\n", dagname, dag_devnum, strerror(errno));
            log_print(LOGN_CRI, "%s: FAIL[dag_start_stream] [%s]", __FUNCTION__, strerror(errno));
            close_device();
            exit(errno);
        }
#endif

        diff = top - bottom;
        if( diff > 0 ) {
            /* read multiple packets */
            buffer_len = diff;
            total_read = 0;
            while( total_read < diff )
            {
                read_size = read_one_packet(&bottom[total_read], buffer_len, 0 );
                if( read_size < 0 ) {
                    close_device();
                    exit(-1);
                }
                else if( read_size == 0 )
                    break;

                total_read += read_size;
                buffer_len -= read_size;
            }

            bottom += total_read;
        }
        else {
#if 0
            curTime = time(NULL);
            if(oldTime + 5 < curTime) {
                if(Send_Node_Cnt && Send_Node_Cnt == Diff_Node_Cnt) {
                    /* Send Buffring Packet  */
                    if((ret = Send_CAPD_Data(pstMEMSINFO, myqid, NULL, 0)) < 0) {
                        log_print(LOGN_CRI, "[%s.%d] Send_CAPD_Data [%d][%s]", __FUNCTION__, __LINE__, ret, strerror(-ret));
                    }
                    Collection_Cnt = 50;    // COLLECTIONCNT_MIN 으로 설정
                }
                Diff_Node_Cnt = Send_Node_Cnt;
                oldTime = curTime;
            }
#endif
            usleep(0);
        }

        /* CHECK ACTIVE & STANDBY STATUS */
        curTime = time(NULL);
        if( chkTime != curTime ) {

            if( stPortStatus[0].uiLastCnt == stPortStatus[0].uiCurrCnt ) {
                stPortStatus[0].uiRetryCnt++;

                if( stPortStatus[0].uiRetryCnt > 2 ) {
                    fidb->mirrorsts[0] = CRITICAL;

                    if( fidb->mirrorActsts[0] == DEF_ACTIVE && fidb->mirrorsts[1] != CRITICAL ) {
                        fidb->mirrorActsts[0] = DEF_STANDBY;
                        fidb->mirrorActsts[1] = DEF_ACTIVE;

                        log_print( LOGN_CRI, "CHANGE ACTIVE 0 -> 1..");
                    }
                }
            }
            else {
                fidb->mirrorsts[0] = NORMAL;
                stPortStatus[0].uiRetryCnt = 0;
                stPortStatus[0].uiLastCnt = stPortStatus[0].uiCurrCnt;
            }

            if( stPortStatus[1].uiLastCnt == stPortStatus[1].uiCurrCnt ) {
                stPortStatus[1].uiRetryCnt++;

                if( stPortStatus[1].uiRetryCnt > 2 ) {
                    fidb->mirrorsts[1] = CRITICAL;

                    if( fidb->mirrorActsts[1] == DEF_ACTIVE && fidb->mirrorsts[0] != CRITICAL ) {
                        fidb->mirrorActsts[1] = DEF_STANDBY;
                        fidb->mirrorActsts[0] = DEF_ACTIVE;

                        log_print( LOGN_CRI, "CHANGE ACTIVE 1 -> 0..");
                    }
                }
            }
            else {
                fidb->mirrorsts[1] = NORMAL;
                stPortStatus[1].uiRetryCnt = 0;
                stPortStatus[1].uiLastCnt = stPortStatus[1].uiCurrCnt;
            }

            chkTime = curTime;

            log_print( LOGN_DEBUG, "[0] STS:0X%02X ACT:%u [1] STS:0X%02X ACT:%u",
                       fidb->mirrorsts[0], fidb->mirrorActsts[0], fidb->mirrorsts[1], fidb->mirrorActsts[1] );
        }
    }
}
Exemple #2
0
MVMint64 socket_read_bytes(MVMThreadContext *tc, MVMOSHandle *h, char **buf, MVMint64 bytes) {
    MVMIOSyncSocketData *data = (MVMIOSyncSocketData *)h->body.data;
    char *use_last_packet = NULL;
    MVMuint16 use_last_packet_start, use_last_packet_end;

    /* If at EOF, nothing more to do. */
    if (data->eof) {
        *buf = NULL;
        return 0;
    }

    /* See if there's anything in the packet buffer. */
    if (data->last_packet) {
        MVMuint16 last_remaining = data->last_packet_end - data->last_packet_start;
        if (bytes <= last_remaining) {
            /* There's enough, and it's sufficient for the request. Extract it
             * and return, discarding the last packet buffer if we drain it. */
            *buf = MVM_malloc(bytes);
            memcpy(*buf, data->last_packet + data->last_packet_start, bytes);
            if (bytes == last_remaining) {
                MVM_free(data->last_packet);
                data->last_packet = NULL;
            }
            else {
                data->last_packet_start += bytes;
            }
            return bytes;
        }
        else {
            /* Something, but not enough. Take the last packet for use, then
             * we'll read another one. */
            use_last_packet = data->last_packet;
            use_last_packet_start = data->last_packet_start;
            use_last_packet_end = data->last_packet_end;
            data->last_packet = NULL;
        }
    }

    /* If we get here, we need to read another packet. */
    read_one_packet(tc, data);

    /* Now assemble the result. */
    if (data->last_packet && use_last_packet) {
        /* Need to assemble it from two places. */
        MVMuint32 last_available = use_last_packet_end - use_last_packet_start;
        MVMuint32 available = last_available + data->last_packet_end;
        if (bytes > available)
            bytes = available;
        *buf = MVM_malloc(bytes);
        memcpy(*buf, use_last_packet + use_last_packet_start, last_available);
        memcpy(*buf + last_available, data->last_packet, bytes - last_available);
        if (bytes == available) {
            /* We used all of the just-read packet. */
            MVM_free(data->last_packet);
            data->last_packet = NULL;
        }
        else {
            /* Still something left in the just-read packet for next time. */
            data->last_packet_start += bytes - last_available;
        }
    }
    else if (data->last_packet) {
        /* Only data from the just-read packet. */
        if (bytes >= data->last_packet_end) {
            /* We need all of it, so no copying needed, just hand it back. */
            *buf = data->last_packet;
            bytes = data->last_packet_end;
            data->last_packet = NULL;
        }
        else {
            /* Only need some of it. */
            *buf = MVM_malloc(bytes);
            memcpy(*buf, data->last_packet, bytes);
            data->last_packet_start += bytes;
        }
    }
    else if (use_last_packet) {
        /* Nothing read this time, so at the end. Drain previous packet data
         * and mark EOF. */
        bytes = use_last_packet_end - use_last_packet_start;
        *buf = MVM_malloc(bytes);
        memcpy(*buf, use_last_packet + use_last_packet_start, bytes);
        data->eof = 1;
    }
    else {
        /* Nothing to hand back; at EOF. */
        *buf = NULL;
        bytes = 0;
        data->eof = 1;
    }

    return bytes;
}