Commit be6241db627e76d483b362e7f2dcf0227c5c91b6

Authored by Imanol-Mikel Barba Sabariego
1 parent 5db313ca

git-svn-id: svn://imanolbarba.net/PAD@48 c2ee353e-ed0d-4329-bf56-03aec153487f

QChatClient/Socket.cpp
... ... @@ -27,7 +27,7 @@ void Socket::Create()
27 27 throw SocketException ( "TCP: Could not create socket" );
28 28 }
29 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 33 void Socket::Bind(string address, int port)
... ... @@ -35,7 +35,7 @@ void Socket::Bind(string address, int port)
35 35 sockAddr.sin_family = AF_INET;
36 36 sockAddr.sin_port = htons(port);
37 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 40 stringstream sstream;
41 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 93 else if(recv == "EXIT_OK")
94 94 {
95 95 cout << "Exiting" << endl;
96   - chat->toggleConnected(true);
  96 + chat->toggleConnected(false);
97 97 msgListNotEmpty.notify_all();
98 98 chat->close();
99 99 break;
... ... @@ -148,26 +148,16 @@ void Chatroom::closeEvent(QCloseEvent *event)
148 148 {
149 149 if(recv == NULL && send == NULL)
150 150 {
151   - ui->inputText->setReadOnly(true);
152   - ui->chatText->printServerMsg("Disconnected");
153 151 event->accept();
154 152 return;
155 153 }
156 154 sendQueue.clear();
157 155 QString exitMsg("/exit");
158 156 putMsgToSendQueue(exitMsg);
159   - ui->inputText->setReadOnly(true);
160 157 recv->join();
161 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 161 event->accept();
172 162 }
173 163  
... ... @@ -251,9 +241,11 @@ void Chatroom::newChat(QString peerNick)
251 241  
252 242 void Chatroom::removeChat(QString &nickname)
253 243 {
  244 + chatMutex.lock();
254 245 ChatWindow* chat = (ChatWindow*) activeChats.find(nickname.toStdString())->second;
255 246 inactiveChats.insert(std::pair<string,ChatWindow*>(nickname.toStdString(),chat));
256 247 activeChats.erase(nickname.toStdString());
  248 + chatMutex.unlock();
257 249 }
258 250  
259 251 void Chatroom::launchChatWindow(QModelIndex index)
... ... @@ -386,24 +378,35 @@ void Chatroom::startSession()
386 378 void Chatroom::freeChats(bool active)
387 379 {
388 380 int count = 0;
  381 + map<string,void*>::iterator it;
389 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 394 count++;
396 395 ChatWindow* chat = (ChatWindow*) it->second;
397   - activeChats.erase(it);
398   - delete chat;
  396 + chat->notifyClose();
399 397 }
400 398 }
401 399 else
402 400 {
403   - cout << "FREE INACTIVE" << endl;
  401 + cout << "FREE INACTIVE (" << inactiveChats.size() << " remaining)" << endl;
404 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 410 count++;
408 411 ChatWindow* chat = (ChatWindow*) it->second;
409 412 inactiveChats.erase(it);
... ... @@ -419,8 +422,6 @@ Chatroom::~Chatroom()
419 422 delete chatLock;
420 423 delete recv;
421 424 delete send;
422   - freeChats(false);
423   - freeChats(true);
424 425 delete ui;
425 426 }
426 427  
... ...
QChatClient/chatwindow.cpp
... ... @@ -24,6 +24,11 @@ ChatWindow::ChatWindow(QString nick, QWidget *parent) :
24 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 32 void ChatWindow::notifyPrint(string &msg)
28 33 {
29 34 emit msgToPrint(QString::fromStdString(msg));
... ...