2021-10-09 00:14:49 +08:00
|
|
|
//
|
|
|
|
// Created by 29019 on 2020/4/18.
|
|
|
|
//
|
2021-10-11 00:00:10 +08:00
|
|
|
#pragma once
|
2021-10-09 00:14:49 +08:00
|
|
|
|
|
|
|
#ifndef _WIN32_WINNT
|
|
|
|
#define _WIN32_WINNT 0x0500
|
|
|
|
#endif
|
2021-10-11 00:00:10 +08:00
|
|
|
|
|
|
|
|
2021-10-09 00:14:49 +08:00
|
|
|
#ifdef linux
|
|
|
|
#include<sys/types.h>
|
|
|
|
#include<sys/socket.h>
|
|
|
|
#include<arpa/inet.h>
|
|
|
|
#define EVENT__HAVE_PTHREADS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C"{
|
|
|
|
#include "event2/bufferevent.h"
|
2021-10-11 00:00:10 +08:00
|
|
|
#include "event2/bufferevent_struct.h"
|
2021-10-09 00:14:49 +08:00
|
|
|
#include "event2/buffer.h"
|
|
|
|
#include "event2/listener.h"
|
|
|
|
#include "event2/util.h"
|
|
|
|
#include "event2/event.h"
|
|
|
|
#include "event2/thread.h"
|
2021-10-11 00:00:10 +08:00
|
|
|
/* For int types. */
|
|
|
|
#include <event2/util.h>
|
|
|
|
/* For struct event */
|
|
|
|
#include <event2/event_struct.h>
|
|
|
|
|
2021-10-09 00:14:49 +08:00
|
|
|
};
|
2021-10-11 00:00:10 +08:00
|
|
|
|
2021-10-09 00:14:49 +08:00
|
|
|
#include<string.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2021-11-22 23:53:21 +08:00
|
|
|
#include <functional>
|
|
|
|
|
2021-10-09 00:14:49 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class TcpClientLibevent {
|
|
|
|
public:
|
|
|
|
typedef enum {
|
2021-10-11 00:00:10 +08:00
|
|
|
UNCONNECTED, // 未连接
|
2021-10-12 00:51:48 +08:00
|
|
|
CONNECTING, //已经连接
|
2021-10-11 00:00:10 +08:00
|
|
|
CONNECTED, //已经连接
|
|
|
|
FAIL, // 连接失败
|
2021-10-12 00:51:48 +08:00
|
|
|
STOP, // 初始状态
|
2021-10-09 00:14:49 +08:00
|
|
|
}Status;
|
2021-10-11 00:00:10 +08:00
|
|
|
|
|
|
|
class TcpClientObserver {
|
2021-10-09 00:14:49 +08:00
|
|
|
public:
|
2021-10-11 00:00:10 +08:00
|
|
|
virtual ~TcpClientObserver() { return; }
|
2021-10-09 00:14:49 +08:00
|
|
|
mutex mMux;
|
|
|
|
virtual void OnConnected() { return; };
|
2021-11-22 23:53:21 +08:00
|
|
|
virtual void OnDisConnected(std::string) { return; };
|
2021-10-11 00:00:10 +08:00
|
|
|
virtual void OnData(uint8_t* dat, uint64_t len) { return; };
|
|
|
|
virtual void OnClose() { return; };
|
2021-10-09 00:14:49 +08:00
|
|
|
};
|
2021-10-11 00:00:10 +08:00
|
|
|
TcpClientLibevent(std::string addrinfo, int port, TcpClientObserver* p);
|
|
|
|
~TcpClientLibevent() {
|
2022-02-18 00:18:28 +08:00
|
|
|
mMux.lock();
|
2022-02-20 11:55:16 +08:00
|
|
|
mStatus = TcpClientLibevent::STOP;
|
2022-02-18 00:18:28 +08:00
|
|
|
mMux.unlock();
|
|
|
|
|
2022-02-20 11:55:16 +08:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 10000;
|
|
|
|
event_base_loopexit(mBase,&tv);
|
2022-02-18 00:18:28 +08:00
|
|
|
mThread->join();
|
2022-02-20 11:55:16 +08:00
|
|
|
std::cout << "12345\r\n";
|
2022-02-18 00:18:28 +08:00
|
|
|
};
|
2021-10-11 00:00:10 +08:00
|
|
|
friend void conn_eventcb(struct bufferevent*, short, void*);
|
2021-10-09 00:14:49 +08:00
|
|
|
int ConnectServer();
|
2021-10-12 00:51:48 +08:00
|
|
|
int ConnectServerSync();
|
|
|
|
|
2021-10-09 00:14:49 +08:00
|
|
|
bool Connected();
|
|
|
|
int Dispatch();
|
2021-10-11 00:00:10 +08:00
|
|
|
int OnTCPPackage(uint8_t*, uint16_t);
|
2021-10-09 00:14:49 +08:00
|
|
|
int SetReconnect(bool);
|
|
|
|
int SetObserver(TcpClientObserver*);
|
|
|
|
int Close();
|
2021-11-22 23:53:21 +08:00
|
|
|
int SendDataAsync(const char*, int len);
|
2021-10-11 00:00:10 +08:00
|
|
|
uint64_t SocketFd();
|
|
|
|
|
2021-10-09 00:14:49 +08:00
|
|
|
Status mStatus;
|
2021-10-11 00:00:10 +08:00
|
|
|
TcpClientObserver* mObserver;
|
2021-10-09 00:14:49 +08:00
|
|
|
private:
|
|
|
|
bool mReConnect = false;
|
2021-10-11 00:00:10 +08:00
|
|
|
int sendData(void*, size_t);
|
|
|
|
struct event_base* mBase;
|
2021-10-12 00:51:48 +08:00
|
|
|
struct bufferevent* mBev;
|
2021-10-09 00:14:49 +08:00
|
|
|
struct sockaddr_in mSrv;
|
2021-10-11 00:00:10 +08:00
|
|
|
std::thread* mThread;
|
|
|
|
mutex mLock; // 互斥锁
|
|
|
|
uint64_t mByteSend; // 发送字节数
|
|
|
|
uint64_t mByteRecv; // 接收字节数
|
|
|
|
evutil_socket_t mSocketFD; // 操作系统原生socket
|
2022-02-18 00:18:28 +08:00
|
|
|
mutex mMux;
|
2021-10-11 00:00:10 +08:00
|
|
|
|
2021-10-09 00:14:49 +08:00
|
|
|
};
|
|
|
|
|
2021-10-11 00:00:10 +08:00
|
|
|
|