From 271a8baaeb5cbd19047c50681d955c14115fc86f Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 4 Dec 2022 14:04:06 +0100 Subject: Connect on mouse click With this commit the client starts the connection to the server as soon as the user clicks into the application window. Disconnect or reading / writing to the stream is not implemented. --- gui.c | 5 ++++- xmpp.c | 40 +++++++++++++++++++++------------------- xmpp.h | 2 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/gui.c b/gui.c index a7f0886..6d0d267 100644 --- a/gui.c +++ b/gui.c @@ -31,6 +31,7 @@ void gui_run(void) { SDL_Renderer* renderer; SDL_Texture* texture; uint32_t* pixels; + int xmpp_fd = -1; // init SDL2 and create window SDL_Init(SDL_INIT_VIDEO); @@ -62,7 +63,9 @@ void gui_run(void) { quit = true; break; case SDL_MOUSEBUTTONDOWN: - xmpp_connect(); + if (xmpp_fd == -1) { + xmpp_fd = xmpp_connect(); + } break; } SDL_RenderClear(renderer); diff --git a/xmpp.c b/xmpp.c index c574ba5..dcf19a5 100644 --- a/xmpp.c +++ b/xmpp.c @@ -135,11 +135,11 @@ int xmpp_start_stream(int sock_fd, char *jid, char *domainpart) } /* - * Initialize the network connection to the XMPP server + * Initialize the network connection to the XMPP server and return socket fd * * TODO: Error handling is missing. */ -void xmpp_connect(void) +int xmpp_connect(void) { printf("net_init()\n"); @@ -155,36 +155,38 @@ void xmpp_connect(void) int sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (sock == -1) { printf("Failed to get socket from OS!."); - return; + return sock; } else { printf("Got socket number %d from OS.\n", sock); } if (connect(sock, addr->ai_addr, addr->ai_addrlen) == -1) { printf("Failed to connect!\n"); - return; + return sock; } else { printf("Successfully connected.\n"); } if (xmpp_start_stream(sock, jid, domain) == -1) { printf("Failed to init stream!\n"); - return; + return sock; } printf("Stream init sent.\n"); - printf("This is the server response:\n"); - char buf; - while(1) { - int retval = recv(sock, (void *)&buf, 1, 0); - if (retval == 0) { - printf("Connection closed!\n"); - break; - } else if (retval == 1) { - write(1, (const void *)&buf, 1); - } else { - printf(strerror(errno)); - printf("Unhandled recv() return value %d!\n", retval); - } - } + return sock; + +// printf("This is the server response:\n"); +// char buf; +// while(1) { +// int retval = recv(sock, (void *)&buf, 1, 0); +// if (retval == 0) { +// printf("Connection closed!\n"); +// break; +// } else if (retval == 1) { +// write(1, (const void *)&buf, 1); +// } else { +// printf(strerror(errno)); +// printf("Unhandled recv() return value %d!\n", retval); +// } +// } } diff --git a/xmpp.h b/xmpp.h index 9752e38..f6b9071 100644 --- a/xmpp.h +++ b/xmpp.h @@ -17,5 +17,5 @@ */ -void xmpp_connect(void); +int xmpp_connect(void); char *get_domainpart(char *jid); -- cgit v1.2.3-70-g09d2