Esempio n. 1
0
/* send a message after connected */
static void  on_connected(PER_HANDLE_DATA* handle_data)
{
    int error;
    DWORD bytes;
    const char* msg = "GET /index.html HTTP/1.0\r\n\r\n";
    size_t len = strlen(msg) + 1;

    assert(handle_data);
    fprintf(stdout, ("socket %d connected at %s.\n"), handle_data->socket, Now());

    memcpy_s(handle_data->buffer, kDefaultBufferSize, msg, len);
    memset(&handle_data->overlap, 0, sizeof(handle_data->overlap));
    handle_data->wsbuf.len = len;
    handle_data->opertype = OperSend;

    error = WSASend(handle_data->socket, &handle_data->wsbuf, 1, &bytes,
        0, &handle_data->overlap, NULL);
    if (error == SOCKET_ERROR)
    {
        error = WSAGetLastError();
        if (error != WSA_IO_PENDING)
        {
            fprintf(stderr, "WSASend() failed, socket: %d, %d: %s", handle_data->socket,
                error, LAST_ERROR_MSG);
            on_close(handle_data);
        }
        on_close(handle_data);
    }
}
Esempio n. 2
0
int worker_epoll_start(int epoll_fd, unsigned int max_events, int channel_fd)
{
	int nfds;
	struct epoll_event events[max_events];

	nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, EPOLL_DELAY);
	if(nfds == -1) {
		perror("epoll_wait");
		return L_HTTP_FAIL;
	}

	for(int n = 0; n < nfds; ++n) {
		if (events[n].data.fd == channel_fd) {
			//if((epoll_fd, listen_sock) == L_HTTP_FAIL)
			//	continue;
			if(on_channel(epoll_fd, channel_fd) == L_HTTP_FAIL)
				continue;
		}
		else if(events[n].events & EPOLLIN)//如果是已经连接的用户,并且收到数据,那么进行读入。
		{
			if(on_read(epoll_fd, events[n].data.fd) == L_HTTP_FAIL) {
				on_close(epoll_fd, events[n].data.fd);
				continue;
			}

		}
		else if(events[n].events & EPOLLOUT) // 如果有数据发送
		{
			on_write(epoll_fd, events[n].data.fd);
			on_close(epoll_fd, events[n].data.fd);
		}
	}

	return L_HTTP_SUCCESS;
}
Esempio n. 3
0
void msg_dialog_frame::CreateOsk(const std::string& msg, char16_t* osk_text)
{
	if (osk_dialog)
	{
		osk_dialog->close();
		delete osk_dialog;
	}

	osk_dialog = new custom_dialog(type.disable_cancel);
	osk_dialog->setModal(true);
	osk_text_return = osk_text;

	//Title
	osk_dialog->setWindowTitle(qstr(msg));
	osk_dialog->setWindowFlags(osk_dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint);

	//Text Input
	QLineEdit* input = new QLineEdit(osk_dialog);
	input->setFixedWidth(200);
	input->setFocus();

	//Ok Button
	QPushButton* button_ok = new QPushButton("Ok", osk_dialog);
	button_ok->setFixedWidth(50);

	//Layout
	QHBoxLayout* buttonsLayout = new QHBoxLayout;
	buttonsLayout->setAlignment(Qt::AlignCenter);
	buttonsLayout->addWidget(button_ok);

	QFormLayout* layout = new QFormLayout(osk_dialog);
	layout->setFormAlignment(Qt::AlignHCenter);
	layout->addRow(input);
	layout->addRow(buttonsLayout);
	osk_dialog->setLayout(layout);

	//Events
	connect(input, &QLineEdit::textChanged, [=] {
		std::memcpy(osk_text_return, reinterpret_cast<const char16_t*>(input->text().constData()), input->text().size() * 2);
		on_osk_input_entered();
	});
	connect(input, &QLineEdit::returnPressed, [=] { on_close(CELL_MSGDIALOG_BUTTON_OK); osk_dialog->accept(); });
	connect(button_ok, &QAbstractButton::clicked, [=] { on_close(CELL_MSGDIALOG_BUTTON_OK); osk_dialog->accept(); });
	connect(osk_dialog, &QDialog::rejected, [=] {if (!type.disable_cancel) { on_close(CELL_MSGDIALOG_BUTTON_ESCAPE); }});

	//Fix size
	osk_dialog->setFixedSize(osk_dialog->sizeHint());
	osk_dialog->show();
}
Esempio n. 4
0
/* send message back after recieved */
static void  on_recv(PER_HANDLE_DATA* handle_data)
{
    int error;
    DWORD bytes;

    assert(handle_data);

    /* pause */
    Sleep(200);

    bytes = handle_data->overlap.InternalHigh;
    memset(&handle_data->overlap, 0, sizeof(handle_data->overlap));
    handle_data->wsbuf.len = bytes;
    handle_data->wsbuf.buf[bytes] = '\0';
    handle_data->opertype =  OperSend;

    error = WSASend(handle_data->socket, &handle_data->wsbuf, 1, &bytes,
        0, &handle_data->overlap, NULL);
    if (error == SOCKET_ERROR)
    {
        error = WSAGetLastError();
        if (error != WSA_IO_PENDING)
        {
            fprintf(stderr, "WSASend() failed, socket: %d, %d: %s", handle_data->socket,
                error, LAST_ERROR_MSG);
            on_close(handle_data);
        }
    }
}
Esempio n. 5
0
void h2o_websocket_proceed(h2o_websocket_conn_t *conn)
{
    int handled;

    /* run the loop until getting to a point where no more progress can be achieved */
    do {
        handled = 0;
        if (!h2o_socket_is_writing(conn->sock) && wslay_event_want_write(conn->ws_ctx)) {
            if (wslay_event_send(conn->ws_ctx) != 0) {
                goto Close;
            }
            handled = 1;
        }
        if (conn->sock->input->size != 0 && wslay_event_want_read(conn->ws_ctx)) {
            if (wslay_event_recv(conn->ws_ctx) != 0) {
                goto Close;
            }
            handled = 1;
        }
    } while (handled);

    if (wslay_event_want_read(conn->ws_ctx)) {
        h2o_socket_read_start(conn->sock, on_recv);
    } else if (h2o_socket_is_writing(conn->sock) || wslay_event_want_write(conn->ws_ctx)) {
        h2o_socket_read_stop(conn->sock);
    } else {
        /* nothing is going on... close the socket */
        goto Close;
    }

    return;

Close:
    on_close(conn);
}
Esempio n. 6
0
void MainWindow::on_data_ready()
{
	while (true) {
		char raw;
		auto rc = port->read(&raw, sizeof(raw));
		if (rc == 0) {
			// no more data for now
			return;
		}
		if (rc < 0) {
			// an error has ocurred
			on_close();
			return;
		}

		switch (raw) {
			case '\r':
				break;
			case '\n':
				process_nmea();
				received_data.clear();
				break;
			default:
				if (received_data.size() > marnav::nmea::sentence::max_length) {
					// error ocurred, discard data
					received_data.clear();
				} else {
					received_data += raw;
				}
				break;
		}
	}
}
Esempio n. 7
0
File: main.cpp Progetto: vadmium/aot
LRESULT CALLBACK wnd_proc(HWND hw, UINT msg, WPARAM wp, LPARAM lp) {
    switch(msg)
    {
    case WM_CREATE:
        return on_create(hw);

    case WM_DESTROY:
        on_destroy(hw);
        break;

    case WM_CLOSE:
        on_close(hw);
        return 0;

    case WM_NOTIFY_ICON:
        return on_notify_icon(hw, lp);

        //~ case WM_COMMAND:
        //~ switch(LOWORD(wp))
        //~ {
        //~ case IDM_EXIT:
        //~ on_exit(hw);
        //~ break;
        //~ }
        //~ return 0;
    }

    return DefWindowProc(hw, msg, wp, lp);
}
Esempio n. 8
0
void timeout_callback(void *arg)
{
    module_data_t *mod = arg;

    asc_timer_destroy(mod->timeout_timer);
    mod->timeout_timer = NULL;

    switch(mod->is_connected)
    {
        case 0:
            call_error(mod, "connection timeout");
            break;
        case 1:
            call_error(mod, "connection failed");
            break;
        case 2:
            call_error(mod, "response timeout");
            break;
        default:
            call_error(mod, "unknown error");
            break;
    }

    on_close(mod);
}
Esempio n. 9
0
static void on_connect_err(void *arg)
{
    module_data_t *mod = arg;
    mod->is_connected = 1;
    timeout_callback(mod);
    on_close(mod);
}
Esempio n. 10
0
Iksu2000Plugin::Iksu2000Plugin()
{
    serial = new  QSerialPort();
    connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(on_serrial_error(QSerialPort::SerialPortError)));
    connect(serial, SIGNAL(aboutToClose()), this, SLOT(on_close()));
    connect(serial, SIGNAL(readyRead()),this, SLOT(sl_readyRead()));
    connect(this, SIGNAL(send_next_commond()), this, SLOT(sl_send_next_command()));
}
Esempio n. 11
0
  void close() {
    assert_none_locked();

#ifndef USE_GDI
    on_close();
#else /* ENABLE_SDL */
    ::SendMessage(hWnd, WM_CLOSE, 0, 0);
#endif
  }
