本人一直在寻找一个跨平台的网络库,boost与ACE比较庞大,不考虑。对比了libevent,libev,libuv后,最终选择了libuv.可libuv文档少,例子也简单,对于tcp只有个echo-server的例子。网上也找过对其封装的例子,如下
libsourcey库,封装了许多库。对libuv的封装跟其他代码耦合比较紧,难为剥离 http://sourcey.com/libuv-cpp-wrappers/
C++11封装的,可惜VS10未完全支持C++11 https://github.com/larroy/uvpp
C++封装的 https://github.com/keepallsimple/uvpp
本人想实现一个raw tcp server,支持上万链接数的,网上找到的都没合适我的,没办法只能参照各例子自己封装了。
/***************************************
* @file tcpsocket.h
* @brief 基于libuv封装的tcp服务器与客户端,使用log4z作日志工具
* @details
* @author phata, [email protected]
* @date 2014-5-13
* @mod 2014-5-13 phata 修正服务器与客户端的错误.现服务器支持多客户端连接
修改客户端测试代码,支持并发多客户端测试
****************************************/
#ifndef TCPSocket_H
#define TCPSocket_H
#include "uv.h"
#include
#include
#include
#include "tcpsocket.h"
#include "log4z.h"
std::string GetUVError(int retcode)
{
std::string err;
err = uv_err_name(retcode);
err +=":";
err += uv_strerror(retcode);
return std::move(err);
}
namespace uv
{
/*****************************************TCP Server*************************************************************/
TCPServer::TCPServer(uv_loop_t* loop)
:newconcb_(nullptr), isinit_(false)
{
loop_ = loop;
}
TCPServer::~TCPServer()
{
close();
LOGI("tcp server exit.");
}
//初始化与关闭--服务器与客户端一致
bool TCPServer::init()
{
if (isinit_) {
return true;
}
if (!loop_) {
errmsg_ = "loop is null on tcp init.";
LOGE(errmsg_);
return false;
}
int iret = uv_mutex_init(&mutex_handle_);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
iret = uv_tcp_init(loop_,&server_);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
isinit_ = true;
server_.data = this;
//iret = uv_tcp_keepalive(&server_, 1, 60);//调用此函数后后续函数会调用出错
//if (iret) {
// errmsg_ = GetUVError(iret);
// return false;
//}
return true;
}
void TCPServer::close()
{
for (auto it = clients_list_.begin(); it!=clients_list_.end(); ++it) {
auto data = it->second;
uv_close((uv_handle_t*)data->client_handle,AfterClientClose);
}
clients_list_.clear();
LOGI("close server");
if (isinit_) {
uv_close((uv_handle_t*) &server_, AfterServerClose);
LOGI("close server");
}
isinit_ = false;
uv_mutex_destroy(&mutex_handle_);
}
bool TCPServer::run(int status)
{
LOGI("server runing.");
int iret = uv_run(loop_,(uv_run_mode)status);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
return true;
}
//属性设置--服务器与客户端一致
bool TCPServer::setNoDelay(bool enable)
{
int iret = uv_tcp_nodelay(&server_, enable ? 1 : 0);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
return true;
}
bool TCPServer::setKeepAlive(int enable, unsigned int delay)
{
int iret = uv_tcp_keepalive(&server_, enable , delay);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
return true;
}
//作为server时的函数
bool TCPServer::bind(const char* ip, int port)
{
struct sockaddr_in bind_addr;
int iret = uv_ip4_addr(ip, port, &bind_addr);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
iret = uv_tcp_bind(&server_, (const struct sockaddr*)&bind_addr,0);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
LOGI("server bind ip="<second->writebuffer.len < len) {
itfind->second->writebuffer.base = (char*)realloc(itfind->second->writebuffer.base,len);
itfind->second->writebuffer.len = len;
}
memcpy(itfind->second->writebuffer.base,data,len);
uv_buf_t buf = uv_buf_init((char*)itfind->second->writebuffer.base,len);
int iret = uv_write(&itfind->second->write_req, (uv_stream_t*)itfind->second->client_handle, &buf, 1, AfterSend);
if (iret) {
errmsg_ = GetUVError(iret);
LOGE(errmsg_);
return false;
}
return true;
}
//服务器-新客户端函数
void TCPServer::acceptConnection(uv_stream_t *server, int status)
{
if (!server->data) {
return;
}
TCPServer *tcpsock = (TCPServer *)server->data;
int clientid = tcpsock->GetAvailaClientID();
clientdata* cdata = new clientdata(clientid);//uv_close回调函数中释放
cdata->tcp_server = tcpsock;//保存服务器的信息
int iret = uv_tcp_init(tcpsock->loop_, cdata->client_handle);//析构函数释放
if (iret) {
delete cdata;
tcpsock->errmsg_ = GetUVError(iret);
LOGE(tcpsock->errmsg_);
return;
}
iret = uv_accept((uv_stream_t*)&tcpsock->server_, (uv_stream_t*) cdata->client_handle);
if ( iret) {
tcpsock->errmsg_ = GetUVError(iret);
uv_close((uv_handle_t*) cdata->client_handle, NULL);
delete cdata;
LOGE(tcpsock->errmsg_);
return;
}
tcpsock->clients_list_.insert(std::make_pair(clientid,cdata));//加入到链接队列
if (tcpsock->newconcb_) {
tcpsock->newconcb_(clientid);
}
LOGI("new client("<client_handle<<") id="<< clientid);
iret = uv_read_start((uv_stream_t*)cdata->client_handle, onAllocBuffer, AfterServerRecv);//服务器开始接收客户端的数据
return;
}
//服务器-接收数据回调函数
void TCPServer::setrecvcb(int clientid, server_recvcb cb )
{
auto itfind = clients_list_.find(clientid);
if (itfind != clients_list_.end()) {
itfind->second->recvcb_ = cb;
}
}
//服务器-新链接回调函数
void TCPServer::setnewconnectcb(newconnect cb )
{
newconcb_ = cb;
}
//服务器分析空间函数
void TCPServer::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
{
if (!handle->data) {
return;
}
clientdata *client = (clientdata*)handle->data;
*buf = client->readbuffer;
}
void TCPServer::AfterServerRecv(uv_stream_t *handle, ssize_t nread, const uv_buf_t* buf)
{
if (!handle->data) {
return;
}
clientdata *client = (clientdata*)handle->data;//服务器的recv带的是clientdata
if (nread < 0) {/* Error or EOF */
TCPServer *server = (TCPServer *)client->tcp_server;
if (nread == UV_EOF) {
fprintf(stdout,"客户端(%d)连接断开,关闭此客户端\n",client->client_id);
LOGW("客户端("<client_id<<")主动断开");
} else if (nread == UV_ECONNRESET) {
fprintf(stdout,"客户端(%d)异常断开\n",client->client_id);
LOGW("客户端("<client_id<<")异常断开");
} else {
fprintf(stdout,"%s\n",GetUVError(nread));
LOGW("客户端("<client_id<<")异常断开:"<DeleteClient(client->client_id);//连接断开,关闭客户端
return;
} else if (0 == nread) {/* Everything OK, but nothing read. */
} else if (client->recvcb_) {
client->recvcb_(client->client_id,buf->base,nread);
}
}
//服务器与客户端一致
void TCPServer::AfterSend(uv_write_t *req, int status)
{
if (status < 0) {
LOGE("发送数据有误:"<data;
LOGI("client "<client_id<<" had closed.");
delete cdata;
}
int TCPServer::GetAvailaClientID() const
{
static int s_id = 0;
return ++s_id;
}
bool TCPServer::DeleteClient( int clientid )
{
uv_mutex_lock(&mutex_handle_);
auto itfind = clients_list_.find(clientid);
if (itfind == clients_list_.end()) {
errmsg_ = "can't find client ";
errmsg_ += std::to_string((long long)clientid);
LOGE(errmsg_);
uv_mutex_unlock(&mutex_handle_);
return false;
}
if (uv_is_active((uv_handle_t*)itfind->second->client_handle)) {
uv_read_stop((uv_stream_t*)itfind->second->client_handle);
}
uv_close((uv_handle_t*)itfind->second->client_handle,AfterClientClose);
clients_list_.erase(itfind);
LOGI("删除客户端"<SetLoggerMonthdir(LOG4Z_MAIN_LOGGER_ID, true);
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerDisplay(LOG4Z_MAIN_LOGGER_ID,false);
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerLevel(LOG4Z_MAIN_LOGGER_ID,LOG_LEVEL_DEBUG);
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerLimitSize(LOG4Z_MAIN_LOGGER_ID,100);
if (logpath) {
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerPath(LOG4Z_MAIN_LOGGER_ID,logpath);
}
zsummer::log4z::ILog4zManager::GetInstance()->Start();
}
/*****************************************TCP Client*************************************************************/
TCPClient::TCPClient(uv_loop_t* loop)
:recvcb_(nullptr),userdata_(nullptr)
,connectstatus_(CONNECT_DIS)
, isinit_(false)
{
readbuffer_ = uv_buf_init((char*) malloc(BUFFERSIZE), BUFFERSIZE);
writebuffer_ = uv_buf_init((char*) malloc(BUFFERSIZE), BUFFERSIZE);
loop_ = loop;
connect_req_.data = this;
write_req_.data = this;
}
TCPClient::~TCPClient()
{
free(readbuffer_.base);
readbuffer_.base = nullptr;
readbuffer_.len = 0;
free(writebuffer_.base);
writebuffer_.base = nullptr;
writebuffer_.len = 0;
close();
LOGI("客户端("<connectip_.c_str(), pclient->connectport_, &bind_addr);
if (iret) {
pclient->errmsg_ = GetUVError(iret);
LOGE(pclient->errmsg_);
return;
}
iret = uv_tcp_connect(&pclient->connect_req_, &pclient->client_, (const sockaddr*)&bind_addr, AfterConnect);
if (iret) {
pclient->errmsg_ = GetUVError(iret);
LOGE(pclient->errmsg_);
return;
}
fprintf(stdout,"client(%p) ConnectThread end, connect status %d\n",pclient, pclient->connectstatus_);
pclient->run();
}
void TCPClient::ConnectThread6( void* arg )
{
TCPClient *pclient = (TCPClient*)arg;
LOGI("客户端("<connectip_.c_str(), pclient->connectport_, &bind_addr);
if (iret) {
pclient->errmsg_ = GetUVError(iret);
LOGE(pclient->errmsg_);
return;
}
iret = uv_tcp_connect(&pclient->connect_req_, &pclient->client_, (const sockaddr*)&bind_addr, AfterConnect);
if (iret) {
pclient->errmsg_ = GetUVError(iret);
LOGE(pclient->errmsg_);
return;
}
fprintf(stdout,"client(%p) ConnectThread end, connect status %d\n",pclient, pclient->connectstatus_);
LOGI("客户端("<connectstatus_);
pclient->run();
}
void TCPClient::AfterConnect(uv_connect_t* handle, int status)
{
fprintf(stdout,"start after connect\n");
TCPClient *pclient = (TCPClient*)handle->handle->data;
if (status) {
pclient->connectstatus_ = CONNECT_ERROR;
fprintf(stdout,"connect error:%s\n",GetUVError(status));
return;
}
int iret = uv_read_start(handle->handle, onAllocBuffer, AfterClientRecv);//客户端开始接收服务器的数据
if (iret) {
fprintf(stdout,"uv_read_start error:%s\n",GetUVError(iret));
pclient->connectstatus_ = CONNECT_ERROR;
} else {
pclient->connectstatus_ = CONNECT_FINISH;
}
LOGI("客户端("<data) {
return;
}
TCPClient *client = (TCPClient*)handle->data;
*buf = client->readbuffer_;
}
void TCPClient::AfterClientRecv(uv_stream_t *handle, ssize_t nread, const uv_buf_t* buf)
{
if (!handle->data) {
return;
}
TCPClient *client = (TCPClient*)handle->data;//服务器的recv带的是TCPClient
if (nread < 0) {
if (nread == UV_EOF) {
fprintf(stdout,"服务器(%p)主动断开\n",handle);
LOGW("服务器主动断开");
} else if (nread == UV_ECONNRESET) {
fprintf(stdout,"服务器(%p)异常断开\n",handle);
LOGW("服务器异常断开");
} else {
fprintf(stdout,"服务器(%p)异常断开:%s\n",handle,GetUVError(nread));
LOGW("服务器异常断开"< 0 && client->recvcb_) {
client->recvcb_(buf->base,nread,client->userdata_);
}
}
//服务器与客户端一致
void TCPClient::AfterSend(uv_write_t *req, int status)
{
TCPClient *client = (TCPClient *)req->handle->data;
uv_mutex_unlock(&client->write_mutex_handle_);
if (status < 0) {
LOGE("发送数据有误:"<SetLoggerMonthdir(LOG4Z_MAIN_LOGGER_ID, true);
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerDisplay(LOG4Z_MAIN_LOGGER_ID,false);
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerLevel(LOG4Z_MAIN_LOGGER_ID,LOG_LEVEL_DEBUG);
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerLimitSize(LOG4Z_MAIN_LOGGER_ID,100);
if (logpath) {
zsummer::log4z::ILog4zManager::GetInstance()->SetLoggerPath(LOG4Z_MAIN_LOGGER_ID,logpath);
}
zsummer::log4z::ILog4zManager::GetInstance()->Start();
}
}
按照例子,客户端可以并发20多路,超过uv_write就会assert出错,未找到原因
服务器可以接收几十路连接。万百上千路未测试过,因为没有模拟环境。