1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 
22 #include <net/NetMessageHandler.h>
23 #include <net/NetMessagePool.h>
24 
~NetMessageHandlerI()25 NetMessageHandlerI::~NetMessageHandlerI()
26 {
27 }
28 
NetMessageHandler()29 NetMessageHandler::NetMessageHandler() :
30 	handler_(0), messagesMutex_(0), messagesWaiting_(false)
31 {
32 	messagesMutex_ = SDL_CreateMutex();
33 }
34 
~NetMessageHandler()35 NetMessageHandler::~NetMessageHandler()
36 {
37 	SDL_LockMutex(messagesMutex_);
38 	while (!messages_.empty())
39 	{
40 		NetMessage *message = messages_.front();
41 		messages_.pop_front();
42 		NetMessagePool::instance()->addToPool(message);
43 	}
44 	SDL_UnlockMutex(messagesMutex_);
45 
46 	SDL_DestroyMutex(messagesMutex_);
47 }
48 
addMessage(NetMessage * message)49 void NetMessageHandler::addMessage(NetMessage *message)
50 {
51 	SDL_LockMutex(messagesMutex_);
52 	messages_.push_back(message);
53 	messagesWaiting_ = true;
54 	SDL_UnlockMutex(messagesMutex_);
55 }
56 
processMessages()57 int NetMessageHandler::processMessages()
58 {
59 	int result = 0;
60 	while (processSingleMessage())
61 	{
62 		result++;
63 	}
64 	return result;
65 }
66 
processSingleMessage()67 bool NetMessageHandler::processSingleMessage()
68 {
69 	if (!messagesWaiting_) return false;
70 
71 	// Get the list of messages that should be processed
72 	// Process one at a time, incase this method is called re-entrantly
73 	NetMessage *message = 0;
74 	SDL_LockMutex(messagesMutex_);
75 	if (!messages_.empty())
76 	{
77 		message = messages_.front();
78 		messages_.pop_front();
79 	}
80 	messagesWaiting_ = !messages_.empty();
81 	SDL_UnlockMutex(messagesMutex_);
82 
83 	if (message)
84 	{
85 		// Call user to process message
86 		if (handler_) handler_->processMessage(*message);
87 
88 		// Delete message
89 		NetMessagePool::instance()->addToPool(message);
90 	}
91 	return (message != 0);
92 }
93 
setMessageHandler(NetMessageHandlerI * handler)94 void NetMessageHandler::setMessageHandler(NetMessageHandlerI *handler)
95 {
96 	handler_ = handler;
97 }
98