This commit is contained in:
aozhiwei 2020-01-08 14:38:18 +08:00
parent f094eb0376
commit f9f17a60fb
3 changed files with 28 additions and 28 deletions

View File

@ -11,7 +11,7 @@
#include <netinet/tcp.h>
#include <a8/a8.h>
#include <a8/tcpclient2.h>
#include <a8/asynctcpclient.h>
#include <a8/ioloop.h>
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_,

View File

@ -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<void (a8::TcpClient2*, int)> on_error;
std::function<void (a8::TcpClient2*)> on_connect;
std::function<void (a8::TcpClient2*)> on_disconnect;
std::function<void (a8::TcpClient2*, char*, unsigned int)> on_socketread;
std::function<void (a8::AsyncTcpClient*, int)> on_error;
std::function<void (a8::AsyncTcpClient*)> on_connect;
std::function<void (a8::AsyncTcpClient*)> on_disconnect;
std::function<void (a8::AsyncTcpClient*, char*, unsigned int)> on_socketread;
std::string remote_address;
int remote_port = 0;
TcpClient2();
virtual ~TcpClient2();
AsyncTcpClient();
virtual ~AsyncTcpClient();
void Open();
void Close();

View File

@ -11,7 +11,7 @@
#include <thread>
#include <a8/ioloop.h>
#include <a8/tcpclient2.h>
#include <a8/asynctcpclient.h>
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) {