diff --git a/protoDebuger/NetSelect.qml b/protoDebuger/NetSelect.qml index e9380ca..aa04426 100644 --- a/protoDebuger/NetSelect.qml +++ b/protoDebuger/NetSelect.qml @@ -64,23 +64,17 @@ Item { color: "white" textMargin: 4 } - RadioButton { - id: isServer + ComboBox { + id: conn_type Layout.leftMargin: 20 - width: 138 - height: 41 - text: qsTr("服务端") - checked: true + Layout.topMargin: 20 + width: 143 + height: 40 - onClicked:{ - if(isServer.checked == true){ - isServer.checked = false - isServer.checkable = false - }else{ - isServer.checked = true - isServer.checkable = true - } - } + model:[ + "client", + "server" + ] } } } @@ -91,9 +85,9 @@ Item { Layout.leftMargin: 20 Label { id: label7 + text: "协议选择:" Layout.leftMargin: 20 Layout.topMargin: 20 - text: "协议选择:" Layout.preferredHeight: 40 renderType: Text.QtRendering Layout.preferredWidth: 125 @@ -179,16 +173,13 @@ Item { onClicked: { if(button_network.text == "打开网络"){ let type_network = -1 - if(proto_combox.currentText == "udp" && isServer.checked){ + if(proto_combox.currentText == "udp" ){ type_network = 0 } - if(proto_combox.currentText == "udp" && !isServer.checked){ - type_network = 2 - } - if(proto_combox.currentText == "tcp" && isServer.checked){ + if(proto_combox.currentText == "tcp" && (conn_type.currentText == "server")){ type_network = 1 } - if(proto_combox.currentText == "tcp" && !isServer.checked){ + if(proto_combox.currentText == "tcp" && (conn_type.currentText == "client")){ type_network = 3 } console.log(hostEdit.text, diff --git a/protoDebuger/ProtoDebug.qml b/protoDebuger/ProtoDebug.qml index 0c094fd..437e4c5 100644 --- a/protoDebuger/ProtoDebug.qml +++ b/protoDebuger/ProtoDebug.qml @@ -146,9 +146,7 @@ Item { tip.text = "更新lua脚本成功" tip.visible = true } - } - Rectangle { Layout.preferredHeight: 41 Layout.preferredWidth: input.contentWidth<300 ? 300 : input.contentWidth + 10 diff --git a/protoDebuger/SerialSelect.qml b/protoDebuger/SerialSelect.qml index 085cefa..359e53a 100644 --- a/protoDebuger/SerialSelect.qml +++ b/protoDebuger/SerialSelect.qml @@ -198,14 +198,14 @@ Item { dataBits.currentText, stopBits.currentText, flow.currentText) - if(ret == 0){ + if(ret === 0){ uart_open = true button.text = "关闭串口" serial_select_view.notify("串口已经连接") } }else{ let ret = DataWrap.closeSerial(); - if(ret == 0){ + if(ret === 0){ button.text = "打开串口" uart_open = false serial_select_view.notify("串口已经关闭") diff --git a/protoDebuger/icon/net.svg b/protoDebuger/icon/net.svg new file mode 100644 index 0000000..7f53142 --- /dev/null +++ b/protoDebuger/icon/net.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/protoDebuger/lua_wraper.cpp b/protoDebuger/lua_wraper.cpp index ca888b8..235026b 100644 --- a/protoDebuger/lua_wraper.cpp +++ b/protoDebuger/lua_wraper.cpp @@ -31,7 +31,7 @@ void LuaDelegate::OnSerialData(QString data){ void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port) { - int i = lua_getglobal(mVM,"OnNetworkData"); + int ret = lua_getglobal(mVM,"OnNetworkData"); lua_pushstring(mVM,addr); lua_pushstring(mVM,data); lua_pushnumber(mVM,port); diff --git a/protoDebuger/main.cpp b/protoDebuger/main.cpp index 9395edb..5adcf14 100644 --- a/protoDebuger/main.cpp +++ b/protoDebuger/main.cpp @@ -14,13 +14,17 @@ #include "serialcontroller.h" #include "lua_wraper.h" #include "mainwindow.h" - +#include int main(int argc, char *argv[]) { + AllocConsole(); + freopen("CONOUT$","w",stdout); + QApplication app(argc, argv); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + printf("hello world"); MainWindow w; w.setWindowTitle("协议调试器"); w.show(); diff --git a/protoDebuger/main.qml b/protoDebuger/main.qml index 7637d90..7152ea7 100644 --- a/protoDebuger/main.qml +++ b/protoDebuger/main.qml @@ -21,7 +21,7 @@ Rectangle { height: parent.height Rectangle { - width: 100; + width: 130; height: parent.height color: "black" Flow{ @@ -32,14 +32,14 @@ Rectangle { Button{ id: btn_serial width: parent.width - height:60 + height:80 onClicked: { uart_choose.visible = true net_choose.visible = false proto_debug.visible = false } background: Image { - source: btn_serial.hovered? "qrc:///res/proto_hover.svg": "qrc:///res/serial.svg" + source: btn_serial.hovered? "qrc:///res/serial1.svg": "qrc:///res/serial.svg" } } Text { @@ -47,15 +47,15 @@ Rectangle { text: qsTr("串口设置") width: parent.width color: "white" - font.pointSize: 10 + font.pointSize: 15 anchors.top: btn_serial.bottom - leftPadding: 13 + leftPadding: 15 } Button{ id: button_net width: parent.width - height:60 + height:80 anchors.top: label_serial.bottom onClicked: { @@ -63,30 +63,26 @@ Rectangle { background: Image { source: button_net.hovered? "qrc:///res/nethover.svg":"qrc:///res/net.svg" } - onPressed:{ uart_choose.visible = false net_choose.visible = true proto_debug.visible = false } - - - } Text { id: label_net text: qsTr("网口设置") width: parent.width color: "white" - font.pointSize: 10 + font.pointSize: 15 anchors.top: button_net.bottom - leftPadding: 13 + leftPadding: 15 } Button{ id: button_proto width: parent.width - height:60 + height:80 anchors.top: label_net.bottom onClicked: { uart_choose.visible = false @@ -95,7 +91,7 @@ Rectangle { DataWrap.getValFromQml(1) } background: Image { - source: "qrc:///res/proto.svg" + source: !button_proto.hovered? "qrc:///res/proto.svg":"qrc:///res/proto_hover.svg" } } Text { @@ -103,9 +99,9 @@ Rectangle { text: qsTr("协议调试") width: parent.width color: "white" - font.pointSize: 10 + font.pointSize: 15 anchors.top: button_proto.bottom - leftPadding: 13 + leftPadding: 15 } Button{ @@ -130,9 +126,9 @@ Rectangle { text: qsTr("tcp集群") width: parent.width color: "white" - font.pointSize: 10 + font.pointSize: 15 anchors.top: button_tcp_swarm.bottom - leftPadding: 13 + leftPadding: 15 } Text { @@ -140,7 +136,7 @@ Rectangle { text: qsTr("未连接") width: parent.width color: "white" - font.pointSize: 13 + font.pointSize: 10 anchors.bottom: parent.bottom Layout.leftMargin: 10 Layout.bottomMargin: 10 @@ -150,25 +146,25 @@ Rectangle { } SerialSelect{ id: uart_choose - width: parent.width - 100 + width: parent.width - 130 height: parent.height } NetSelect{ id: net_choose - width: parent.width - 100 + width: parent.width - 130 height: parent.height } ProtoDebug{ id: proto_debug - width: parent.width - 100 + width: parent.width - 130 height: parent.height visible: false } TcpSwarm{ id: tcp_swarm - width: parent.width - 100 + width: parent.width - 130 height: parent.height visible: false } diff --git a/protoDebuger/network_controller.cpp b/protoDebuger/network_controller.cpp index f8f3043..bbd48cb 100644 --- a/protoDebuger/network_controller.cpp +++ b/protoDebuger/network_controller.cpp @@ -8,7 +8,8 @@ NetworkController::NetworkType NetworkController::Type() NetworkController::NetworkController(NetworkController::NetworkType type, QString ip, - uint16_t port):mTcp(nullptr), + uint16_t port): + mTcp(nullptr), mUDP(nullptr), mTcpServer(nullptr), mCnn(nullptr), @@ -22,10 +23,11 @@ NetworkController::NetworkController(NetworkController::NetworkType type, mTcp = new QTcpSocket(); mCnn = mTcp; + QObject::connect(mTcp, SIGNAL(connected()), this, SLOT(on_conected())); QObject::connect(mTcp, SIGNAL(readyRead()), this, SLOT(on_ready_read())); QObject::connect(mTcp, SIGNAL(disconnected()), this, SLOT(on_disconect())); - mTcp->connectToHost(ip,port,QIODevice::ReadWrite); + mTcp->connectToHost(ip,port,QIODevice::ReadWrite,QAbstractSocket::IPv4Protocol); qDebug()<state(); if(mTcp->state() == QAbstractSocket::ConnectedState) mState = true; @@ -43,7 +45,7 @@ NetworkController::NetworkController(NetworkController::NetworkType type, qDebug() << "m_pTcpServer->listen() error"; } } - if((type == NetworkType::TYPE_UDP_SERVER) || (type == NetworkType::TYPE_TCP_CLIENT)){ + if((type == NetworkType::TYPE_UDP_SERVER) || (type == NetworkType::TYPE_UDP_CLIENT)){ mUDP = new QUdpSocket(this); mUDP->bind(QHostAddress::Any, port); mCnn = mUDP; @@ -94,6 +96,8 @@ int NetworkController::Close() mCnn->close(); delete mTcpServer; delete mCnn; + mTcpServer = nullptr; + mCnn = nullptr; } if(mType == TYPE_TCP_CLIENT){ qDebug()<<(((QTcpSocket*)mTcp)->state()); @@ -102,6 +106,7 @@ int NetworkController::Close() if(((QTcpSocket*)mTcp)->waitForDisconnected(3000)); mTcp->close(); delete mTcp; + mTcp = nullptr; } mState = false; return 0; @@ -110,10 +115,28 @@ int NetworkController::Close() NetworkController::~NetworkController() { - delete mTcp; - delete mCnn; - delete mTcpServer; - delete mUDP; + if(mState){ + if(mTcp != nullptr) + delete mTcp; + if((mCnn != nullptr) && (this->mType != NetworkType::TYPE_TCP_CLIENT)) + delete mCnn; + if(mTcpServer != nullptr) + delete mTcpServer; + if(mUDP != nullptr) + delete mUDP; + } + +} + +bool NetworkController::State() +{ + return this->mState; +} + +void NetworkController::on_conected() +{ + qDebug()<<"connected"; + mState = true; } void NetworkController::on_ready_read() diff --git a/protoDebuger/network_controller.h b/protoDebuger/network_controller.h index c4cb7d3..dcd0c6e 100644 --- a/protoDebuger/network_controller.h +++ b/protoDebuger/network_controller.h @@ -33,8 +33,10 @@ public: RecvResult ReadData(int8_t *data); int Close(); ~NetworkController(); - + bool State(); public slots: + void on_conected(); + void on_ready_read(); void on_disconect(); void on_server_accept(); diff --git a/protoDebuger/qml.qrc b/protoDebuger/qml.qrc index dc27ee4..0c6f4a8 100644 --- a/protoDebuger/qml.qrc +++ b/protoDebuger/qml.qrc @@ -10,5 +10,6 @@ TcpSwarm.qml res/nethover.svg res/proto_hover.svg + res/serial1.svg diff --git a/protoDebuger/res/net_hover.svg b/protoDebuger/res/net_hover.svg deleted file mode 100644 index 4b03831..0000000 --- a/protoDebuger/res/net_hover.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/protoDebuger/res/serial.svg b/protoDebuger/res/serial.svg index 7a4e899..1b47b61 100644 --- a/protoDebuger/res/serial.svg +++ b/protoDebuger/res/serial.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/protoDebuger/res/serial1.svg b/protoDebuger/res/serial1.svg new file mode 100644 index 0000000..709efa9 --- /dev/null +++ b/protoDebuger/res/serial1.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/protoDebuger/serialcontroller.cpp b/protoDebuger/serialcontroller.cpp index 0bed121..8e01e71 100644 --- a/protoDebuger/serialcontroller.cpp +++ b/protoDebuger/serialcontroller.cpp @@ -1,6 +1,5 @@ #include "serialcontroller.h" - SerialController::SerialController(QObject *parent) : QObject(parent), mCurrentPort(nullptr), @@ -11,9 +10,7 @@ SerialController::SerialController(QObject *parent) : } - -int SerialController::OpenSerial(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow) -{ +int SerialController::OpenSerial(QString port, QString baudRate, QString dataBits, QString stopBits, QString flow){ GetSetting(baudRate,dataBits,stopBits,flow); mCurrentPort = new QSerialPort(); if(nullptr == mCurrentPort){ @@ -43,15 +40,12 @@ int SerialController::CloseSerial() return OK; } - - QList SerialController::GetPorts() { this->mPorts = QSerialPortInfo::availablePorts(); return mPorts; } - int SerialController::SetProto(QSerialProto * proto) { this->mProto = proto; diff --git a/protoDebuger/sharedata.cpp b/protoDebuger/sharedata.cpp index e4e7e83..e3b961a 100644 --- a/protoDebuger/sharedata.cpp +++ b/protoDebuger/sharedata.cpp @@ -9,7 +9,8 @@ QmlShareData::QmlShareData(QObject *parent) : QObject(parent), m_qml_view_(nullptr), - m_luavm_status_(false) + m_luavm_status_(false), + m_network_(nullptr) { m_serial_controller_ = new SerialController(nullptr); QFile file("Test.lua"); @@ -82,11 +83,21 @@ int QmlShareData::openUart(QString port, QString baudRate, QString dataBits, QSt int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type) { - if(type == NetworkController::TYPE_TCP_CLIENT){ - m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port); + qDebug()<<"connect to server"; + if(m_network_ == nullptr) + m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port); + else{ + delete m_network_; + m_network_ = nullptr; + m_network_ = new NetworkController(NetworkController::TYPE_TCP_CLIENT,ip,port); + } connect(m_network_,SIGNAL(on_data_recv()),this,SLOT(on_network_data_recv())); - return 0; + if(m_network_->State()) + return 0; + else{ + return -1; + } } if((type == NetworkController::TYPE_UDP_CLIENT) || (type == NetworkController::TYPE_UDP_SERVER)){ diff --git a/protoDebuger/tcp_server_libevent.cpp b/protoDebuger/tcp_server_libevent.cpp new file mode 100644 index 0000000..a3bf7bb --- /dev/null +++ b/protoDebuger/tcp_server_libevent.cpp @@ -0,0 +1,316 @@ +#define _WSPIAPI_H_ +#define _WINSOCKAPI_ +#include "tcp_server_libevent.h" +#include + +/** + * @description: + * @param {*} + * @return {*} + */ +ConnectionLibevent::ConnectionLibevent(TcpServerLibevent *p,struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1): +m_parent_server(nullptr), +m_event(nullptr), +m_fd(-1), +m_addr(nullptr) +{ + m_parent_server = p; + m_event = ev; + m_fd = fd; + m_addr = p1; +} +/** + * @description: + * @param {*} + * @return {*} + */ +ConnectionLibevent::ConnectionLibevent(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1): +m_parent_server(nullptr), +m_event(nullptr), +m_fd(-1), +m_addr(nullptr) +{ + m_event = ev; + m_fd = fd; + m_addr = p1; +} +/** + * @description: + * @param {*} + * @return {*} + */ +ConnectionLibevent* defaultConnAccept(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1){ + return new ConnectionLibevent(ev,fd,p1); +} +/** + * @description: + * @param {*} + * @return {*} + */ +int ConnectionLibevent::OnRecv(char *p,uint32_t len){ + std::cout<<"OnRecv "<m_fd << " "<IpAddress()<m_event,p,len); +} + +uint32_t ConnectionLibevent::SocketFd(){ + return m_fd; +} +/** + * @description: + * @param {*} + * @return {*} + */ +int ConnectionLibevent::SetServer(TcpServerLibevent *p){ + if(nullptr != p){ + this->m_parent_server = p; + return 0; + } + return -1; +} + +string ConnectionLibevent::IpAddress(){ + if(nullptr != m_addr){ + return string(inet_ntoa(m_addr->sin_addr)); + } + return ""; +} +TcpServerLibevent *ConnectionLibevent::Server(){ + return m_parent_server; +} + +void read_cb(struct bufferevent *bev, void *arg) +{ + char buf[1024] = {0}; + ConnectionLibevent* conn = (ConnectionLibevent*)arg; + bufferevent_read(bev, buf, sizeof(buf)); + + cout << "client " << conn->IpAddress() << " say:" << buf << endl; + conn->OnRecv(buf,sizeof(buf)); +} + +void write_cb(struct bufferevent *bev, void *arg) +{ + ConnectionLibevent* conn = (ConnectionLibevent*)arg; + std::cout<<"connection "<IpAddress()<<" sended data success"<< std::endl; +} +/** + * @description: + * @param {*} + * @return {*} + */ +void event_cb(struct bufferevent *bev, short events, void *arg) +{ + ConnectionLibevent *conn = (ConnectionLibevent*)(arg); + TcpServerLibevent *server = conn->Server(); + if (events & BEV_EVENT_EOF) + { + cout << "connection closed: " << conn->IpAddress() << " " << conn->SocketFd() << endl; + conn->OnClose(); + bufferevent_free(bev); + server->RemoveConnection(conn->SocketFd()); + } + else if (events & BEV_EVENT_ERROR) + { + conn->OnClose(); + bufferevent_free(bev); + server->RemoveConnection(conn->SocketFd()); + cout << "some other error !" << endl; + } + delete conn; +} + +/** + * @description: + * @param {*} + * @return {*} + */ +void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr) { + struct sockaddr_in* client = (sockaddr_in*)addr ; + cout << "connect new client: " << inet_ntoa(client->sin_addr) << " " << fd << " ::"<< ntohs(client->sin_port)<< endl; + TcpServerLibevent *server = (TcpServerLibevent*)ptr; + if(server != nullptr){ + struct bufferevent *bev; + bev = bufferevent_socket_new(server->m_event_base, fd, BEV_OPT_CLOSE_ON_FREE); + ConnectionLibevent *conn = server->m_handle_accept(bev,ntohs(client->sin_port),client); + conn->SetServer(server); + server->AddConnection(ntohs(client->sin_port),conn); + bufferevent_setcb(bev, read_cb, write_cb, event_cb, conn); + bufferevent_enable(bev, EV_READ); + } +} + +/** + * @description: + * @param {*} + * @return {*} + */ +void server_run(TcpServerLibevent *p){ + if(nullptr != p){ + if(p->m_status == TcpServerLibevent::STOP){ + p->m_status = TcpServerLibevent::RUNNING; + event_base_dispatch(p->m_event_base); + evconnlistener_free(p->m_event_listener); + event_base_free(p->m_event_base); + } + } +} +/** + * @description: + * @param {*} + * @return {*} + */ +TcpServerLibevent::SERVER_STATUS TcpServerLibevent::Status(){ + return m_status; +} +/** + * @description: + * @param {*} + * @return {*} + */ +int TcpServerLibevent::AddConnection(uint32_t fd,ConnectionLibevent *p){ + if( m_map_client.find(fd) == m_map_client.end()){ + if(nullptr != p) + m_map_client[fd] = p; + else + return -1; + } + return 0; +} +/** + * @description: + * @param {*} + * @return {*} + */ +int TcpServerLibevent::RemoveConnection(uint32_t fd){ +if( m_map_client.find(fd) != m_map_client.end()){ + m_map_client.erase(fd); + return 0; + }else{ + return -1; + } +} +/** + * @description: + * @param {*} + * @return {*} + */ +int TcpServerLibevent::SetNewConnectionHandle(OnAccept p){ + m_handle_accept = p; + return 0; +} +/** + * @description: + * @param {int} ports + * @param {string} bindip + * @return {*} + */ +TcpServerLibevent::TcpServerLibevent(int port,string bindip) : + m_thread(nullptr), + m_event_base(nullptr), + m_event_listener(nullptr) +{ + m_handle_accept = defaultConnAccept; + m_backlog = 10000; + this->m_bind_ip = bindip; + this->m_port = port; + + memset(&m_server_addr, 0, sizeof(m_server_addr)); + m_server_addr.sin_family = AF_INET; + m_server_addr.sin_port = htons(port); + m_server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + + // 创建 event_base + m_event_base = event_base_new(); + if(NULL == m_event_base){ + return; + } + + m_event_listener = evconnlistener_new_bind(m_event_base, + cb_listener, + this, + LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, + m_backlog, + (struct sockaddr*)&m_server_addr, + sizeof(m_server_addr)); + if(NULL == m_event_listener) + { + m_status = FAIL; + } + m_status = STOP; +} +/** + * @description: start server synchronous + * @param {*} + * @return {*} + */ +int TcpServerLibevent::StartServerSync(){ + if(m_status == STOP){ + m_status = RUNNING; + event_base_dispatch(m_event_base); + evconnlistener_free(m_event_listener); + event_base_free(m_event_base); + return 0; + } + return -1; +} +/** + * @description: start server asynchronous + * @param {*} + * @return {*} + */ +int TcpServerLibevent::StartServerAsync(){ + if(m_status == STOP){ +#ifdef WIN32 + evthread_use_windows_threads(); +#endif +#ifdef linux + evthread_use_pthreads(); +#endif + m_thread = new thread(server_run,this); + m_thread->detach(); + return 0; + } + return -1; +} +/** + * @description: start server asynchronous + * @param {*} + * @return {*} + */ +TcpServerLibevent::~TcpServerLibevent(){ + if(this->m_status == RUNNING){ + m_thread->detach(); + event_base_loopbreak(m_event_base); + this->m_status = STOP; + } +} diff --git a/protoDebuger/tcp_server_libevent.h b/protoDebuger/tcp_server_libevent.h new file mode 100644 index 0000000..57658ee --- /dev/null +++ b/protoDebuger/tcp_server_libevent.h @@ -0,0 +1,107 @@ + +/* + * @Author: your name + * @Date: 2021-06-30 16:23:10 + * @LastEditTime: 2021-07-15 22:29:01 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edit + * @FilePath: \server\tcp_server_libevent.h + */ + +#ifndef GENERAL_TCPSERVER_H +#define GENERAL_TCPSERVER_H + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0500 +#endif +#ifdef linux +#include +#include +#include +#define EVENT__HAVE_PTHREADS +#endif + +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" + #include +}; + +#include +#include +#include +#include + +using namespace std; + +class TcpServerLibevent; +// tcp 连接 +class ConnectionLibevent{ +public: + ConnectionLibevent(TcpServerLibevent *p, + struct bufferevent*v, + uint32_t fd, + struct sockaddr_in *p1); + ConnectionLibevent(struct bufferevent*v, + uint32_t fd, + struct sockaddr_in *p1); + virtual int OnRecv(char *p,uint32_t len); // 接收到 + virtual int OnClose(); // 接收到 + virtual int OnWrite(); + int WriteData(const char *p,uint16_t len); + int SetServer(TcpServerLibevent *); + TcpServerLibevent *Server(); + string IpAddress(); + uint32_t SocketFd(); +private: + int m_bytes_send; + int m_bytes_recv; + TcpServerLibevent *m_parent_server; + struct bufferevent *m_event; + struct sockaddr_in *m_addr; + uint32_t m_fd; +}; + +// 管理服务端 +class TcpServerLibevent{ +typedef enum{ + RUNNING, + STOP, + FAIL +}SERVER_STATUS; +public: + typedef ConnectionLibevent* (*OnAccept)(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1); + TcpServerLibevent(int port,string bindip); + SERVER_STATUS Status(); + ~TcpServerLibevent(); + int StartServerSync(); // 同步启动服务器 + int StartServerAsync(); // 异步启动服务 + int RemoveConnection(uint32_t ); + int SetNewConnectionHandle(OnAccept ); + friend void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr); + friend void read_cb(struct bufferevent *bev, void *arg); + friend void event_cb(struct bufferevent *bev, short events, void *arg); + friend void write_cb(struct bufferevent *bev, void *arg); + friend void server_run(TcpServerLibevent *p); + +private: + uint32_t m_port; // 监听端口号 + string m_bind_ip; // 绑定端口号 + int m_current_conection; // 当前连接数目 + uint16_t m_backlog; + struct sockaddr_in m_server_addr; // 服务器地址 + struct event_base * m_event_base; + struct evconnlistener* m_event_listener; + SERVER_STATUS m_status; + thread *m_thread; + map m_map_client; + OnAccept m_handle_accept; + int AddConnection(uint32_t fd,ConnectionLibevent *p); +}; + + +#endif \ No newline at end of file diff --git a/protoDebuger/tcp_swarm_libevent.cpp b/protoDebuger/tcp_swarm_libevent.cpp index de39818..e5eb0ca 100644 --- a/protoDebuger/tcp_swarm_libevent.cpp +++ b/protoDebuger/tcp_swarm_libevent.cpp @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-07-04 16:06:47 - * @LastEditTime: 2021-07-15 22:52:22 + * @LastEditTime: 2021-08-29 01:00:36 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \server\tcp_swarm_libevent.cpp @@ -14,10 +14,9 @@ void conn_writecb(struct bufferevent *, void *); void conn_readcb(struct bufferevent *, void *); void conn_eventcb(struct bufferevent *, short, void *); -void conn_writecb(struct bufferevent *bev, void *user_data) { - +void conn_writecb(struct bufferevent *bev, void *user_data) +{ } - // 运行线程 int thread_dispatch(TcpSwarmClientLibevent *p) { if (nullptr != p) { @@ -59,6 +58,8 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data) { evutil_socket_t fd = bufferevent_getfd(bev); server->addConection(fd,bev); + std::cout << "conect new fd " << fd << std::endl; + bufferevent_write(bev, string("hello world").c_str(), strlen("hello world")); } }