1
This commit is contained in:
parent
f094eb0376
commit
f9f17a60fb
@ -11,7 +11,7 @@
|
|||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
|
|
||||||
#include <a8/a8.h>
|
#include <a8/a8.h>
|
||||||
#include <a8/tcpclient2.h>
|
#include <a8/asynctcpclient.h>
|
||||||
#include <a8/ioloop.h>
|
#include <a8/ioloop.h>
|
||||||
|
|
||||||
static const int DEFAULT_MAX_PACKET_LEN = 1024 * 10;
|
static const int DEFAULT_MAX_PACKET_LEN = 1024 * 10;
|
||||||
@ -19,44 +19,44 @@ static const int DEFAULT_MAX_RECV_BUFFERSIZE = 1024 * 64;
|
|||||||
|
|
||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
TcpClient2::TcpClient2()
|
AsyncTcpClient::AsyncTcpClient()
|
||||||
{
|
{
|
||||||
send_buffer_mutex_ = new std::mutex();
|
send_buffer_mutex_ = new std::mutex();
|
||||||
epoll_fd = a8::IoLoop::Instance()->epoll_fd;
|
epoll_fd = a8::IoLoop::Instance()->epoll_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpClient2::~TcpClient2()
|
AsyncTcpClient::~AsyncTcpClient()
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
delete send_buffer_mutex_;
|
delete send_buffer_mutex_;
|
||||||
send_buffer_mutex_ = nullptr;
|
send_buffer_mutex_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::Open()
|
void AsyncTcpClient::Open()
|
||||||
{
|
{
|
||||||
if (!IsActive()) {
|
if (!IsActive()) {
|
||||||
SetActive(true);
|
SetActive(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::Close()
|
void AsyncTcpClient::Close()
|
||||||
{
|
{
|
||||||
if (IsActive()) {
|
if (IsActive()) {
|
||||||
SetActive(false);
|
SetActive(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TcpClient2::IsActive()
|
bool AsyncTcpClient::IsActive()
|
||||||
{
|
{
|
||||||
return socket_ != a8::INVALID_SOCKET;
|
return socket_ != a8::INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TcpClient2::Connected()
|
bool AsyncTcpClient::Connected()
|
||||||
{
|
{
|
||||||
return connected_;
|
return connected_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::SendBuff(const char* buff, unsigned int bufflen)
|
void AsyncTcpClient::SendBuff(const char* buff, unsigned int bufflen)
|
||||||
{
|
{
|
||||||
if (bufflen > 0) {
|
if (bufflen > 0) {
|
||||||
a8::SendQueueNode* p = (a8::SendQueueNode*)malloc(sizeof(a8::SendQueueNode));
|
a8::SendQueueNode* p = (a8::SendQueueNode*)malloc(sizeof(a8::SendQueueNode));
|
||||||
@ -79,7 +79,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::DoConnect()
|
void AsyncTcpClient::DoConnect()
|
||||||
{
|
{
|
||||||
connected_ = true;
|
connected_ = true;
|
||||||
if (on_connect) {
|
if (on_connect) {
|
||||||
@ -87,7 +87,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::DoRecv()
|
void AsyncTcpClient::DoRecv()
|
||||||
{
|
{
|
||||||
if (socket_ == -1) {
|
if (socket_ == -1) {
|
||||||
return;
|
return;
|
||||||
@ -116,7 +116,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::DoSend()
|
void AsyncTcpClient::DoSend()
|
||||||
{
|
{
|
||||||
if (!connected_) {
|
if (!connected_) {
|
||||||
DoConnect();
|
DoConnect();
|
||||||
@ -136,7 +136,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::DoDisConnect()
|
void AsyncTcpClient::DoDisConnect()
|
||||||
{
|
{
|
||||||
connected_ = false;
|
connected_ = false;
|
||||||
if (on_disconnect) {
|
if (on_disconnect) {
|
||||||
@ -144,7 +144,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::SetActive(bool active)
|
void AsyncTcpClient::SetActive(bool active)
|
||||||
{
|
{
|
||||||
if (active) {
|
if (active) {
|
||||||
ActiveStart();
|
ActiveStart();
|
||||||
@ -153,7 +153,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TcpClient2::ActiveStart()
|
bool AsyncTcpClient::ActiveStart()
|
||||||
{
|
{
|
||||||
socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
if (INVALID_SOCKET == socket_) {
|
if (INVALID_SOCKET == socket_) {
|
||||||
@ -212,7 +212,7 @@ namespace a8
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::ActiveStop()
|
void AsyncTcpClient::ActiveStop()
|
||||||
{
|
{
|
||||||
sending_ = false;
|
sending_ = false;
|
||||||
connected_ = false;
|
connected_ = false;
|
||||||
@ -223,7 +223,7 @@ namespace a8
|
|||||||
socket_ = INVALID_SOCKET;
|
socket_ = INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::NotifyEpollSend()
|
void AsyncTcpClient::NotifyEpollSend()
|
||||||
{
|
{
|
||||||
sending_ = true;
|
sending_ = true;
|
||||||
struct epoll_event ev;
|
struct epoll_event ev;
|
||||||
@ -233,7 +233,7 @@ namespace a8
|
|||||||
::epoll_ctl(epoll_fd, EPOLL_CTL_MOD, socket_, &ev);
|
::epoll_ctl(epoll_fd, EPOLL_CTL_MOD, socket_, &ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient2::AsyncSend()
|
void AsyncTcpClient::AsyncSend()
|
||||||
{
|
{
|
||||||
while (work_node_) {
|
while (work_node_) {
|
||||||
int sentbytes = ::send(socket_,
|
int sentbytes = ::send(socket_,
|
@ -1,20 +1,20 @@
|
|||||||
#ifndef A8_TCPCLIENT2_H
|
#ifndef A8_ASYNC_TCPCLIENT_H
|
||||||
#define A8_TCPCLIENT2_H
|
#define A8_ASYNC_TCPCLIENT_H
|
||||||
|
|
||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
class TcpClient2
|
class AsyncTcpClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::function<void (a8::TcpClient2*, int)> on_error;
|
std::function<void (a8::AsyncTcpClient*, int)> on_error;
|
||||||
std::function<void (a8::TcpClient2*)> on_connect;
|
std::function<void (a8::AsyncTcpClient*)> on_connect;
|
||||||
std::function<void (a8::TcpClient2*)> on_disconnect;
|
std::function<void (a8::AsyncTcpClient*)> on_disconnect;
|
||||||
std::function<void (a8::TcpClient2*, char*, unsigned int)> on_socketread;
|
std::function<void (a8::AsyncTcpClient*, char*, unsigned int)> on_socketread;
|
||||||
std::string remote_address;
|
std::string remote_address;
|
||||||
int remote_port = 0;
|
int remote_port = 0;
|
||||||
|
|
||||||
TcpClient2();
|
AsyncTcpClient();
|
||||||
virtual ~TcpClient2();
|
virtual ~AsyncTcpClient();
|
||||||
|
|
||||||
void Open();
|
void Open();
|
||||||
void Close();
|
void Close();
|
@ -11,7 +11,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include <a8/ioloop.h>
|
#include <a8/ioloop.h>
|
||||||
#include <a8/tcpclient2.h>
|
#include <a8/asynctcpclient.h>
|
||||||
|
|
||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
@ -38,7 +38,7 @@ namespace a8
|
|||||||
while (!worker_thread_shutdown_) {
|
while (!worker_thread_shutdown_) {
|
||||||
int nfds = ::epoll_wait(epoll_fd, events, max_client_num_, 1000 * 10);
|
int nfds = ::epoll_wait(epoll_fd, events, max_client_num_, 1000 * 10);
|
||||||
for (int i = 0; i < nfds; ++i) {
|
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) {
|
if (events[i].events & EPOLLOUT) {
|
||||||
session->DoSend();
|
session->DoSend();
|
||||||
} else if (events[i].events & EPOLLIN) {
|
} else if (events[i].events & EPOLLIN) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user