From 4fd4d91c782a5109cff8f35a1b8137c05f29e098 Mon Sep 17 00:00:00 2001 From: wlgq2 Date: Wed, 11 Sep 2019 23:30:44 +0800 Subject: [PATCH] add idle event. --- examples/helloworld/main.cpp | 7 ++- .../all_example/all_example.cbp | 2 + .../helloworld/helloworld.cbp | 2 + .../codeblocks_linux/pingpong/pingpong.cbp | 2 + .../vs2017/all_example/all_example.vcxproj | 2 + .../all_example/all_example.vcxproj.filters | 6 +++ project/vs2017/helloworld/helloworld.vcxproj | 2 + .../helloworld/helloworld.vcxproj.filters | 6 +++ project/vs2017/pingpong/pingpong.vcxproj | 2 + .../vs2017/pingpong/pingpong.vcxproj.filters | 6 +++ uv/Idle.cpp | 44 +++++++++++++++++++ uv/Idle.h | 38 ++++++++++++++++ uv/uv11.h | 3 +- 13 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 uv/Idle.cpp create mode 100644 uv/Idle.h diff --git a/examples/helloworld/main.cpp b/examples/helloworld/main.cpp index 0ea1c3e..87b00ef 100644 --- a/examples/helloworld/main.cpp +++ b/examples/helloworld/main.cpp @@ -5,8 +5,8 @@ int main(int argc, char** args) { //event's loop + //uv::EventLoop* loop = new uv::EventLoop(); uv::EventLoop* loop = uv::EventLoop::DefalutLoop(); - //Tcp Server uv::SocketAddr serverAddr("0.0.0.0", 10002, uv::SocketAddr::Ipv4); uv::TcpServer server(loop, serverAddr); @@ -14,6 +14,9 @@ int main(int argc, char** args) [](std::shared_ptr conn, const char* data , ssize_t size) { std::cout << std::string(data, size) << std::endl; + std::string str("hex :"); + uv::LogWriter::ToHex(str, data, size); + std::cout << str << std::endl; conn->write(data, size,nullptr); }); server.start(); @@ -34,6 +37,6 @@ int main(int argc, char** args) } }); client.connect(serverAddr); - + loop->run(); } diff --git a/project/codeblocks_linux/all_example/all_example.cbp b/project/codeblocks_linux/all_example/all_example.cbp index 9b6069c..4b2d92d 100644 --- a/project/codeblocks_linux/all_example/all_example.cbp +++ b/project/codeblocks_linux/all_example/all_example.cbp @@ -48,6 +48,8 @@ + + diff --git a/project/codeblocks_linux/helloworld/helloworld.cbp b/project/codeblocks_linux/helloworld/helloworld.cbp index 6f20a37..d42e44e 100644 --- a/project/codeblocks_linux/helloworld/helloworld.cbp +++ b/project/codeblocks_linux/helloworld/helloworld.cbp @@ -45,6 +45,8 @@ + + diff --git a/project/codeblocks_linux/pingpong/pingpong.cbp b/project/codeblocks_linux/pingpong/pingpong.cbp index fe4de1e..8d977a2 100644 --- a/project/codeblocks_linux/pingpong/pingpong.cbp +++ b/project/codeblocks_linux/pingpong/pingpong.cbp @@ -49,6 +49,8 @@ + + diff --git a/project/vs2017/all_example/all_example.vcxproj b/project/vs2017/all_example/all_example.vcxproj index 37c22a4..97dbbad 100644 --- a/project/vs2017/all_example/all_example.vcxproj +++ b/project/vs2017/all_example/all_example.vcxproj @@ -141,6 +141,7 @@ + @@ -160,6 +161,7 @@ + diff --git a/project/vs2017/all_example/all_example.vcxproj.filters b/project/vs2017/all_example/all_example.vcxproj.filters index db731cf..8d58903 100644 --- a/project/vs2017/all_example/all_example.vcxproj.filters +++ b/project/vs2017/all_example/all_example.vcxproj.filters @@ -60,6 +60,9 @@ uv + + uv + @@ -119,5 +122,8 @@ example + + uv + \ No newline at end of file diff --git a/project/vs2017/helloworld/helloworld.vcxproj b/project/vs2017/helloworld/helloworld.vcxproj index d0e21e9..714b5ad 100644 --- a/project/vs2017/helloworld/helloworld.vcxproj +++ b/project/vs2017/helloworld/helloworld.vcxproj @@ -139,6 +139,7 @@ + @@ -156,6 +157,7 @@ + diff --git a/project/vs2017/helloworld/helloworld.vcxproj.filters b/project/vs2017/helloworld/helloworld.vcxproj.filters index 48f9f94..c8c4214 100644 --- a/project/vs2017/helloworld/helloworld.vcxproj.filters +++ b/project/vs2017/helloworld/helloworld.vcxproj.filters @@ -59,6 +59,9 @@ uv + + uv + @@ -112,5 +115,8 @@ uv + + uv + \ No newline at end of file diff --git a/project/vs2017/pingpong/pingpong.vcxproj b/project/vs2017/pingpong/pingpong.vcxproj index 0095bd1..a5f97ab 100644 --- a/project/vs2017/pingpong/pingpong.vcxproj +++ b/project/vs2017/pingpong/pingpong.vcxproj @@ -141,6 +141,7 @@ + @@ -161,6 +162,7 @@ + diff --git a/project/vs2017/pingpong/pingpong.vcxproj.filters b/project/vs2017/pingpong/pingpong.vcxproj.filters index 03cc9a6..2bb3704 100644 --- a/project/vs2017/pingpong/pingpong.vcxproj.filters +++ b/project/vs2017/pingpong/pingpong.vcxproj.filters @@ -60,6 +60,9 @@ uv + + uv + @@ -122,5 +125,8 @@ example + + uv + \ No newline at end of file diff --git a/uv/Idle.cpp b/uv/Idle.cpp new file mode 100644 index 0000000..321aa29 --- /dev/null +++ b/uv/Idle.cpp @@ -0,0 +1,44 @@ +/* + Copyright 2017, orcaer@yeah.net All rights reserved. + + Author: orcaer@yeah.net + + Last modified: 2019-9-10 + + Description: uv-cpp +*/ + +#include "Idle.h" + +using namespace uv; + +Idle::Idle(EventLoop* loop) + :idle_(new uv_idle_t()) +{ + idle_->data = this; + uv_idle_init(loop->hanlde(), idle_); + uv_idle_start(idle_, idleCallback); +} + +Idle::~Idle() +{ + uv_idle_stop(idle_); + delete idle_; +} + +void Idle::onCallback() +{ + if (callback_) + callback_(); +} + +void Idle::setCallback(DefaultCallback callback) +{ + callback_ = callback; +} + +void Idle::idleCallback(uv_idle_t* handle) +{ + Idle* ptr = static_cast(handle->data); + ptr->onCallback(); +} diff --git a/uv/Idle.h b/uv/Idle.h new file mode 100644 index 0000000..412f366 --- /dev/null +++ b/uv/Idle.h @@ -0,0 +1,38 @@ +/* + Copyright 2017, orcaer@yeah.net All rights reserved. + + Author: orcaer@yeah.net + + Last modified: 2019-9-11 + + Description: https://github.com/wlgq2/uv-cpp +*/ + +#ifndef UV_IDLE_H +#define UV_IDLE_H + +#include "EventLoop.h" + +namespace uv +{ + +class Idle +{ +public: + Idle(EventLoop* loop); + virtual ~Idle(); + + void onCallback(); + void setCallback(DefaultCallback callback); + +private: + uv_idle_t* idle_; + + DefaultCallback callback_; + +private: + static void idleCallback(uv_idle_t *handle); +}; + +} +#endif diff --git a/uv/uv11.h b/uv/uv11.h index 5e0b70d..4b3923a 100644 --- a/uv/uv11.h +++ b/uv/uv11.h @@ -3,7 +3,7 @@ Author: orcaer@yeah.net - Last modified: 2019-8-5 + Last modified: 2019-9-11 Description: https://github.com/wlgq2/uv-cpp */ @@ -18,5 +18,6 @@ #include "LogWriter.h" #include "Packet.h" #include "Udp.h" +#include "Idle.h" #endif