Problems with GIOChannel



I am writing a MUD client under GTK+.  Ive got everything working so far
except for one problem that is holding me back.  Ive set up the
GIOChannel to watch for G_IO_IN, and the callback is there to read it,
and it all works great, until I write data to the channel using
g_io_channel_write_chars.  I know the MUD (what it is connecting to) is
sending more information, because I am also writing the MUD Im
connecting to, so any ideas on what may be causing this?  Here is the
relevant code:

void create_connection (gchar *charname, gchar *mudname, gchar *host,
gint port)
{
	GtkLabel * conn_label;
	GtkTextView * view;
	GtkTextBuffer * buffer;
	GtkEntry * entry;
	GtkVBox * box;
	struct sockaddr_in sock_in;
	int sock;
	static GIOChannel * gio;
	static GList * list;
	GError * err;
	PangoFontDescription * pfd;
	GtkScrolledWindow * gsw;
	
	err = NULL;
	
	list = NULL;
	
	sock = socket (PF_INET, SOCK_STREAM, 0);
	
	sock_in.sin_family = AF_INET;
	sock_in.sin_addr = *((struct in_addr*)gethostbyname
(host)->h_addr_list[0]);
	sock_in.sin_port = htons(port);
	
	if (connect (sock, (struct sockaddr *) &sock_in, sizeof(sock_in)) < 0)
	{
		perror ("MudGnome: connect");
		gtk_main_quit ();
		exit (-1);
	}
	
	gsw = gtk_scrolled_window_new (NULL, NULL);
	
	gtk_scrolled_window_set_policy (gsw, GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
	buffer = gtk_text_buffer_new (NULL);
	
	pfd = pango_font_description_new ();
	pango_font_description_set_family (pfd, "fixed");
	pango_font_description_set_size (pfd, 10);
	
	view = gtk_text_view_new_with_buffer (buffer);
	
	gtk_widget_modify_font (GTK_WIDGET(view), pfd);
	
	gtk_container_add (GTK_CONTAINER (gsw), view);
	box = gtk_vbox_new (NULL, 5);
	
	entry = gtk_entry_new ();
	
	conn_label = gtk_label_new (g_strconcat (charname, " (", mudname, ")",
NULL));
	
	gtk_box_pack_end (box, entry, FALSE, FALSE, 0);
	
	gtk_box_pack_end (box, gsw, TRUE, TRUE, 0);
	
	gtk_widget_show (GTK_WIDGET (conn_label));
	gtk_widget_show (GTK_WIDGET (gsw));
	gtk_widget_show (GTK_WIDGET (view));
	gtk_widget_show (GTK_WIDGET (entry));
	gtk_widget_show (GTK_WIDGET (box));
	gtk_notebook_append_page (mga->mud_notebook, box, conn_label);
	
	gtk_notebook_set_current_page (mga->mud_notebook, -1);
	gio = g_io_channel_unix_new (sock);
	
	g_io_channel_set_buffered (gio, FALSE);
	g_io_channel_set_flags (gio, G_IO_FLAG_NONBLOCK, &err);
	g_io_channel_set_encoding (gio, NULL, &err);
	
	g_io_add_watch (gio, G_IO_IN, conn_read, buffer);
	
	list = g_list_append (list, buffer);
	list = g_list_append (list, gio);
	
	g_signal_connect (G_OBJECT (entry), "key-release-event", G_CALLBACK
(conn_key_release), gio);
}

gboolean    conn_read (GIOChannel *source,
                       GIOCondition condition,
                       gpointer data)
{
	GtkTextBuffer * buffer;
	gchar * read;
	gsize size;
	int i = 0;
	GError * err;
	gchar ** buf;
	GtkTextIter iter;
	
	err = NULL;
	
	g_print ("Got G_IO_IN...\n");
	buffer = (GtkTextBuffer*)data;
	
	read = g_malloc0 (4096);
	g_io_channel_read_chars (source, read, 4096, &size, &err);
	
	buf = g_strsplit (read, "\r", 0);
	read = g_strjoinv (NULL, buf);
	
	g_print (":%s\n", read);
	if (read != NULL)
	{
		gtk_text_buffer_get_end_iter (buffer, &iter);
	
		gtk_text_buffer_insert (buffer, &iter, read, strlen(read));
	}
	
	g_strfreev (buf);
	g_free (read);
	
	return TRUE;
}

gboolean    conn_key_release (GtkWidget *widget,
                             GdkEventKey *event,
                             gpointer user_data)
{
	GtkEntry * entry;
	GtkTextBuffer * buffer;
	GtkTextView * view;
	gsize size;
	GtkTextIter iter;
	gchar * txt;
	gint page;
	GtkScrolledWindow * gsw;
	GIOChannel * gio;
	GError * ge;
	GList * list;
	
	ge = NULL;
	
	if (event->keyval == GDK_Return)
	{
		entry = (GtkEntry*)widget;
		
		page = gtk_notebook_get_current_page (mga->mud_notebook);
		
		g_print ("[%d]\n", page);
		
		list = gtk_container_get_children(gtk_notebook_get_nth_page
(mga->mud_notebook, page));
		
		gsw = g_list_nth_data (list, 0);
		
		view = gtk_bin_get_child (GTK_BIN (gsw));
		
		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
		
		gio = (GIOChannel*)user_data;
		
		txt = gtk_entry_get_text (entry);
		gtk_text_buffer_get_end_iter (buffer, &iter);
	
		gtk_text_buffer_insert (buffer, &iter, txt, strlen(txt));
		
		g_io_channel_write_chars (gio, txt, -1, &size, &ge);
		
		g_io_channel_flush (gio, &ge);
		
		gtk_editable_select_region (entry, 0, -1);
	}
	
	return TRUE;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]