Example #1
0
bool FiffTag::read_tag(FiffStream* p_pStream, FiffTag::SPtr& p_pTag, qint64 pos)
{
    if (pos >= 0)
    {
        p_pStream->device()->seek(pos);
    }

    p_pTag = FiffTag::SPtr(new FiffTag());

    //
    // Read fiff tag header from stream
    //
    *p_pStream  >> p_pTag->kind;
    *p_pStream  >> p_pTag->type;
    qint32 size;
    *p_pStream  >> size;
    p_pTag->resize(size);
    *p_pStream  >> p_pTag->next;

//    qDebug() << "read_tag" << "  Kind:" << p_pTag->kind << "  Type:" << p_pTag->type << "  Size:" << p_pTag->size() << "  Next:" << p_pTag->next;

    //
    // Read data when available
    //
    if (p_pTag->size() > 0)
    {
        p_pStream->readRawData(p_pTag->data(), p_pTag->size());
        FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN);
    }

    if (p_pTag->next != FIFFV_NEXT_SEQ)
        p_pStream->device()->seek(p_pTag->next);//fseek(fid,tag.next,'bof');

    return true;
}
Example #2
0
bool FiffTag::read_tag_info(FiffStream* p_pStream, FiffTag::SPtr &p_pTag, bool p_bDoSkip)
{
    p_pTag = FiffTag::SPtr(new FiffTag());

    //Option 1
//    t_DataStream.readRawData((char *)p_pTag, FIFFC_TAG_INFO_SIZE);
//    p_pTag->kind = Fiff::swap_int(p_pTag->kind);
//    p_pTag->type = Fiff::swap_int(p_pTag->type);
//    p_pTag->size = Fiff::swap_int(p_pTag->size);
//    p_pTag->next = Fiff::swap_int(p_pTag->next);

    //Option 2
    *p_pStream  >> p_pTag->kind;
    *p_pStream  >> p_pTag->type;
    qint32 size;
    *p_pStream  >> size;
    p_pTag->resize(size);
    *p_pStream  >> p_pTag->next;

//    qDebug() << "read_tag_info" << "  Kind:" << p_pTag->kind << "  Type:" << p_pTag->type << "  Size:" << p_pTag->size() << "  Next:" << p_pTag->next;

    if (p_bDoSkip)
    {
        QTcpSocket* t_qTcpSocket = qobject_cast<QTcpSocket*>(p_pStream->device());
        if(t_qTcpSocket)
        {
            p_pStream->skipRawData(p_pTag->size());
        }
        else
        {
            if (p_pTag->next == FIFFV_NEXT_SEQ)
            {
                p_pStream->device()->seek(p_pStream->device()->pos()+p_pTag->size()); //fseek(fid,tag.size,'cof');
            }
            else if (p_pTag->next > 0)
            {
                p_pStream->device()->seek(p_pTag->next); //fseek(fid,tag.next,'bof');
            }
        }
    }

    return true;
}
Example #3
0
int ShmemSocket::receive_tag (FiffTag::SPtr& p_pTag)
{
    struct  sockaddr_un from;	/* Address (not used) */
    socklen_t fromlen;

    dacqDataMessageRec mess;	/* This is the kind of message we receive */
    int rlen;
    int data_ok = 0;

    p_pTag = FiffTag::SPtr(new FiffTag());
    dacqShmBlock  shmem = this->get_shmem();
    dacqShmBlock  shmBlock;
    dacqShmClient shmClient;
    int           k;


    long read_loc = 0;

    if (m_iShmemSock < 0)
        return (OK);

    //
    // read from the socket
    //
    fromlen = sizeof(from);
    rlen = recvfrom(m_iShmemSock, (void *)(&mess), DATA_MESS_SIZE, 0, (sockaddr *)(&from), &fromlen);

//    qDebug() << "Mess Kind: " << mess.kind << " Type: " << mess.type << "Size: " << mess.size;

    //
    // Parse received message
    //
    if (rlen == -1)
    {
        printf("recvfrom");//dacq_perror("recvfrom");
        this->close_socket ();
        return (FAIL);
    }

    /* A sanity check to survive at least some crazy messages */

    if (mess.kind > 20000 || (unsigned long) mess.size > (size_t) 100000000)
    {
        printf("ALERT: Unreasonable data received, skipping! (size=%d)(kind=%d)", mess.kind, mess.size);
        //dacq_log("ALERT: Unreasonable data received, skipping! (size=%d)(kind=%d)", mess.kind, mess.size);
        mess.size = 0;
        mess.kind = FIFF_NOP;
    }

    p_pTag->kind = mess.kind;
    p_pTag->type = mess.type;
    p_pTag->next = 0;

    if ((unsigned long) mess.size > (size_t) 0)
    {
        p_pTag->resize(mess.size);
    }

//    qDebug() << mess.loc << " " << mess.size << " " << mess.shmem_buf << " " << mess.shmem_loc;

    if (mess.loc < 0 && (unsigned long) mess.size > (size_t) 0 && mess.shmem_buf < 0 && mess.shmem_loc < 0)
    {
        fromlen = sizeof(from);
        rlen = recvfrom(m_iShmemSock, (void *)p_pTag->data(), mess.size, 0, (sockaddr *)(&from), &fromlen);
        if (rlen == -1)
        {
            printf("recvfrom");//dacq_perror("recvfrom");
            this->close_socket();
            return (FAIL);
        }
        data_ok = 1;
//        if (mess.type == FIFFT_STRING)
//            data[mess.size] = '\0';
    }
    else if ((unsigned long) mess.size > (size_t) 0) {
        /*
         * Copy data from shared memory
         */
        if (mess.shmem_buf >= 0 && m_iShmemId/10000 > 0)
        {
            shmBlock  = shmem + mess.shmem_buf;
            shmClient = shmBlock->clients;

            if (interesting_data(mess.kind))
            {
                memcpy(p_pTag->data(),shmBlock->data,mess.size);
                data_ok = 1;
            #ifdef DEBUG
                printf("client # %d read shmem buffer # %d\n", m_iShmemId, mess.shmem_buf);//dacq_log("client # %d read shmem buffer # %d\n", id,mess.shmem_buf);
            #endif
            }
            /*
            * Indicate that this client has processed the data
            */
            for (k = 0; k < SHM_MAX_CLIENT; k++,shmClient++)
                if (shmClient->client_id == m_iShmemId)
                    shmClient->done = 1;
        }
        /*
        * Read data from file
        */
        else {
            /*
            * Possibly read from shmem file
            */
            if (m_iShmemId/10000 > 0 && mess.shmem_loc >= 0) {
                read_fd  = shmem_fd;
                read_loc = mess.shmem_loc;
            }
            else {
                read_fd  = fd;
                read_loc = mess.loc;
            }
            if (interesting_data(mess.kind)) {
                if (read_fif (read_fd,read_loc,mess.size,(char *)p_pTag->data()) == -1) {
                    printf("Could not read data (tag = %d, size = %d, pos = %li)!\n", mess.kind,mess.size,read_loc);//dacq_log("Could not read data (tag = %d, size = %d, pos = %d)!\n", mess.kind,mess.size,read_loc);
                    //dacq_log("%s\n",err_get_error());
                }
                else {
                    data_ok = 1;
//                    if (mess.type == FIFFT_STRING)
//                        data[mess.size] = '\0';
                    FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN);
                }
            }
        }
    }

    /*
    * Special case: close old input file
    */
    if (mess.kind == FIFF_CLOSE_FILE) {
        if (fd != NULL) {
            printf("File to be closed (lib/FIFF_CLOSE_FILE).\n");//dacq_log("File to be closed (lib/FIFF_CLOSE_FILE).\n");
            (void)fclose(fd);
            fd = NULL;
        }
        else
            printf("No file to close (lib/FIFF_CLOSE_FILE).\n");//dacq_log("No file to close (lib/FIFF_CLOSE_FILE).\n");
    }
    /*
    * Another special case: open new input file
    */
    else if (mess.kind == FIFF_NEW_FILE) {
        if (fd != NULL) {
            (void)fclose(fd);
            printf("File closed (lib/FIFF_NEW_FILE).\n");//dacq_log("File closed (lib/FIFF_NEW_FILE).\n");
        }
        fd = open_fif((char *)p_pTag->data());
        free (filename);
        filename = strdup((char *)p_pTag->data());

        if (shmem_fd == NULL)
            shmem_fd = open_fif (SHM_FAIL_FILE);
    }

    if (p_pTag->size() <= 0)
    {
        data_ok  = 0;
        return (FAIL);
    }
    return (OK);
}