From 9c707cd6bde34d1cf50977c98ee9ccb9bff227e5 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Mon, 6 Dec 2021 16:33:51 +0800 Subject: [PATCH] no message --- general/CMakeLists.txt | 5 +- general/src/function/Timer.cpp | 11 -- general/src/function/daemon.h | 11 +- general/src/net/package_receiver.cpp | 104 ----------- general/src/net/package_receiver.h | 37 ---- general/src/net/tcp_client.cpp | 255 --------------------------- general/src/net/tcp_client.h | 101 ----------- general/src/pattern/ringbuffer.hpp | 1 - test/src/cpp11/conanfile.txt | 4 - test/src/cpp11/tcp_bench.cpp | 0 10 files changed, 4 insertions(+), 525 deletions(-) delete mode 100644 general/src/net/package_receiver.cpp delete mode 100644 general/src/net/package_receiver.h delete mode 100644 general/src/net/tcp_client.cpp delete mode 100644 general/src/net/tcp_client.h delete mode 100644 test/src/cpp11/conanfile.txt create mode 100644 test/src/cpp11/tcp_bench.cpp diff --git a/general/CMakeLists.txt b/general/CMakeLists.txt index ec16a37..ae5f886 100644 --- a/general/CMakeLists.txt +++ b/general/CMakeLists.txt @@ -32,9 +32,8 @@ aux_source_directory(src DIRSRCS) aux_source_directory(src/pattern PaternSrc) aux_source_directory(src/function FunctionSrc) aux_source_directory(src/algorithm AlgorithmSrc) -aux_source_directory(src/net NetSrc) aux_source_directory(src/encrypt EncryptSrc) -message("source file is " ${DIRSRCS} ${PaternSrc} ${EncryptSrc} ${NetSrc} ${FunctionSrc} ${AlgorithmSrc}) +message("source file is " ${DIRSRCS} ${PaternSrc} ${EncryptSrc} ${FunctionSrc} ${AlgorithmSrc}) -add_library(General OBJECT ${DIRSRCS} ${PaternSrc} ${EncryptSrc} ${NetSrc} ${FunctionSrc} ${AlgorithmSrc} ) +add_library(General OBJECT ${DIRSRCS} ${PaternSrc} ${EncryptSrc} ${FunctionSrc} ${AlgorithmSrc} ) diff --git a/general/src/function/Timer.cpp b/general/src/function/Timer.cpp index 764ce44..39af4fb 100644 --- a/general/src/function/Timer.cpp +++ b/general/src/function/Timer.cpp @@ -33,18 +33,12 @@ Timer::Timer() startTimeInMicroSec = 0; endTimeInMicroSec = 0; } - - - /////////////////////////////////////////////////////////////////////////////// // distructor /////////////////////////////////////////////////////////////////////////////// Timer::~Timer() { } - - - /////////////////////////////////////////////////////////////////////////////// // start timer. // startCount will be set at this point. @@ -58,9 +52,6 @@ void Timer::start() gettimeofday(&startCount, NULL); #endif } - - - /////////////////////////////////////////////////////////////////////////////// // stop the timer. // endCount will be set at this point. @@ -105,8 +96,6 @@ double Timer::getElapsedTimeInMilliSec() return this->getElapsedTimeInMicroSec() * 0.001; } - - /////////////////////////////////////////////////////////////////////////////// // divide elapsedTimeInMicroSec by 1000000 /////////////////////////////////////////////////////////////////////////////// diff --git a/general/src/function/daemon.h b/general/src/function/daemon.h index 159cbb8..91f4864 100644 --- a/general/src/function/daemon.h +++ b/general/src/function/daemon.h @@ -1,18 +1,12 @@ /* - * @Author: your name - * @Date: 2021-07-23 23:40:36 - - * @LastEditTime: 2021-10-06 23:48:41 - + * @LastEditTime: 2021-12-06 10:53:37 * @LastEditors: Please set LastEditors - * @Description: In User Settings Edi - * @FilePath: \generallib\general\src\function\daemon.h - */ + #if __cplusplus >= 201103L #pragma message("编译器支持c++11") #endif @@ -40,7 +34,6 @@ using namespace std; #include #include - class DaemonizeMonitor{ public: DaemonizeMonitor(string path); diff --git a/general/src/net/package_receiver.cpp b/general/src/net/package_receiver.cpp deleted file mode 100644 index d837672..0000000 --- a/general/src/net/package_receiver.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "package_receiver.h" - -PackageReceiver::PackageReceiver() -{ - mUnsortLen = 0; -} -/* -0 1 2 3 -0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 -+ -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| 0x40 | 0x41 | length[3] | length[2] | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| length[1] | length[0] | verify | funcode | -包头长度16个字节 -长度包含了包头 8 字节 -*/ -int PackageReceiver::SortPack(uint8_t * inData, uint32_t size) -{ - if (nullptr == inData) - return -1; - if (mUnsortLen == 0) { - if (size < 8) { //几乎不可能出现这种情况,除非有人捣乱 - memcpy(mUnsortData, inData, size); - mUnsortLen = size; - return 0; - } - if ((inData[0] != 0x40 || inData[1] != 0x41)) { - return -1; - } - uint32_t len = (inData[2] << 24 )+ (inData[3] << 16 ) + - (inData[4] << 8) + inData[5]; // 长度 - if (len > size) { // 大包还需要重组 - memcpy(mUnsortData, inData, size); - mUnsortLen = size; - return 0; - } - if (len < size) { //沾包 - if (mObserver != nullptr) { - this->mObserver->OnTCPPackage(inData , len ); - } - this->SortPack(inData + len, size - len); //下个包进去重组 - } - if (len == size) { //刚刚好一包 - if (mObserver != nullptr) { - this->mObserver->OnTCPPackage(inData, len ); - } - } - } - // 大包情况,还有未重组的包 - else { - if (mUnsortLen + size < 8) { - memcpy(mUnsortData + mUnsortLen, inData, size); - mUnsortLen = +size; - return 0; - } - if (mUnsortLen < 8) { - // 补到8为止 - memcpy(mUnsortData + mUnsortLen, inData, 8 - mUnsortLen); - mUnsortLen = 8; - inData += 8 - mUnsortLen; - size -= 8 - mUnsortLen; - } - - // 包头格式错误 - if ((this->mUnsortData[0] != 0x40 || this->mUnsortData[1] != 0x41)) { - return -1; - } - uint32_t len = mUnsortData[2] << (24 + mUnsortData[3]) << (16 + mUnsortData[4]) - << (8 + mUnsortData[0]); // 长度 - if (len > size + mUnsortLen) { //大包还需要重组 - memcpy(mUnsortData + mUnsortLen, inData, size); - mUnsortLen += size; - return 0; - } - if (len < size + mUnsortLen) { // 沾包的情况,说明还有其他的包 - uint8_t *buf = new uint8_t[len]; - memcpy(buf, mUnsortData, mUnsortLen); - memcpy(buf + mUnsortLen, inData, len - mUnsortLen); - if (nullptr != mObserver) { - this->mObserver->OnTCPPackage(buf, len); - } - free(buf); - this->SortPack(inData, size - len - mUnsortLen); - } - if (len == size + mUnsortLen) { //刚好一整包 - - } - } - return 0; -} - -int PackageReceiver::SetObserver(PackageReceiverObserver *p) -{ - if (nullptr != p) { - this->mObserver = p; - } - return 0; -} - -int PackageReceiver::Clear() -{ - this->mUnsortLen = 0; - return 0; -} diff --git a/general/src/net/package_receiver.h b/general/src/net/package_receiver.h deleted file mode 100644 index fb73f69..0000000 --- a/general/src/net/package_receiver.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include - -#define BUFFER_MAX 1024//每个包的最大包头 - -typedef enum { - FUNC_C2S = 1, //客户端请求 - FUNC_S2C // 服务端请求 -}FUNCODE; - -/* -0 1 2 3 -0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| 0x40 | 0x41 | length[3] | length[2] | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| length[1] | length[0] | verify | funcode | - -*/ -class PackageReceiver { -public: - class PackageReceiverObserver { - public: - virtual int OnTCPPackage(uint8_t *, uint16_t) { return 0; }; - }; - PackageReceiver(); - int SortPack(uint8_t *inData,uint32_t size); - int SetObserver(PackageReceiverObserver *); - int Clear(); -private: - uint8_t mUnsortData[BUFFER_MAX]; - uint32_t mUnsortLen = 0; - PackageReceiverObserver *mObserver; -}; \ No newline at end of file diff --git a/general/src/net/tcp_client.cpp b/general/src/net/tcp_client.cpp deleted file mode 100644 index a4f585d..0000000 --- a/general/src/net/tcp_client.cpp +++ /dev/null @@ -1,255 +0,0 @@ - -// Created by 29019 on 2020/4/18. -// -#define _WSPIAPI_H_ -#define _WINSOCKAPI_ -#include "tcp_client.h" -#include -#include -#include -#include - -using namespace std::chrono; - -static void conn_writecb(struct bufferevent *, void *); -static void conn_readcb(struct bufferevent *, void *); -static void conn_eventcb(struct bufferevent *, short, void *); - -void delay(int ms); -int ThreadRun(TcpClientLibevent *p); - -void conn_writecb(struct bufferevent *bev, void *user_data) -{ - TcpClientLibevent *client = (TcpClientLibevent*)user_data; - struct evbuffer *output = bufferevent_get_output(bev); - size_t sz = evbuffer_get_length(output); - std::cout<<"write data length: "<mStatus == TcpClientLibevent::STOP)){ - Sleep(100); - continue; - } - if ((p->mStatus == TcpClientLibevent::FAIL) || - (p->mStatus == TcpClientLibevent::UNCONNECTED)){ //连接失败,如果有设置自动重连就一直重连 - p->ConnectServer(); -#ifdef _WIN32 - Sleep(100); -#else - //todo linux版本sleep -#endif - } - int ret = p->Dispatch(); - if(ret < 0){ - break; - } - } - } - return 0; -} - -void conn_readcb(struct bufferevent *bev, void *user_data) -{ - TcpClientLibevent *client = (TcpClientLibevent*)user_data; - struct evbuffer *input = bufferevent_get_input(bev); - size_t sz = evbuffer_get_length(input); - if (sz > 0) - { - uint8_t *msg = new uint8_t[sz]; - int ret = bufferevent_read(bev, msg, sz); - printf("%s\n", msg); - if(client->mObserver != nullptr){ - client->mObserver->OnData(msg, ret); - } - delete[] msg; - } -} - -void conn_eventcb(struct bufferevent *bev, short events, void *user_data) -{ - TcpClientLibevent *p; - p = (TcpClientLibevent *)user_data; - if (p == nullptr) { - return; - } - if (events & BEV_EVENT_EOF) { - if (nullptr != p->mObserver) - p->mObserver->OnDisConnected("服务器主动断开连接"); - if (p != nullptr) - p->mStatus = TcpClientLibevent::UNCONNECTED; - printf("Connection closed\n"); - } - else if (events & BEV_EVENT_ERROR) { - printf("Got an error on the connection: %s\n", strerror(errno)); - if (nullptr != p->mObserver) - p->mObserver->OnDisConnected("连接失败"); - p->mStatus = TcpClientLibevent::FAIL; - } - else if (events & BEV_EVENT_CONNECTED) { - - p->mSocketFD = (uint64_t)event_get_fd(&(bev->ev_read)); - //客户端链接成功后,给服务器发送第一条消息 - std::cout << "连接成功 socket fd" << p->mSocketFD << std::endl; - if (nullptr != p->mObserver) - p->mObserver->OnConnected(); - p->mStatus = TcpClientLibevent::CONNECTED; - return; - } - bufferevent_free(bev); -} - -void delay(int ms) -{ - clock_t start = clock(); - while (clock() - start < ms); -} - -bool TcpClientLibevent::Connected() { - return ((mStatus == CONNECTED)?true:false); -} - -TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLibevent::TcpClientObserver *p) : - mStatus(UNCONNECTED), - mObserver(nullptr), - mBev(nullptr) -{ - memset(&mSrv, 0, sizeof(mSrv)); -#ifdef linux - mSrv.sin_addr.s_addr = inet_addr(addrinfo.c_str()); - mSrv.sin_family = AF_INET; -#endif -#ifdef _WIN32 - mSrv.sin_addr.S_un.S_addr = inet_addr(addrinfo.c_str()); - mSrv.sin_family = AF_INET; -#endif - mSrv.sin_port = htons(port); - mBase = event_base_new(); - if (!mBase) - { - printf("Could not initialize libevent\n"); - } - -#ifdef WIN32 - evthread_use_windows_threads(); -#else - evthread_use_pthreads(); -#endif - this->mThread = new thread(ThreadRun,this); - this->mObserver = p; - mByteRecv = 0; - mByteSend = 0; - this->mStatus = TcpClientLibevent::Status::STOP; -} - -int TcpClientLibevent::ConnectServer() { - printf("server conecting...\r\n"); - if(this->mStatus == TcpClientLibevent::CONNECTED) { // 已经连接 - return 0; - } - if(this->mStatus == TcpClientLibevent::CONNECTING) { // 正在连接等待连接成功 - return -1; - } - - evthread_make_base_notifiable(mBase); - mBev = bufferevent_socket_new(mBase, -1, - BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE); - if (nullptr == mBev) { - this->mStatus = TcpClientLibevent::FAIL; - return - 1; - } - bufferevent_setcb(mBev, conn_readcb, conn_writecb, conn_eventcb, this); - int flag = bufferevent_socket_connect(mBev, (struct sockaddr *)&mSrv, sizeof(mSrv)); - bufferevent_enable(mBev, EV_READ | EV_WRITE); - if (-1 == flag) { - this->mStatus = TcpClientLibevent::FAIL; - bufferevent_free(mBev); - mBev = nullptr; - printf("Connect failed\n"); - return -1; - } - this->mStatus = TcpClientLibevent::CONNECTING; - return 0; -} - -int TcpClientLibevent::ConnectServerSync() -{ - evthread_make_base_notifiable(mBase); - if (nullptr != mBev) { - delete mBev; - } - mBev = bufferevent_socket_new(mBase, -1, - BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE); - if (nullptr == mBev) { - this->mStatus = TcpClientLibevent::FAIL; - return -1; - } - bufferevent_setcb(mBev, conn_readcb, conn_writecb, conn_eventcb, this); - int flag = bufferevent_socket_connect(mBev, (struct sockaddr*)&mSrv, sizeof(mSrv)); - bufferevent_enable(mBev, EV_READ | EV_WRITE); - if (-1 == flag) { - this->mStatus = TcpClientLibevent::FAIL; - bufferevent_free(mBev); - mBev = nullptr; - printf("Connect failed\n"); - return -1; - } - - this->mStatus = TcpClientLibevent::CONNECTING; - auto start = system_clock::to_time_t(system_clock::now()); - while (this->mStatus != TcpClientLibevent::CONNECTED) { - auto end = system_clock::to_time_t(system_clock::now()); - if ((end - start) > 5) { - this->mStatus = TcpClientLibevent::FAIL; - break; - } - } - return 0; -} - -int TcpClientLibevent::SetReconnect(bool reconn) { - this->mReConnect = reconn; - return 0; -} - -int TcpClientLibevent::SetObserver(TcpClientLibevent::TcpClientObserver *ob) { - this->mObserver = ob; - return 0; -} - -int TcpClientLibevent::Dispatch() { - return event_base_dispatch(mBase);; -} - -int TcpClientLibevent::Close() { - event_base_free(mBase); - return 0; -} - -int TcpClientLibevent::SendDataAsync(const char* data, int len) -{ - if(data == nullptr){ - return -1; - } - int res; - //将data开始size大小的字节接到输出缓冲区的尾部 - res = evbuffer_add(bufferevent_get_output(mBev), data, len); - //调用失败 - if (res == -1) - return (res); - /* If everything is okay, we need to schedule a write */ - //注册写事件 - if (len > 0 && (mBev->enabled & EV_WRITE)) { - // event_active(&mBev->ev_write,EV_WRITE,1); - } - return (res); -} - -uint64_t TcpClientLibevent::SocketFd() -{ - return mSocketFD; -} diff --git a/general/src/net/tcp_client.h b/general/src/net/tcp_client.h deleted file mode 100644 index 409a916..0000000 --- a/general/src/net/tcp_client.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * @Author: your name - * @Date: 2021-06-30 10:02:08 - * @LastEditTime: 2021-12-06 10:00:18 - * @LastEditors: Please set LastEditors - * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE - * @FilePath: \server\tcp_client.h - */ -// -// Created by 29019 on 2020/4/18. -// -#pragma once - -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x0500 -#endif - -#ifdef linux -#include -#include -#include -#define EVENT__HAVE_PTHREADS -#endif - -extern "C"{ - #include "event2/bufferevent.h" - #include "event2/bufferevent_struct.h" - #include "event2/buffer.h" - #include "event2/listener.h" - #include "event2/util.h" - #include "event2/event.h" - #include "event2/thread.h" - /* For int types. */ - #include - /* For struct event */ - #include - -}; - -#include -#include -#include -#include -#include - -using namespace std; - -class TcpClientLibevent { -public: - typedef enum { - UNCONNECTED, // 未连接 - CONNECTING, //已经连接 - CONNECTED, //已经连接 - FAIL, // 连接失败 - STOP, // 初始状态 - }Status; - - class TcpClientObserver { - public: - virtual ~TcpClientObserver() { return; } - mutex mMux; - virtual void OnConnected() { return; }; - virtual void OnDisConnected(std::string) { return; }; - virtual void OnData(uint8_t* dat, uint64_t len) { return; }; - virtual void OnClose() { return; }; - }; - TcpClientLibevent(std::string addrinfo, int port, TcpClientObserver* p); - ~TcpClientLibevent() { - event_base_free(mBase); - }; - - friend void conn_eventcb(struct bufferevent*, short, void*); - - int ConnectServer(); - int ConnectServerSync(); - - bool Connected(); - int Dispatch(); - int OnTCPPackage(uint8_t*, uint16_t); - int SetReconnect(bool); - int SetObserver(TcpClientObserver*); - int Close(); - int SendDataAsync(const char*, int len); - uint64_t SocketFd(); - - Status mStatus; - TcpClientObserver* mObserver; -private: - bool mReConnect = false; - int sendData(void*, size_t); - struct event_base* mBase; - struct bufferevent* mBev; - struct sockaddr_in mSrv; - std::thread* mThread; - mutex mLock; // 互斥锁 - uint64_t mByteSend; // 发送字节数 - uint64_t mByteRecv; // 接收字节数 - evutil_socket_t mSocketFD; // 操作系统原生socket -}; - - diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index 2f80440..446f754 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -88,7 +88,6 @@ int RingBuffer::Copy(T *data,uint64_t len){ return bytes_read; } } - } template diff --git a/test/src/cpp11/conanfile.txt b/test/src/cpp11/conanfile.txt deleted file mode 100644 index 6093c3c..0000000 --- a/test/src/cpp11/conanfile.txt +++ /dev/null @@ -1,4 +0,0 @@ -[requires] -lua/1.1.1 -[imports] -.,* -> ./third @ folder=True, ignore_case=True, excludes=*.html *.jpeg \ No newline at end of file diff --git a/test/src/cpp11/tcp_bench.cpp b/test/src/cpp11/tcp_bench.cpp new file mode 100644 index 0000000..e69de29