From 371a5d0728c9b21fce3680ecb6732d7527847332 Mon Sep 17 00:00:00 2001 From: nforla Date: Mon, 15 Jul 2019 21:13:09 -0300 Subject: [PATCH] =?UTF-8?q?Se=20crea=20el=20callback=20para=20la=20descone?= =?UTF-8?q?xi=C3=B3n=20de=20los=20clientes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chatroom_client.c | 30 ++++++++++++++++++++++++++++++ chatroom_client_UI.glade | 1 + 2 files changed, 31 insertions(+) diff --git a/chatroom_client.c b/chatroom_client.c index fcb7b6e..69a7e86 100644 --- a/chatroom_client.c +++ b/chatroom_client.c @@ -48,6 +48,7 @@ void init_app(){ void on_menu_connect_click(GtkWidget *widget, gpointer *data); void on_connect_btn_clicked(GtkWidget *widget, ConnectionRequest *conn_request); +void on_disconnect_btn_clicked(GtkWidget *widget, gpointer *data); void on_send_btn_clicked(GtkWidget *widget, gpointer *data); int main(int argc, char* argv[]) { @@ -173,6 +174,20 @@ void update_chat_view(char* msg_display){ gtk_text_buffer_insert(buffer, &iter, "\n", 1); //Inserts a new line } +void clear_chat_view(){ + + GtkTextIter* start = malloc(sizeof(GtkTextIter)); + GtkTextIter* end = malloc(sizeof(GtkTextIter)); + + GtkTextView *chat_view = GTK_TEXT_VIEW(gtk_builder_get_object(app_builder, "chatroom_view")); + GtkTextBuffer *buffer = gtk_text_view_get_buffer(chat_view); + gtk_text_buffer_get_bounds(buffer, start, end); + gtk_text_buffer_delete(buffer, start, end); + + free(start); + free(end); +} + void* chat_listener_thread(void* param){ printf("Chat thread initialized\n"); @@ -209,6 +224,8 @@ void create_chat_thread(){ pthread_attr_t atributes; pthread_attr_init(&atributes); pthread_attr_setdetachstate(&atributes, PTHREAD_CREATE_DETACHED); //Creates detached thread that frees resources when finish + int status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + printf("Stat th: %d\n",status ); pthread_create(chat_data->chat_listener, &atributes, chat_listener_thread, NULL); } @@ -245,6 +262,19 @@ void on_connect_btn_clicked(GtkWidget *widget, ConnectionRequest *conn_request){ free(conn_request); } +void on_disconnect_btn_clicked(GtkWidget *widget, gpointer *data){ + + printf("Disconnect clicked\n"); + + pthread_cancel(*chat_data->chat_listener); // Cancels thread listening incoming msgs. + + close(chat_data->sockfd); // Closes connection + + clear_chat_view(); + + printf("Connection closed\n"); +} + void on_send_btn_clicked(GtkWidget *widget, gpointer *data){ printf("Send click\n"); diff --git a/chatroom_client_UI.glade b/chatroom_client_UI.glade index bfc31c0..8e3d365 100644 --- a/chatroom_client_UI.glade +++ b/chatroom_client_UI.glade @@ -43,6 +43,7 @@ False True True +