From 65771d0be6878a3c2f02fa2d04157fa32255270a Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Mon, 22 Nov 2021 23:53:21 +0800 Subject: [PATCH] no message --- examples/proto_debuger/base_form.cpp | 2 +- examples/proto_debuger/loger.cpp | 108 ++++++++++++++++++ examples/proto_debuger/loger.h | 44 +++++++ examples/proto_debuger/main.cpp | 2 +- examples/proto_debuger/new_monitor_form.cpp | 24 ++-- examples/proto_debuger/proto_debuger.vcxproj | 2 + .../proto_debuger.vcxproj.filters | 6 + examples/proto_debuger/tcp_client.cpp | 14 ++- examples/proto_debuger/tcp_client.h | 6 +- examples/proto_debuger/tcp_client_form.cpp | 49 +++++++- examples/proto_debuger/tcp_client_form.h | 21 +++- examples/proto_debuger/utils.cpp | 20 +++- examples/proto_debuger/utils.h | 1 + 13 files changed, 267 insertions(+), 32 deletions(-) create mode 100644 examples/proto_debuger/loger.cpp create mode 100644 examples/proto_debuger/loger.h diff --git a/examples/proto_debuger/base_form.cpp b/examples/proto_debuger/base_form.cpp index d1809ffd..60e70047 100644 --- a/examples/proto_debuger/base_form.cpp +++ b/examples/proto_debuger/base_form.cpp @@ -95,7 +95,7 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) wprintf(L"%s\r\n", dynamic_cast (ev->pSender)->GetText().c_str()); printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); TcpClientForm* p = mTcpClientForm[dynamic_cast (ev->pSender)->GetText()]; - + if (p != nullptr) { printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); diff --git a/examples/proto_debuger/loger.cpp b/examples/proto_debuger/loger.cpp new file mode 100644 index 00000000..520bc61c --- /dev/null +++ b/examples/proto_debuger/loger.cpp @@ -0,0 +1,108 @@ +// +// Created by bt110 on 2019/8/19. +// +#include "loger.h" +using namespace Loger; +string getTimeDate() +{ + time_t timep; + time (&timep); + char tmp[64]; + strftime(tmp, sizeof(tmp), "%Y-%m-%d",localtime(&timep) ); + return string(tmp); +} + +string getTime() +{ + time_t timep; + time (&timep); + char tmp[64]; + strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) ); + return string(tmp); +} + +int _C_Loger::Debug(string dat,string function,int line){ + // 还没有过天 + if(getTimeDate() == this->mCurrentDate){ + string tmp = getTime(); + tmp += ": "; + tmp += dat; + tmp += "at [" + function + " line " + std::to_string(line) + "]"; + tmp += "\n"; + int ret =fwrite(tmp.c_str(),tmp.size(),1,this->mFile); + fflush(this->mFile); + return ret; + }else{ // 已经过天了 + this->mCurrentDate = getTimeDate(); + string path = getTimeDate() + ".log"; + this->mFile = fopen(path.c_str(),"w+"); + if (this->mFile == nullptr){ + this->error = true; + } + string tmp = getTime(); + tmp += ": "; + tmp += dat; + tmp += "at [" + function + " line " + std::to_string(line) + "]"; + tmp += "\n"; + int ret = fwrite(tmp.c_str(),tmp.size(),1,this->mFile); + fflush(this->mFile); + return ret; + } + return 0; +} + +void _C_Loger::operator<<(const string& wb){ + // 还没有过天 + if(getTimeDate() == this->mCurrentDate){ + string tmp = getTime(); + tmp += ":"; + tmp += wb; + tmp += " \n"; + fwrite(tmp.c_str(),tmp.size(),1,this->mFile); + fflush(this->mFile); + }else{ // 已经过天了 + this->mCurrentDate = getTimeDate(); + string path = getTimeDate() + ".log"; + this->mFile = fopen(path.c_str(),"w+"); + if (this->mFile == nullptr){ + this->error = true; + } + + string tmp = getTime(); + tmp += ":"; + tmp += wb; + tmp += " \n"; + fwrite(tmp.c_str(),tmp.size(),1,this->mFile); + fflush(this->mFile); + } +} + +bool file_existed(string path) { + fstream _file; + _file.open(path.c_str(),ios::in); + if(!_file) + { + return false; + } + else + { + _file.close(); + return true; + } +} +_C_Loger::_C_Loger(FILE *p){ + this->mFile = p; + this->mCurrentDate = getTime(); +} + +_C_Loger::_C_Loger(string path) { + this->mCurrentDate = getTimeDate(); + this->mCurrentPath = path + this->mCurrentDate; + this->mFile = fopen(this->mCurrentPath.c_str(),"a+"); + if(! this->mFile){ + fprintf(stderr,"error open log files %s code %d,please check file path",this->mCurrentPath.c_str(),errno); + exit(0); + }else{ + + } +} \ No newline at end of file diff --git a/examples/proto_debuger/loger.h b/examples/proto_debuger/loger.h new file mode 100644 index 00000000..0915ca37 --- /dev/null +++ b/examples/proto_debuger/loger.h @@ -0,0 +1,44 @@ +// +// Created by bt110 on 2019/8/19. +// + +#ifndef CPP11FEATURETEST_LOGER_H +#define CPP11FEATURETEST_LOGER_H + +#include +#include +#include +#include +#include + +using namespace std; + +namespace Loger{ + + typedef enum Mode{ + Mode_Daily, // 每天保存一次日志 + MODE_Monthly, // 每个月保存一次日志 + MODE_Weekly // 每周保存一次日志 + }ESaveMode; + typedef class _C_Loger { + private: + string mCurrentPath; + FILE *mFile; // 日志文件 + ESaveMode mMode; // 工作模式 + string mCurrentDate; // 当天 + int error; + + public: + _C_Loger(FILE *p); + _C_Loger(string path); + #define DEBUG_FILE_POSITION __FILE__,__LINE__ + int Debug(string,string,int); + int Log(); + int LogFile(); + void operator+(const string&); + void operator<<(const string&); + }Loger; +} + + +#endif //CPP11FEATURETEST_LOGER_H diff --git a/examples/proto_debuger/main.cpp b/examples/proto_debuger/main.cpp index 34d370a7..8f426e7c 100644 --- a/examples/proto_debuger/main.cpp +++ b/examples/proto_debuger/main.cpp @@ -6,7 +6,7 @@ #include"resource1.h" #include #include "lua_wraper.h" - +#include "loger.h" enum ThreadId { diff --git a/examples/proto_debuger/new_monitor_form.cpp b/examples/proto_debuger/new_monitor_form.cpp index 2b2e9cb5..623eef4e 100644 --- a/examples/proto_debuger/new_monitor_form.cpp +++ b/examples/proto_debuger/new_monitor_form.cpp @@ -132,23 +132,21 @@ void NewMonitorForm::InitWindow() } } if (m_combo_type->GetText() == L"tcp client") { - wprintf(L"%s\r\n",m_ip_select->GetText().c_str()); + wprintf(L"%s\r\n", m_ip_select->GetText().c_str()); wprintf(L"%s\r\n", m_port_select->GetText().c_str()); int port = atoi(wstring2string(m_port_select->GetText()).c_str()); - m_tcp_client = new TcpClientLibevent(wstring2string(m_ip_select->GetText()), port,nullptr); - if (m_tcp_client->Connected()) { - TcpClientInfo* p = new TcpClientInfo; - p->ip = m_port_select->GetText(); - p->port = port; - p->socket_fd = m_tcp_client->SocketFd(); + m_tcp_client = new TcpClientLibevent(wstring2string(m_ip_select->GetText()), port, nullptr); + TcpClientInfo* p = new TcpClientInfo; + p->ip = m_ip_select->GetText(); + p->port = port; + p->socket_fd = m_tcp_client->SocketFd(); - printf("ӳɹ %d \r\n", p->socket_fd); + printf("ӳɹ %d \r\n", p->socket_fd); - auto succ = ::PostMessage(m_parent->GetHWND(), - WM_ADD_TCPCLIENT_MONITOR, (WPARAM)p, (LPARAM)m_tcp_client); - if (!succ) { - printf("postmessage error :%d\r\n", GetLastError()); - } + auto succ = ::PostMessage(m_parent->GetHWND(), + WM_ADD_TCPCLIENT_MONITOR, (WPARAM)p, (LPARAM)m_tcp_client); + if (!succ) { + printf("postmessage error :%d\r\n", GetLastError()); } } diff --git a/examples/proto_debuger/proto_debuger.vcxproj b/examples/proto_debuger/proto_debuger.vcxproj index ffdb4663..7d24abb9 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj +++ b/examples/proto_debuger/proto_debuger.vcxproj @@ -163,6 +163,7 @@ + @@ -175,6 +176,7 @@ + diff --git a/examples/proto_debuger/proto_debuger.vcxproj.filters b/examples/proto_debuger/proto_debuger.vcxproj.filters index 3a0379ef..66933793 100644 --- a/examples/proto_debuger/proto_debuger.vcxproj.filters +++ b/examples/proto_debuger/proto_debuger.vcxproj.filters @@ -45,6 +45,9 @@ 源文件 + + 源文件 + @@ -74,6 +77,9 @@ 头文件 + + 头文件 + diff --git a/examples/proto_debuger/tcp_client.cpp b/examples/proto_debuger/tcp_client.cpp index c76e9841..12d2b729 100644 --- a/examples/proto_debuger/tcp_client.cpp +++ b/examples/proto_debuger/tcp_client.cpp @@ -55,6 +55,7 @@ void conn_readcb(struct bufferevent *bev, void *user_data) { uint8_t *msg = new uint8_t[sz]; int ret = bufferevent_read(bev, msg, sz); + msg[ret - 1] = '\0'; printf("%s\n", msg); if(server->mObserver != nullptr){ } @@ -72,7 +73,7 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data) } if (events & BEV_EVENT_EOF) { if (nullptr != p->mObserver) - p->mObserver->OnDisConnected(); + p->mObserver->OnDisConnected("服务器主动断开连接"); if (p != nullptr) p->mStatus = TcpClientLibevent::UNCONNECTED; printf("Connection closed\n"); @@ -80,7 +81,7 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data) 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->mObserver->OnDisConnected("连接失败"); p->mStatus = TcpClientLibevent::FAIL; } else if (events & BEV_EVENT_CONNECTED) { @@ -134,7 +135,6 @@ TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLi mByteRecv = 0; mByteSend = 0; - ConnectServerSync(); } int TcpClientLibevent::ConnectServer() { @@ -189,7 +189,7 @@ int TcpClientLibevent::ConnectServerSync() while (this->mStatus != TcpClientLibevent::CONNECTED) { auto end = system_clock::to_time_t(system_clock::now()); - if ((end - start) > 2) { + if ((end - start) > 5) { this->mStatus = TcpClientLibevent::FAIL; break; } @@ -216,6 +216,12 @@ int TcpClientLibevent::Close() { return 0; } +int TcpClientLibevent::SendDataAsync(const char* data, int len) +{ + return bufferevent_write(this->mBev, data, len); + return 0; +} + uint64_t TcpClientLibevent::SocketFd() { return mSocketFD; diff --git a/examples/proto_debuger/tcp_client.h b/examples/proto_debuger/tcp_client.h index 710ebf90..b8ced753 100644 --- a/examples/proto_debuger/tcp_client.h +++ b/examples/proto_debuger/tcp_client.h @@ -34,6 +34,8 @@ extern "C"{ #include #include #include +#include + using namespace std; class TcpClientLibevent { @@ -51,7 +53,7 @@ public: virtual ~TcpClientObserver() { return; } mutex mMux; virtual void OnConnected() { return; }; - virtual void OnDisConnected() { return; }; + virtual void OnDisConnected(std::string) { return; }; virtual void OnData(uint8_t* dat, uint64_t len) { return; }; virtual void OnClose() { return; }; }; @@ -71,7 +73,7 @@ public: int SetReconnect(bool); int SetObserver(TcpClientObserver*); int Close(); - + int SendDataAsync(const char*, int len); uint64_t SocketFd(); Status mStatus; diff --git a/examples/proto_debuger/tcp_client_form.cpp b/examples/proto_debuger/tcp_client_form.cpp index 07b54560..a36fb77b 100644 --- a/examples/proto_debuger/tcp_client_form.cpp +++ b/examples/proto_debuger/tcp_client_form.cpp @@ -1,15 +1,16 @@ #include "tcp_client_form.h" #include "utils.h" -TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, TcpClientLibevent* p) +TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, TcpClientLibevent* p): + m_connected(false) { mClient = p; + mClient->SetObserver(this); if (nullptr != hwnd) { this->SetWindow(hwnd, nullptr, false); } m_url = url; m_port = port; - } void TcpClientForm::Init() @@ -19,8 +20,8 @@ void TcpClientForm::Init() m_label_1 = dynamic_cast(FindSubControl(L"uart_info_label")); m_rich_edit_1 = dynamic_cast(FindSubControl(L"uart_recv_eidt")); m_rich_edit_2 = dynamic_cast(FindSubControl(L"lua_script")); - m_rich_edit_3 = dynamic_cast(FindSubControl(L"uart_send_edit")); - m_button_1 = dynamic_cast(FindSubControl(L"btn_send_data")); + m_uart_send_edit = dynamic_cast(FindSubControl(L"uart_send_edit")); + m_btn_send_data = dynamic_cast(FindSubControl(L"btn_send_data")); m_check_box_1 = dynamic_cast(FindSubControl(L"check_new_line")); m_check_box_2 = dynamic_cast(FindSubControl(L"check_time_send")); m_check_box_3 = dynamic_cast(FindSubControl(L"check_hex_send")); @@ -28,9 +29,47 @@ void TcpClientForm::Init() wchar_t p[100] = { 0 }; - wsprintf(p, L"ַ%s,˿ں%d ", string2wstring(m_url).c_str(),m_port); + wsprintf(p, L"ַ%s,˿ں%d δ", string2wstring(m_url).c_str(),m_port); m_label_1->SetText(std::wstring(p)); + m_btn_send_data->AttachAllEvents([this](ui::EventArgs* ev) { + if (ev->Type == ui::EventType::kEventClick) { + std::wcout << m_uart_send_edit->GetText() << std::endl; + auto data = wstring2string(m_uart_send_edit->GetText()); + this->ClientEvent()->SendDataAsync(data.c_str(), data.size()); + } + return true; + }); +} + +TcpClientLibevent* TcpClientForm::ClientEvent() +{ + return mClient; +} + +void TcpClientForm::OnConnected() +{ + std::cout << "ӳɹ\r\n"<<__FILE__<<" " << __LINE__ << std::endl; + wchar_t p[100] = { 0 }; + wsprintf(p, L"ַ%s,˿ں%d ӳɹ", string2wstring(m_url).c_str(), m_port); + m_label_1->SetText(std::wstring(p)); +} + +void TcpClientForm::OnDisConnected(std::string reason) +{ + wchar_t p[100] = { 0 }; + wsprintf(p, L"ַ%s,˿ں%d δ " , string2wstring(m_url).c_str(), m_port); + m_label_1->SetText(std::wstring(p) + L" " + string2wstring(reason)); + m_connected = false; +} + +void TcpClientForm::OnData(uint8_t* dat, uint64_t len) +{ + std::cout << (char*)dat << std::endl; +} + +void TcpClientForm::OnClose() +{ } diff --git a/examples/proto_debuger/tcp_client_form.h b/examples/proto_debuger/tcp_client_form.h index 0295846b..cb5ffc2f 100644 --- a/examples/proto_debuger/tcp_client_form.h +++ b/examples/proto_debuger/tcp_client_form.h @@ -15,28 +15,39 @@ #include "tcp_client.h" using namespace std; -class TcpClientForm : public ui::ChildBox { +class TcpClientForm : + public ui::ChildBox, + public TcpClientLibevent::TcpClientObserver{ #pragma once public: TcpClientForm(ui::Window* hwnd,string url, uint32_t port, TcpClientLibevent* p); virtual void Init() override; + TcpClientLibevent* ClientEvent(); + +protected: + virtual void OnConnected(); + virtual void OnDisConnected(std::string) ; + virtual void OnData(uint8_t* dat, uint64_t len) ; + virtual void OnClose(); + + private: - TcpClientLibevent* mClient; ui::Label* m_label_1; ui::RichEdit* m_rich_edit_1; ui::RichEdit* m_rich_edit_2; - ui::RichEdit* m_rich_edit_3; - ui::Button* m_button_1; + ui::RichEdit* m_uart_send_edit; + ui::Button* m_btn_send_data; ui::CheckBox* m_check_box_1; ui::CheckBox* m_check_box_2; ui::CheckBox* m_check_box_3; ui::CheckBox* m_check_box_4; + TcpClientLibevent* mClient; virtual void HandleMessage(ui::EventArgs& msg); std::string m_url; int m_port; - + bool m_connected; }; \ No newline at end of file diff --git a/examples/proto_debuger/utils.cpp b/examples/proto_debuger/utils.cpp index 32645e1f..0eeaf275 100644 --- a/examples/proto_debuger/utils.cpp +++ b/examples/proto_debuger/utils.cpp @@ -29,4 +29,22 @@ std::string wstring2string(std::wstring wstr) result.append(buffer); delete[] buffer; return result; -} \ No newline at end of file +} + + + +std::string GenerateGuid() +{ + GUID guid; + CoCreateGuid(&guid); + char cBuffer[64] = { 0 }; + sprintf_s(cBuffer, sizeof(cBuffer), + "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", + guid.Data1, guid.Data2, + guid.Data3, guid.Data4[0], + guid.Data4[1], guid.Data4[2], + guid.Data4[3], guid.Data4[4], + guid.Data4[5], guid.Data4[6], + guid.Data4[7]); + return std::string(cBuffer); +} diff --git a/examples/proto_debuger/utils.h b/examples/proto_debuger/utils.h index 2ff85cd2..8798804c 100644 --- a/examples/proto_debuger/utils.h +++ b/examples/proto_debuger/utils.h @@ -7,3 +7,4 @@ std::wstring string2wstring(std::string str); std::string wstring2string(std::wstring wstr); +std::string GenerateGuid();