diff --git a/QChatClient/Socket.cpp b/QChatClient/Socket.cpp index 4f9ce9c..11d4342 100644 --- a/QChatClient/Socket.cpp +++ b/QChatClient/Socket.cpp @@ -27,7 +27,7 @@ void Socket::Create() throw SocketException ( "TCP: Could not create socket" ); } setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,&optval,sizeof optval); - setsockopt(sock, SOL_TCP, TCP_NODELAY, &optval, sizeof optval); + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof optval); } void Socket::Bind(string address, int port) @@ -35,7 +35,7 @@ void Socket::Bind(string address, int port) sockAddr.sin_family = AF_INET; sockAddr.sin_port = htons(port); sockAddr.sin_addr.s_addr = inet_addr(address.c_str()); - if(bind(sock, (struct sockaddr *)&sockAddr, sizeof(struct sockaddr)) != 0) + if(::bind(sock, (struct sockaddr *)&sockAddr, sizeof(struct sockaddr)) != 0) { stringstream sstream; sstream << "TCP: Could not bind to address " << address << " on port " << port; diff --git a/QChatClient/chatroom.cpp b/QChatClient/chatroom.cpp index da17c68..d2e730e 100644 --- a/QChatClient/chatroom.cpp +++ b/QChatClient/chatroom.cpp @@ -93,7 +93,7 @@ void recvThread(Socket* s, Chatroom* chat) else if(recv == "EXIT_OK") { cout << "Exiting" << endl; - chat->toggleConnected(true); + chat->toggleConnected(false); msgListNotEmpty.notify_all(); chat->close(); break; @@ -148,26 +148,16 @@ void Chatroom::closeEvent(QCloseEvent *event) { if(recv == NULL && send == NULL) { - ui->inputText->setReadOnly(true); - ui->chatText->printServerMsg("Disconnected"); event->accept(); return; } sendQueue.clear(); QString exitMsg("/exit"); putMsgToSendQueue(exitMsg); - ui->inputText->setReadOnly(true); recv->join(); send->join(); - map::iterator it; - for(it = activeChats.begin(); it != activeChats.end(); it++) - { - ChatWindow* chat = (ChatWindow*) it->second; - chat->close(); - } - ui->chatText->printServerMsg("Disconnected"); - send = NULL; - recv = NULL; + freeChats(true); + freeChats(false); event->accept(); } @@ -251,9 +241,11 @@ void Chatroom::newChat(QString peerNick) void Chatroom::removeChat(QString &nickname) { + chatMutex.lock(); ChatWindow* chat = (ChatWindow*) activeChats.find(nickname.toStdString())->second; inactiveChats.insert(std::pair(nickname.toStdString(),chat)); activeChats.erase(nickname.toStdString()); + chatMutex.unlock(); } void Chatroom::launchChatWindow(QModelIndex index) @@ -386,24 +378,35 @@ void Chatroom::startSession() void Chatroom::freeChats(bool active) { int count = 0; + map::iterator it; if(active) { - cout << "FREE ACTIVE" << endl; - map::iterator it; - for(it = activeChats.begin(); it != activeChats.end(); it++) + cout << "FREE ACTIVE (" << activeChats.size() << " remaining)" << endl; + while(true) { + chatMutex.lock(); + it = activeChats.begin(); + chatMutex.unlock(); + if(it == activeChats.end()) + { + break; + } count++; ChatWindow* chat = (ChatWindow*) it->second; - activeChats.erase(it); - delete chat; + chat->notifyClose(); } } else { - cout << "FREE INACTIVE" << endl; + cout << "FREE INACTIVE (" << inactiveChats.size() << " remaining)" << endl; map::iterator it; - for(it = inactiveChats.begin(); it != inactiveChats.end(); it++) + while(true) { + it = inactiveChats.begin(); + if(it == inactiveChats.end()) + { + break; + } count++; ChatWindow* chat = (ChatWindow*) it->second; inactiveChats.erase(it); @@ -419,8 +422,6 @@ Chatroom::~Chatroom() delete chatLock; delete recv; delete send; - freeChats(false); - freeChats(true); delete ui; } diff --git a/QChatClient/chatwindow.cpp b/QChatClient/chatwindow.cpp index ccf25bf..56fc9aa 100644 --- a/QChatClient/chatwindow.cpp +++ b/QChatClient/chatwindow.cpp @@ -24,6 +24,11 @@ ChatWindow::ChatWindow(QString nick, QWidget *parent) : connect(this,SIGNAL(msgToPrint(QString)),this,SLOT(printMsg(QString))); } +void ChatWindow::notifyClose() +{ + emit ui->actionExit->triggered(); +} + void ChatWindow::notifyPrint(string &msg) { emit msgToPrint(QString::fromStdString(msg));