no message
This commit is contained in:
parent
12b3047006
commit
ef927012e3
@ -1,5 +1,17 @@
|
|||||||
#include "tcp_server_form.h"
|
#include "tcp_server_form.h"
|
||||||
#include "utils.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)
|
TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p)
|
||||||
{
|
{
|
||||||
@ -8,6 +20,13 @@ TcpServerFrom::TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpSer
|
|||||||
m_port = port;
|
m_port = port;
|
||||||
|
|
||||||
|
|
||||||
|
TcpServerLibevent::OnAccept handler = std::bind(&TcpServerFrom::OnNewConnAccept, this,std::placeholders::_1 ,
|
||||||
|
std::placeholders::_2, std::placeholders::_3 );
|
||||||
|
m_server->SetNewConnectionHandle(handler);
|
||||||
|
TcpServerLibevent::OnDisconnect handler2 = std::bind(&TcpServerFrom::OnDisConnected,
|
||||||
|
this, std::placeholders::_1);
|
||||||
|
m_server->SetConnectionLeaveHandle(handler2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -19,19 +38,20 @@ TcpServerLibevent* TcpServerFrom::ServerP()
|
|||||||
void TcpServerFrom::Init()
|
void TcpServerFrom::Init()
|
||||||
{
|
{
|
||||||
ui::ChildBox::Init(); // 非常重要,没出事后childbox 后面的FindSubControl无法生效
|
ui::ChildBox::Init(); // 非常重要,没出事后childbox 后面的FindSubControl无法生效
|
||||||
ui::Label* m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"server_info"));
|
|
||||||
ui::RichEdit* m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_recv_edit"));
|
m_label_1 = dynamic_cast<ui::Label*>(FindSubControl(L"server_info"));
|
||||||
ui::RichEdit* m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script_edit"));
|
m_rich_edit_1 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_recv_edit"));
|
||||||
ui::RichEdit* m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_send_edit"));
|
m_rich_edit_2 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"lua_script_edit"));
|
||||||
ui::Button* m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
|
m_rich_edit_3 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"server_send_edit"));
|
||||||
ui::CheckBox* m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
|
m_button_1 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_send_data"));
|
||||||
ui::RichEdit* m_rich_edit_4 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"cycle_eidt"));
|
m_check_box_2 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_time_send"));
|
||||||
ui::CheckBox* m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send"));
|
m_rich_edit_4 = dynamic_cast<ui::RichEdit*>(FindSubControl(L"cycle_eidt"));
|
||||||
ui::CheckBox* m_check_box_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
|
m_check_box_3 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_send"));
|
||||||
ui::Button* m_button_2 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
m_check_box_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
|
||||||
ui::Label* m_label_2 = dynamic_cast<ui::Label*>(FindSubControl(L"title"));
|
m_button_2 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
||||||
ui::Combo* m_combo_1 = dynamic_cast<ui::Combo*>(FindSubControl(L"clients"));
|
m_label_2 = dynamic_cast<ui::Label*>(FindSubControl(L"title"));
|
||||||
ui::Button* m_button_3 = dynamic_cast<ui::Button*>(FindSubControl(L"btn_do_lua"));
|
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"";
|
wchar_t p[200] = L"";
|
||||||
wsprintf(p,L"监听地址: %s 端口: %d 在线连接数目: %d",string2wstring(m_url).c_str(),
|
wsprintf(p,L"监听地址: %s 端口: %d 在线连接数目: %d",string2wstring(m_url).c_str(),
|
||||||
@ -40,8 +60,18 @@ void TcpServerFrom::Init()
|
|||||||
m_label_1->SetText(p);
|
m_label_1->SetText(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionLibevent* TcpServerFrom::OnNewConnAccept(bufferevent* ev, uint32_t fd, sockaddr_in* p1)
|
ConnectionLibevent* TcpServerFrom::OnNewConnAccept(struct bufferevent* ev, uint32_t fd, struct sockaddr_in* p1)
|
||||||
{
|
{
|
||||||
|
std::cout << "TcpServerFrom OnNewConnAccept fd" << fd << " addr " << inet_ntoa(p1->sin_addr)<<std::endl;
|
||||||
|
|
||||||
|
wchar_t text[40] = L"";
|
||||||
|
wsprintf(text, L"Á¬½ÓÊý:%d", m_server->ConnectionCount());
|
||||||
|
m_label_1->SetText(text);
|
||||||
|
return new ConnectionLibevent(ev, fd, p1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpServerFrom::OnDisConnected(ConnectionLibevent* cli)
|
||||||
|
{
|
||||||
|
std::cout << "TcpServerFrom Disconnected" << cli->IpAddress() << " fd " << cli->SocketFd() << std::endl;
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,10 @@ public:
|
|||||||
TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p);
|
TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p);
|
||||||
TcpServerLibevent* ServerP();
|
TcpServerLibevent* ServerP();
|
||||||
virtual void Init() override;
|
virtual void Init() override;
|
||||||
protected:
|
|
||||||
ConnectionLibevent* OnNewConnAccept(struct bufferevent* ev, uint32_t fd, struct sockaddr_in* p1);
|
ConnectionLibevent* OnNewConnAccept(struct bufferevent* ev, uint32_t fd, struct sockaddr_in* p1);
|
||||||
|
void OnDisConnected(ConnectionLibevent*);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TcpServerLibevent* m_server;
|
TcpServerLibevent* m_server;
|
||||||
|
@ -33,7 +33,6 @@ ConnectionLibevent::ConnectionLibevent(struct bufferevent* ev, uint32_t fd, stru
|
|||||||
m_fd(-1),
|
m_fd(-1),
|
||||||
m_addr(nullptr)
|
m_addr(nullptr)
|
||||||
{
|
{
|
||||||
|
|
||||||
m_event = ev;
|
m_event = ev;
|
||||||
m_fd = fd;
|
m_fd = fd;
|
||||||
m_addr = p1;
|
m_addr = p1;
|
||||||
@ -47,7 +46,6 @@ ConnectionLibevent* defaultConnAccept(struct bufferevent* ev, uint32_t fd, struc
|
|||||||
|
|
||||||
void defaultConnClose(ConnectionLibevent*p) {
|
void defaultConnClose(ConnectionLibevent*p) {
|
||||||
std::cout << "defaultConnClose close connection " << p->IpAddress() << p->SocketFd()<<std::endl;
|
std::cout << "defaultConnClose close connection " << p->IpAddress() << p->SocketFd()<<std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -153,6 +151,7 @@ void ServerCallbacks::cb_listener(struct evconnlistener* listener, evutil_socket
|
|||||||
struct bufferevent* bev = nullptr;
|
struct bufferevent* bev = nullptr;
|
||||||
bev = bufferevent_socket_new(server->m_event_base, fd, BEV_OPT_CLOSE_ON_FREE);
|
bev = bufferevent_socket_new(server->m_event_base, fd, BEV_OPT_CLOSE_ON_FREE);
|
||||||
std::cout << "null 4" << bev << std::endl;
|
std::cout << "null 4" << bev << std::endl;
|
||||||
|
// 这是一个bad design,
|
||||||
ConnectionLibevent* conn = server->m_handle_accept(bev, ntohs(client->sin_port), client);
|
ConnectionLibevent* conn = server->m_handle_accept(bev, ntohs(client->sin_port), client);
|
||||||
conn->SetServer(server);
|
conn->SetServer(server);
|
||||||
server->AddConnection(ntohs(client->sin_port), conn);
|
server->AddConnection(ntohs(client->sin_port), conn);
|
||||||
@ -264,6 +263,12 @@ int TcpServerLibevent::SetNewConnectionHandle(OnAccept p) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int TcpServerLibevent::SetConnectionLeaveHandle(OnDisconnect p) {
|
||||||
|
m_handle_disconnect = p;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description:
|
* @description:
|
||||||
* @param {int} ports
|
* @param {int} ports
|
||||||
@ -305,7 +310,6 @@ TcpServerLibevent::TcpServerLibevent(int port, string bindip) :
|
|||||||
m_status = FAIL;
|
m_status = FAIL;
|
||||||
}
|
}
|
||||||
m_status = STOP;
|
m_status = STOP;
|
||||||
std::cout << "3" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -349,6 +353,7 @@ TcpServerLibevent::~TcpServerLibevent() {
|
|||||||
event_base_loopbreak(m_event_base);
|
event_base_loopbreak(m_event_base);
|
||||||
this->m_status = STOP;
|
this->m_status = STOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,6 +71,7 @@ public:
|
|||||||
int StartServerAsync(); // 异步启动服务
|
int StartServerAsync(); // 异步启动服务
|
||||||
int RemoveConnection(uint32_t);
|
int RemoveConnection(uint32_t);
|
||||||
int SetNewConnectionHandle(OnAccept);
|
int SetNewConnectionHandle(OnAccept);
|
||||||
|
int SetConnectionLeaveHandle(OnDisconnect);
|
||||||
int AddConnection(uint32_t fd, ConnectionLibevent* p);
|
int AddConnection(uint32_t fd, ConnectionLibevent* p);
|
||||||
uint64_t SocketFd();
|
uint64_t SocketFd();
|
||||||
int ConnectionCount();
|
int ConnectionCount();
|
||||||
|
Loading…
Reference in New Issue
Block a user