Skip to content

Commit

Permalink
Fixed huge memleak. FIxed a couple of other issues. Switched to signa…
Browse files Browse the repository at this point in the history
…lfd instead of ppoll on linux.
  • Loading branch information
FedeDP committed Oct 20, 2017
1 parent a712bbe commit 74ad986
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 64 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(slack++ VERSION 0.1 LANGUAGES CXX C)

#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Debug)
set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

include_directories(include src)
Expand Down
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
TODO:

- [x] enhance mouse support (wheel on roster, left click on roster select it, right click mute it)
- [ ] cleanup code...private things/fix includes
- [ ] switch to signalfd instead of ppoll?
- [x] switch to signalfd instead of ppoll?
- [ ] fix all issues from valgrind...

LONGTERM AIMS:
Expand Down
6 changes: 5 additions & 1 deletion include/SlackUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#include <map>
#include <mutex>
#include <poll.h>
#include "signal.h"

#ifdef __linux__
#include <sys/signalfd.h>
#include <signal.h>
#endif

#ifdef LIBNOTIFY_FOUND
#include <libnotify/notify.h>
Expand Down
33 changes: 16 additions & 17 deletions include/WebsocketClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,20 @@ class WebsocketClient {
CURLcode res = curl_easy_perform(curl);

if (res == CURLE_OK) {
long s;
curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &s);
socket = s;

std::string data;

for (const auto& h: headers) {
data += h + "\r\n";
res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &socket);
if (res == CURLE_OK) {
std::string data;

for (const auto& h: headers) {
data += h + "\r\n";
}

data += "\r\n";

_send(data);
ping_pong();
return true;
}

data += "\r\n";

_send(data);
ping_pong();
return true;
}
Log::d() << "An error occurred during connection." << std::endl;
return false;
Expand Down Expand Up @@ -122,7 +121,7 @@ class WebsocketClient {
int offset = 0;

while (data.size() > offset && data.size() > 3) {
auto buff = std::string(data.begin()+offset, data.end());
auto buff = std::string(data.begin() + offset, data.end());
if (buff.size() < 3) break;

int length = buff[1] & 127;
Expand Down Expand Up @@ -152,9 +151,9 @@ class WebsocketClient {

}

std::string event(buff.begin()+index, buff.begin()+index+length);
std::string event(buff.begin() + index, buff.begin() + index + length);
events.push_back(event);
offset = index+length;
offset += index + length;
}
}

Expand Down
11 changes: 5 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
#include <assert.h>
#include <string.h>

static void check_cmdline(int, char **);

static std::string TOKEN_NAME;
static void check_cmdline(int, char **, std::string&);

int main(int argc, char* argv[]) {

check_cmdline(argc, argv);
std::string TOKEN_NAME;
check_cmdline(argc, argv, TOKEN_NAME);

auto token = std::getenv(TOKEN_NAME.c_str());
if (!token) {
Expand All @@ -32,9 +30,10 @@ int main(int argc, char* argv[]) {

c.set_token(token);
ui.show();
return 0;
}

static void check_cmdline(int argc, char *argv[]) {
static void check_cmdline(int argc, char *argv[], std::string& TOKEN_NAME) {

if (argc == 2) {
if (strcmp(argv[1], "--help") == 0) {
Expand Down
40 changes: 21 additions & 19 deletions src/SlackClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,31 @@ Json::Value SlackClient::call(const std::string &api, const std::vector<std::str
}

CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_FILE, &os);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

if (res != CURLE_OK) {
goto error;
}

long respcode; //response code of the http transaction
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respcode);
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_FILE, &os);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
goto error;
}

if (respcode != 200) {
goto error;
long respcode; //response code of the http transaction
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respcode);
if (respcode != 200) {
goto error;
}
curl_easy_cleanup(curl);
reader.parse(os.str(), d);
return d;
}

reader.parse(os.str(), d);
return d;

error:
if (curl) {
curl_easy_cleanup(curl);
}
Log::d() << "An error occurred during the call." << std::endl;
throw 1;
}
Expand Down
48 changes: 31 additions & 17 deletions src/SlackUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ void SlackUI::show() {
}

#ifdef __linux__
static void sig_handler (int signum) {
static void sig_handler(int fd) {
quit = true;
Log::d() << "received " << signum << " signal." << std::endl;

struct signalfd_siginfo fdsi;
ssize_t s;

s = read(fd, &fdsi, sizeof(struct signalfd_siginfo));
if (s != sizeof(struct signalfd_siginfo)) {
Log::d() << "an error occurred while getting signalfd data." << std::endl;
} else {
Log::d() << "received " << fdsi.ssi_signo << "signal. Leaving." << std::endl;
}
}
#endif

Expand All @@ -43,7 +52,12 @@ void SlackUI::main_ui_cycle() {

const int NCURSES_EVT = 0;
const int SOCKET_EVT = 1;
#ifdef __linux__
const int SIGNAL_EVT = 2;
const int nfds = 3;
#else
const int nfds = 2;
#endif

struct pollfd main_p[nfds];

Expand All @@ -62,31 +76,27 @@ void SlackUI::main_ui_cycle() {
#endif

#ifdef __linux__
struct sigaction main_act = {{0}};
sigset_t mask, main_mask;
sigset_t mask;

main_act.sa_handler = sig_handler;
sigaction(SIGINT, &main_act, 0);
sigaction(SIGTERM, &main_act, 0);
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigprocmask(SIG_BLOCK, &mask, &main_mask);
sigprocmask(SIG_BLOCK, &mask, NULL);
main_p[SOCKET_EVT] = (struct pollfd) {
.fd = signalfd(-1, &mask, 0),
.events = POLLIN,
};
#endif

active_win = roster.get();
int c;
while (!quit) {
#ifdef __linux__
int ret = ppoll(main_p, nfds, NULL, &main_mask);
#else
int ret = poll(main_p, nfds, -1);
#endif
for (int i = 0; i < nfds && ret > 0; i++) {
if (main_p[i].revents & POLLIN) {
switch (i) {
case NCURSES_EVT:
// ncurses event
// ncurses event
c = active_win->wait(get_session());
switch (c) {
case KEY_TAB:
Expand All @@ -111,17 +121,21 @@ void SlackUI::main_ui_cycle() {
}
break;
case SOCKET_EVT:
// socket event
// socket event
client->receive();
break;
#ifdef __linux__
case SIGNAL_EVT:
// signal event on linux
sig_handler(main_p[SIGNAL_EVT].fd);
break;
#endif
}
ret--;
}
}
}

close(main_p[0].fd);
close(main_p[1].fd);
// before leaving, make sure we updated current chat's mark
// only if a chat had been selected (ie the user did not leave slack++ immediately)
update_mark(get_session(),
Expand Down Expand Up @@ -189,8 +203,8 @@ Session& SlackUI::get_session() {
}

SlackUI::~SlackUI() {
endwin();
delwin(stdscr);
endwin();
}

void SlackUI::setup_ncurses() {
Expand Down

0 comments on commit 74ad986

Please sign in to comment.