Пример #1
0
void demo_events (void)
{
	//
	// General
	//

    set_event (SDLK_ESCAPE, MODIFIER_NONE, SDL_RELEASED, exit_game);

	set_event (SDLK_q, MODIFIER_LEFT_CONTROL, SDL_RELEASED, exit_game);

	set_event (SDLK_x, MODIFIER_LEFT_CONTROL, SDL_RELEASED, exit_apache_havoc);

	//
	// time acceleration
	//

   set_event (SDLK_EQUALS, MODIFIER_LEFT_CONTROL, SDL_PRESSED, set_small_inc_time_acceleration);

   set_event (SDLK_MINUS, MODIFIER_LEFT_CONTROL, SDL_PRESSED, set_small_dec_time_acceleration);

   set_event (SDLK_p, MODIFIER_NONE, SDL_PRESSED, toggle_pause_time_acceleration);

	//
	//
	//

#if ( OEM_3DLABS_VERSION )

	set_event ( SDLK_1, MODIFIER_NONE, SDL_PRESSED, increase_3d_resolutions );

	set_event ( SDLK_1, MODIFIER_LEFT_SHIFT, SDL_PRESSED, decrease_3d_resolutions );

	set_event ( SDLK_2, MODIFIER_NONE, SDL_PRESSED, cycle_3d_colourdepth );

	set_event ( SDLK_3, MODIFIER_NONE, SDL_PRESSED, toggle_3d_object_lod_calculations );

	set_event ( SDLK_i, MODIFIER_NONE, SDL_PRESSED, toggle_3d_information );

#endif

	set_demo_view_mode_events ();
}
Пример #2
0
static void on_complete_cb(void *udata, int status) {
    struct request_data *ld = (struct request_data *) udata;
    set_event(ld->event);
}
Пример #3
0
void set_havoc_avionics_events (void)
{
	if (!get_gunship_entity ())
	{
		return;
	}

	////////////////////////////////////////
	//
	// KEYBOARD EVENTS
	//
	////////////////////////////////////////

	//
	// select target acquisition system
	//

	set_event (DIK_DELETE, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, select_target_acquisition_system_off_event);

	set_event (DIK_INSERT, MODIFIER_NONE, KEY_STATE_DOWN, select_target_acquisition_system_ground_radar_event);

	set_event (DIK_HOME, MODIFIER_NONE, KEY_STATE_DOWN, select_target_acquisition_system_air_radar_event);

	set_event (DIK_DELETE, MODIFIER_NONE, KEY_STATE_DOWN, select_target_acquisition_system_flir_event);

	set_event (DIK_END, MODIFIER_NONE, KEY_STATE_DOWN, select_target_acquisition_system_llltv_event);
	set_event (DIK_END, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, toggle_eo_system_event);

	set_event (DIK_PRIOR, MODIFIER_NONE, KEY_STATE_DOWN, select_target_acquisition_system_hms_event);

	//
	// repeated for programmable joysticks ...
	//

	set_event (DIK_1, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, select_target_acquisition_system_ground_radar_event);

	set_event (DIK_2, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, select_target_acquisition_system_air_radar_event);

	set_event (DIK_3, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, select_target_acquisition_system_hms_event);

	set_event (DIK_4, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, select_target_acquisition_system_flir_event);

	set_event (DIK_5, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, select_target_acquisition_system_llltv_event);

	//
	// target acquisition system control
	//

	set_event (DIK_NUMPAD4, MODIFIER_NONE, KEY_STATE_EITHER, steer_left_event);
	set_event (DIK_NUMPAD4, MODIFIER_LEFT_SHIFT, KEY_STATE_EITHER, steer_left_fast_event);
	set_event (DIK_NUMPAD4, MODIFIER_LEFT_ALT, KEY_STATE_EITHER, steer_left_fine_event);

	set_event (DIK_NUMPAD6, MODIFIER_NONE, KEY_STATE_EITHER, steer_right_event);
	set_event (DIK_NUMPAD6, MODIFIER_LEFT_SHIFT, KEY_STATE_EITHER, steer_right_fast_event);
	set_event (DIK_NUMPAD6, MODIFIER_LEFT_ALT, KEY_STATE_EITHER, steer_right_fine_event);

	set_event (DIK_NUMPAD8, MODIFIER_NONE, KEY_STATE_EITHER, steer_up_event);
	set_event (DIK_NUMPAD8, MODIFIER_LEFT_SHIFT, KEY_STATE_EITHER, steer_up_fast_event);
	set_event (DIK_NUMPAD8, MODIFIER_LEFT_ALT, KEY_STATE_EITHER, steer_up_fine_event);

	set_event (DIK_NUMPAD2, MODIFIER_NONE, KEY_STATE_EITHER, steer_down_event);
	set_event (DIK_NUMPAD2, MODIFIER_LEFT_SHIFT, KEY_STATE_EITHER, steer_down_fast_event);
	set_event (DIK_NUMPAD2, MODIFIER_LEFT_ALT, KEY_STATE_EITHER, steer_down_fine_event);

	set_event (DIK_ADD, MODIFIER_NONE, KEY_STATE_DOWN, inc_range_event);
	set_event (DIK_ADD, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, inc_range_fast_event);
	set_event (DIK_ADD, MODIFIER_LEFT_CONTROL, KEY_STATE_DOWN, inc_eo_zoom_event);

	set_event (DIK_SUBTRACT, MODIFIER_NONE, KEY_STATE_DOWN, dec_range_event);
	set_event (DIK_SUBTRACT, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, dec_range_fast_event);
	set_event (DIK_SUBTRACT, MODIFIER_LEFT_CONTROL, KEY_STATE_DOWN, dec_eo_zoom_event);

	set_event (DIK_NUMPAD5, MODIFIER_NONE, KEY_STATE_DOWN, steer_centre_event);
	set_event (DIK_NUMPAD5, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, steer_centre_event);

	set_event (DIK_NUMPAD7, MODIFIER_NONE, KEY_STATE_DOWN, toggle_show_allied_targets_event);

	set_event (DIK_NUMPAD9, MODIFIER_NONE, KEY_STATE_DOWN, inc_target_priority_event);
	set_event (DIK_NUMPAD9, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, inc_target_priority_fast_event);

	set_event (DIK_NUMPAD3, MODIFIER_NONE, KEY_STATE_DOWN, dec_target_priority_event);
	set_event (DIK_NUMPAD3, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, dec_target_priority_fast_event);

	set_event (DIK_NUMPAD0, MODIFIER_NONE, KEY_STATE_DOWN, select_next_target_event);
	set_event (DIK_NUMPAD0, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, select_previous_target_event);

	set_event (DIK_NUMPAD1, MODIFIER_NONE, KEY_STATE_DOWN, toggle_auto_target_event);

	set_event (DIK_NUMPADENTER, MODIFIER_NONE, KEY_STATE_DOWN, toggle_lock_target_event);
//	set_event (DIK_NUMPADENTER, MODIFIER_LEFT_ALT, KEY_STATE_DOWN, toggle_lock_target_event); // Jabberwock 031107 Designated targets
	set_event (DIK_NUMPADENTER, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, toggle_lock_target_event);

// arneh 2006-11-16 - manual radar/laser control
	set_event (DIK_DIVIDE, MODIFIER_NONE, KEY_STATE_DOWN, target_acquisition_system_misc_function2_event);
	set_event (DIK_DIVIDE, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, target_acquisition_system_misc_function1_event);

	set_event (DIK_MULTIPLY, MODIFIER_NONE, KEY_STATE_DOWN, activate_laser_event);

	set_event (DIK_S, MODIFIER_LEFT_CONTROL, KEY_STATE_DOWN, toggle_ground_stabilisation_event);

	//
	// miscellaneous
	//

	set_event (DIK_M, MODIFIER_NONE, KEY_STATE_DOWN, acknowledge_master_caution_event);

	set_event (DIK_N, MODIFIER_NONE, KEY_STATE_DOWN, toggle_night_vision_system_event);

	set_event (DIK_O, MODIFIER_NONE, KEY_STATE_DOWN, set_hud_bob_up_overlay_event);

	set_event (DIK_O, MODIFIER_LEFT_CONTROL, KEY_STATE_DOWN, clear_hud_bob_up_overlay_event);

	set_event (DIK_V, MODIFIER_NONE, KEY_STATE_DOWN, toggle_navigation_lights_event);

	set_event (DIK_1, MODIFIER_NONE, KEY_STATE_DOWN, virtual_cockpit_track_target_event);

	set_event (DIK_NUMPADENTER, MODIFIER_LEFT_CONTROL, KEY_STATE_DOWN, virtual_cockpit_track_target_event);

	set_event (DIK_BACKSPACE, MODIFIER_LEFT_ALT, KEY_STATE_DOWN, select_cannon_event);

	set_event (DIK_I, MODIFIER_NONE, KEY_STATE_DOWN, toggle_infra_red_jammer_event);

	set_event (DIK_J, MODIFIER_NONE, KEY_STATE_DOWN, toggle_radar_jammer_event);

	set_event (DIK_LBRACKET, MODIFIER_NONE, KEY_STATE_DOWN, select_next_mfd_event);
	set_event (DIK_LBRACKET, MODIFIER_LEFT_CONTROL, KEY_STATE_DOWN, toggle_mfd_on_off_event);

	////////////////////////////////////////
	//
	// JOYSTICK EVENTS
	//
	////////////////////////////////////////

	set_event ((JOYSTICK_BUTTON + 3), MODIFIER_NONE, KEY_STATE_DOWN, select_next_target_event);

	set_event ((JOYSTICK_BUTTON + 4), MODIFIER_NONE, KEY_STATE_DOWN, virtual_cockpit_track_target_event);
	
}
Пример #4
0
void flight_events (void)
{

	//
	// Mouse buttons
	//

	set_event (MOUSE_LEFT_BUTTON, MODIFIER_NONE, BUTTON_STATE_EITHER, ui_left_mouse_button);

	set_event (MOUSE_RIGHT_BUTTON, MODIFIER_NONE, BUTTON_STATE_EITHER, ui_right_mouse_button);

	//
	// General
	//

	set_event (SDLK_x, MODIFIER_LEFT_CONTROL, SDL_RELEASED, exit_apache_havoc);

	set_event (SDLK_q, MODIFIER_LEFT_CONTROL, SDL_RELEASED, ingame_quit_function);

    set_event (SDLK_RETURN, MODIFIER_NONE, SDL_PRESSED, toggle_in_flight_game_modes);

    set_event (SDLK_RETURN, MODIFIER_LEFT_SHIFT, SDL_PRESSED, campaign_screen_show_external_view_entity);

	#if DEMO_VERSION

	set_event (SDLK_u, MODIFIER_NONE, SDL_PRESSED, feature_disabled_event);

	#else

	set_event (SDLK_u, MODIFIER_NONE, SDL_PRESSED, set_gunship_entity_to_external_view_entity);

	#endif

	//
	// time acceleration
	//

   set_event (SDLK_EQUALS, MODIFIER_LEFT_CONTROL, SDL_PRESSED, set_small_inc_time_acceleration);

   set_event (SDLK_MINUS, MODIFIER_LEFT_CONTROL, SDL_PRESSED, set_small_dec_time_acceleration);

	// for keyboards that don't support the +/- in the normal place (Japanese?)
   set_event (SDLK_KP_PLUS, MODIFIER_LEFT_CONTROL, SDL_PRESSED, set_small_inc_time_acceleration);

   set_event (SDLK_KP_MINUS, MODIFIER_LEFT_CONTROL, SDL_PRESSED, set_small_dec_time_acceleration);

   set_event (SDLK_p, MODIFIER_NONE, SDL_PRESSED, toggle_pause_time_acceleration);

	//
	// screen shots
	//

	set_event (SDLK_SYSREQ, MODIFIER_NONE, SDL_PRESSED, save_screen_image_event);

	//
	// messaging system
	//

	set_event (SDLK_TAB, MODIFIER_NONE, SDL_PRESSED, start_messaging_system);

	set_event (SDLK_TAB, MODIFIER_LEFT_CONTROL, SDL_PRESSED, display_last_received_message);

	#ifndef COMMERCIAL

   set_event (SDLK_SCROLLOCK, MODIFIER_NONE, SDL_PRESSED, breakout);

	set_flight_debug_events ();

	// debug time acceleration

   //set_event (SDLK_MINUS, MODIFIER_RIGHT_SHIFT, SDL_PRESSED, set_min_time_acceleration);

   //set_event (SDLK_EQUALS, MODIFIER_RIGHT_SHIFT, SDL_PRESSED, set_big_inc_time_acceleration);

   //set_event (SDLK_EQUALS, MODIFIER_RIGHT_CONTROL, SDL_PRESSED, set_small_inc_time_acceleration);

   //set_event (SDLK_MINUS, MODIFIER_RIGHT_CONTROL, SDL_PRESSED, set_small_dec_time_acceleration);

	#endif

   process_user_defined_flight_events ();

	generate_key_bound_triggers ();
}
Пример #5
0
void set_alpha_events (void)
{

	int
		loop;

	if (!alpha_entry_function)
	{

		debug_fatal ("ERROR: Alpha event function not setup");
	}

	//
	// Many special keys need to be uppercase so set all keys first to upper
	//

	for (loop = DIK_1; loop <= DIK_APPS; loop ++)
	{
			
		set_event (loop, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_uppercase);

		if ((loop != DIK_LSHIFT) && (loop != DIK_RSHIFT))
		{
	
			set_event (loop, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, alpha_entry_uppercase);
			set_event (loop, MODIFIER_RIGHT_SHIFT, KEY_STATE_DOWN, alpha_entry_uppercase);
		}
	}

	//
	// lowercase alpha keys
	// 	

	for (loop = DIK_Q; loop <= DIK_P; loop ++)
	{
			
		set_event (loop, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	}

	for (loop = DIK_A; loop <= DIK_L; loop ++)
	{
			
		set_event (loop, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	}

	for (loop = DIK_Z; loop <= DIK_M; loop ++)
	{
			
		set_event (loop, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	}

	for (loop = DIK_1; loop <= DIK_EQUALS; loop++)
	{

		set_event (loop, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	}

	set_event (DIK_COMMA, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	set_event (DIK_PERIOD, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	set_event (DIK_SLASH, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	set_event (DIK_MINUS, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	set_event (DIK_SUBTRACT, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	set_event (DIK_LBRACKET, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);
	set_event (DIK_RBRACKET, MODIFIER_NONE, KEY_STATE_DOWN, alpha_entry_lowercase);

	set_event (DIK_UNDERLINE, MODIFIER_LEFT_SHIFT, KEY_STATE_DOWN, alpha_entry_lowercase);
	set_event (DIK_UNDERLINE, MODIFIER_RIGHT_SHIFT, KEY_STATE_DOWN, alpha_entry_lowercase);

	//
	// special keys, clear shift keys
	//

	set_event (DIK_LSHIFT, MODIFIER_NONE, KEY_STATE_EITHER, NULL);
	set_event (DIK_RSHIFT, MODIFIER_NONE, KEY_STATE_EITHER, NULL);

	//
	// end and abort keys
	//

	set_event (DIK_RETURN, MODIFIER_NONE, KEY_STATE_DOWN, end_alpha_entry);
	set_event (DIK_ESCAPE, MODIFIER_NONE, KEY_STATE_DOWN, abort_alpha_entry);

	//
	// mouse buttons
	//

	set_event (MOUSE_LEFT_BUTTON, MODIFIER_NONE, BUTTON_STATE_DOWN, end_alpha_entry);
	set_event (MOUSE_RIGHT_BUTTON, MODIFIER_NONE, BUTTON_STATE_DOWN, end_alpha_entry);
}
Пример #6
0
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
					LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
{
	HANDLE handle;
	WINPR_THREAD* thread;

	thread = (WINPR_THREAD*) calloc(1, sizeof(WINPR_THREAD));

	if (!thread)
		return NULL;

	thread->dwStackSize = dwStackSize;
	thread->lpParameter = lpParameter;
	thread->lpStartAddress = lpStartAddress;
	thread->lpThreadAttributes = lpThreadAttributes;
	thread->ops = &ops;

#if defined(WITH_DEBUG_THREADS)
	thread->create_stack = winpr_backtrace(20);
	dump_thread(thread);
#endif
	thread->pipe_fd[0] = -1;
	thread->pipe_fd[1] = -1;
	
#ifdef HAVE_EVENTFD_H
	thread->pipe_fd[0] = eventfd(0, EFD_NONBLOCK);

	if (thread->pipe_fd[0] < 0)
	{
		WLog_ERR(TAG, "failed to create thread pipe fd 0");
		goto error_pipefd0;
	}
#else
	if (pipe(thread->pipe_fd) < 0)
	{
		WLog_ERR(TAG, "failed to create thread pipe");
		goto error_pipefd0;
	}
	
	{
		int flags = fcntl(thread->pipe_fd[0], F_GETFL);
		fcntl(thread->pipe_fd[0], F_SETFL, flags | O_NONBLOCK);
	}
#endif
	
	if(pthread_mutex_init(&thread->mutex, 0) != 0)
	{
		WLog_ERR(TAG, "failed to initialize thread mutex");
		goto error_mutex;
	}

	WINPR_HANDLE_SET_TYPE(thread, HANDLE_TYPE_THREAD);
	handle = (HANDLE) thread;

	if (!thread_list)
	{
		thread_list = ListDictionary_New(TRUE);
		if (!thread_list)
		{
			WLog_ERR(TAG, "Couldn't create global thread list");
			goto error_thread_list;
		}
		thread_list->objectKey.fnObjectEquals = thread_compare;
	}

	if (!(dwCreationFlags & CREATE_SUSPENDED))
	{
		if (!winpr_StartThread(thread))
			goto error_thread_list;
	}
	else
	{
		if (!set_event(thread))
			goto error_thread_list;
	}

	return handle;

error_thread_list:
	pthread_mutex_destroy(&thread->mutex);
error_mutex:
	if (thread->pipe_fd[1] >= 0)
		close(thread->pipe_fd[1]);
	if (thread->pipe_fd[0] >= 0)
		close(thread->pipe_fd[0]);
error_pipefd0:
	free(thread);
	return NULL;
}
Пример #7
0
void Dispatcher::schedule_event(Events events)
{
    disp_curr_state->handle_event(events);
    set_event(events);
}
Пример #8
0
void Core::schedule_event(Events event)
{
    core_curr_state->handle_event(event);
    set_event(event);
}
Пример #9
0
static void *net_async_connect(void *arg) {
    am_net_t *n = (am_net_t *) arg;
    static const char *thisfunc = "net_async_connect():";
    struct in6_addr serveraddr;
    struct addrinfo *rp, hints;
    int err = 0, on = 1;
    char port[7];
    am_timer_t tmr;

    memset(&hints, 0, sizeof (struct addrinfo));
    hints.ai_flags = AI_NUMERICSERV;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    err = INETPTON(AF_INET, n->uv.host, &serveraddr);
    if (err == 1) {
        hints.ai_family = AF_INET;
        hints.ai_flags |= AI_NUMERICHOST;
    } else {
        err = INETPTON(AF_INET6, n->uv.host, &serveraddr);
        if (err == 1) {
            hints.ai_family = AF_INET6;
            hints.ai_flags |= AI_NUMERICHOST;
        }
    }

    snprintf(port, sizeof (port), "%d", n->uv.port);

    am_timer_start(&tmr);
    if ((err = getaddrinfo(n->uv.host, port, &hints, &n->ra)) != 0) {
        n->error = AM_EHOSTUNREACH;
        am_timer_stop(&tmr);
        am_timer_report(n->instance_id, &tmr, "getaddrinfo");
        set_event(n->ce);
        return NULL;
    }

    am_timer_stop(&tmr);
    am_timer_report(n->instance_id, &tmr, "getaddrinfo");

    n->error = 0;

    for (rp = n->ra; rp != NULL; rp = rp->ai_next) {

        if (rp->ai_family != AF_INET && rp->ai_family != AF_INET6 &&
                rp->ai_socktype != SOCK_STREAM && rp->ai_protocol != IPPROTO_TCP) continue;

        if ((n->sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == INVALID_SOCKET) {
            AM_LOG_ERROR(n->instance_id,
                    "%s: cannot create socket while connecting to %s:%d",
                    thisfunc, n->uv.host, n->uv.port);
            net_log_error(n->instance_id, net_error());
            continue;
        }

        if (setsockopt(n->sock, IPPROTO_TCP, TCP_NODELAY, (void *) &on, sizeof (on)) < 0) {
            net_log_error(n->instance_id, net_error());
        }
        if (setsockopt(n->sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof (on)) < 0) {
            net_log_error(n->instance_id, net_error());
        }
#ifdef SO_NOSIGPIPE
        if (setsockopt(n->sock, SOL_SOCKET, SO_NOSIGPIPE, (void *) &on, sizeof (on)) < 0) {
            net_log_error(n->instance_id, net_error());
        }
#endif
        if (set_nonblocking(n, 1) != 0) {
            n->error = AM_EPERM;
            continue;
        }

        err = connect(n->sock, rp->ai_addr, (SOCKLEN_T) rp->ai_addrlen);
        if (err == 0) {
            AM_LOG_DEBUG(n->instance_id, "%s: connected to %s:%d (%s)",
                    thisfunc, n->uv.host, n->uv.port,
                    rp->ai_family == AF_INET ? "IPv4" : "IPv6");
            n->error = 0;
            if (n->uv.ssl) {
                net_connect_ssl(n);
                if (n->ssl.error != AM_SUCCESS) {
                    AM_LOG_ERROR(n->instance_id,
                            "%s: SSL/TLS connection to %s:%d (%s) failed (%s)",
                            thisfunc, n->uv.host, n->uv.port,
                            rp->ai_family == AF_INET ? "IPv4" : "IPv6",
                            am_strerror(n->ssl.error));
                    net_close_socket(n->sock);
                    n->sock = INVALID_SOCKET;
                    n->error = n->ssl.error;
                    break;
                }
            }
            net_async_poll(n);
            break;
        }

        if (err == INVALID_SOCKET && net_in_progress(net_error())) {
#ifdef _WIN32
            WSAPOLLFD fds[1];
#else
            struct pollfd fds[1];
#endif
            memset(fds, 0, sizeof (fds));
            fds[0].fd = n->sock;
            fds[0].events = connect_ev;
            fds[0].revents = 0;

            err = sockpoll(fds, 1, n->timeout > 0 ? n->timeout * 1000 : -1);
            if (err > 0 && fds[0].revents & connected_ev) {
                int pe = 0;
                SOCKLEN_T pe_sz = sizeof (pe);
                err = getsockopt(n->sock, SOL_SOCKET, SO_ERROR, (char *) &pe, &pe_sz);
                if (err == 0 && pe == 0) {
                    AM_LOG_DEBUG(n->instance_id, "%s: connected to %s:%d (%s)",
                            thisfunc, n->uv.host, n->uv.port,
                            rp->ai_family == AF_INET ? "IPv4" : "IPv6");

                    n->error = 0;
                    if (n->uv.ssl) {
                        net_connect_ssl(n);
                        if (n->ssl.error != AM_SUCCESS) {
                            AM_LOG_ERROR(n->instance_id,
                                    "%s: SSL/TLS connection to %s:%d (%s) failed (%s)",
                                    thisfunc, n->uv.host, n->uv.port,
                                    rp->ai_family == AF_INET ? "IPv4" : "IPv6",
                                    am_strerror(n->ssl.error));
                            net_close_socket(n->sock);
                            n->sock = INVALID_SOCKET;
                            n->error = n->ssl.error;
                            break;
                        }
                    }
                    net_async_poll(n);
                    break;
                }
                net_log_error(n->instance_id, pe);
                n->error = AM_ECONNREFUSED;
            } else if (err == 0) {
                AM_LOG_WARNING(n->instance_id,
                        "%s: timeout connecting to %s:%d (%s)",
                        thisfunc, n->uv.host, n->uv.port,
                        rp->ai_family == AF_INET ? "IPv4" : "IPv6");
                n->error = AM_ETIMEDOUT;
            } else {
                int pe = 0;
                SOCKLEN_T pe_sz = sizeof (pe);
                err = getsockopt(n->sock, SOL_SOCKET, SO_ERROR, (char *) &pe, &pe_sz);
                n->error = AM_ETIMEDOUT;
                break;
            }
        }

        net_close_socket(n->sock);
        n->sock = INVALID_SOCKET;
    }

    if (n->error != 0) {
        set_event(n->ce);
    }
    return NULL;
}
Пример #10
0
bool master_threads::run_alone(const char* addrs, const char* path /* = NULL */,
	unsigned int count /* = 1 */, int threads_count /* = 1 */)
{
	// 每个进程只能有一个实例在运行
	acl_assert(has_called == false);
	has_called = true;
	daemon_mode_ = false;
	acl_assert(addrs && *addrs);

	__count_limit = count;

#ifdef ACL_WINDOWS
	acl_init();
#endif

	std::vector<ACL_VSTREAM*> sstreams;
	ACL_EVENT* eventp = acl_event_new_select_thr(1, 0);
	set_event(eventp);  // 设置基类的事件句柄

	ACL_ARGV*  tokens = acl_argv_split(addrs, ";,| \t");
	ACL_ITER   iter;

	acl_foreach(iter, tokens)
	{
		const char* addr = (const char*) iter.data;
		ACL_VSTREAM* sstream = acl_vstream_listen(addr, 128);
		if (sstream == NULL)
		{
			logger_error("listen %s error(%s)",
				addr, acl_last_serror());
			acl_argv_free(tokens);
			close_sstreams(eventp, sstreams);
			acl_event_free(eventp);
			return false;
		}
		acl_event_enable_listen(eventp, sstream, 0,
			listen_callback, sstream);
		sstreams.push_back(sstream);
	}

	acl_argv_free(tokens);

	// 初始化配置参数
	conf_.load(path);

	service_pre_jail(NULL);
	service_init(NULL);

	if (threads_count > 1)
	{
		__thread_pool = acl_thread_pool_create(threads_count, 120);
		acl_pthread_pool_atinit(__thread_pool, thread_begin, NULL);
		acl_pthread_pool_atfree(__thread_pool, thread_finish, NULL);
	}
	else
		thread_init(NULL);

	while (!__stop)
		acl_event_loop(eventp);

	if (__thread_pool)
		acl_pthread_pool_destroy(__thread_pool);
	else
		thread_exit(NULL);

	service_exit(NULL);

	// 必须在调用 acl_event_free 前调用 close_sstreams,因为在关闭
	// 网络流对象时依然有对 ACL_EVENT 引擎的使用
	close_sstreams(eventp, sstreams);
	acl_event_free(eventp);
	eventp = NULL;

	return true;
}
Пример #11
0
int am_net_write(am_net_t *n, const char *data, size_t data_sz) {
    int status = 0, sent = 0, flags = 0;
    int er = 0, error = 0;
    SOCKLEN_T errlen = sizeof (error);
    if (n != NULL && data != NULL && data_sz > 0) {

        if (wait_for_event(n->ce, 0) == 0) {
            size_t len = data_sz;
            const char *buf = data;
            if (n->error != 0) {
                set_event(n->ce);
                return n->error;
            }
            if (n->ssl.on) {
                n->ssl.request_data_sz = 0;
                am_free(n->ssl.request_data);
                n->ssl.request_data = malloc(data_sz);
                if (n->ssl.request_data != NULL) {
                    memcpy(n->ssl.request_data, data, data_sz);
                    n->ssl.request_data_sz = data_sz;
                    net_write_ssl(n);
                } else {
                    set_event(n->ce);
                    return AM_ENOMEM;
                }
            } else {
#ifdef MSG_NOSIGNAL
                flags |= MSG_NOSIGNAL;
#endif 
                er = getsockopt(n->sock, SOL_SOCKET, SO_ERROR, (void *) &error, &errlen);
                while (sent < len) {
                    int rv = send(n->sock, buf + sent, (int) len - sent, flags);
                    if (rv < 0) {
                        if (net_in_progress(
#ifdef _WIN32                 
                                WSAGetLastError()
#else
                                errno
#endif
                                )) {
#ifdef _WIN32
                            WSAPOLLFD fds[1];
#else
                            struct pollfd fds[1];
#endif
                            memset(fds, 0, sizeof (fds));
                            fds[0].fd = n->sock;
                            fds[0].events = connect_ev;
                            fds[0].revents = 0;
                            if (sockpoll(fds, 1, -1) == -1) {
                                break;
                            }
                            continue;
                        }
                        break;
                    }
                    if (rv == 0) {
                        break;
                    }
                    sent += rv;
                }
            }
            set_event(n->ce);
        }
    }
    return status;
}
Пример #12
0
static void net_async_poll(am_net_t *n) {
    static const char *thisfunc = "net_async_poll():";
    int ev = 0;
    char first_run = 1;
#ifdef _WIN32
    WSAPOLLFD fds[1];
#else
    struct pollfd fds[1];
#endif

    n->tm = am_create_timer_event(AM_TIMER_EVENT_ONCE, AM_NET_POOL_TIMEOUT, n,
            net_async_poll_timeout);
    if (n->tm == NULL) {
        AM_LOG_ERROR(n->instance_id,
                "%s failed to create response timeout control", thisfunc);
        n->error = AM_ENOMEM;
        return;
    }
    if (n->tm->error != 0) {
        AM_LOG_ERROR(n->instance_id,
                "%s error %d creating response timeout control", thisfunc, n->tm->error);
        n->error = AM_ERROR;
        return;
    }

    am_start_timer_event(n->tm);

    memset(fds, 0, sizeof (fds));
    while (ev != -1) {

        fds[0].fd = n->sock;
        fds[0].events = read_ev;
        fds[0].revents = 0;

        if (first_run) {
            set_event(n->ce);
            if (n->on_connected) n->on_connected(n->data, 0);
            first_run = 0;
        }

        if (wait_for_exit_event(n->de) != 0) {
            break;
        }

        ev = sockpoll(fds, 1, 100);
        if (ev < 0) {
            net_log_error(n->instance_id, net_error());
            break;
        }
        if (ev == 1 && fds[0].revents & (POLLNVAL | POLLERR)) {
            if (n->on_close) n->on_close(n->data, 0);
            break;
        }
        if (ev == 1 && fds[0].revents & read_avail_ev) {
            /* read an output from a remote side */
            int er = 0, error = 0;
            char tmp[1024];
            int got = 0;
            SOCKLEN_T errlen = sizeof (error);
            er = getsockopt(n->sock, SOL_SOCKET, SO_ERROR, (void *) &error, &errlen);
            memset(&tmp[0], 0, sizeof (tmp));
            if (error != 0) break;

            got = recv(n->sock, tmp, sizeof (tmp), 0);
            if (n->ssl.on) {
                error = net_read_ssl(n, tmp, got);
                if (error != AM_SUCCESS) {
                    if (error != AM_EAGAIN) {
                        if (n->on_close) n->on_close(n->data, 0);
                        break;
                    }
                }
            } else {
                if (got < 0) {
                    if (!net_in_progress(errno)) {
                        if (n->on_close) n->on_close(n->data, 0);
                        break;
                    }
                } else if (got == 0) {
                    if (n->on_close) n->on_close(n->data, 0);
                    break;
                } else {
                    http_parser_execute(n->hp, n->hs, tmp, got);
                }
            }
        }
    }
}
Пример #13
0
 void base_lco::set_event_nonvirt()
 {
     set_event();
 }
Пример #14
0
int main (int argc, const char* argv[])
{
    char *line, *cmd, *args;
    ChessGame* game;
    ChessGameIterator iter;
    int quit = 0;

    chess_generate_init();

    game = chess_game_new();
    chess_game_iterator_init(&iter, game);
    print_board(&iter);

    line = 0;

    for (;;)
    {
        if (line)
            free(line);

        if (quit)
            break;

        line = read_line("> ");
        if (!parse_line(line, &cmd, &args))
            continue;

        if (!strcmp(cmd, "quit") || !strcmp(cmd, "q"))
        {
            quit = 1;
        }
        else if (!strcmp(cmd, "new"))
        {
            chess_game_iterator_cleanup(&iter);
            chess_game_reset(game);
            chess_game_iterator_init(&iter, game);
            print_board(&iter);
        }
        else if (!strcmp(cmd, "fen"))
        {
            load_fen(game, args);
        }
        else if (!strcmp(cmd, "pgn"))
        {
            save_pgn(game);
        }
        else if (!strcmp(cmd, "ls"))
        {
            list_moves(&iter);
        }
        else if (!strcmp(cmd, "moves"))
        {
            game_moves(game);
        }
        else if (!strcmp(cmd, "bd"))
        {
            print_board(&iter);
        }
        else if (!strcmp(cmd, "undo"))
        {
            undo_move(&iter);
        }
        else if (!strcmp(cmd, "event"))
        {
            set_event(game, args);
        }
        else if (!strcmp(cmd, "site"))
        {
            set_site(game, args);
        }
        else if (!strcmp(cmd, "date"))
        {
            set_date(game, args);
        }
        else if (!strcmp(cmd, "round"))
        {
            set_round(game, args);
        }
        else if (!strcmp(cmd, "white"))
        {
            set_white(game, args);
        }
        else if (!strcmp(cmd, "black"))
        {
            set_black(game, args);
        }
        else if (!strcmp(cmd, "result"))
        {
            set_result(game, args);
        }
        else
        {
            handle_move(&iter, cmd);
        }
    }

    chess_game_iterator_cleanup(&iter);
    chess_game_destroy(game);

    return 0;
}
Пример #15
0
NTSTATUS SERVICECALL
NtSignalAndWaitForSingleObject(IN HANDLE ObjectHandleToSignal,
		IN HANDLE WaitableObjectHandle,
		IN BOOLEAN Alertable,
		IN PLARGE_INTEGER TimeOut OPTIONAL)
{
	PVOID signal_obj, wait_obj;
	struct dispatcher_header *signal_header;
	LARGE_INTEGER _timeout;
	MODE previous_mode;
	NTSTATUS status;

	ktrace("ObjectHandleToSignal %p, WaitableObjectHandle %p, Alertable %d\n",
			ObjectHandleToSignal, WaitableObjectHandle, Alertable);
	previous_mode = (unsigned long)TimeOut > TASK_SIZE ? KernelMode : UserMode;
	if(TimeOut){
		if (previous_mode == UserMode) {
			if (copy_from_user(&_timeout, TimeOut, sizeof(_timeout)))
				return STATUS_NO_MEMORY;
		} else
			_timeout = *TimeOut;
	}

	status = ref_object_by_handle(ObjectHandleToSignal, 0,
			NULL, KernelMode, &signal_obj, NULL);
	if (!NT_SUCCESS(status))
		return status;

	status = ref_object_by_handle(WaitableObjectHandle, SYNCHRONIZE,
			NULL, KernelMode, &wait_obj, NULL);
	if (!NT_SUCCESS(status)) {
		deref_object(signal_obj);
		return status;
	}

	signal_header = (struct dispatcher_header *)signal_obj;

	if (is_wine_object(signal_header->type)) {
		struct object *obj = (struct object*)signal_obj;
		unsigned int access = get_handle_access(process2eprocess(current_thread->process), WaitableObjectHandle);
		if (BODY_TO_HEADER(obj)->ops->signal)
			BODY_TO_HEADER(obj)->ops->signal(obj, access);
	}
	else
		switch (signal_header->type) {
			case EventNotificationObject:
			case EventSynchronizationObject:
				set_event(signal_obj, EVENT_INCREMENT, TRUE);
				break;

			case MutantObject:
				release_mutant(signal_obj, IO_NO_INCREMENT, FALSE, TRUE);
				break;

			case SemaphoreObject:
				release_semaphore(signal_obj, SEMAPHORE_INCREMENT, 1, TRUE);
				break;

			default:
				deref_object(signal_obj);
				deref_object(wait_obj);
				return STATUS_OBJECT_TYPE_MISMATCH;
		}

	if(TimeOut){
		status = wait_for_single_object(wait_obj,
				UserRequest, KernelMode, Alertable, &_timeout);
	} else {
		status = wait_for_single_object(wait_obj,
				UserRequest, KernelMode, Alertable, NULL);
	}	

	if (!NT_SUCCESS(status))
		goto out;

	if (TimeOut) {
		if (previous_mode == UserMode) {
			if (copy_to_user(TimeOut, &_timeout, sizeof(_timeout))) {
				status = STATUS_NO_MEMORY;
				goto out;
			}
		} else
			*TimeOut = _timeout;
	}

out:
	deref_object(signal_obj);
	deref_object(wait_obj);

	return status;
} /* end NtSignalAndWaitForSingleObject */