#include #include #include #include #include #include "Socket.h" #include "argpopt.h" void sigHandler(int sig) { exit(0); } bool showMessage(const std::string& title, const std::string& body = "") { NotifyNotification* message = notify_notification_new (title.c_str(), body.c_str(), nullptr); bool ret = notify_notification_show (message, NULL); g_object_unref(G_OBJECT(message)); return ret; } void processLineBuffer(std::string& buffer) { if(buffer.find("bcst:") == 0) buffer.erase(0, 6); if(buffer.size() > 5 && buffer.find("MESG") == 0) { size_t titleEnd = buffer.size(); size_t bodyBegin = buffer.find("BODY"); if(bodyBegin != std::string::npos && bodyBegin > 5) { titleEnd = bodyBegin - 1; } const std::string body = (bodyBegin == std::string::npos && buffer.begin()+bodyBegin+5 < buffer.end()) ? "" : std::string(buffer.begin()+bodyBegin+5, buffer.end()); const std::string title(buffer.begin()+5, buffer.begin()+titleEnd); showMessage(title, body); } } static TCPSocket* socket = nullptr; void exitHandler() { notify_uninit(); if(socket)delete socket; } bool reconnect(std::string host, unsigned short port) { try { if(socket)delete socket; socket = new TCPSocket(host, port); } catch(SocketException &e) { std::cerr<recv(buffer, sizeof(buffer)); if(len <= 0) { std::cerr<<"socket error\n"; int i = 0; for(; i < 30 && reconnect(config.hostName, config.port) == false; ++i) sleep(10); if(i >= 10) { std::cerr<<"reconnect failed\n"; return 1; } } for(int i = 0; i