From f9f17a60fbef4174c2dd8ae406f742371b62421f Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 8 Jan 2020 14:38:18 +0800 Subject: [PATCH] 1 --- a8/{tcpclient2.cc => asynctcpclient.cc} | 34 ++++++++++++------------- a8/{tcpclient2.h => asynctcpclient.h} | 18 ++++++------- a8/ioloop.cc | 4 +-- 3 files changed, 28 insertions(+), 28 deletions(-) rename a8/{tcpclient2.cc => asynctcpclient.cc} (91%) rename a8/{tcpclient2.h => asynctcpclient.h} (68%) diff --git a/a8/tcpclient2.cc b/a8/asynctcpclient.cc similarity index 91% rename from a8/tcpclient2.cc rename to a8/asynctcpclient.cc index b9e5857..552db50 100644 --- a/a8/tcpclient2.cc +++ b/a8/asynctcpclient.cc @@ -11,7 +11,7 @@ #include #include -#include +#include #include static const int DEFAULT_MAX_PACKET_LEN = 1024 * 10; @@ -19,44 +19,44 @@ static const int DEFAULT_MAX_RECV_BUFFERSIZE = 1024 * 64; namespace a8 { - TcpClient2::TcpClient2() + AsyncTcpClient::AsyncTcpClient() { send_buffer_mutex_ = new std::mutex(); epoll_fd = a8::IoLoop::Instance()->epoll_fd; } - TcpClient2::~TcpClient2() + AsyncTcpClient::~AsyncTcpClient() { Close(); delete send_buffer_mutex_; send_buffer_mutex_ = nullptr; } - void TcpClient2::Open() + void AsyncTcpClient::Open() { if (!IsActive()) { SetActive(true); } } - void TcpClient2::Close() + void AsyncTcpClient::Close() { if (IsActive()) { SetActive(false); } } - bool TcpClient2::IsActive() + bool AsyncTcpClient::IsActive() { return socket_ != a8::INVALID_SOCKET; } - bool TcpClient2::Connected() + bool AsyncTcpClient::Connected() { return connected_; } - void TcpClient2::SendBuff(const char* buff, unsigned int bufflen) + void AsyncTcpClient::SendBuff(const char* buff, unsigned int bufflen) { if (bufflen > 0) { a8::SendQueueNode* p = (a8::SendQueueNode*)malloc(sizeof(a8::SendQueueNode)); @@ -79,7 +79,7 @@ namespace a8 } } - void TcpClient2::DoConnect() + void AsyncTcpClient::DoConnect() { connected_ = true; if (on_connect) { @@ -87,7 +87,7 @@ namespace a8 } } - void TcpClient2::DoRecv() + void AsyncTcpClient::DoRecv() { if (socket_ == -1) { return; @@ -116,7 +116,7 @@ namespace a8 } } - void TcpClient2::DoSend() + void AsyncTcpClient::DoSend() { if (!connected_) { DoConnect(); @@ -136,7 +136,7 @@ namespace a8 } } - void TcpClient2::DoDisConnect() + void AsyncTcpClient::DoDisConnect() { connected_ = false; if (on_disconnect) { @@ -144,7 +144,7 @@ namespace a8 } } - void TcpClient2::SetActive(bool active) + void AsyncTcpClient::SetActive(bool active) { if (active) { ActiveStart(); @@ -153,7 +153,7 @@ namespace a8 } } - bool TcpClient2::ActiveStart() + bool AsyncTcpClient::ActiveStart() { socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET == socket_) { @@ -212,7 +212,7 @@ namespace a8 return true; } - void TcpClient2::ActiveStop() + void AsyncTcpClient::ActiveStop() { sending_ = false; connected_ = false; @@ -223,7 +223,7 @@ namespace a8 socket_ = INVALID_SOCKET; } - void TcpClient2::NotifyEpollSend() + void AsyncTcpClient::NotifyEpollSend() { sending_ = true; struct epoll_event ev; @@ -233,7 +233,7 @@ namespace a8 ::epoll_ctl(epoll_fd, EPOLL_CTL_MOD, socket_, &ev); } - void TcpClient2::AsyncSend() + void AsyncTcpClient::AsyncSend() { while (work_node_) { int sentbytes = ::send(socket_, diff --git a/a8/tcpclient2.h b/a8/asynctcpclient.h similarity index 68% rename from a8/tcpclient2.h rename to a8/asynctcpclient.h index 8a61f6e..38661f7 100644 --- a/a8/tcpclient2.h +++ b/a8/asynctcpclient.h @@ -1,20 +1,20 @@ -#ifndef A8_TCPCLIENT2_H -#define A8_TCPCLIENT2_H +#ifndef A8_ASYNC_TCPCLIENT_H +#define A8_ASYNC_TCPCLIENT_H namespace a8 { - class TcpClient2 + class AsyncTcpClient { public: - std::function on_error; - std::function on_connect; - std::function on_disconnect; - std::function on_socketread; + std::function on_error; + std::function on_connect; + std::function on_disconnect; + std::function on_socketread; std::string remote_address; int remote_port = 0; - TcpClient2(); - virtual ~TcpClient2(); + AsyncTcpClient(); + virtual ~AsyncTcpClient(); void Open(); void Close(); diff --git a/a8/ioloop.cc b/a8/ioloop.cc index 7262148..bea4f4a 100644 --- a/a8/ioloop.cc +++ b/a8/ioloop.cc @@ -11,7 +11,7 @@ #include #include -#include +#include namespace a8 { @@ -38,7 +38,7 @@ namespace a8 while (!worker_thread_shutdown_) { int nfds = ::epoll_wait(epoll_fd, events, max_client_num_, 1000 * 10); for (int i = 0; i < nfds; ++i) { - a8::TcpClient2* session = (a8::TcpClient2*)events[i].data.ptr; + a8::AsyncTcpClient* session = (a8::AsyncTcpClient*)events[i].data.ptr; if (events[i].events & EPOLLOUT) { session->DoSend(); } else if (events[i].events & EPOLLIN) {