//����д��������Ƿ�����
void CEpollReactor::check_connection(int32_t rc, CEventHandler* handler)
{
	if(rc < 0)
	{
		delete_handler(handler);

		handler->handle_close(handler->get_handle(), MASK_TIMEOUT);
	}
}
Beispiel #2
0
void WIPResourceManager::free_resource(ResHandler* handler)
{
    switch (handler->type)
    {
    case TEXT:
    {

    }
    break;
    case TEXTURE:
        //FreeImage_Unload((FIBITMAP*)handler->ptr_1);
        delete[](unsigned char*)handler->ptr;
        delete (TextureData *)handler->extra;
        //delete_handler(handler);
        break;
    case SOUND:
    {

    }
    default:
        break;
    }
    delete_handler(handler);
}
int32_t CEpollReactor::event_loop()
{
	if(epfd_ == -1)
	{
		return -1;
	}

	int32_t count = epoll_wait(epfd_, events_, nevent_, epoll_delay_);
	if(count < 0)
	{
		if(error_no() == EINTR)
		{
			return 0;
		}

		return -1;
	}

	CEventHandler* handler = 0;
	BASE_HANDLER handler_id = -1;

	for(register int32_t i = 0; i < count; ++i)
	{
		handler = (CEventHandler *)events_[i].data.ptr;
		if(handler == 0)
		{
			continue;
		}

		handler_id = handler->get_handle();
		
		if(events_[i].events & EPOLLIN) //���¼�
		{
			int32_t rc = handler->handle_input(handler_id);

			check_connection(rc, handler);
			if(rc < 0)
			{
				continue;
			}
		}
		
		if(events_[i].events & EPOLLOUT) //д�¼�
		{
			int32_t rc = handler->handle_output(handler_id);

			check_connection(rc, handler);
			if(rc < 0)
			{
				continue;
			}
		}

		if((events_[i].events & EPOLLHUP) /*| (events_[i].events & EPOLLRDHUP)*/) //�ر��¼�
		{
			delete_handler(handler);

			handler->handle_close(handler_id, MASK_TIMEOUT);
			continue;
		}

		if(events_[i].events & EPOLLERR)//�쳣�¼�
		{
			delete_handler(handler);

			handler->handle_close(handler_id, MASK_TIMEOUT);
		}
	}

	//��������¼���
	if(count >= nevent_ && nevent_ < MAX_NEVENT) //8192��LIB EVENT�е�����¼���
	{
		nevent_ = 2 * nevent_;
	}

	//ɨ�趨ʱ��
	uint64_t cur_ts = CBaseTimeValue::get_time_value().msec();
	if(cur_ts > prev_ts_ + 5)
	{
		epoll_delay_ = timer_queue_.expire();

		//ɨ���ڲ�����
		if(msg_proc_ != 0)
		{
			msg_proc_->processor();
		}

		prev_ts_ = cur_ts;
	}
	else
		epoll_delay_ = 5;

	return 0;
}
Beispiel #4
0
int main (int argc, char **argv) {
    int c;
    conn *l_conn;
    struct in_addr addr;
    int lock_memory = 0;
    int daemonize = 0;
    int maxcore = 0;
    char *username = 0;
    struct passwd *pw;
    struct sigaction sa;
    struct rlimit rlim;
    char *pid_file = NULL;

    /* init settings */
    settings_init();

    /* process arguments */
    while ((c = getopt(argc, argv, "p:m:Mc:khirvdl:u:P:")) != -1) {
        switch (c) {
        case 'p':
            settings.port = atoi(optarg);
            break;
        case 'm':
            settings.maxbytes = atoi(optarg)*1024*1024;
            break;
        case 'M':
            settings.evict_to_free = 0;
            break;
        case 'c':
            settings.maxconns = atoi(optarg);
            break;
        case 'h':
            usage();
            exit(0);
        case 'i':
            usage_license();
            exit(0);
        case 'k':
            lock_memory = 1;
            break;
        case 'v':
            settings.verbose++;
            break;
        case 'l':
            if (!inet_aton(optarg, &addr)) {
                fprintf(stderr, "Illegal address: %s\n", optarg);
                return 1;
            } else {
                settings.interface = addr;
            }
            break;
        case 'd':
            daemonize = 1;
            break;
        case 'r':
            maxcore = 1;
            break;
        case 'u':
            username = optarg;
            break;
        case 'P':
            pid_file = optarg;
            break;
        default:
            fprintf(stderr, "Illegal argument \"%c\"\n", c);
            return 1;
        }
    }

    if (maxcore) {
        struct rlimit rlim_new;
        /*
         * First try raising to infinity; if that fails, try bringing
         * the soft limit to the hard.
         */
        if (getrlimit(RLIMIT_CORE, &rlim)==0) {
            rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
            if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
                /* failed. try raising just to the old max */
                rlim_new.rlim_cur = rlim_new.rlim_max =
                                        rlim.rlim_max;
                (void) setrlimit(RLIMIT_CORE, &rlim_new);
            }
        }
        /*
         * getrlimit again to see what we ended up with. Only fail if
         * the soft limit ends up 0, because then no core files will be
         * created at all.
         */

        if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) {
            fprintf(stderr, "failed to ensure corefile creation\n");
            exit(1);
        }
    }

    /*
     * If needed, increase rlimits to allow as many connections
     * as needed.
     */

    if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
        fprintf(stderr, "failed to getrlimit number of files\n");
        exit(1);
    } else {
        int maxfiles = settings.maxconns;
        if (rlim.rlim_cur < maxfiles)
            rlim.rlim_cur = maxfiles + 3;
        if (rlim.rlim_max < rlim.rlim_cur)
            rlim.rlim_max = rlim.rlim_cur;
        if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
            fprintf(stderr, "failed to set rlimit for open files. Try running as root or requesting smaller maxconns value.\n");
            exit(1);
        }
    }

    /*
     * initialization order: first create the listening socket
     * (may need root on low ports), then drop root if needed,
     * then daemonise if needed, then init libevent (in some cases
     * descriptors created by libevent wouldn't survive forking).
     */

    /* create the listening socket and bind it */
    l_socket = server_socket(settings.port);
    if (l_socket == -1) {
        fprintf(stderr, "failed to listen\n");
        exit(1);
    }

    /* lose root privileges if we have them */
    if (getuid()== 0 || geteuid()==0) {
        if (username==0 || *username=='\0') {
            fprintf(stderr, "can't run as root without the -u switch\n");
            return 1;
        }
        if ((pw = getpwnam(username)) == 0) {
            fprintf(stderr, "can't find the user %s to switch to\n", username);
            return 1;
        }
        if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) {
            fprintf(stderr, "failed to assume identity of user %s\n", username);
            return 1;
        }
    }

    /* daemonize if requested */
    /* if we want to ensure our ability to dump core, don't chdir to / */
    if (daemonize) {
        int res;
        res = daemon(maxcore, settings.verbose);
        if (res == -1) {
            fprintf(stderr, "failed to daemon() in order to daemonize\n");
            return 1;
        }
    }


    /* initialize other stuff */
    item_init();
    event_init();
    stats_init();
    assoc_init();
    conn_init();
    slabs_init(settings.maxbytes);

    /* lock paged memory if needed */
    if (lock_memory) {
#ifdef HAVE_MLOCKALL
        mlockall(MCL_CURRENT | MCL_FUTURE);
#else
        fprintf(stderr, "warning: mlockall() not supported on this platform.  proceeding without.\n");
#endif
    }

    /*
     * ignore SIGPIPE signals; we can use errno==EPIPE if we
     * need that information
     */
    sa.sa_handler = SIG_IGN;
    sa.sa_flags = 0;
    if (sigemptyset(&sa.sa_mask) == -1 ||
            sigaction(SIGPIPE, &sa, 0) == -1) {
        perror("failed to ignore SIGPIPE; sigaction");
        exit(1);
    }

    /* create the initial listening connection */
    if (!(l_conn = conn_new(l_socket, conn_listening, EV_READ | EV_PERSIST))) {
        fprintf(stderr, "failed to create listening connection");
        exit(1);
    }

    /* initialise deletion array and timer event */
    deltotal = 200;
    delcurr = 0;
    todelete = malloc(sizeof(item *)*deltotal);
    delete_handler(0,0,0); /* sets up the event */

    /* save the PID in if we're a daemon */
    if (daemonize)
        save_pid(getpid(),pid_file);

    /* enter the loop */
    event_loop(0);

    /* remove the PID file if we're a daemon */
    if (daemonize)
        remove_pidfile(pid_file);

    return 0;
}
int main (int argc, char **argv) {
    int c;
    int l_socket;
    conn *l_conn;
    struct in_addr addr;
    int lock_memory = 0;
    int daemonize = 0;

    /* initialize stuff */
    event_init();
    stats_init();
    assoc_init();
    settings_init();
    conn_init();

    /* process arguments */
    while ((c = getopt(argc, argv, "p:s:m:c:khdl:")) != -1) {
        switch (c) {
        case 'p':
            settings.port = atoi(optarg);
            break;
        case 's':
            settings.maxitems = atoi(optarg);
            break;
        case 'm':
            settings.maxbytes = atoi(optarg)*1024*1024;
            break;
        case 'c':
            settings.maxconns = atoi(optarg);
            break;
        case 'h':
            usage();
            exit(0);
        case 'k':
            lock_memory = 1;
            break;
        case 'l':
            if (!inet_aton(optarg, &addr)) {
                fprintf(stderr, "Illegal address: %s\n", optarg);
                return 1;
            } else {
                settings.interface = addr;
            }
            break;
        case 'd':
            daemonize = 1;
            break;
        default:
            fprintf(stderr, "Illegal argument \"%c\"\n", c);
            return 1;
        }
    }

    if (daemonize) {
        int child;
        child = fork();
        if (child == -1) {
            fprintf(stderr, "failed to fork() in order to daemonize\n");
            return 1;
        }
        if (child) {        /* parent */
            exit(0);
        } else {            /* child */
            setsid();       /* become a session group leader */
            child = fork(); /* stop being a session group leader */
            if (child) {    /* parent */
                exit(0);
            } else {
                int null;
                chdir("/");
                null = open("/dev/null", O_RDWR);
                dup2(null, 0);
                dup2(null, 1);
                dup2(null, 2);
                close(null);
            }
        }
    }

    /* lock paged memory if needed */
    if (lock_memory) {
        mlockall(MCL_CURRENT | MCL_FUTURE);
    }

    /* create the listening socket and bind it */
    l_socket = server_socket(settings.port);
    if (l_socket == -1) {
        fprintf(stderr, "failed to listen\n");
        exit(1);
    }

    /* create the initial listening connection */
    if (!(l_conn = conn_new(l_socket, conn_listening, EV_READ | EV_PERSIST))) {
        fprintf(stderr, "failed to create listening connection");
        exit(1);
    }

    /* initialise deletion array and timer event */
    deltotal = 200; delcurr = 0;
    todelete = malloc(sizeof(item *)*deltotal);
    delete_handler(0,0,0); /* sets up the event */

    /* enter the loop */
    event_loop(0);

    return;
}
Beispiel #6
0
HandlerResult ImmortalProperty::operation(LocatedEntity * e,
        const Operation & op, OpVector & res)
{
    return delete_handler(e, op, res);
}
Beispiel #7
0
//!!!be sure to release memory before return null
ResHandler* WIPResourceManager::alloc(const char* filename, WIPResourceType type)
{
    ResHandler* res = new ResHandler;
    switch (type)
    {
    case TEXT:
    {

    }
    break;
    case TEXTURE:
    {
        TextureData *data = new TextureData;
        //初始化FreeImage
        FreeImage_Initialise(TRUE);

        FIBITMAP* bmpConverted = NULL;
        FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
        fif = FreeImage_GetFileType(filename);
        if (fif == FIF_UNKNOWN)
            fif = FreeImage_GetFIFFromFilename(filename);
        if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif))
        {

            FIBITMAP *dib = FreeImage_Load(fif, filename);

            if (!dib)
            {
                printf("[fatal error]: \"%s\" load error!\n", filename);
                delete data;
                delete_handler(res);
                return NULL;
            }

            // we are top left, FIBITMAP is bottom left
            FreeImage_FlipVertical(dib);
            //get infomation
            FREE_IMAGE_TYPE imgType = FreeImage_GetImageType(dib);
            FREE_IMAGE_COLOR_TYPE colorType = FreeImage_GetColorType(dib);
            unsigned int bpp = FreeImage_GetBPP(dib);

            data->bpp = bpp;
            data->color_type = colorType;
            data->image_type = imgType;
            data->channel = 4;

            int x, y;
            RGBQUAD m_rgb;

            //获取图片长宽
            int width = (int)FreeImage_GetWidth(dib);
            int height = (int)FreeImage_GetHeight(dib);

            unsigned char* imgBuf = new unsigned char[width*height * 4];



            bool is_tr = FreeImage_IsTransparent(dib);

            bool is_little = FreeImage_IsLittleEndian();
            //获取图片数据
            //按RGBA格式保存到数组中
            for (y = 0; y<height; y++)
            {
                for (x = 0; x<width; x++)
                {
                    //获取像素值
                    FreeImage_GetPixelColor(dib, x, y, &m_rgb);

                    if (is_little)
                    {
                        imgBuf[y*width * 4 + x * 4 + 0] = m_rgb.rgbRed;
                        imgBuf[y*width * 4 + x * 4 + 1] = m_rgb.rgbGreen;
                        imgBuf[y*width * 4 + x * 4 + 2] = m_rgb.rgbBlue;
                        //判断是否透明图片
                        //如果是就取alpha值保存
                        if (is_tr)
                            imgBuf[y*width * 4 + x * 4 + 3] = m_rgb.rgbReserved;
                        else
                            imgBuf[y*width * 4 + x * 4 + 3] = 255;
                    }
                    else
                    {
                        //大端警告!
                        //Big Endian Warnning!
                        printf("Note:This is a Big endian!\n");
                    }
                }
            }


            data->width = width;
            data->height = height;
            data->size = height*width * 4;


            FreeImage_Unload(dib);

            res->file_name = filename;
            res->extra = data;
            res->nref = 1;
            res->ptr = imgBuf;
            res->size = width*height * 4;
            res->type = TEXTURE;

            _map[filename] = res;

            return res;
        }
    }
    break;

    case SOUND:
    {
    }
    default:
        return res;
        break;
    }
    delete_handler(res);
    return NULL;
}