1 /** \file EventHandler.cpp
3 ** \author grymse@alhem.net
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.
32 #pragma warning(disable:4786)
35 #include "EventHandler.h"
36 #include "IEventOwner.h"
39 #include "TcpSocket.h"
40 #include "ListenSocket.h"
43 #ifdef SOCKETS_NAMESPACE
44 namespace SOCKETS_NAMESPACE
{
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();
64 e
-> GetFrom() -> SetHandlerInvalid();
71 bool EventHandler::GetTimeUntilNextEvent(struct timeval
*tv
)
75 std::list
<Event
*>::iterator it
= m_events
.begin();
76 if (it
!= m_events
.end())
79 mytime_t diff
= (*it
) -> GetTime() - now
;
84 tv
-> tv_sec
= static_cast<long>(diff
/ 1000000);
85 tv
-> tv_usec
= static_cast<long>(diff
% 1000000);
92 void EventHandler::CheckEvents()
95 std::list
<Event
*>::iterator it
= m_events
.begin();
96 while (it
!= m_events
.end() && (*it
) -> GetTime() < now
)
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();
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
)
125 m_events
.insert(it
, e
);
128 m_socket
-> Send("\n");
135 void EventHandler::ClearEvents(IEventOwner
*from
)
141 for (std::list
<Event
*>::iterator it
= m_events
.begin(); it
!= m_events
.end(); it
++)
144 if (e
-> GetFrom() == from
)
157 void EventHandler::EventLoop()
162 if (GetTimeUntilNextEvent(&tv
))
175 void EventHandler::SetQuit(bool x
)
181 void EventHandler::RemoveEvent(IEventOwner
*from
, long eid
)
183 for (std::list
<Event
*>::iterator it
= m_events
.begin(); it
!= m_events
.end(); it
++)
186 if (from
== e
-> GetFrom() && eid
== e
-> GetID())
196 void EventHandler::Add(Socket
*p
)
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);
212 m_socket
-> Open("127.0.0.1", m_port
);
213 SocketHandler::Add(m_socket
);
215 SocketHandler::Add( p
);
219 #ifdef SOCKETS_NAMESPACE