no message

This commit is contained in:
zcy 2021-11-22 23:53:21 +08:00
parent a08bf0ccd4
commit 65771d0be6
13 changed files with 267 additions and 32 deletions

View File

@ -95,7 +95,7 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
wprintf(L"%s\r\n", dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
TcpClientForm* p = mTcpClientForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
if (p != nullptr) {
printf("GetCurSel %d\r\n", mRightSide->GetCurSel());

View File

@ -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{
}
}

View File

@ -0,0 +1,44 @@
//
// Created by bt110 on 2019/8/19.
//
#ifndef CPP11FEATURETEST_LOGER_H
#define CPP11FEATURETEST_LOGER_H
#include <stdio.h>
#include <string>
#include <time.h>
#include <fstream>
#include <iostream>
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

View File

@ -6,7 +6,7 @@
#include"resource1.h"
#include <math.h>
#include "lua_wraper.h"
#include "loger.h"
enum ThreadId
{

View File

@ -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());
}
}

View File

@ -163,6 +163,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="base_form.cpp" />
<ClCompile Include="loger.cpp" />
<ClCompile Include="lua_wraper.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="new_monitor_form.cpp" />
@ -175,6 +176,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="base_form.h" />
<ClInclude Include="lua_wraper.h" />
<ClInclude Include="main.h" />
<ClInclude Include="msgdef.h" />
<ClInclude Include="new_monitor_form.h" />

View File

@ -45,6 +45,9 @@
<ClCompile Include="lua_wraper.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="loger.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
@ -74,6 +77,9 @@
<ClInclude Include="msgdef.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="lua_wraper.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Xml Include="..\Debug\resources\themes\default\global.xml">

View File

@ -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;

View File

@ -34,6 +34,8 @@ extern "C"{
#include <iostream>
#include <mutex>
#include <thread>
#include <functional>
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;

View File

@ -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<ui::Label*>(FindSubControl(L"uart_info_label"));
m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_recv_eidt"));
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script"));
m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
m_uart_send_edit = dynamic_cast<ui::RichEdit*>(FindSubControl(L"uart_send_edit"));
m_btn_send_data = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
m_check_box_1 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_new_line"));
m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
m_check_box_3 = dynamic_cast<ui::CheckBox*>(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()
{
}

View File

@ -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;
};

View File

@ -29,4 +29,22 @@ std::string wstring2string(std::wstring wstr)
result.append(buffer);
delete[] buffer;
return result;
}
}
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);
}

View File

@ -7,3 +7,4 @@ std::wstring string2wstring(std::string str);
std::string wstring2string(std::wstring wstr);
std::string GenerateGuid();