static VALUE rg_readline(gint argc, VALUE *argv, VALUE self) { gchar* str; VALUE line_term, ret; GIOStatus status; GError* err = NULL; const gchar* old_line_term = NULL; gint old_line_term_len; rb_scan_args(argc, argv, "01", &line_term); if (! NIL_P(line_term)){ StringValue(line_term); old_line_term = g_io_channel_get_line_term(_SELF(self), &old_line_term_len); g_io_channel_set_line_term(_SELF(self), RVAL2CSTR(line_term), RSTRING_LEN(line_term)); } status = g_io_channel_read_line(_SELF(self), &str, NULL, NULL, &err); if (! NIL_P(line_term)){ g_io_channel_set_line_term(_SELF(self), old_line_term, old_line_term_len); } ioc_error(status, err); ret = str ? CSTR2RVAL(str) : CSTR2RVAL(""); g_free(str); return ret; }
static gboolean setup_connection (BaconMessageConnection *conn) { g_return_val_if_fail (conn->chan == NULL, FALSE); conn->chan = g_io_channel_unix_new (conn->fd); if (!conn->chan) { return FALSE; } g_io_channel_set_line_term (conn->chan, "\n", 1); conn->conn_id = g_io_add_watch (conn->chan, G_IO_IN, server_cb, conn); return TRUE; }
/* Internal use only */ static VALUE ioc_set_line_term(VALUE args) { VALUE self = RARRAY_PTR(args)[0]; VALUE doit = RARRAY_PTR(args)[1]; VALUE line_term = RARRAY_PTR(args)[2]; if (doit == Qtrue){ StringValue(line_term); g_io_channel_set_line_term(_SELF(self), RVAL2CSTR(line_term), RSTRING_LEN(line_term)); } return self; }
// try to connect to MEGASync notify server // return TRUE if connection is established static gboolean mega_notify_client_try_connect(MEGAExt *mega_ext) { int len; struct sockaddr_un remote; gchar *sock_path; const gchar sock_file[] = "notify.socket"; // XXX: current path MEGASync uses to store private data const gchar sock_path_hardcode[] = ".local/share/data/Mega Limited/MEGAsync"; if ((mega_ext->notify_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { g_warning("socket() failed: %s", strerror(errno)); mega_notify_client_destroy(mega_ext); return FALSE; } sock_path = g_build_filename(g_get_home_dir(), sock_path_hardcode, sock_file, NULL); remote.sun_family = AF_UNIX; strncpy(remote.sun_path, sock_path, sizeof(remote.sun_path)); g_free(sock_path); len = strlen(remote.sun_path) + sizeof(remote.sun_family); if (connect(mega_ext->notify_sock, (struct sockaddr *)&remote, len) == -1) { g_warning("connect() failed"); mega_notify_client_destroy(mega_ext); return FALSE; } g_debug("Connected to notify server!"); mega_ext->notify_chan = g_io_channel_unix_new(mega_ext->notify_sock); if (!mega_ext->notify_chan) { g_warning("g_io_channel_unix_new() failed"); mega_notify_client_destroy(mega_ext); return FALSE; } g_io_channel_set_line_term(mega_ext->notify_chan, "\n", -1); g_io_channel_set_close_on_unref(mega_ext->notify_chan, TRUE); if (!g_io_add_watch(mega_ext->notify_chan, G_IO_IN | G_IO_HUP, mega_notify_client_read, mega_ext)) { g_warning("g_io_add_watch() failed!"); mega_notify_client_destroy(mega_ext); return FALSE; } return TRUE; }
static VALUE rg_each(gint argc, VALUE *argv, VALUE self) { gchar* str; VALUE line_term; GIOStatus status; GError* err = NULL; GIOChannel *channel; const gchar* old_line_term = NULL; gint old_line_term_len; if (!rb_block_given_p()) { rb_raise(rb_eArgError, "called without a block"); } rb_scan_args(argc, argv, "01", &line_term); channel = _SELF(self); if (!NIL_P(line_term)) { StringValue(line_term); old_line_term = g_io_channel_get_line_term(channel, &old_line_term_len); g_io_channel_set_line_term(channel, RVAL2CSTR(line_term), RSTRING_LEN(line_term)); } while (TRUE) { status = g_io_channel_read_line(channel, &str, NULL, NULL, &err); if (status == G_IO_STATUS_EOF) { break; } else { VALUE rstr; ioc_error(status, err); if (str) { rstr = CSTR2RVAL(str); } else { rstr = CSTR2RVAL(""); } g_free(str); rb_ensure(rb_yield, rstr, ioc_set_line_term, rb_ary_new3(3, self, NIL_P(line_term) ? Qfalse : Qtrue, CSTR2RVAL(old_line_term))); } } return self; }
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); }
void server(const char *working_directory) { char *socket_filename = g_strdup_printf("%s/sphubd", working_directory); GIOChannel *ioc = io_bind_unix_socket(socket_filename); free(socket_filename); fail_unless(ioc); GIOChannel *aioc = io_accept_connection(ioc); fail_unless(aioc); g_io_channel_set_line_term(aioc, "|", 1); ui_t *ui = ui_init(); fail_unless(ui); ui->io_channel = aioc; /* setup callbacks */ ui->cb_search = test_cb_search; ui->cb_search_all = test_cb_search_all; ui->cb_connect = test_cb_connect; ui->cb_disconnect = test_cb_disconnect; ui->cb_public_message = test_cb_public_message; ui->cb_private_message = test_cb_private_message; ui->cb_download_file = test_cb_download_file; ui->cb_download_filelist = test_cb_download_filelist; ui->cb_queue_remove_target = test_cb_queue_remove_target; ui->cb_queue_remove_filelist = test_cb_queue_remove_filelist; ui->cb_queue_remove_source = test_cb_queue_remove_source; ui->cb_queue_remove_nick = test_cb_queue_remove_nick; ui->cb_cancel_transfer = test_cb_cancel_transfer; ui->cb_transfer_stats_interval = test_cb_set_transfer_stats_interval; ui->cb_set_port = test_cb_set_port; ui->cb_add_shared_path = test_cb_add_shared_path; ui->cb_remove_shared_path = test_cb_remove_shared_path; ui->cb_set_password = test_cb_set_password; /* must be last, quits the runloop */ ui->cb_shutdown = test_cb_shutdown; loop = g_main_loop_new(NULL, FALSE); g_io_add_watch(ui->io_channel, G_IO_IN | G_IO_ERR | G_IO_HUP, test_in_event, ui); g_main_loop_run(loop); printf("main loop finished\n"); }
static gpointer dropbox_command_client_thread(DropboxCommandClient *dcc) { struct sockaddr_un addr; socklen_t addr_len; int connection_attempts = 1; /* intialize address structure */ addr.sun_family = AF_UNIX; g_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/.dropbox/command_socket", g_get_home_dir()); addr_len = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path); while (1) { GIOChannel *chan = NULL; GError *gerr = NULL; int sock; gboolean failflag = TRUE; do { int flags; if (0 > (sock = socket(PF_UNIX, SOCK_STREAM, 0))) { /* WTF */ break; } /* set timeout on socket, to protect against bad servers */ { struct timeval tv = {3, 0}; if (0 > setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) || 0 > setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval))) { /* debug("setsockopt failed"); */ break; } } /* set native non-blocking, for connect timeout */ { if ((flags = fcntl(sock, F_GETFL, 0)) < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) { /* debug("fcntl failed"); */ break; } } /* if there was an error we have to try again later */ if (connect(sock, (struct sockaddr *) &addr, addr_len) < 0) { if (errno == EINPROGRESS) { fd_set writers; struct timeval tv = {1, 0}; FD_ZERO(&writers); FD_SET(sock, &writers); /* if nothing was ready after 3 seconds, fail out homie */ if (select(sock+1, NULL, &writers, NULL, &tv) == 0) { /* debug("connection timeout"); */ break; } if (connect(sock, (struct sockaddr *) &addr, addr_len) < 0) { /* debug("couldn't connect to command server after 1 second"); */ break; } } /* errno != EINPROGRESS */ else { /* debug("bad connection"); */ break; } } /* set back to blocking */ if (fcntl(sock, F_SETFL, flags) < 0) { /* debug("fcntl2 failed"); */ break; } failflag = FALSE; } while (0); if (failflag) { ConnectionAttempt *ca = g_new(ConnectionAttempt, 1); ca->dcc = dcc; ca->connect_attempt = connection_attempts; g_idle_add((GSourceFunc) on_connection_attempt, ca); if (sock >= 0) { close(sock); } g_usleep(G_USEC_PER_SEC); connection_attempts++; continue; } else { connection_attempts = 0; } /* connected */ debug("command client connected"); chan = g_io_channel_unix_new(sock); g_io_channel_set_close_on_unref(chan, TRUE); g_io_channel_set_line_term(chan, "\n", -1); #define SET_CONNECTED_STATE(s) { \ g_mutex_lock(dcc->command_connected_mutex); \ dcc->command_connected = s; \ g_mutex_unlock(dcc->command_connected_mutex); \ } SET_CONNECTED_STATE(TRUE); g_idle_add((GSourceFunc) on_connect, dcc); while (1) { DropboxCommand *dc; while (1) { GTimeVal gtv; g_get_current_time(>v); g_time_val_add(>v, G_USEC_PER_SEC / 10); /* get a request from caja */ dc = g_async_queue_timed_pop(dcc->command_queue, >v); if (dc != NULL) { break; } else { if (check_connection(chan) == FALSE) { goto BADCONNECTION; } } } /* this pointer should be unique */ if ((gpointer (*)(DropboxCommandClient *data)) dc == &dropbox_command_client_thread) { debug("got a reset request"); goto BADCONNECTION; } switch (dc->request_type) { case GET_FILE_INFO: { debug("doing file info command"); do_file_info_command(chan, (DropboxFileInfoCommand *) dc, &gerr); } break; case GENERAL_COMMAND: { debug("doing general command"); do_general_command(chan, (DropboxGeneralCommand *) dc, &gerr); } break; default: g_assert_not_reached(); break; } debug("done."); if (gerr != NULL) { // debug("COMMAND ERROR*****************************"); /* mark this request as never to be completed */ end_request(dc); debug("command error: %s", gerr->message); g_error_free(gerr); BADCONNECTION: /* grab all the rest of the data off the async queue and mark it never to be completed, who knows how long we'll be disconnected */ while ((dc = g_async_queue_try_pop(dcc->command_queue)) != NULL) { end_request(dc); } g_io_channel_unref(chan); SET_CONNECTED_STATE(FALSE); /* call the disconnect handler */ g_idle_add((GSourceFunc) on_disconnect, dcc); break; } } #undef SET_CONNECTED_STATE } return NULL; }
static gboolean process_mail (int argc, char *argv[]) { GIOChannel *io = NULL; GIOStatus status; gchar *line = NULL; gsize length; int i; DBusGProxy *proxy = NULL; GString *body = NULL; gboolean next_password = FALSE; proxy = get_dbus_proxy(); /* if (!proxy) return FALSE; */ if (proxy && !org_MilterZipcrypt_Sendmail_from(proxy, from, NULL)) goto fail; if (proxy) { for (i = 1; i < argc; i++) { if (!org_MilterZipcrypt_Sendmail_recipient(proxy, argv[i], NULL)) goto fail; } } io = g_io_channel_unix_new(STDIN_FILENO); g_io_channel_set_line_term(io, "\r\n", 2); body = g_string_new(NULL); do { status = g_io_channel_read_line(io, &line, &length, NULL, NULL); if (line) { if (next_password) { gchar *password; password = line; if (password && proxy && !org_MilterZipcrypt_Sendmail_password(proxy, password, NULL)) goto fail; next_password = FALSE; } if (g_str_equal(line, ".\r\n")) break; if (g_str_has_prefix(line, "The password of ")) next_password = TRUE; g_string_append(body, line); } } while (status == G_IO_STATUS_NORMAL || status == G_IO_STATUS_AGAIN); g_io_channel_unref(io); io = NULL; if (proxy && !org_MilterZipcrypt_Sendmail_body(proxy, body->str, NULL)) goto fail; g_string_free(body, TRUE); body = NULL; return (status == G_IO_STATUS_NORMAL) ? TRUE : FALSE; fail: g_object_unref(proxy); if (body) g_string_free(body, TRUE); if (io) g_io_channel_unref(io); return FALSE; }
static gboolean try_to_connect(NautilusDropboxHookserv *hookserv) { /* create socket */ hookserv->socket = socket(PF_UNIX, SOCK_STREAM, 0); /* set native non-blocking, for connect timeout */ { unsigned int flags; if ((flags = fcntl(hookserv->socket, F_GETFL, 0)) < 0) { goto FAIL_CLEANUP; } if (fcntl(hookserv->socket, F_SETFL, flags | O_NONBLOCK) < 0) { goto FAIL_CLEANUP; } } /* connect to server, might fail of course */ { struct sockaddr_un addr; socklen_t addr_len; /* intialize address structure */ addr.sun_family = AF_UNIX; g_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/.dropbox/iface_socket", g_get_home_dir()); addr_len = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path); /* if there was an error we have to try again later */ if (connect(hookserv->socket, (struct sockaddr *) &addr, addr_len) < 0) { if (errno == EINPROGRESS) { fd_set writers; struct timeval tv = {1, 0}; FD_ZERO(&writers); FD_SET(hookserv->socket, &writers); /* if nothing was ready after 3 seconds, fail out homie */ if (select(hookserv->socket+1, NULL, &writers, NULL, &tv) == 0) { goto FAIL_CLEANUP; } if (connect(hookserv->socket, (struct sockaddr *) &addr, addr_len) < 0) { debug("couldn't connect to hook server after 1 second"); goto FAIL_CLEANUP; } } else { goto FAIL_CLEANUP; } } } /* lol sometimes i write funny codez */ if (FALSE) { FAIL_CLEANUP: close(hookserv->socket); g_timeout_add_seconds(1, (GSourceFunc) try_to_connect, hookserv); return FALSE; } /* great we connected!, let's create the channel and wait on it */ hookserv->chan = g_io_channel_unix_new(hookserv->socket); g_io_channel_set_line_term(hookserv->chan, "\n", -1); g_io_channel_set_close_on_unref(hookserv->chan, TRUE); /*debug("create channel"); */ /* Set non-blocking ;) (again just in case) */ { GIOFlags flags; GIOStatus iostat; flags = g_io_channel_get_flags(hookserv->chan); iostat = g_io_channel_set_flags(hookserv->chan, flags | G_IO_FLAG_NONBLOCK, NULL); if (iostat == G_IO_STATUS_ERROR) { g_io_channel_unref(hookserv->chan); g_timeout_add_seconds(1, (GSourceFunc) try_to_connect, hookserv); return FALSE; } } /*debug("set non blocking"); */ /* this is fun, async io watcher */ hookserv->hhsi.line = 0; hookserv->hhsi.command_args = NULL; hookserv->hhsi.command_name = NULL; hookserv->event_source = g_io_add_watch_full(hookserv->chan, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL, (GIOFunc) handle_hook_server_input, hookserv, (GDestroyNotify) watch_killer); debug("hook client connected"); hookserv->connected = TRUE; g_hook_list_invoke(&(hookserv->onconnect_hooklist), FALSE); /*debug("added watch");*/ return FALSE; }
gboolean StarDictClient::on_io_in_event(GIOChannel *ch, GIOCondition cond, gpointer user_data) { StarDictClient *stardict_client = static_cast<StarDictClient *>(user_data); if (!stardict_client->channel_) { //g_warning(_("No channel available\n")); return FALSE; } if (cond & G_IO_ERR) { /*gchar *mes = g_strdup_printf(_("Connection failed to the dictionary server at %s:%d"), stardict_client->host_.c_str(), stardict_client->port_); on_error_.emit(mes); g_free(mes);*/ stardict_client->disconnect(); return FALSE; } GError *err = NULL; gsize term, len; gchar *line; GIOStatus res; for (;;) { if (!stardict_client->channel_) break; bool result; if (stardict_client->reading_type_ == READ_SIZE) { gsize bytes_read; res = g_io_channel_read_chars(stardict_client->channel_, stardict_client->size_data+(stardict_client->size_count-stardict_client->size_left), stardict_client->size_left, &bytes_read, &err); if (res == G_IO_STATUS_ERROR || res == G_IO_STATUS_EOF) { if (err) { gchar *str = g_strdup_printf(_("Error while reading reply from server: %s"), err->message); on_error_.emit(str); g_free(str); g_error_free(err); } stardict_client->disconnect(); return FALSE; } stardict_client->size_left -= bytes_read; if (stardict_client->size_left == 0) result = stardict_client->parse(stardict_client->size_data); else break; } else { if (stardict_client->reading_type_ == READ_LINE) g_io_channel_set_line_term(stardict_client->channel_, "\n", 1); else if (stardict_client->reading_type_ == READ_STRING) g_io_channel_set_line_term(stardict_client->channel_, "", 1); res = g_io_channel_read_line(stardict_client->channel_, &line, &len, &term, &err); if (res == G_IO_STATUS_ERROR || res == G_IO_STATUS_EOF) { if (err) { gchar *str = g_strdup_printf(_("Error while reading reply from server: %s"), err->message); on_error_.emit(str); g_free(str); g_error_free(err); } stardict_client->disconnect(); return FALSE; } if (!len) break; //truncate the line terminator before parsing line[term] = '\0'; result = stardict_client->parse(line); } if (!result) { stardict_client->disconnect(); return FALSE; } } return TRUE; }
static void facq_log_window_constructed(GObject *self) { gchar *window_title = NULL; GtkWidget *window = NULL; GError *local_err = NULL; gchar *log_content = NULL; gsize log_size = 0; FacqLogWindow *log_window = FACQ_LOG_WINDOW(self); /* create a new #GIOChannel for reading the log file */ log_window->priv->log = g_io_channel_new_file(log_window->priv->filename,"r",&local_err); if(local_err) goto error; /* read the file to the end */ if( g_io_channel_read_to_end(log_window->priv->log, &log_content, &log_size, &local_err) != G_IO_STATUS_NORMAL ){ g_io_channel_unref(log_window->priv->log); log_window->priv->log = NULL; goto error; } /* set \n as line term */ g_io_channel_set_line_term(log_window->priv->log,"\n",-1); /* create the file monitor, first we create a #GFile. */ log_window->priv->file = g_file_new_for_path(log_window->priv->filename); log_window->priv->mon = g_file_monitor(log_window->priv->file, G_FILE_MONITOR_NONE, NULL, &local_err); /* create the text view and put the last lines, lines, in buffer */ log_window->priv->text_view = gtk_text_view_new(); gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(log_window->priv->text_view), FALSE); gtk_text_view_set_justification(GTK_TEXT_VIEW(log_window->priv->text_view), GTK_JUSTIFY_LEFT); gtk_text_view_set_editable(GTK_TEXT_VIEW(log_window->priv->text_view), FALSE); facq_log_window_from_log_to_text_buffer(log_window,log_content); /* connect the file monitor with the callback function, every time the * file changes the callback will be called*/ g_signal_connect(log_window->priv->mon,"changed", G_CALLBACK(log_file_changed_callback),log_window); /* create a scrolled window and append the text view to it*/ window = gtk_scrolled_window_new(NULL,NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(window),GTK_SHADOW_NONE); log_window->priv->scrolled_window = window; gtk_container_add(GTK_CONTAINER(window),log_window->priv->text_view); /* Main log window, this window contains all the other components * and will have a title according to your main application window title */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); if(gtk_window_get_title(GTK_WINDOW(log_window->priv->top_window))) window_title = g_strdup_printf("%s Log", gtk_window_get_title(GTK_WINDOW(log_window->priv->top_window))); else window_title = g_strdup_printf("Log Window"); gtk_window_set_title(GTK_WINDOW(window),window_title); g_free(window_title); gtk_window_set_destroy_with_parent(GTK_WINDOW(window),TRUE); gtk_window_set_transient_for(GTK_WINDOW(window),GTK_WINDOW(log_window->priv->top_window)); gtk_container_add(GTK_CONTAINER(window),log_window->priv->scrolled_window); g_signal_connect(window,"delete-event", G_CALLBACK(delete_event),log_window); /* ready to go */ gtk_widget_show_all(window); log_window->priv->window = window; return; error: if(local_err) g_propagate_error(&log_window->priv->construct_error,local_err); return; }