Commit be6241db627e76d483b362e7f2dcf0227c5c91b6
1 parent
5db313ca
git-svn-id: svn://imanolbarba.net/PAD@48 c2ee353e-ed0d-4329-bf56-03aec153487f
Showing
3 changed files
with
30 additions
and
24 deletions
QChatClient/Socket.cpp
@@ -27,7 +27,7 @@ void Socket::Create() | @@ -27,7 +27,7 @@ void Socket::Create() | ||
27 | throw SocketException ( "TCP: Could not create socket" ); | 27 | throw SocketException ( "TCP: Could not create socket" ); |
28 | } | 28 | } |
29 | setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,&optval,sizeof optval); | 29 | setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,&optval,sizeof optval); |
30 | - setsockopt(sock, SOL_TCP, TCP_NODELAY, &optval, sizeof optval); | 30 | + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof optval); |
31 | } | 31 | } |
32 | 32 | ||
33 | void Socket::Bind(string address, int port) | 33 | void Socket::Bind(string address, int port) |
@@ -35,7 +35,7 @@ void Socket::Bind(string address, int port) | @@ -35,7 +35,7 @@ void Socket::Bind(string address, int port) | ||
35 | sockAddr.sin_family = AF_INET; | 35 | sockAddr.sin_family = AF_INET; |
36 | sockAddr.sin_port = htons(port); | 36 | sockAddr.sin_port = htons(port); |
37 | sockAddr.sin_addr.s_addr = inet_addr(address.c_str()); | 37 | sockAddr.sin_addr.s_addr = inet_addr(address.c_str()); |
38 | - if(bind(sock, (struct sockaddr *)&sockAddr, sizeof(struct sockaddr)) != 0) | 38 | + if(::bind(sock, (struct sockaddr *)&sockAddr, sizeof(struct sockaddr)) != 0) |
39 | { | 39 | { |
40 | stringstream sstream; | 40 | stringstream sstream; |
41 | sstream << "TCP: Could not bind to address " << address << " on port " << port; | 41 | sstream << "TCP: Could not bind to address " << address << " on port " << port; |
QChatClient/chatroom.cpp
@@ -93,7 +93,7 @@ void recvThread(Socket* s, Chatroom* chat) | @@ -93,7 +93,7 @@ void recvThread(Socket* s, Chatroom* chat) | ||
93 | else if(recv == "EXIT_OK") | 93 | else if(recv == "EXIT_OK") |
94 | { | 94 | { |
95 | cout << "Exiting" << endl; | 95 | cout << "Exiting" << endl; |
96 | - chat->toggleConnected(true); | 96 | + chat->toggleConnected(false); |
97 | msgListNotEmpty.notify_all(); | 97 | msgListNotEmpty.notify_all(); |
98 | chat->close(); | 98 | chat->close(); |
99 | break; | 99 | break; |
@@ -148,26 +148,16 @@ void Chatroom::closeEvent(QCloseEvent *event) | @@ -148,26 +148,16 @@ void Chatroom::closeEvent(QCloseEvent *event) | ||
148 | { | 148 | { |
149 | if(recv == NULL && send == NULL) | 149 | if(recv == NULL && send == NULL) |
150 | { | 150 | { |
151 | - ui->inputText->setReadOnly(true); | ||
152 | - ui->chatText->printServerMsg("Disconnected"); | ||
153 | event->accept(); | 151 | event->accept(); |
154 | return; | 152 | return; |
155 | } | 153 | } |
156 | sendQueue.clear(); | 154 | sendQueue.clear(); |
157 | QString exitMsg("/exit"); | 155 | QString exitMsg("/exit"); |
158 | putMsgToSendQueue(exitMsg); | 156 | putMsgToSendQueue(exitMsg); |
159 | - ui->inputText->setReadOnly(true); | ||
160 | recv->join(); | 157 | recv->join(); |
161 | send->join(); | 158 | send->join(); |
162 | - map<string,void*>::iterator it; | ||
163 | - for(it = activeChats.begin(); it != activeChats.end(); it++) | ||
164 | - { | ||
165 | - ChatWindow* chat = (ChatWindow*) it->second; | ||
166 | - chat->close(); | ||
167 | - } | ||
168 | - ui->chatText->printServerMsg("Disconnected"); | ||
169 | - send = NULL; | ||
170 | - recv = NULL; | 159 | + freeChats(true); |
160 | + freeChats(false); | ||
171 | event->accept(); | 161 | event->accept(); |
172 | } | 162 | } |
173 | 163 | ||
@@ -251,9 +241,11 @@ void Chatroom::newChat(QString peerNick) | @@ -251,9 +241,11 @@ void Chatroom::newChat(QString peerNick) | ||
251 | 241 | ||
252 | void Chatroom::removeChat(QString &nickname) | 242 | void Chatroom::removeChat(QString &nickname) |
253 | { | 243 | { |
244 | + chatMutex.lock(); | ||
254 | ChatWindow* chat = (ChatWindow*) activeChats.find(nickname.toStdString())->second; | 245 | ChatWindow* chat = (ChatWindow*) activeChats.find(nickname.toStdString())->second; |
255 | inactiveChats.insert(std::pair<string,ChatWindow*>(nickname.toStdString(),chat)); | 246 | inactiveChats.insert(std::pair<string,ChatWindow*>(nickname.toStdString(),chat)); |
256 | activeChats.erase(nickname.toStdString()); | 247 | activeChats.erase(nickname.toStdString()); |
248 | + chatMutex.unlock(); | ||
257 | } | 249 | } |
258 | 250 | ||
259 | void Chatroom::launchChatWindow(QModelIndex index) | 251 | void Chatroom::launchChatWindow(QModelIndex index) |
@@ -386,24 +378,35 @@ void Chatroom::startSession() | @@ -386,24 +378,35 @@ void Chatroom::startSession() | ||
386 | void Chatroom::freeChats(bool active) | 378 | void Chatroom::freeChats(bool active) |
387 | { | 379 | { |
388 | int count = 0; | 380 | int count = 0; |
381 | + map<string,void*>::iterator it; | ||
389 | if(active) | 382 | if(active) |
390 | { | 383 | { |
391 | - cout << "FREE ACTIVE" << endl; | ||
392 | - map<string,void*>::iterator it; | ||
393 | - for(it = activeChats.begin(); it != activeChats.end(); it++) | 384 | + cout << "FREE ACTIVE (" << activeChats.size() << " remaining)" << endl; |
385 | + while(true) | ||
394 | { | 386 | { |
387 | + chatMutex.lock(); | ||
388 | + it = activeChats.begin(); | ||
389 | + chatMutex.unlock(); | ||
390 | + if(it == activeChats.end()) | ||
391 | + { | ||
392 | + break; | ||
393 | + } | ||
395 | count++; | 394 | count++; |
396 | ChatWindow* chat = (ChatWindow*) it->second; | 395 | ChatWindow* chat = (ChatWindow*) it->second; |
397 | - activeChats.erase(it); | ||
398 | - delete chat; | 396 | + chat->notifyClose(); |
399 | } | 397 | } |
400 | } | 398 | } |
401 | else | 399 | else |
402 | { | 400 | { |
403 | - cout << "FREE INACTIVE" << endl; | 401 | + cout << "FREE INACTIVE (" << inactiveChats.size() << " remaining)" << endl; |
404 | map<string,void*>::iterator it; | 402 | map<string,void*>::iterator it; |
405 | - for(it = inactiveChats.begin(); it != inactiveChats.end(); it++) | 403 | + while(true) |
406 | { | 404 | { |
405 | + it = inactiveChats.begin(); | ||
406 | + if(it == inactiveChats.end()) | ||
407 | + { | ||
408 | + break; | ||
409 | + } | ||
407 | count++; | 410 | count++; |
408 | ChatWindow* chat = (ChatWindow*) it->second; | 411 | ChatWindow* chat = (ChatWindow*) it->second; |
409 | inactiveChats.erase(it); | 412 | inactiveChats.erase(it); |
@@ -419,8 +422,6 @@ Chatroom::~Chatroom() | @@ -419,8 +422,6 @@ Chatroom::~Chatroom() | ||
419 | delete chatLock; | 422 | delete chatLock; |
420 | delete recv; | 423 | delete recv; |
421 | delete send; | 424 | delete send; |
422 | - freeChats(false); | ||
423 | - freeChats(true); | ||
424 | delete ui; | 425 | delete ui; |
425 | } | 426 | } |
426 | 427 |
QChatClient/chatwindow.cpp
@@ -24,6 +24,11 @@ ChatWindow::ChatWindow(QString nick, QWidget *parent) : | @@ -24,6 +24,11 @@ ChatWindow::ChatWindow(QString nick, QWidget *parent) : | ||
24 | connect(this,SIGNAL(msgToPrint(QString)),this,SLOT(printMsg(QString))); | 24 | connect(this,SIGNAL(msgToPrint(QString)),this,SLOT(printMsg(QString))); |
25 | } | 25 | } |
26 | 26 | ||
27 | +void ChatWindow::notifyClose() | ||
28 | +{ | ||
29 | + emit ui->actionExit->triggered(); | ||
30 | +} | ||
31 | + | ||
27 | void ChatWindow::notifyPrint(string &msg) | 32 | void ChatWindow::notifyPrint(string &msg) |
28 | { | 33 | { |
29 | emit msgToPrint(QString::fromStdString(msg)); | 34 | emit msgToPrint(QString::fromStdString(msg)); |