no message
This commit is contained in:
parent
ad524aab97
commit
2603a06533
@ -123,7 +123,5 @@ private:
|
|||||||
std::map<std::string, lua_CFunction> mFunc;
|
std::map<std::string, lua_CFunction> mFunc;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -39,10 +39,11 @@ int ThreadRun(TcpClientLibevent *p) {
|
|||||||
#else
|
#else
|
||||||
//todo linux版本sleep
|
//todo linux版本sleep
|
||||||
#endif
|
#endif
|
||||||
}
|
}else {
|
||||||
ret = p->Dispatch();
|
ret = p->Dispatch();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,9 +54,10 @@ void conn_readcb(struct bufferevent *bev, void *user_data)
|
|||||||
size_t sz = evbuffer_get_length(input);
|
size_t sz = evbuffer_get_length(input);
|
||||||
if (sz > 0)
|
if (sz > 0)
|
||||||
{
|
{
|
||||||
uint8_t *msg = new uint8_t[sz];
|
uint8_t *msg = new uint8_t[sz + 1];
|
||||||
int ret = bufferevent_read(bev, msg, sz);
|
int ret = bufferevent_read(bev, msg, sz);
|
||||||
printf("%s\n", msg);
|
printf("%s\n", msg);
|
||||||
|
msg[sz] = '\0';
|
||||||
if(server->mObserver != nullptr){
|
if(server->mObserver != nullptr){
|
||||||
server->mObserver->OnData(msg, ret);
|
server->mObserver->OnData(msg, ret);
|
||||||
}
|
}
|
||||||
@ -134,7 +136,6 @@ TcpClientLibevent::TcpClientLibevent(std::string addrinfo, int port, TcpClientLi
|
|||||||
this->mObserver = p;
|
this->mObserver = p;
|
||||||
mByteRecv = 0;
|
mByteRecv = 0;
|
||||||
mByteSend = 0;
|
mByteSend = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int TcpClientLibevent::ConnectServer() {
|
int TcpClientLibevent::ConnectServer() {
|
||||||
@ -219,7 +220,6 @@ int TcpClientLibevent::Close() {
|
|||||||
int TcpClientLibevent::SendDataAsync(const char* data, int len)
|
int TcpClientLibevent::SendDataAsync(const char* data, int len)
|
||||||
{
|
{
|
||||||
return bufferevent_write(this->mBev, data, len);
|
return bufferevent_write(this->mBev, data, len);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t TcpClientLibevent::SocketFd()
|
uint64_t TcpClientLibevent::SocketFd()
|
||||||
|
16
examples/proto_debuger/tcp_client.lua
Normal file
16
examples/proto_debuger/tcp_client.lua
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
require("string")
|
||||||
|
local file = io.open("writetest.txt", "w+")
|
||||||
|
|
||||||
|
function OnNewClient(ip,port)
|
||||||
|
showdata("on new client from lua " .. ip..port)
|
||||||
|
end
|
||||||
|
|
||||||
|
function OnClientRecvData(data,ip,port)
|
||||||
|
showdata("client recieve data from lua "..data.. ip..port)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function OnClientDisconnect(ip,port)
|
||||||
|
showdata("client leave from lua " .. ip..port)
|
||||||
|
end
|
||||||
|
|
@ -10,6 +10,21 @@ TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, Tc
|
|||||||
if (nullptr != hwnd) {
|
if (nullptr != hwnd) {
|
||||||
this->SetWindow(hwnd, nullptr, false);
|
this->SetWindow(hwnd, nullptr, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mLua = new LuaDelegate;
|
||||||
|
mLuaFile.open("tcp_server.lua");
|
||||||
|
std::string lua_script;
|
||||||
|
if (mLuaFile.is_open()) {
|
||||||
|
std::string s;
|
||||||
|
while (getline(mLuaFile, s)) {
|
||||||
|
lua_script += s + "\r\n";
|
||||||
|
}
|
||||||
|
mLuaFile.close();
|
||||||
|
}
|
||||||
|
mLuaScript = lua_script;
|
||||||
|
mLua->DoString(lua_script);
|
||||||
|
|
||||||
|
std::cout << "lua script is " << lua_script << std::endl;
|
||||||
m_url = url;
|
m_url = url;
|
||||||
m_port = port;
|
m_port = port;
|
||||||
mLua = new LuaDelegate();
|
mLua = new LuaDelegate();
|
||||||
@ -36,6 +51,10 @@ void TcpClientForm::Init()
|
|||||||
m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send"));
|
m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send"));
|
||||||
m_check_box_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
|
m_check_box_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
|
||||||
|
|
||||||
|
m_rich_edit_2->SetRich(true);
|
||||||
|
m_rich_edit_2->SetReturnMsgWantCtrl(true);
|
||||||
|
m_rich_edit_2->SetText(string2wstring(mLuaScript));
|
||||||
|
|
||||||
|
|
||||||
wchar_t p[100] = { 0 };
|
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);
|
||||||
@ -82,14 +101,14 @@ void TcpClientForm::OnDisConnected(std::string reason)
|
|||||||
|
|
||||||
void TcpClientForm::OnData(uint8_t* dat, uint64_t len)
|
void TcpClientForm::OnData(uint8_t* dat, uint64_t len)
|
||||||
{
|
{
|
||||||
std::cout << (char*)dat << std::endl;
|
//std::cout << (char*)dat << "len is " << len << std::endl;
|
||||||
m_rich_edit_1->AppendText(string2wstring((char*)dat), false);
|
//m_rich_edit_1->AppendText(string2wstring(std::string((char*)dat)), false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClientForm::OnClose()
|
void TcpClientForm::OnClose()
|
||||||
{
|
{
|
||||||
m_rich_edit_1->AppendText(string2wstring("蟀諉眒冪剿羲\r\n"), false);
|
m_rich_edit_1->AppendText(string2wstring("蟀諉眒冪剿羲\r\n"), false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
#include "lua_wraper.h"
|
#include "lua_wraper.h"
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <istream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
class TcpClientForm :
|
class TcpClientForm :
|
||||||
@ -49,9 +51,9 @@ private:
|
|||||||
ui::CheckBox* m_check_box_3;
|
ui::CheckBox* m_check_box_3;
|
||||||
ui::CheckBox* m_check_box_4;
|
ui::CheckBox* m_check_box_4;
|
||||||
TcpClientLibevent* mClient;
|
TcpClientLibevent* mClient;
|
||||||
|
std::ifstream mLuaFile;
|
||||||
virtual void HandleMessage(ui::EventArgs& msg);
|
virtual void HandleMessage(ui::EventArgs& msg);
|
||||||
|
std::string mLuaScript;
|
||||||
std::string m_url;
|
std::string m_url;
|
||||||
int m_port;
|
int m_port;
|
||||||
bool m_connected;
|
bool m_connected;
|
||||||
|
16
examples/proto_debuger/tcp_server.lua
Normal file
16
examples/proto_debuger/tcp_server.lua
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
require("string")
|
||||||
|
local file = io.open("writetest.txt", "w+")
|
||||||
|
|
||||||
|
function OnNewClient(ip,port)
|
||||||
|
showdata("on new client from lua " .. ip..port)
|
||||||
|
end
|
||||||
|
|
||||||
|
function OnClientRecvData(data,ip,port)
|
||||||
|
showdata("client recieve data from lua "..data.. ip..port)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function OnClientDisconnect(ip,port)
|
||||||
|
showdata("client leave from lua " .. ip..port)
|
||||||
|
end
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
#include "tcp_server_form.h"
|
#include "tcp_server_form.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include "lua_bind.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "event2/bufferevent.h"
|
#include "event2/bufferevent.h"
|
||||||
@ -22,7 +22,7 @@ TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url,
|
|||||||
mFlagSelectClient = true;
|
mFlagSelectClient = true;
|
||||||
|
|
||||||
mLua = new LuaDelegate;
|
mLua = new LuaDelegate;
|
||||||
mLuaFile.open("script.lua");
|
mLuaFile.open("tcp_server.lua");
|
||||||
std::string lua_script;
|
std::string lua_script;
|
||||||
if (mLuaFile.is_open()) {
|
if (mLuaFile.is_open()) {
|
||||||
std::string s;
|
std::string s;
|
||||||
@ -40,6 +40,7 @@ TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url,
|
|||||||
TcpServerLibevent::OnDisconnect handler2 = std::bind(&TcpServerFrom::OnDisConnected,
|
TcpServerLibevent::OnDisconnect handler2 = std::bind(&TcpServerFrom::OnDisConnected,
|
||||||
this, std::placeholders::_1);
|
this, std::placeholders::_1);
|
||||||
m_server->SetConnectionLeaveHandle(handler2);
|
m_server->SetConnectionLeaveHandle(handler2);
|
||||||
|
this->mLua->BindFunction("showdata", LuaShowData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -70,7 +71,45 @@ void TcpServerFrom::Init()
|
|||||||
m_label_2 = dynamic_cast<ui::Label*>(FindSubControl(L"title"));
|
m_label_2 = dynamic_cast<ui::Label*>(FindSubControl(L"title"));
|
||||||
m_combo_1 = dynamic_cast<ui::Combo*>(FindSubControl(L"clients"));
|
m_combo_1 = dynamic_cast<ui::Combo*>(FindSubControl(L"clients"));
|
||||||
m_button_3 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_do_lua"));
|
m_button_3 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_do_lua"));
|
||||||
|
m_btn_save_lua = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
||||||
|
|
||||||
|
if (nullptr != m_button_1) {
|
||||||
|
m_button_1->AttachClick([this](ui::EventArgs* ev) {
|
||||||
|
auto client = m_combo_1->GetText();
|
||||||
|
if (client != L"全部连接") {
|
||||||
|
int clii = atoi(wstring2string(client).c_str());
|
||||||
|
if (nullptr != this->mClients[clii]) {
|
||||||
|
std::wstring send = m_rich_edit_3->GetText();
|
||||||
|
this->mClients[clii]->WriteData(wstring2string(send).c_str(),
|
||||||
|
wstring2string(send).size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ((nullptr != m_rich_edit_3)) {
|
||||||
|
std::wstring send = m_rich_edit_3->GetText();
|
||||||
|
for (auto itr = this->mClients.begin(); itr != this->mClients.end(); itr++) {
|
||||||
|
itr->second->WriteData(wstring2string(send).c_str(),
|
||||||
|
wstring2string(send).size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
m_btn_save_lua->AttachClick([this](ui::EventArgs* ev) {
|
||||||
|
std::cout << "保存lua脚本\r\n";
|
||||||
|
std::string lua = wstring2string(mEditLua->GetText());
|
||||||
|
if (0 == this->mLua->UpdateScript(lua)) {
|
||||||
|
this->mLuaScript = lua;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
MessageBox(0, L"lua脚本错误", L"", 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::cout << lua.c_str() << "\r\n";
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
m_button_3->AttachClick([this](ui::EventArgs*ev) {
|
m_button_3->AttachClick([this](ui::EventArgs*ev) {
|
||||||
std::cout <<"m_button_3 " << wstring2string(this->m_combo_1->GetText())<<std::endl;
|
std::cout <<"m_button_3 " << wstring2string(this->m_combo_1->GetText())<<std::endl;
|
||||||
@ -96,7 +135,10 @@ void TcpServerFrom::OnNewConnAccept(ConnectionLibevent* p)
|
|||||||
{
|
{
|
||||||
std::cout << "TcpServerFrom OnNewConnAccept addr " << p->IpAddress() << p->SocketFd() << std::endl;
|
std::cout << "TcpServerFrom OnNewConnAccept addr " << p->IpAddress() << p->SocketFd() << std::endl;
|
||||||
updateStatus();
|
updateStatus();
|
||||||
|
int ret = mLua->CallFuntion < std::string, LUA_INTEGER >("OnNewClient",p->IpAddress(),p->SocketFd());
|
||||||
|
if (ret < 0) {
|
||||||
|
std::cout << "call funtion error" << ret<<"std::endl";
|
||||||
|
}
|
||||||
auto element = new ui::ListContainerElement;
|
auto element = new ui::ListContainerElement;
|
||||||
element->SetClass(L"listitem");
|
element->SetClass(L"listitem");
|
||||||
element->SetFixedHeight(30);
|
element->SetFixedHeight(30);
|
||||||
@ -106,11 +148,15 @@ void TcpServerFrom::OnNewConnAccept(ConnectionLibevent* p)
|
|||||||
mClients[p->SocketFd()] = p;
|
mClients[p->SocketFd()] = p;
|
||||||
m_combo_1->Add(element);
|
m_combo_1->Add(element);
|
||||||
p->SetRecvHandler(
|
p->SetRecvHandler(
|
||||||
[this](char* p, uint32_t len) {
|
[this,p](char* recv, uint32_t len) {
|
||||||
std::cout << "SetRecvHandler " << p;
|
std::cout << "RecvHandler "<<p->IpAddress()<<p->SocketFd() <<" " << recv;
|
||||||
|
int ret = this->mLua->CallFuntion <std::string,std::string,LUA_INTEGER >
|
||||||
|
("OnClientRecvData", std::string(recv),p->IpAddress(), p->SocketFd());
|
||||||
|
if (ret < 0) {
|
||||||
|
MessageBox(0, L"lua脚本错误 OnClientRecvData", L"", 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,6 +165,11 @@ void TcpServerFrom::OnDisConnected(ConnectionLibevent* cli)
|
|||||||
std::cout << "TcpServerFrom Disconnected" << cli->IpAddress() << " fd " << cli->SocketFd() << std::endl;
|
std::cout << "TcpServerFrom Disconnected" << cli->IpAddress() << " fd " << cli->SocketFd() << std::endl;
|
||||||
updateStatus();
|
updateStatus();
|
||||||
mClients.erase(cli->SocketFd());
|
mClients.erase(cli->SocketFd());
|
||||||
|
int ret = this->mLua->CallFuntion <std::string, LUA_INTEGER >
|
||||||
|
("OnClientDisconnect", cli->IpAddress(), cli->SocketFd());
|
||||||
|
if (ret < 0) {
|
||||||
|
MessageBox(0, L"lua脚本错误 OnClientRecvData", L"", 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaDelegate* TcpServerFrom::LuaVM()
|
LuaDelegate* TcpServerFrom::LuaVM()
|
||||||
@ -132,7 +183,7 @@ void TcpServerFrom::ShowDataInEdit(const char* value)
|
|||||||
std::cout << "nullp pointer in ShowDataInEdit" << std::endl;
|
std::cout << "nullp pointer in ShowDataInEdit" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
m_rich_edit_1->AppendText(string2wstring(std::string(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpServerFrom::updateStatus()
|
void TcpServerFrom::updateStatus()
|
||||||
|
@ -22,7 +22,7 @@ public:
|
|||||||
void OnNewConnAccept(ConnectionLibevent*);
|
void OnNewConnAccept(ConnectionLibevent*);
|
||||||
void OnDisConnected(ConnectionLibevent*);
|
void OnDisConnected(ConnectionLibevent*);
|
||||||
LuaDelegate* LuaVM();
|
LuaDelegate* LuaVM();
|
||||||
|
std::map<uint32_t, ConnectionLibevent*> mClients;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void ShowDataInEdit(const char*) override;
|
void ShowDataInEdit(const char*) override;
|
||||||
@ -43,11 +43,11 @@ private:
|
|||||||
ui::CheckBox* m_check_box_3;
|
ui::CheckBox* m_check_box_3;
|
||||||
ui::CheckBox* m_check_box_4;
|
ui::CheckBox* m_check_box_4;
|
||||||
ui::Button* m_button_2;
|
ui::Button* m_button_2;
|
||||||
|
ui::Button* m_btn_save_lua;
|
||||||
ui::Label* m_label_2;
|
ui::Label* m_label_2;
|
||||||
ui::Combo* m_combo_1;
|
ui::Combo* m_combo_1;
|
||||||
ui::Button* m_button_3;
|
ui::Button* m_button_3;
|
||||||
std::ifstream mLuaFile;
|
std::ifstream mLuaFile;
|
||||||
std::map<uint32_t, ConnectionLibevent*> mClients;
|
|
||||||
bool mFlagSelectClient;
|
bool mFlagSelectClient;
|
||||||
LuaDelegate *mLua;
|
LuaDelegate *mLua;
|
||||||
std::string mLuaScript;
|
std::string mLuaScript;
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
<Button class="btn_global_blue_80x30" name="btn_close_uart" width="100" text="关闭" bkcolor="lightcolor" margin="25,3,5,3" />
|
<Button class="btn_global_blue_80x30" name="btn_close_uart" width="100" text="关闭" bkcolor="lightcolor" margin="25,3,5,3" />
|
||||||
<Label name="title" text="客户端:" width="60" valign="bottom" margin="0,0,0,0" height="30" font="arial_14"/>
|
<Label name="title" text="客户端:" width="60" valign="bottom" margin="0,0,0,0" height="30" font="arial_14"/>
|
||||||
<Combo class="list" name="clients" width="100" height="30" bkimage="file='../public/combo/normal.png' corner='5,5,30,5'"/>
|
<Combo class="list" name="clients" width="100" height="30" bkimage="file='../public/combo/normal.png' corner='5,5,30,5'"/>
|
||||||
|
<Button class="btn_global_blue_80x30" name="btn_save_lua" width="100" text="保存lua脚本" bkcolor="lightcolor" margin="25,3,5,3" />
|
||||||
|
|
||||||
</HBox>
|
</HBox>
|
||||||
<HBox height="40">
|
<HBox height="40">
|
||||||
<Button class="btn_global_blue_80x30" name="btn_do_lua" width="100" text="执行" bkcolor="lightcolor" margin="25,3,5,3" />
|
<Button class="btn_global_blue_80x30" name="btn_do_lua" width="100" text="执行" bkcolor="lightcolor" margin="25,3,5,3" />
|
||||||
|
Loading…
Reference in New Issue
Block a user