Skip to content

Commit

Permalink
add idle event.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlgq2 committed Sep 11, 2019
1 parent 8b24310 commit 4fd4d91
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 3 deletions.
7 changes: 5 additions & 2 deletions examples/helloworld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
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);
server.setMessageCallback(
[](std::shared_ptr<uv::TcpConnection> 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();
Expand All @@ -34,6 +37,6 @@ int main(int argc, char** args)
}
});
client.connect(serverAddr);

loop->run();
}
2 changes: 2 additions & 0 deletions project/codeblocks_linux/all_example/all_example.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<Unit filename="../../../uv/CycleBuffer.h" />
<Unit filename="../../../uv/EventLoop.cpp" />
<Unit filename="../../../uv/EventLoop.h" />
<Unit filename="../../../uv/Idle.cpp" />
<Unit filename="../../../uv/Idle.h" />
<Unit filename="../../../uv/ListBuffer.cpp" />
<Unit filename="../../../uv/ListBuffer.h" />
<Unit filename="../../../uv/LogWriter.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions project/codeblocks_linux/helloworld/helloworld.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<Unit filename="../../../uv/CycleBuffer.h" />
<Unit filename="../../../uv/EventLoop.cpp" />
<Unit filename="../../../uv/EventLoop.h" />
<Unit filename="../../../uv/Idle.cpp" />
<Unit filename="../../../uv/Idle.h" />
<Unit filename="../../../uv/ListBuffer.cpp" />
<Unit filename="../../../uv/ListBuffer.h" />
<Unit filename="../../../uv/LogWriter.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions project/codeblocks_linux/pingpong/pingpong.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
<Unit filename="../../../uv/CycleBuffer.h" />
<Unit filename="../../../uv/EventLoop.cpp" />
<Unit filename="../../../uv/EventLoop.h" />
<Unit filename="../../../uv/Idle.cpp" />
<Unit filename="../../../uv/Idle.h" />
<Unit filename="../../../uv/ListBuffer.cpp" />
<Unit filename="../../../uv/ListBuffer.h" />
<Unit filename="../../../uv/LogWriter.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions project/vs2017/all_example/all_example.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<ClCompile Include="..\..\..\uv\Async.cpp" />
<ClCompile Include="..\..\..\uv\CycleBuffer.cpp" />
<ClCompile Include="..\..\..\uv\EventLoop.cpp" />
<ClCompile Include="..\..\..\uv\Idle.cpp" />
<ClCompile Include="..\..\..\uv\ListBuffer.cpp" />
<ClCompile Include="..\..\..\uv\LogWriter.cpp" />
<ClCompile Include="..\..\..\uv\Packet.cpp" />
Expand All @@ -160,6 +161,7 @@
<ClInclude Include="..\..\..\uv\Async.h" />
<ClInclude Include="..\..\..\uv\CycleBuffer.h" />
<ClInclude Include="..\..\..\uv\EventLoop.h" />
<ClInclude Include="..\..\..\uv\Idle.h" />
<ClInclude Include="..\..\..\uv\ListBuffer.h" />
<ClInclude Include="..\..\..\uv\LogWriter.h" />
<ClInclude Include="..\..\..\uv\Packet.h" />
Expand Down
6 changes: 6 additions & 0 deletions project/vs2017/all_example/all_example.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ClCompile Include="..\..\..\uv\Timer.cpp">
<Filter>uv</Filter>
</ClCompile>
<ClCompile Include="..\..\..\uv\Idle.cpp">
<Filter>uv</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\uv\Async.h">
Expand Down Expand Up @@ -119,5 +122,8 @@
<ClInclude Include="..\..\..\examples\all_example\EchoServer.h">
<Filter>example</Filter>
</ClInclude>
<ClInclude Include="..\..\..\uv\Idle.h">
<Filter>uv</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions project/vs2017/helloworld/helloworld.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<ClCompile Include="..\..\..\uv\Async.cpp" />
<ClCompile Include="..\..\..\uv\CycleBuffer.cpp" />
<ClCompile Include="..\..\..\uv\EventLoop.cpp" />
<ClCompile Include="..\..\..\uv\Idle.cpp" />
<ClCompile Include="..\..\..\uv\ListBuffer.cpp" />
<ClCompile Include="..\..\..\uv\LogWriter.cpp" />
<ClCompile Include="..\..\..\uv\Packet.cpp" />
Expand All @@ -156,6 +157,7 @@
<ClInclude Include="..\..\..\uv\Async.h" />
<ClInclude Include="..\..\..\uv\CycleBuffer.h" />
<ClInclude Include="..\..\..\uv\EventLoop.h" />
<ClInclude Include="..\..\..\uv\Idle.h" />
<ClInclude Include="..\..\..\uv\ListBuffer.h" />
<ClInclude Include="..\..\..\uv\LogWriter.h" />
<ClInclude Include="..\..\..\uv\Packet.h" />
Expand Down
6 changes: 6 additions & 0 deletions project/vs2017/helloworld/helloworld.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<ClCompile Include="..\..\..\uv\Timer.cpp">
<Filter>uv</Filter>
</ClCompile>
<ClCompile Include="..\..\..\uv\Idle.cpp">
<Filter>uv</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\uv\Async.h">
Expand Down Expand Up @@ -112,5 +115,8 @@
<ClInclude Include="..\..\..\uv\Udp.h">
<Filter>uv</Filter>
</ClInclude>
<ClInclude Include="..\..\..\uv\Idle.h">
<Filter>uv</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions project/vs2017/pingpong/pingpong.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<ClCompile Include="..\..\..\uv\Async.cpp" />
<ClCompile Include="..\..\..\uv\CycleBuffer.cpp" />
<ClCompile Include="..\..\..\uv\EventLoop.cpp" />
<ClCompile Include="..\..\..\uv\Idle.cpp" />
<ClCompile Include="..\..\..\uv\ListBuffer.cpp" />
<ClCompile Include="..\..\..\uv\LogWriter.cpp" />
<ClCompile Include="..\..\..\uv\Packet.cpp" />
Expand All @@ -161,6 +162,7 @@
<ClInclude Include="..\..\..\uv\Async.h" />
<ClInclude Include="..\..\..\uv\CycleBuffer.h" />
<ClInclude Include="..\..\..\uv\EventLoop.h" />
<ClInclude Include="..\..\..\uv\Idle.h" />
<ClInclude Include="..\..\..\uv\ListBuffer.h" />
<ClInclude Include="..\..\..\uv\LogWriter.h" />
<ClInclude Include="..\..\..\uv\Packet.h" />
Expand Down
6 changes: 6 additions & 0 deletions project/vs2017/pingpong/pingpong.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ClCompile Include="..\..\..\uv\Timer.cpp">
<Filter>uv</Filter>
</ClCompile>
<ClCompile Include="..\..\..\uv\Idle.cpp">
<Filter>uv</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\uv\Async.h">
Expand Down Expand Up @@ -122,5 +125,8 @@
<ClInclude Include="..\..\..\examples\pingpang\ModeDefine.h">
<Filter>example</Filter>
</ClInclude>
<ClInclude Include="..\..\..\uv\Idle.h">
<Filter>uv</Filter>
</ClInclude>
</ItemGroup>
</Project>
44 changes: 44 additions & 0 deletions uv/Idle.cpp
Original file line number Diff line number Diff line change
@@ -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<Idle*>(handle->data);
ptr->onCallback();
}
38 changes: 38 additions & 0 deletions uv/Idle.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion uv/uv11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -18,5 +18,6 @@
#include "LogWriter.h"
#include "Packet.h"
#include "Udp.h"
#include "Idle.h"

#endif

0 comments on commit 4fd4d91

Please sign in to comment.