Exemple #1
0
 int read_file_all(const char* file, char* buff, const uint32_t bufsize)
 {
     if (file == NULL || buff == NULL || bufsize == 0) {
         ALARM("param error. file[%p] buff[%p] bufsize[%u]",
                 file, buff, bufsize);
         return -1;
     }
     int fd = open(file, O_RDONLY);
     if (fd == -1) {
         ALARM( "open file[%s] failed. %m", file);
         return -1;
     }
     int32_t offset = (int32_t)lseek(fd, 0, SEEK_END);
     if (offset == -1) {
         ALARM( "lssek file[%s] failed. %m", file);
         return -1;
     }
     if ((uint32_t)offset > bufsize || offset == 0) {
         ALARM( "file[%s] size[%d] tooo big or small. bufsize[%d]", file, offset, bufsize);
         return -1;
     }
     lseek(fd, 0, SEEK_SET);
     int left = offset;
     while (left > 0) {
         int len = (int)read(fd, buff+offset-left, left);
         if (len == -1 || len == 0) {
             ALARM( "readfile failed. ERROR[%m]");
             return -1;
         }
         left -= len;
     }
     DEBUG( "read file[%s] all ok.", file);
     close(fd);
     return offset;
 }
Exemple #2
0
int32_t fileblock :: get(const uint32_t offset, void* buff, const uint32_t length)
{
    uint32_t file_no  = offset / m_cell_num_per_file;
    uint32_t inoffset = offset % m_cell_num_per_file;
    if (length != m_cell_size)
    {
        ALARM("length[%u] != m_cell_size[%u]",
                length, m_cell_size);
        return -1;
    }
    if (file_no >= m_max_file_no)
    {
        ALARM("offset[%u] too big. m_cell_num_per_file[%u] file_no[%u] max_file_no[%u]",
                offset, m_cell_num_per_file, file_no, m_max_file_no);
        return -1;
    }

    if (m_fd[file_no] == -1)
    {
        char tmpstr[128];
        snprintf(tmpstr, sizeof(tmpstr), FORMAT_PATH, m_fb_dir, m_fb_name, file_no);
        mode_t amode = (0 == access(tmpstr, F_OK)) ? O_RDWR : O_RDWR|O_CREAT;
        m_fd[file_no] = open(tmpstr, amode, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    }

    MyThrowAssert(m_fd[file_no] != -1);
    return (int32_t)pread(m_fd[file_no], buff, m_cell_size, inoffset * m_cell_size);
}
Exemple #3
0
int32_t fileblock :: get(const uint32_t offset, const uint32_t count, void* buff, const uint32_t length)
{
    uint32_t file_no  = offset / m_cell_num_per_file;
    uint32_t inoffset = offset % m_cell_num_per_file;
    if (length < count * m_cell_size || NULL == buff)
    {
        ALARM("length[%u] too short or buff[%p]. m_cell_size[%u] count[%u]",
                length, buff, m_cell_size, count);
        return -1;
    }
    if (file_no >= m_max_file_no)
    {
        ALARM("offset[%u] too big. m_cell_num_per_file[%u] file_no[%u] max_file_no[%u]",
                offset, m_cell_num_per_file, file_no, m_max_file_no);
        return -1;
    }
    // 判断是否跨文件访问
    memset(buff, 0, count*m_cell_size);
    if ( inoffset + count > m_cell_num_per_file)
    {
        // 跨文件访问
        // 递归调用
        int32_t ret1 = 0;
        int32_t ret2 = 0;
        uint32_t count1 = m_cell_num_per_file - inoffset;
        ret1 = get(offset, count1, buff, count1 * m_cell_size);

        if ((file_no+1) < m_max_file_no)
        {
            ret2 = get((file_no+1)*m_cell_num_per_file, count - count1,
                    &(((char*)buff)[count1*m_cell_size]), (count-count1)*m_cell_size);
        }
        DEBUG("offset1[%u] offset2[%u] count[%u] count1[%u] count2[%u] ret1[%d] ret2[%d]",
                offset, (file_no+1)*m_cell_num_per_file, count, count1, count - count1, ret1, ret2);
        if (ret1 < 0)
        {
            return -1;
        }
        else
        {
            return (ret2 < 0) ? ret1 : ret1+ret2;
        }
    }
    else
    {
        if (m_fd[file_no] == -1)
        {
            char tmpstr[128];
            snprintf(tmpstr, sizeof(tmpstr), FORMAT_PATH, m_fb_dir, m_fb_name, file_no);
            mode_t amode = (0 == access(tmpstr, F_OK)) ? O_RDWR : O_RDWR|O_CREAT;
            m_fd[file_no] = open(tmpstr, amode, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        }
        MyThrowAssert(m_fd[file_no] != -1);
        return (int32_t)pread(m_fd[file_no], buff, m_cell_size*count, inoffset * m_cell_size);
    }
}
Exemple #4
0
int diskv :: get(const diskv_idx_t& idx, void* buff, const uint32_t length)
{
    if (idx.file_no >= m_max_file_no
            || idx.offset > MAX_FILE_SIZE
            || idx.data_len > length
            || NULL == buff
            || ((idx.file_no == m_max_file_no - 1) && ((uint32_t)(idx.offset + idx.data_len) > m_last_file_offset))
       )
    {
        ALARM ("params error. idx.file_no[%u] idx.offset[%u] idx.data_len[%u] "
                "max_file_no[%u] buff[%p] length[%u] limit_length[%u] cur_offset[%u]",
                idx.file_no, idx.offset, idx.data_len,
                m_max_file_no - 1, buff, length, MAX_FILE_SIZE, m_last_file_offset);
        return -1;
    }

    if (m_read_fd[idx.file_no] == -1)
    {
        char full_name[MAX_PATH_LENGTH];
        snprintf(full_name, sizeof(full_name), FORMAT_PATH, m_dir, m_module, idx.file_no);
        m_read_fd[idx.file_no] = open(full_name, O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    }
    MyThrowAssert(m_read_fd[idx.file_no] != -1);
    return (int)pread(m_read_fd[idx.file_no], buff, idx.data_len, idx.offset);
}
Exemple #5
0
	void*
ServiceThread(void* args)
{
	thread_data_t* ptd = (thread_data_t*)args;
	int sock = -1;
	int ret = -1;
	int netret = -1;
	socklen_t   caddr_len=0;

	ROUTN( "ServiceThread[%u] Create OK", ptd->tid);

	while( 1 )
	{
		netret = -1;
		ret = -1;
        sock = ptd->poll->fetch_socket();
		if( sock>0 ){
            sockaddr_in cltaddr;
            caddr_len = sizeof(cltaddr);
            getpeername(sock, (sockaddr*)&cltaddr, &caddr_len );
            inet_ntop(AF_INET, (void *)&cltaddr.sin_addr, ptd->cltip, sizeof(ptd->cltip));
        }
        else
        {
            ALARM( "sock:%d %m", sock );
            continue;
        }

        memset (ptd->RecvHead, 0, sizeof(ptd->RecvHead));
        netret = xrecv(sock, ptd->RecvHead, ptd->RecvBuffSize, myConfig->RTimeMS());
        if( netret <0 && errno != EAGAIN)
        {
            DEBUG( "xrecv error. sock:%d ret:%d name:%s body_len:%d to[%u] errno[%d] msg[%m]",
                    sock, netret, ptd->RecvHead->srvname,ptd->RecvHead->detail_len,
                    myConfig->RTimeMS(), errno);
            ptd->poll->free_socket(sock, 0);
            continue;
        }
        else
        {
            ret = (ptd->servapp)( ptd );
            netret = xsend(sock, ptd->SendHead, myConfig->WTimeMS());
            if(( netret<0 || ret<0 ) && errno != EAGAIN){
                DEBUG( "xsend error. sock:%d netret:%d ret:%d errno[%d] %m", sock, netret, ret, errno );
                ptd->poll->free_socket(sock, 0);
                continue;
            }
        }
        ptd->poll->free_socket(sock, 1);
    }
    return NULL;
}
Exemple #6
0
    int connect_ms(const char* host, const uint16_t port, const uint32_t timeout_ms)
    {
        struct sockaddr_in adr_srvr;  /* AF_INET */
        int len_inet = sizeof (adr_srvr);
        memset (&adr_srvr, 0, len_inet);
        adr_srvr.sin_family = AF_INET;
        adr_srvr.sin_port = htons (port);
        adr_srvr.sin_addr.s_addr = inet_addr (host);

        if (adr_srvr.sin_addr.s_addr == INADDR_NONE)
        {
            return -1;
        }

        int sockfd = socket (PF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
        {
            ALARM("socket() fail. msg[%m]");
            return -1;
        }
        struct timeval timeout = {0, 0};
        timeout.tv_sec  = timeout_ms/1000;
        timeout.tv_usec = 1000*(timeout_ms%1000);
        setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeval));

        if (0 == connect (sockfd, (struct sockaddr *) &adr_srvr, len_inet))
        {
            return sockfd;
        }
        else
        {
            ALARM("connect() fail. msg[%m]");
            close(sockfd);
        }
        return -1;
    }
Exemple #7
0
void SC_timeout(int to, PFSignal_handler fnc, void *a, int nb)
   {

#if defined(HAVE_POSIX_SYS)

    int nn, ns;
    void *an;
    PFSignal_handler fn;

/* since alarms do not nest (without much more logic than we have here)
 * do nothing if there is an active handler going
 */
    if ((_SC.to_lst == (PFSignal_handler) -1) || (_SC.to_lst == fnc))

/* settle on the handler */
       {fn = fnc;
	an = a;
	nn = nb;
	if (fnc == NULL)
	   {if (to > 0)
	       {fn = _SC_timeout_error;
		an = a;
	        nn = -1;}
	    else
	       {fn = _SC.to_err.f;
		an = _SC.to_err.a;
	        nn = _SC.to_err.nb;};};

/* set the handler and the alarm */
	_SC.to_err = SC_signal_n(SIGALRM, fn, an, nn);
	ns         = ALARM(to);
	SC_ASSERT(ns >= 0);

	if (_SC.to_err.f == SIG_ERR)
	   SC_error(-1, "SETTING SIGALRM FAILED (%d) - SC_TIMEOUT\n", errno);

/* set _SC.to_lst appropriately */
	if (to == 0)
	   _SC.to_lst = (PFSignal_handler) -1;
        else
	   _SC.to_lst = fnc;};

#endif

    return;}
Exemple #8
0
int diskv :: set(diskv_idx_t& idx, const void* buff, const uint32_t length)
{
    if (length > MAX_DATA_SIZE || NULL == buff)
    {
        ALARM ("params error. length[%u] length_limit[%u] buff[%p]",
                length, MAX_DATA_SIZE, buff);
        return -1;
    }

    check_new_file(length);
    MyThrowAssert(m_last_file_offset == (uint32_t)lseek(m_append_fd, 0, SEEK_END));
    MyThrowAssert(length == (uint32_t)write(m_append_fd, buff, length));
    idx.offset   = m_last_file_offset;
    idx.file_no  = m_max_file_no - 1;
    idx.data_len = length;
    m_last_file_offset += length;
    return 0;
}