Chunk is purely a wrapper class, any 'state variables', like 'characters in room...
[UnsignedByte.git] / src / Sockets / EventHandler.cpp
blobf3823424861988f72c6f43b178b2d4987a1a5201
1 /** \file EventHandler.cpp
2 ** \date 2005-12-07
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2005,2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef _WIN32
31 #ifndef __MINGW32__
32 #pragma warning(disable:4786)
33 #endif
34 #endif
35 #include "EventHandler.h"
36 #include "IEventOwner.h"
37 #include "Event.h"
38 #include "Socket.h"
39 #include "TcpSocket.h"
40 #include "ListenSocket.h"
43 #ifdef SOCKETS_NAMESPACE
44 namespace SOCKETS_NAMESPACE {
45 #endif
48 EventHandler::EventHandler(StdLog *p) : SocketHandler(p), m_quit(false), m_socket(NULL)
53 EventHandler::EventHandler(Mutex& m,StdLog *p) : SocketHandler(m, p), m_quit(false), m_socket(NULL)
58 EventHandler::~EventHandler()
60 while (m_events.size())
62 std::list<Event *>::iterator it = m_events.begin();
63 Event *e = *it;
64 e -> GetFrom() -> SetHandlerInvalid();
65 delete e;
66 m_events.erase(it);
71 bool EventHandler::GetTimeUntilNextEvent(struct timeval *tv)
73 if (!m_events.size())
74 return false;
75 std::list<Event *>::iterator it = m_events.begin();
76 if (it != m_events.end())
78 EventTime now;
79 mytime_t diff = (*it) -> GetTime() - now;
80 if (diff < 1)
82 diff = 1;
84 tv -> tv_sec = static_cast<long>(diff / 1000000);
85 tv -> tv_usec = static_cast<long>(diff % 1000000);
86 return true;
88 return false;
92 void EventHandler::CheckEvents()
94 EventTime now;
95 std::list<Event *>::iterator it = m_events.begin();
96 while (it != m_events.end() && (*it) -> GetTime() < now)
98 Event *e = *it;
99 Socket *s = dynamic_cast<Socket *>(e -> GetFrom());
101 s == NULL This is another object implementing 'IEventOwner' and not a socket.
102 s != NULL This is a Socket implementing IEventOwner, and we can check that the
103 object instance still is valid using SocketHandler::Valid.
105 if (!s || (s && Valid(s)))
107 e -> GetFrom() -> OnEvent(e -> GetID());
108 e -> GetFrom() -> Decrease();
110 delete e;
111 m_events.erase(it);
112 it = m_events.begin();
117 long EventHandler::AddEvent(IEventOwner *from,long sec,long usec)
119 Event *e = new Event(from, sec, usec);
120 std::list<Event *>::iterator it = m_events.begin();
121 while (it != m_events.end() && *(*it) < *e)
123 it++;
125 m_events.insert(it, e);
126 if (m_socket)
128 m_socket -> Send("\n");
130 from -> Increase();
131 return e -> GetID();
135 void EventHandler::ClearEvents(IEventOwner *from)
137 bool repeat;
140 repeat = false;
141 for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); it++)
143 Event *e = *it;
144 if (e -> GetFrom() == from)
146 delete e;
147 m_events.erase(it);
148 repeat = true;
149 from -> Decrease();
150 break;
153 } while (repeat);
157 void EventHandler::EventLoop()
159 while (!m_quit)
161 struct timeval tv;
162 if (GetTimeUntilNextEvent(&tv))
164 Select(&tv);
165 CheckEvents();
167 else
169 Select();
175 void EventHandler::SetQuit(bool x)
177 m_quit = x;
181 void EventHandler::RemoveEvent(IEventOwner *from, long eid)
183 for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); it++)
185 Event *e = *it;
186 if (from == e -> GetFrom() && eid == e -> GetID())
188 delete e;
189 m_events.erase(it);
190 break;
196 void EventHandler::Add(Socket *p)
198 if (!m_socket)
200 ListenSocket<TcpSocket> *l = new ListenSocket<TcpSocket>(*this);
201 l -> SetDeleteByHandler();
202 l -> Bind("127.0.0.1", 0);
203 m_port = l -> GetPort();
204 SocketHandler::Add(l);
205 m_socket = new TcpSocket( *this );
206 m_socket -> SetDeleteByHandler();
207 m_socket -> SetConnectTimeout(5);
208 m_socket -> SetConnectionRetry(-1);
209 #ifdef ENABLE_RECONNECT
210 m_socket -> SetReconnect(true);
211 #endif
212 m_socket -> Open("127.0.0.1", m_port);
213 SocketHandler::Add(m_socket);
215 SocketHandler::Add( p );
219 #ifdef SOCKETS_NAMESPACE
221 #endif