switched from PracticalSocket to libasio
[anytun.git] / src / Sockets / tests / events.cpp
blob5d0897359bd810f7093040551657904c0f2c126f
1 #include <EventHandler.h>
2 #include <TcpSocket.h>
3 #include <IEventOwner.h>
4 #include <ListenSocket.h>
5 #include <StdoutLog.h>
6 #include <iostream>
9 double start;
11 double gettime()
13 struct timeval tv;
14 Utility::GetTime(&tv);
15 return tv.tv_sec + (double)tv.tv_usec / 1000000;
19 class MyEvHandler : public EventHandler
21 public:
22 MyEvHandler() : EventHandler() {}
23 MyEvHandler(StdLog *p) : EventHandler(p) {}
28 class EvThread : public Thread
30 public:
31 EvThread(Socket *p) : m_socket(p) {}
33 void Run();
35 private:
36 Socket *m_socket;
40 class EvSocket : public TcpSocket, public IEventOwner
42 public:
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;
50 SetCloseAndDelete();
53 void OnAccept() {
54 std::cout << "Incoming" << std::endl;
57 void OnConnect() {
58 std::cout << "Connected" << std::endl;
59 EvThread *thr = new EvThread( this );
60 thr -> SetDeleteOnExit();
63 void OnDelete() {
64 std::cout << "EvSocket::OnDelete()" << std::endl;
69 void EvThread::Run()
71 Socket *p0 = (Socket *)m_socket;
72 EvSocket *p = dynamic_cast<EvSocket *>(p0);
73 if (p)
75 #ifdef _WIN32
76 Sleep( 5000 );
77 #else
78 sleep(10);
79 #endif
80 std::cout << "Add event" << std::endl;
81 start = gettime();
82 p -> AddEvent(1, 50000);
84 else
86 std::cout << "Thread: not an EvSocket" << std::endl;
91 //------------------------------------------------------------
92 // myTimer class
93 //------------------------------------------------------------
94 class myTimer : public IEventOwner
96 public:
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);
102 ~myTimer();
104 private:
105 void OnEvent(int id);
106 long ssec_;
107 long susec_;
108 long psec_;
109 long pusec_;
110 myTimer_cb user_func_;
111 void* user_id_;
112 bool periodic_;
116 //------------------------------------------------------------
117 myTimer::myTimer(IEventHandler& h, long ssec, long susec,
118 long psec, long pusec, myTimer_cb user_func,
119 void* user_id) :
120 IEventOwner(h),
121 ssec_(ssec),
122 susec_(susec),
123 psec_(psec),
124 pusec_(pusec),
125 user_func_(user_func),
126 user_id_(user_id),
127 periodic_(psec != 0 || pusec != 0)
129 AddEvent(ssec_, susec_);
133 //------------------------------------------------------------
134 myTimer::~myTimer()
136 ClearEvents();
140 //------------------------------------------------------------
141 void myTimer::OnEvent(int id)
143 if (periodic_)
145 int n = AddEvent(psec_, pusec_);
148 user_func_(this, user_id_);
150 if (!periodic_)
152 delete this;
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[])
167 EventHandler h;
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 ..
181 h.EventLoop();
186 int main(int argc, char *argv[])
188 StdoutLog log;
189 MyEvHandler h(&log);
190 ListenSocket<EvSocket> l(h);
191 l.Bind("localhost", 12344);
192 h.Add(&l);
194 EvSocket sock(h);
195 sock.Open("localhost", 12344);
196 h.Add(&sock);
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");
209 h.EventLoop();