1 #include <EventHandler.h>
3 #include <IEventOwner.h>
4 #include <ListenSocket.h>
14 Utility::GetTime(&tv
);
15 return tv
.tv_sec
+ (double)tv
.tv_usec
/ 1000000;
19 class MyEvHandler
: public EventHandler
22 MyEvHandler() : EventHandler() {}
23 MyEvHandler(StdLog
*p
) : EventHandler(p
) {}
28 class EvThread
: public Thread
31 EvThread(Socket
*p
) : m_socket(p
) {}
40 class EvSocket
: public TcpSocket
, public IEventOwner
43 EvSocket(ISocketHandler
& h
) : TcpSocket(h
)
44 , IEventOwner( static_cast<MyEvHandler
&>(h
) ) {
47 void OnEvent(int id
) {
48 std::cout
<< "OnEvent" << std::endl
;
49 std::cout
<< gettime() - start
<< std::endl
;
54 std::cout
<< "Incoming" << std::endl
;
58 std::cout
<< "Connected" << std::endl
;
59 EvThread
*thr
= new EvThread( this );
60 thr
-> SetDeleteOnExit();
64 std::cout
<< "EvSocket::OnDelete()" << std::endl
;
71 Socket
*p0
= (Socket
*)m_socket
;
72 EvSocket
*p
= dynamic_cast<EvSocket
*>(p0
);
80 std::cout
<< "Add event" << std::endl
;
82 p
-> AddEvent(1, 50000);
86 std::cout
<< "Thread: not an EvSocket" << std::endl
;
91 //------------------------------------------------------------
93 //------------------------------------------------------------
94 class myTimer
: public IEventOwner
97 // Prototype of user call back function
98 typedef void (*myTimer_cb
)(myTimer
* user_func
, void* user_id
);
100 myTimer(IEventHandler
& h
, long ssec
, long susec
,
101 long psec
, long pusec
, myTimer_cb func
, void* id
);
105 void OnEvent(int id
);
110 myTimer_cb user_func_
;
116 //------------------------------------------------------------
117 myTimer::myTimer(IEventHandler
& h
, long ssec
, long susec
,
118 long psec
, long pusec
, myTimer_cb user_func
,
125 user_func_(user_func
),
127 periodic_(psec
!= 0 || pusec
!= 0)
129 AddEvent(ssec_
, susec_
);
133 //------------------------------------------------------------
140 //------------------------------------------------------------
141 void myTimer::OnEvent(int id
)
145 int n
= AddEvent(psec_
, pusec_
);
148 user_func_(this, user_id_
);
157 //------------------------------------------------------------
158 static void myfunc(myTimer
* t
, void* user_id
)
160 printf("Event %s\n", (char*)user_id
);
165 int main(int argc, char *argv[])
169 // Example 1: Create a 2 sec one-shot timer.
170 // No need to save the pointer as it will delete
171 // itself upon expiry.
172 new myTimer(h, 2, 0, 0, 0, myfunc, (void*)"My Timer 0");
174 // Example 2: Create a 1 sec periodic timer.
175 // Save the pointer because we can stop the timer
176 // later with a "delete mytimer".
177 myTimer* mytimer = new myTimer(h, 0, 0, 1, 0, myfunc,
178 (void*)"My Timer 1");
180 // Drop into the event handler ..
186 int main(int argc
, char *argv
[])
190 ListenSocket
<EvSocket
> l(h
);
191 l
.Bind("localhost", 12344);
195 sock
.Open("localhost", 12344);
198 // Example 1: Create a 2 sec one-shot timer.
199 // No need to save the pointer as it will delete
200 // itself upon expiry.
201 new myTimer(h
, 2, 0, 0, 0, myfunc
, (void*)"My Timer 0");
203 // Example 2: Create a 1 sec periodic timer.
204 // Save the pointer because we can stop the timer
205 // later with a "delete mytimer".
206 myTimer
* mytimer
= new myTimer(h
, 0, 0, 1, 0, myfunc
,
207 (void*)"My Timer 1");