Exemple #1
0
void recv_notify_read(int sock, short event, void *arg)
{
    int bytes;
    int incomesock;
    struct nio_thread_data *pThreadData;
    struct fast_task_info *pTask;
    char szClientIp[IP_ADDRESS_SIZE];
    in_addr_t client_addr;

    while (1)
    {
        if ((bytes=read(sock, &incomesock, sizeof(incomesock))) < 0)
        {
            if (!(errno == EAGAIN || errno == EWOULDBLOCK))
            {
                logError("file: "__FILE__", line: %d, " \
                         "call read failed, " \
                         "errno: %d, error info: %s", \
                         __LINE__, errno, STRERROR(errno));
            }

            break;
        }
        else if (bytes == 0)
        {
            break;
        }

        if (incomesock < 0)
        {
            return;
        }

        client_addr = getPeerIpaddr(incomesock, \
                                    szClientIp, IP_ADDRESS_SIZE);
        if (g_allow_ip_count >= 0)
        {
            if (bsearch(&client_addr, g_allow_ip_addrs, \
                        g_allow_ip_count, sizeof(in_addr_t), \
                        cmp_by_ip_addr_t) == NULL)
            {
                logError("file: "__FILE__", line: %d, " \
                         "ip addr %s is not allowed to access", \
                         __LINE__, szClientIp);

                close(incomesock);
                continue;
            }
        }

        if (tcpsetnonblockopt(incomesock) != 0)
        {
            close(incomesock);
            continue;
        }

        pTask = free_queue_pop();
        if (pTask == NULL)
        {
            logError("file: "__FILE__", line: %d, " \
                     "malloc task buff failed, you should " \
                     "increase the parameter: max_connections", \
                     __LINE__);
            close(incomesock);
            continue;
        }

        strcpy(pTask->client_ip, szClientIp);

        pThreadData = g_thread_data + incomesock % g_work_threads;
        if (ioevent_set(pTask, pThreadData, incomesock, IOEVENT_READ,
                        client_sock_read, g_fdfs_network_timeout) != 0)
        {
            task_finish_clean_up(pTask);
            continue;
        }
    }
}
Exemple #2
0
void recv_notify_read(int sock, short event, void *arg)
{
	int bytes;
	int incomesock;
	int result;
	struct tracker_thread_data *pThreadData;
	struct fast_task_info *pTask;
	char szClientIp[IP_ADDRESS_SIZE];
	in_addr_t client_addr;

	while (1)
	{
		if ((bytes=read(sock, &incomesock, sizeof(incomesock))) < 0)
		{
			if (!(errno == EAGAIN || errno == EWOULDBLOCK))
			{
				logError("file: "__FILE__", line: %d, " \
					"call read failed, " \
					"errno: %d, error info: %s", \
					__LINE__, errno, STRERROR(errno));
			}

			break;
		}
		else if (bytes == 0)
		{
			break;
		}

		if (incomesock < 0)
		{
			struct timeval tv;
                        tv.tv_sec = 1;
                        tv.tv_usec = 0;
			pThreadData = g_thread_data + (-1 * incomesock - 1) % \
					g_work_threads;
			event_base_loopexit(pThreadData->ev_base, &tv);
			return;
		}

		client_addr = getPeerIpaddr(incomesock, \
				szClientIp, IP_ADDRESS_SIZE);
		if (g_allow_ip_count >= 0)
		{
			if (bsearch(&client_addr, g_allow_ip_addrs, \
					g_allow_ip_count, sizeof(in_addr_t), \
					cmp_by_ip_addr_t) == NULL)
			{
				logError("file: "__FILE__", line: %d, " \
					"ip addr %s is not allowed to access", \
					__LINE__, szClientIp);

				close(incomesock);
				continue;
			}
		}

		if (tcpsetnonblockopt(incomesock) != 0)
		{
			close(incomesock);
			continue;
		}

		pTask = free_queue_pop();
		if (pTask == NULL)
		{
			logError("file: "__FILE__", line: %d, " \
				"malloc task buff failed, you should " \
				"increase the parameter: max_connections", \
				__LINE__);
			close(incomesock);
			continue;
		}

		pThreadData = g_thread_data + incomesock % g_work_threads;

		strcpy(pTask->client_ip, szClientIp);
	
		event_set(&pTask->ev_read, incomesock, EV_READ, \
				client_sock_read, pTask);
		if (event_base_set(pThreadData->ev_base, &pTask->ev_read) != 0)
		{
			task_finish_clean_up(pTask);
			close(incomesock);

			logError("file: "__FILE__", line: %d, " \
				"event_base_set fail.", __LINE__);
			continue;
		}

		event_set(&pTask->ev_write, incomesock, EV_WRITE, \
				client_sock_write, pTask);
		if ((result=event_base_set(pThreadData->ev_base, \
				&pTask->ev_write)) != 0)
		{
			task_finish_clean_up(pTask);
			close(incomesock);

			logError("file: "__FILE__", line: %d, " \
					"event_base_set fail.", __LINE__);
			continue;
		}

		if (event_add(&pTask->ev_read, &g_network_tv) != 0)
		{
			task_finish_clean_up(pTask);
			close(incomesock);

			logError("file: "__FILE__", line: %d, " \
				"event_add fail.", __LINE__);
			continue;
		}
	}
}