Esempio n. 1
0
void HttpClient::on_connected(gpointer data, bool succeeded)
{
	HttpClient *oHttpClient = (HttpClient *)data;
	if (!succeeded) {
		gchar *mes = g_strdup_printf("Can not connect to %s: %s\n",
			oHttpClient->host_.c_str(), Socket::get_error_msg().c_str());
		on_error_.emit(oHttpClient, mes);
		g_free(mes);
		return;
	}
#ifdef _WIN32
	oHttpClient->channel_ = g_io_channel_win32_new_socket(oHttpClient->sd_);
#else
	oHttpClient->channel_ = g_io_channel_unix_new(oHttpClient->sd_);
#endif
	g_io_channel_set_encoding(oHttpClient->channel_, NULL, NULL);
	/* make sure that the channel is non-blocking */
	int flags = g_io_channel_get_flags(oHttpClient->channel_);
	flags |= G_IO_FLAG_NONBLOCK;
	GError *err = NULL;
	g_io_channel_set_flags(oHttpClient->channel_, GIOFlags(flags), &err);
	if (err) {
		gchar *str = g_strdup_printf("Unable to set the channel as non-blocking: %s", err->message);
		on_error_.emit(oHttpClient, str);
		g_free(str);
		g_error_free(err);
		return;
	}
	if (oHttpClient->SendGetRequest())
		return;
	oHttpClient->out_source_id_ = g_io_add_watch(oHttpClient->channel_, GIOCondition(G_IO_OUT), on_io_out_event, oHttpClient);
	oHttpClient->in_source_id_ = g_io_add_watch(oHttpClient->channel_, GIOCondition(G_IO_IN | G_IO_ERR), on_io_in_event, oHttpClient);
}
Esempio n. 2
0
void DictClient::on_resolved(gpointer data, struct hostent *ret)
{
	DictClient *oDictClient = (DictClient *)data;
	oDictClient->sd_ = Socket::socket();

	if (oDictClient->sd_ == -1) {
		on_error_.emit("Can not create socket: " + Socket::get_error_msg());
		return;
	}

#ifdef _WIN32
	oDictClient->channel_ = g_io_channel_win32_new_socket(oDictClient->sd_);
#else
	oDictClient->channel_ = g_io_channel_unix_new(oDictClient->sd_);
#endif

/* RFC2229 mandates the usage of UTF-8, so we force this encoding */
	g_io_channel_set_encoding(oDictClient->channel_, "UTF-8", NULL);

	g_io_channel_set_line_term(oDictClient->channel_, "\r\n", 2);

/* make sure that the channel is non-blocking */
	int flags = g_io_channel_get_flags(oDictClient->channel_);
	flags |= G_IO_FLAG_NONBLOCK;
	GError *err = NULL;
	g_io_channel_set_flags(oDictClient->channel_, GIOFlags(flags), &err);
	if (err) {
		g_io_channel_unref(oDictClient->channel_);
		oDictClient->channel_ = NULL;
		on_error_.emit("Unable to set the channel as non-blocking: " +
			       std::string(err->message));
		g_error_free(err);
		return;
	}

	if (!Socket::connect(oDictClient->sd_, ret, oDictClient->port_)) {
		gchar *mes = g_strdup_printf("Can not connect to %s: %s\n",
					     oDictClient->host_.c_str(), Socket::get_error_msg().c_str());
		on_error_.emit(mes);
		g_free(mes);
		return;
	}

	oDictClient->source_id_ = g_io_add_watch(oDictClient->channel_, GIOCondition(G_IO_IN | G_IO_ERR),
				   on_io_event, oDictClient);
}
Esempio n. 3
0
void StarDictClient::on_connected(gpointer data, bool succeeded)
{
    StarDictClient *oStarDictClient = (StarDictClient *)data;
    if (!succeeded) {
	static bool showed_once = false;
	if (!showed_once) {
		showed_once = true;
	        gchar *mes = g_strdup_printf(_("Can not connect to %s: %s\n"), oStarDictClient->host_.c_str(), Socket::get_error_msg().c_str());
        	on_error_.emit(mes);
	        g_free(mes);
	}
        return;
    }
#ifdef _WIN32
    oStarDictClient->channel_ = g_io_channel_win32_new_socket(oStarDictClient->sd_);
#else
    oStarDictClient->channel_ = g_io_channel_unix_new(oStarDictClient->sd_);
#endif

    g_io_channel_set_encoding(oStarDictClient->channel_, NULL, NULL);

    /* make sure that the channel is non-blocking */
    int flags = g_io_channel_get_flags(oStarDictClient->channel_);
    flags |= G_IO_FLAG_NONBLOCK;
    GError *err = NULL;
    g_io_channel_set_flags(oStarDictClient->channel_, GIOFlags(flags), &err);
    if (err) {
        g_io_channel_unref(oStarDictClient->channel_);
        oStarDictClient->channel_ = NULL;
        gchar *str = g_strdup_printf(_("Unable to set the channel as non-blocking: %s"), err->message);
        on_error_.emit(str);
        g_free(str);
        g_error_free(err);
        return;
    }

    oStarDictClient->is_connected_ = true;
    oStarDictClient->waiting_banner_ = true;
    oStarDictClient->reading_type_ = READ_LINE;
    oStarDictClient->in_source_id_ = g_io_add_watch(oStarDictClient->channel_, GIOCondition(G_IO_IN | G_IO_ERR), on_io_in_event, oStarDictClient);
}