-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright © 2017-2019, [email protected] All rights reserved. | ||
Author: [email protected] | ||
Last modified: 2018-4-18 | ||
Description: https://github.com/wlgq2/uv-cpp | ||
*/ | ||
|
||
#include "Client.h" | ||
|
||
|
||
using namespace uv; | ||
using namespace std; | ||
|
||
|
||
Client::Client(uv::EventLoop* loop) | ||
:TcpClient(loop), | ||
sockAddr(nullptr) | ||
{ | ||
setConnectStatusCallback(std::bind(&Client::onConnect,this,std::placeholders::_1)); | ||
setMessageCallback(std::bind(&Client::newMessage,this,std::placeholders::_1,std::placeholders::_2)); | ||
} | ||
|
||
void Client::connectToServer(uv::SocketAddr& addr) | ||
{ | ||
sockAddr = std::make_shared<uv::SocketAddr>(addr); | ||
connect(addr); | ||
} | ||
|
||
void Client::reConnect() | ||
{ | ||
uv::Timer* timer = new uv::Timer(loop_, 500, 0, [this](uv::Timer* ptr) | ||
{ | ||
connect(*(sockAddr.get())); | ||
ptr->close([](uv::Timer* ptr) | ||
{ | ||
delete ptr; | ||
}); | ||
}); | ||
timer->start(); | ||
|
||
} | ||
void Client::onConnect(ConnectStatus status) | ||
{ | ||
if(status != ConnectStatus::OnConnectSuccess) | ||
{ | ||
//重连 | ||
reConnect(); | ||
} | ||
else | ||
{ | ||
char data[] = "test message"; | ||
if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer) | ||
{ | ||
write(data, (int)sizeof(data)); | ||
} | ||
else | ||
{ | ||
uv::Packet packet; | ||
packet.pack(data, sizeof(data)); | ||
write(packet.Buffer().c_str(), packet.PacketSize()); | ||
} | ||
} | ||
} | ||
|
||
void Client::newMessage(const char* buf,ssize_t size) | ||
{ | ||
write(buf, (unsigned)size, nullptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright © 2017-2019, [email protected] All rights reserved. | ||
Author: [email protected] | ||
Last modified: 2018-4-18 | ||
Description: https://github.com/wlgq2/uv-cpp | ||
*/ | ||
|
||
#ifndef CLIENT_H | ||
#define CLIENT_H | ||
|
||
|
||
#include <string> | ||
#include "uv/uv11.h" | ||
|
||
|
||
class Client : public uv::TcpClient | ||
{ | ||
public: | ||
Client(uv::EventLoop* loop); | ||
|
||
void connectToServer(uv::SocketAddr& addr); | ||
void reConnect(); | ||
void onConnect(ConnectStatus status); | ||
void newMessage(const char* buf,ssize_t size); | ||
|
||
private: | ||
std::shared_ptr<uv::SocketAddr> sockAddr; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright © 2017-2019, [email protected] All rights reserved. | ||
Author: [email protected] | ||
Last modified: 2019-10-20 | ||
Description: https://github.com/wlgq2/uv-cpp | ||
*/ | ||
|
||
|
||
#include <iostream> | ||
#include <thread> | ||
#include <memory> | ||
|
||
#include "Client.h" | ||
|
||
|
||
using namespace uv; | ||
|
||
using ClientPtr = std::shared_ptr<Client>; | ||
|
||
void runClients(int count,SocketAddr& server,std::vector<ClientPtr>& clients) | ||
{ | ||
EventLoop loop; | ||
for (int i=0;i<count;i++) | ||
{ | ||
auto client = std::make_shared<Client>(&loop); | ||
client->connectToServer(server); | ||
clients.push_back(client); | ||
} | ||
loop.run(); | ||
} | ||
|
||
|
||
void runClientsCrossThread(std::vector<ClientPtr>& clients) | ||
{ | ||
EventLoop loop; | ||
char data[] = "test"; | ||
uv::Timer timer(&loop,1000,1000, | ||
[&clients,data](uv::Timer*) | ||
{ | ||
for(auto ptr : clients) | ||
{ | ||
ptr->writeInLoop(data,sizeof(data),nullptr); | ||
} | ||
}); | ||
timer.start(); | ||
loop.run(); | ||
} | ||
|
||
int main(int argc, char** args) | ||
{ | ||
SocketAddr addr("127.0.0.1",10005); | ||
std::vector<ClientPtr> clients; | ||
//开1000客户端 | ||
std::thread t1(std::bind(&runClients,1000,addr,clients)); | ||
//跨线程发送消息 | ||
std::thread t2(std::bind(&runClientsCrossThread,clients)); | ||
t1.join(); | ||
t2.join(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright © 2017-2019, [email protected] All rights reserved. | ||
Author: [email protected] | ||
Last modified: 2018-4-18 | ||
Description: https://github.com/wlgq2/uv-cpp | ||
*/ | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
|
||
#include "EchoServer.h" | ||
#include "uv/Packet.h" | ||
|
||
using namespace uv; | ||
using namespace std; | ||
|
||
EchoServer::EchoServer(EventLoop* loop) | ||
:TcpServer(loop) | ||
{ | ||
setMessageCallback(std::bind(&EchoServer::newMessage,this,placeholders::_1,placeholders::_2,placeholders::_3)); | ||
} | ||
|
||
void EchoServer::newMessage(shared_ptr<TcpConnection> connection,const char* buf,ssize_t size) | ||
{ | ||
//不使用buffer | ||
if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer) | ||
{ | ||
//std::cout << "reserve data :" << std::string(buf, size) << std::endl; | ||
#if 1 //直接发送 | ||
connection->write(buf, size, nullptr); | ||
#else //调用write in loop接口 | ||
//实质会直接调用write,并不需要memcpy。 | ||
//writeInLoop需要数据在回调中释放。 | ||
char* data = new char[size](); | ||
memcpy(data, buf, size); | ||
connection->writeInLoop(data, size, | ||
[this](WriteInfo& info) | ||
{ | ||
//write message error. | ||
if (0 != info.status) | ||
{ | ||
cout << "Write error :" << EventLoop::GetErrorMessage(info.status) << endl; | ||
} | ||
delete[] info.buf; | ||
}); | ||
#endif | ||
} | ||
else //使用buffer | ||
{ | ||
Packet packet; | ||
auto packetbuf = connection->getPacketBuffer(); | ||
if (nullptr != packetbuf) | ||
{ | ||
packetbuf->append(buf, static_cast<int>(size)); | ||
//循环读取buffer | ||
while (0 == packetbuf->readPacket(packet)) | ||
{ | ||
std::cout << "reserve data "<< packet.DataSize()<<":" << packet.getData() << std::endl; | ||
connection->write(packet.Buffer().c_str(), packet.PacketSize(), nullptr); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
Copyright © 2017-2019, [email protected] All rights reserved. | ||
Author: [email protected] | ||
Last modified: 2018-4-18 | ||
Description: https://github.com/wlgq2/uv-cpp | ||
*/ | ||
|
||
#ifndef ECHOSERVER_H | ||
#define ECHOSERVER_H | ||
|
||
#include "uv/uv11.h" | ||
|
||
|
||
class EchoServer :public uv::TcpServer | ||
{ | ||
public: | ||
EchoServer(uv::EventLoop* loop); | ||
private : | ||
void newMessage(uv::TcpConnectionPtr connection,const char* buf,ssize_t size); | ||
}; | ||
|
||
#endif // ECHOSERVER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright © 2017-2019, [email protected] All rights reserved. | ||
Author: [email protected] | ||
Last modified: 2019-10-20 | ||
Description: https://github.com/wlgq2/uv-cpp | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
#include "EchoServer.h" | ||
|
||
using namespace uv; | ||
|
||
|
||
int main(int argc, char** args) | ||
{ | ||
EventLoop* loop = EventLoop::DefalutLoop(); | ||
|
||
SocketAddr addr("0.0.0.0", 10005, SocketAddr::Ipv4); | ||
|
||
EchoServer server(loop); | ||
//心跳超时 | ||
server.setTimeout(40); | ||
server.bindAndListen(addr); | ||
loop->run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> | ||
<CodeBlocks_project_file> | ||
<FileVersion major="1" minor="6" /> | ||
<Project> | ||
<Option title="clients" /> | ||
<Option pch_mode="2" /> | ||
<Option compiler="gcc" /> | ||
<Build> | ||
<Target title="Debug"> | ||
<Option output="bin/Debug/clients" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="obj/Debug/" /> | ||
<Option type="1" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-g" /> | ||
</Compiler> | ||
</Target> | ||
<Target title="Release"> | ||
<Option output="bin/Release/clients" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="obj/Release/" /> | ||
<Option type="1" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-O2" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-s" /> | ||
</Linker> | ||
</Target> | ||
</Build> | ||
<Compiler> | ||
<Add option="-Wall" /> | ||
<Add option="-std=c++11" /> | ||
<Add directory="../../../libuv1.22.0/include" /> | ||
<Add directory="../../../" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-pthread" /> | ||
<Add library="../../../libuv1.22.0/lib/gcc7.4.0/libuv.a" /> | ||
</Linker> | ||
<Unit filename="../../../examples/clients/Client.cpp" /> | ||
<Unit filename="../../../examples/clients/Client.h" /> | ||
<Unit filename="../../../examples/clients/main.cpp" /> | ||
<Unit filename="../../../uv/Async.cpp" /> | ||
<Unit filename="../../../uv/Async.h" /> | ||
<Unit filename="../../../uv/CycleBuffer.cpp" /> | ||
<Unit filename="../../../uv/CycleBuffer.h" /> | ||
<Unit filename="../../../uv/EventLoop.cpp" /> | ||
<Unit filename="../../../uv/EventLoop.h" /> | ||
<Unit filename="../../../uv/GlobalConfig.cpp" /> | ||
<Unit filename="../../../uv/GlobalConfig.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" /> | ||
<Unit filename="../../../uv/LogWriter.h" /> | ||
<Unit filename="../../../uv/Packet.cpp" /> | ||
<Unit filename="../../../uv/Packet.h" /> | ||
<Unit filename="../../../uv/PacketBuffer.h" /> | ||
<Unit filename="../../../uv/Signal.cpp" /> | ||
<Unit filename="../../../uv/Signal.h" /> | ||
<Unit filename="../../../uv/SocketAddr.cpp" /> | ||
<Unit filename="../../../uv/SocketAddr.h" /> | ||
<Unit filename="../../../uv/TcpAccepter.cpp" /> | ||
<Unit filename="../../../uv/TcpAccepter.h" /> | ||
<Unit filename="../../../uv/TcpClient.cpp" /> | ||
<Unit filename="../../../uv/TcpClient.h" /> | ||
<Unit filename="../../../uv/TcpConnection.cpp" /> | ||
<Unit filename="../../../uv/TcpConnection.h" /> | ||
<Unit filename="../../../uv/TcpServer.cpp" /> | ||
<Unit filename="../../../uv/TcpServer.h" /> | ||
<Unit filename="../../../uv/Timer.cpp" /> | ||
<Unit filename="../../../uv/Timer.h" /> | ||
<Unit filename="../../../uv/TimerWheel.cpp" /> | ||
<Unit filename="../../../uv/TimerWheel.h" /> | ||
<Unit filename="../../../uv/Udp.cpp" /> | ||
<Unit filename="../../../uv/Udp.h" /> | ||
<Unit filename="../../../uv/uv11.h" /> | ||
<Extensions> | ||
<code_completion /> | ||
<debugger /> | ||
</Extensions> | ||
</Project> | ||
</CodeBlocks_project_file> |
Oops, something went wrong.