Esempio n. 12
0
bool
TopWindow::on_event(const SDL_Event &event)
{
  switch (event.type) {
    Window *w;

  case SDL_VIDEOEXPOSE:
    invalidated_lock.Lock();
    invalidated = false;
    invalidated_lock.Unlock();

    expose();
    return true;

  case SDL_KEYDOWN:
    w = get_focused_window();
    if (w == NULL)
      w = this;

    return w->on_key_down(event.key.keysym.sym);

  case SDL_KEYUP:
    w = get_focused_window();
    if (w == NULL)
      w = this;

    return w->on_key_up(event.key.keysym.sym);

  case SDL_MOUSEMOTION:
    // XXX keys
    return on_mouse_move(event.motion.x, event.motion.y, 0);

  case SDL_MOUSEBUTTONDOWN:
    if (event.button.button == SDL_BUTTON_WHEELUP)
      return on_mouse_wheel(event.button.x, event.button.y, 1);
    else if (event.button.button == SDL_BUTTON_WHEELDOWN)
      return on_mouse_wheel(event.button.x, event.button.y, -1);

    static PeriodClock double_click;
    return double_click.check_always_update(300)
      ? on_mouse_down(event.button.x, event.button.y)
      : on_mouse_double(event.button.x, event.button.y);

  case SDL_MOUSEBUTTONUP:
    if (event.button.button == SDL_BUTTON_WHEELUP ||
        event.button.button == SDL_BUTTON_WHEELDOWN)
      /* the wheel has already been handled in SDL_MOUSEBUTTONDOWN */
      return false;

    return on_mouse_up(event.button.x, event.button.y);

  case SDL_QUIT:
    return on_close();
  }

  return false;
}
Esempio n. 13
0
			LRESULT callback_main(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
			{
				//m_hWnd = hWnd;

				switch( msg )
				{
				case WM_CLOSE:	
					if ( on_close() )
						::DestroyWindow(hWnd);
					return 0;

				case WM_CREATE:	
					return ( on_create((::LPCREATESTRUCT)lParam) ? 0 : -1 );

				case WM_DESTROY:
					on_destroy();
					return 0;

				case WM_MOVE:
					on_move(LOWORD(lParam), HIWORD(lParam));
					return 0;

				case WM_SIZING:
					switch(wParam)
					{
					case WMSZ_LEFT:
						on_sizing(sizing_hold_bit_left, (const LPRECT)lParam);
						break;
					case WMSZ_RIGHT:
						on_sizing(sizing_hold_bit_right, (const LPRECT)lParam);
						break;
					case WMSZ_TOP:
						on_sizing(sizing_hold_bit_top, (const LPRECT)lParam);
						break;
					case WMSZ_BOTTOM:
						on_sizing(sizing_hold_bit_bottom, (const LPRECT)lParam);
						break;
						
					case WMSZ_TOPLEFT:
						on_sizing(sizing_hold_bit_top | sizing_hold_bit_left, (const LPRECT)lParam);
						break;
					case WMSZ_TOPRIGHT:
						on_sizing(sizing_hold_bit_top | sizing_hold_bit_right, (const LPRECT)lParam);
						break;
					case WMSZ_BOTTOMLEFT:
						on_sizing(sizing_hold_bit_bottom | sizing_hold_bit_left, (const LPRECT)lParam);
						break;
					case WMSZ_BOTTOMRIGHT:
						on_sizing(sizing_hold_bit_bottom | sizing_hold_bit_right, (const LPRECT)lParam);
						break;
					}
					return TRUE;
				}

				return ::DefWindowProc( hWnd, msg, wParam, lParam );
			}
Esempio n. 14
0
static void on_recv(h2o_socket_t *sock, const char *err)
{
    h2o_websocket_conn_t *conn = sock->data;

    if (err != NULL) {
        on_close(conn);
        return;
    }
    h2o_websocket_proceed(conn);
}
Esempio n. 15
0
void AmAudioFile::close()
{
  if(fp){
    on_close();

    if(close_on_exit)
      fclose(fp);
    fp = 0;
  }
}
Esempio n. 16
0
static void on_write_complete(h2o_socket_t *sock, int status)
{
    h2o_websocket_conn_t *conn = sock->data;

    if (status == -1) {
        on_close(conn);
        return;
    }
    h2o_websocket_proceed(conn);
}
static void on_timer(int epoll_fd)
{
	// 这次每次都调用is_time_out,会有多次time系统调用,需改进
	std::map<int, socket_buf>::iterator it = sb_array.begin();
	for(; it != sb_array.end(); it++) {
		if(is_time_out(it->second->timer) == HTTP_TIME_OUT){
			printf("connect:%d time out\n", it->first);
			on_close(epoll_fd, it->first);
		}
	}
}
Esempio n. 18
0
int  loop_run(int timeout)
{
    int error;
    DWORD bytes = 0;
    PER_HANDLE_DATA* handle_data = NULL;
    WSAOVERLAPPED* overlap = NULL;

    error = GetQueuedCompletionStatus(default_loop.completion_port, &bytes,
        (ULONG_PTR*)&handle_data, &overlap, timeout);
    if (error == 0)
    {
        error = GetLastError();
        if (error == WAIT_TIMEOUT)
        {
            return 1;
        }
        fprintf(stderr, "%d: %s", error, LAST_ERROR_MSG);
        if (overlap != NULL)
        {
            handle_data->opertype = OperClose;
        }
        else
        {
            if (error == ERROR_INVALID_HANDLE)
            {
                return 0;
            }
            return 1;
        }
    }

    switch(handle_data->opertype)
    {
    case OperConnect:
        on_connected(handle_data);
        break;

    case OperRecv:
        on_recv(handle_data);
        break;

    case OperSend:
        after_sent(handle_data);
        break;

    case OperClose:
        on_close(handle_data);
        break;

    default:
        assert(0);
    }
    return 1;
}
Esempio n. 19
0
bool
TaskAutoPilot::update_computer(TaskAccessor& task,
                               const AIRCRAFT_STATE& state)
{
  if (!far_from_target(task, state)) {
    on_close();
    return do_advance(task);
  }

  advance_if_required(task);

  return !has_finished(task);
}
Esempio n. 20
0
static void on_write_complete(h2o_socket_t *sock, const char *err)
{
    h2o_websocket_conn_t *conn = sock->data;

    if (err != NULL) {
        on_close(conn);
        return;
    }
    assert(conn->_write_buf.cnt > 0);
    free_write_buf(conn);
    conn->_write_buf.cnt = 0;

    h2o_websocket_proceed(conn);
}
Esempio n. 21
0
int app::run(){
	
	while(m_brun){
		
		void* data = NULL;
		if(m_ring_buf.pop(data) == -1){
			continue;
		}
		
		app_hd * msg = (app_hd*)data;
		if(msg->type == tcp_type){
			switch(msg->event){
				case ev_sys_recv:
					on_recv(msg->u.tcp.n, msg->content, msg->length);
					break;
					
				case ev_sys_close:
					on_close(msg->u.tcp.n, *((int*)msg->content));
					delete msg->u.tcp.n;
					break;
					
				case ev_sys_accept:
					on_accept(msg->u.tcp.n);
					break;
					
				case ev_sys_connect_ok:
					on_connect(msg->u.tcp.n);
					break;
					
				case ev_sys_connect_fail:
					on_connect(msg->u.tcp.n);
					delete msg->u.tcp.n;
				break;
				
			}
		}
		else if(msg->type == timer_type){
			on_timer(msg->event, msg->u.timer.interval, msg->u.timer.ptr);
		}
		else if(msg->type == app_type){
			on_app(msg->event, msg->content, msg->length);
		}
		else{
			error_log("msg from unknown app_name(%s)\n", m_name);
		}
		free(msg);
	}
	return 0;
}
Esempio n. 22
0
/*!
 * \brief Open a File handle.
 *
 * Once the handle has been opened, the file operation functions, such as
 * File::read(), are available to use.
 *
 * \note This function should generally only be called by subclasses. Most
 *       subclasses will provide a variant of this function that can take
 *       parameters, such as a filename.
 *
 * \return
 *   * #FileStatus::OK if there is no file open handler or if the file open
 *     handle succeeds
 *   * \<= #FileStatus::WARN if an error occurs
 */
FileStatus File::open()
{
    GET_PIMPL_OR_RETURN(FileStatus::FATAL);
    ENSURE_STATE(FileState::NEW);

    auto ret = on_open();

    if (ret == FileStatus::OK) {
        priv->state = FileState::OPENED;
    } else {
        // If the file was not successfully opened, then close it
        on_close();
    }

    return ret;
}
Esempio n. 23
0
void Server::dispatch_message(string msg) {
	string command; 
	string argument;
	
	if (msg.size() < 1) {
		on_close();
		return;
	}
	
	command = get_command(msg);
	
	if (command != "HANDSHAKE" && this->nick.empty()) {
		say(string("You dont have a nickname, no other actions can be performed. Please use HANDSHAKE command"));
		return;
	}

	if (command == "HANDSHAKE") {
		c_handshake(msg);
	}

	else if (command == "PMSG") {
		c_pmsg(msg);
	}

	else if (command == "SVERSION") {
		say("talkr server");
	}

	else if (command == "JOIN") {
		c_join(msg);
	}

	else if (command == "PART") {
		c_part(msg);
	}

	else if (command == "CMSG") {
		c_cmsg(msg);
	}
	
	else if (command == "QUIT") {
		c_quit(msg);
	}
	else {
		say("UNRECOGNIZED COMMAND");
	} 
}
Esempio n. 24
0
static void on_key( unsigned char key, const int x, int y )
{
    // ignore if no reference
    if( global_gui_context == NULL )
    {
        return;
    }


    // check key
    if( (key == '\e') || (key == GUI_KEY_EXIT) )
    {
        // close
        on_close();
        return;
    }
    else if( key == GUI_KEY_HELP )
Esempio n. 25
0
void on_read(void *arg)
{
    module_data_t *mod = (module_data_t *)arg;

    ssize_t len = asc_socket_recv(mod->sock, mod->buffer, UDP_BUFFER_SIZE);
    if(len <= 0)
    {
        on_close(arg);
        return;
    }

    // 12 - RTP header size
    ssize_t i = (mod->is_rtp) ? 12 : 0;
    for(; i < len; i += TS_PACKET_SIZE)
        module_stream_send(mod, &mod->buffer[i]);
    if (i != len)
        asc_log_warning(MSG("Lost bytes: %d, because UDP packet size is wrong"), len - i);
}
Esempio n. 26
0
BrowserObjectDiagram::~BrowserObjectDiagram()
{
    if (deletedp() && !modifiedp()) {
        QString fn;

        fn.sprintf("%d.diagram", get_ident());

        QDir d = BrowserView::get_dir();

        QFile::remove(d.absFilePath(fn));
    }
    on_close();
//    if(window)
//        window->set_browser_diagram(0);

    all.remove(get_ident());
    delete def;
}
Esempio n. 27
0
/*!
 * \brief Close a File handle.
 *
 * This function will close a File handle if it is open. Regardless of the
 * return value, the handle will be closed. The file handle can then be reused
 * for opening another file.
 *
 * \return
 *   * #FileStatus::OK if no error was encountered when closing the handle.
 *   * \<= #FileStatus::WARN if the handle is opened and an error occurs while
 *     closing the file
 */
FileStatus File::close()
{
    GET_PIMPL_OR_RETURN(FileStatus::FATAL);

    auto ret = FileStatus::OK;

    // Avoid double-closing or closing nothing
    if (!(priv->state & FileState::NEW)) {
        ret = on_close();

        // Don't change state to FileState::FATAL if FileStatus::FATAL is
        // returned. Otherwise, we risk double-closing the file.
    }

    priv->state = FileState::NEW;

    return ret;
}
Esempio n. 28
0
void h2o_websocket_proceed(h2o_websocket_conn_t *conn)
{
    int handled;

    /* run the loop until getting to a point where no more progress can be achieved */
    do {
        handled = 0;
        if (!h2o_socket_is_writing(conn->sock) && wslay_event_want_write(conn->ws_ctx)) {
            if (wslay_event_send(conn->ws_ctx) != 0) {
                goto Close;
            }
            /* avoid infinite loop when user want send more bufers count than ours in on_msg_callback() */
            if (conn->_write_buf.cnt < sizeof(conn->_write_buf.bufs) / sizeof(conn->_write_buf.bufs[0])) {
                handled = 1;
            }
        }
        if (conn->sock->input->size != 0 && wslay_event_want_read(conn->ws_ctx)) {
            if (wslay_event_recv(conn->ws_ctx) != 0) {
                goto Close;
            }
            handled = 1;
        }
    } while (handled);

    if (!h2o_socket_is_writing(conn->sock) && conn->_write_buf.cnt > 0) {
        /* write */
        h2o_socket_write(conn->sock, conn->_write_buf.bufs, conn->_write_buf.cnt, on_write_complete);
    }

    if (wslay_event_want_read(conn->ws_ctx)) {
        h2o_socket_read_start(conn->sock, on_recv);
    } else if (h2o_socket_is_writing(conn->sock) || wslay_event_want_write(conn->ws_ctx)) {
        h2o_socket_read_stop(conn->sock);
    } else {
        /* nothing is going on... close the socket */
        goto Close;
    }

    return;

Close:
    on_close(conn);
}
Esempio n. 29
0
int app::dispatch(const app_hd * msg){
	
	if(msg->type == tcp_type){
		switch(msg->event){
			case ev_sys_recv:
				on_recv(msg->u.tcp.n, msg->content, msg->length);
				break;
				
			case ev_sys_close:
				on_close(msg->u.tcp.n, *((int*)msg->content));
				delete msg->u.tcp.n;
				break;
				
			case ev_sys_accept:
				on_accept(msg->u.tcp.n);
				break;
				
			case ev_sys_connect_ok:
				on_connect(msg->u.tcp.n);
				break;
				
			case ev_sys_connect_fail:
				on_connect(msg->u.tcp.n);
				delete msg->u.tcp.n;
			break;
			
			case ev_sys_write:
				handle_write(msg->u.tcp.n);
			break;
		}
	}
	else if(msg->type == timer_type){
		on_timer(msg->event, msg->u.timer.interval, msg->u.timer.ptr);
	}
	else if(msg->type == app_type){
		on_app(msg->event, msg->content, msg->length);
	}
	else{
		error_log("msg from unknown app_name(%s)\n", m_name);
	}
	return 0;	
}
Esempio n. 30
0
		void user_interface::close()
		{
			// Force unload
			exit = true;
			if (auto manager = fxm::get<display_manager>())
			{
				if (auto dlg = manager->get<rsx::overlays::message_dialog>())
				{
					if (dlg->progress_bar_count())
						Emu.GetCallbacks().handle_taskbar_progress(0, 1);
				}

				manager->remove(uid);
			}

			pad::SetIntercepted(false);

			if (on_close)
				on_close(return_code);
		}