2019独角兽企业重金招聘Python工程师标准>>> ![hot3.png](http://img.e-com-net.com/image/info8/35bf71b4b7ad4a72bcb9c6561b5679b1.jpg)
1
安装方法,源码包有说明:https://github.com/Qihoo360/evpp
----------------------------------------------------------------------------------------------------------------
2
tcp回声服务器(官方示例)
#include
#include
#include
#ifdef _WIN32
#include "../../winmain-inl.h"
#endif
void OnMessage(const evpp::TCPConnPtr& conn,
evpp::Buffer* msg) {
std::string s = msg->NextAllString();
LOG_INFO << "Received a message [" << s << "]";
conn->Send(s);
if (s == "quit" || s == "exit") {
conn->Close();
}
}
void OnConnection(const evpp::TCPConnPtr& conn) {
if (conn->IsConnected()) {
LOG_INFO << "Accept a new connection from " << conn->remote_addr();
} else {
LOG_INFO << "Disconnected from " << conn->remote_addr();
}
}
int main(int argc, char* argv[]) {
std::string port = "9099";
if (argc == 2) {
port = argv[1];
}
std::string addr = std::string("0.0.0.0:") + port;
evpp::EventLoop loop;
evpp::TCPServer server(&loop, addr, "TCPEcho", 0);
server.SetMessageCallback(&OnMessage);
server.SetConnectionCallback(&OnConnection);
server.Init();
server.Start();
loop.Run();
return 0;
}
----------------------------------------------------------------------------------------------------------------
3
evpp::TCPServer类(主要)
#include
#pragma once
#include "evpp/inner_pre.h"
#include "evpp/event_loop.h"
#include "evpp/event_loop_thread_pool.h"
#include "evpp/tcp_callbacks.h"
#include "evpp/thread_dispatch_policy.h"
#include "evpp/server_status.h"
#include
----------------------------------------------------------------------------------------------------------------
4
std::function回调类型
part3中的两个evpp::TCPServer类的成员函数(设置回调函数)
void SetConnectionCallback(const ConnectionCallback& cb)
void SetMessageCallback(MessageCallback cb)
其回调类型在源码 #include "evpp/tcp_callbacks.h"
#pragma once
#include "evpp/inner_pre.h"
namespace evpp {
class Buffer;
class TCPConn;
typedef std::shared_ptr TCPConnPtr;
typedef std::function TimerCallback;
// When a connection established, broken down, connecting failed, this callback will be called
// This is called from a work-thread this is not the listening thread probably
typedef std::function ConnectionCallback;
typedef std::function CloseCallback;
typedef std::function WriteCompleteCallback;
typedef std::function HighWaterMarkCallback;
typedef std::function MessageCallback;
namespace internal {
inline void DefaultConnectionCallback(const TCPConnPtr&) {}
inline void DefaultMessageCallback(const TCPConnPtr&, Buffer*) {}
}
}
----------------------------------------------------------------------------------------------------------------
5
evpp::EventLoop类
evpp::EventLoop类是IO事件的驱动内核。这个类是event_base类的包装器,但不仅仅是一个包装器。它通过简单的方式,运行IO事件驱动循环。一个线程一个循环。
在evpp::TCPServer对象设置完回调函数及调用Init()、Start()之后,必须执行IO事件线程Run(),如part2示例。
----------------------------------------------------------------------------------------------------------------
6
以上是evpp tcp网络库的大致用法。udp、http用法参照源码。
evpp语法采用C++11的特性,大量使用std::string,而不是char*独占天;使用std::stringstream,而不是sprintf;易读的Lambda,而不是函数指针等等。满满的C++11诚意。http库更是可以弥补C++开发http服务器效率低的问题。膜拜360大佬们!