83 lines
2.6 KiB
C++
83 lines
2.6 KiB
C++
#include "tcp_server_form.h"
|
|
#include "utils.h"
|
|
#include <functional>
|
|
|
|
|
|
extern "C" {
|
|
#include "event2/bufferevent.h"
|
|
#include "event2/buffer.h"
|
|
#include "event2/listener.h"
|
|
#include "event2/util.h"
|
|
#include "event2/event.h"
|
|
#include "event2/thread.h"
|
|
};
|
|
|
|
TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p)
|
|
{
|
|
m_server = p;
|
|
m_url = url;
|
|
m_port = port;
|
|
|
|
TcpServerLibevent::OnAccept handler = std::bind(&TcpServerFrom::OnNewConnAccept, this,std::placeholders::_1 );
|
|
m_server->SetNewConnectionHandle(handler);
|
|
TcpServerLibevent::OnDisconnect handler2 = std::bind(&TcpServerFrom::OnDisConnected,
|
|
this, std::placeholders::_1);
|
|
m_server->SetConnectionLeaveHandle(handler2);
|
|
|
|
}
|
|
|
|
|
|
TcpServerLibevent* TcpServerFrom::ServerP()
|
|
{
|
|
return this->m_server;
|
|
}
|
|
|
|
void TcpServerFrom::Init()
|
|
{
|
|
ui::ChildBox::Init(); // 非常重要,没出事后childbox 后面的FindSubControl无法生效
|
|
|
|
m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"server_info"));
|
|
m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_recv_edit"));
|
|
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script_edit"));
|
|
m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_send_edit"));
|
|
m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
|
|
m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
|
|
m_rich_edit_4 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"cycle_eidt"));
|
|
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_button_2 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
|
m_label_2 = dynamic_cast<ui::Label*>(FindSubControl(L"title"));
|
|
m_combo_1 = dynamic_cast<ui::Combo*>(FindSubControl(L"clients"));
|
|
m_button_3 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_do_lua"));
|
|
|
|
wchar_t p[200] = L"";
|
|
wsprintf(p,L"监听地址: %s 端口: %d 在线连接数目: %d",string2wstring(m_url).c_str(),
|
|
m_port,
|
|
m_server->ConnectionCount());
|
|
m_label_1->SetText(p);
|
|
}
|
|
|
|
void TcpServerFrom::OnNewConnAccept(ConnectionLibevent*p)
|
|
{
|
|
std::cout << "TcpServerFrom OnNewConnAccept addr " << p->IpAddress()<<std::endl;
|
|
updateStatus();
|
|
|
|
return;
|
|
}
|
|
|
|
void TcpServerFrom::OnDisConnected(ConnectionLibevent* cli)
|
|
{
|
|
std::cout << "TcpServerFrom Disconnected" << cli->IpAddress() << " fd " << cli->SocketFd() << std::endl;
|
|
|
|
updateStatus();
|
|
}
|
|
|
|
void TcpServerFrom::updateStatus()
|
|
{
|
|
wchar_t tmp[200] = L"";
|
|
wsprintf(tmp, L"监听地址: %s 端口: %d 在线连接数目: %d", string2wstring(m_url).c_str(),
|
|
m_port,
|
|
m_server->ConnectionCount());
|
|
m_label_1->SetText(tmp);
|
|
}
|