no message
parent
d7c5d0e889
commit
62c9df87c8
|
@ -224,7 +224,7 @@ Item {
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if(button_network.text == "正在监听"){
|
if(button_network.text == "正在监听"){
|
||||||
button_close.visible = false
|
button_close.visible = false
|
||||||
DataWrap.closeNetwork();
|
DataWrap.closeNetwork("tcpserver");
|
||||||
button_network.text = "打开网络"
|
button_network.text = "打开网络"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,6 @@ signals:
|
||||||
void on_send_data(QByteArray);
|
void on_send_data(QByteArray);
|
||||||
private:
|
private:
|
||||||
NetworkType _checkType(NetworkType);
|
NetworkType _checkType(NetworkType);
|
||||||
|
|
||||||
NetworkType mType;
|
NetworkType mType;
|
||||||
QUdpSocket *mUDP;
|
QUdpSocket *mUDP;
|
||||||
QTcpSocket *mTcp;
|
QTcpSocket *mTcp;
|
||||||
|
|
|
@ -117,9 +117,18 @@ int QmlShareData::openNetwork(QString ip, unsigned int port, bool is_ws,int type
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int QmlShareData::closeNetwork()
|
int QmlShareData::closeNetwork(QString type)
|
||||||
{
|
{
|
||||||
m_network_->Close();
|
if(type == "tcpserver"){
|
||||||
|
if(nullptr != m_tcp_server){
|
||||||
|
m_tcp_server->StopServer();
|
||||||
|
delete m_tcp_server;
|
||||||
|
m_tcp_server = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_network_->Close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int QmlShareData::closeSerial()
|
int QmlShareData::closeSerial()
|
||||||
|
|
|
@ -40,7 +40,7 @@ public:
|
||||||
Q_INVOKABLE int openUart(QString port,QString baudRate,QString dataBits,QString stopBits,
|
Q_INVOKABLE int openUart(QString port,QString baudRate,QString dataBits,QString stopBits,
|
||||||
QString flow);
|
QString flow);
|
||||||
Q_INVOKABLE int openNetwork(QString ip,unsigned int port,bool is_ws,int type);
|
Q_INVOKABLE int openNetwork(QString ip,unsigned int port,bool is_ws,int type);
|
||||||
Q_INVOKABLE int closeNetwork();
|
Q_INVOKABLE int closeNetwork(QString);
|
||||||
|
|
||||||
Q_INVOKABLE int closeSerial();
|
Q_INVOKABLE int closeSerial();
|
||||||
Q_INVOKABLE int TestLua();
|
Q_INVOKABLE int TestLua();
|
||||||
|
|
|
@ -176,14 +176,12 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc
|
||||||
*/
|
*/
|
||||||
void server_run(TcpServerLibevent *p){
|
void server_run(TcpServerLibevent *p){
|
||||||
if(nullptr != p){
|
if(nullptr != p){
|
||||||
if(p->m_status == TcpServerLibevent::STOP){
|
while(p->m_status == TcpServerLibevent::RUNNING){
|
||||||
p->m_status = TcpServerLibevent::RUNNING;
|
int ret =event_base_loop(p->m_event_base,EVLOOP_NONBLOCK);
|
||||||
int ret =event_base_dispatch(p->m_event_base);
|
|
||||||
if(ret < 0){
|
if(ret < 0){
|
||||||
qDebug()<<"libevent ret is "<<ret;
|
p->m_status = TcpServerLibevent::FAIL;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
evconnlistener_free(p->m_event_listener);
|
|
||||||
event_base_free(p->m_event_base);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -261,7 +259,7 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) :
|
||||||
m_event_listener = evconnlistener_new_bind(m_event_base,
|
m_event_listener = evconnlistener_new_bind(m_event_base,
|
||||||
cb_listener,
|
cb_listener,
|
||||||
this,
|
this,
|
||||||
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
|
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE|LEV_OPT_THREADSAFE,
|
||||||
m_backlog,
|
m_backlog,
|
||||||
(struct sockaddr*)&m_server_addr,
|
(struct sockaddr*)&m_server_addr,
|
||||||
sizeof(m_server_addr));
|
sizeof(m_server_addr));
|
||||||
|
@ -279,10 +277,9 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) :
|
||||||
int TcpServerLibevent::StartServerSync(){
|
int TcpServerLibevent::StartServerSync(){
|
||||||
if(m_status == STOP){
|
if(m_status == STOP){
|
||||||
m_status = RUNNING;
|
m_status = RUNNING;
|
||||||
if( -1 == event_base_dispatch(m_event_base)){
|
if(-1 == event_base_dispatch(m_event_base)){
|
||||||
qDebug()<<"error ";
|
qDebug()<<"error ";
|
||||||
}
|
}
|
||||||
evconnlistener_free(m_event_listener);
|
|
||||||
event_base_free(m_event_base);
|
event_base_free(m_event_base);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -301,11 +298,37 @@ int TcpServerLibevent::StartServerAsync(){
|
||||||
#ifdef linux
|
#ifdef linux
|
||||||
evthread_use_pthreads();
|
evthread_use_pthreads();
|
||||||
#endif
|
#endif
|
||||||
|
m_status = RUNNING;
|
||||||
|
|
||||||
m_thread = new std::thread(server_run,this);
|
m_thread = new std::thread(server_run,this);
|
||||||
m_thread->detach();
|
m_thread->detach();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TcpServerLibevent::StopServer()
|
||||||
|
{
|
||||||
|
struct timeval v;
|
||||||
|
v.tv_usec = 1000;
|
||||||
|
v.tv_sec = 0;
|
||||||
|
|
||||||
|
if(m_status == RUNNING){
|
||||||
|
int ret = event_base_loopexit(m_event_base,&v);
|
||||||
|
if (ret < 0){
|
||||||
|
// todo write log
|
||||||
|
|
||||||
|
}
|
||||||
|
evconnlistener_free(this->m_event_listener);
|
||||||
|
if(ret < 0){
|
||||||
|
// todo write log
|
||||||
|
qDebug()<<"evconnlistener_disable"<<ret;
|
||||||
|
|
||||||
|
}
|
||||||
|
m_status = STOP;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @description: start server asynchronous
|
* @description: start server asynchronous
|
||||||
|
@ -315,7 +338,21 @@ int TcpServerLibevent::StartServerAsync(){
|
||||||
TcpServerLibevent::~TcpServerLibevent(){
|
TcpServerLibevent::~TcpServerLibevent(){
|
||||||
if(this->m_status == RUNNING){
|
if(this->m_status == RUNNING){
|
||||||
m_thread->detach();
|
m_thread->detach();
|
||||||
event_base_loopbreak(m_event_base);
|
struct timeval v;
|
||||||
this->m_status = STOP;
|
v.tv_usec = 1000;
|
||||||
|
v.tv_sec = 1;
|
||||||
|
|
||||||
|
int ret = event_base_loopexit(m_event_base,&v);
|
||||||
|
this->m_status = STOP;
|
||||||
}
|
}
|
||||||
|
if(nullptr != m_event_base){
|
||||||
|
event_base_free(m_event_base);
|
||||||
|
delete m_event_base;
|
||||||
|
m_event_base = nullptr;
|
||||||
|
}
|
||||||
|
if(nullptr != m_event_listener){
|
||||||
|
delete m_event_listener;
|
||||||
|
m_event_listener = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,6 +101,7 @@ public:
|
||||||
~TcpServerLibevent();
|
~TcpServerLibevent();
|
||||||
int StartServerSync(); // 同步启动服务器
|
int StartServerSync(); // 同步启动服务器
|
||||||
int StartServerAsync(); // 异步启动服务
|
int StartServerAsync(); // 异步启动服务
|
||||||
|
int StopServer();
|
||||||
int RemoveConnection(uint32_t );
|
int RemoveConnection(uint32_t );
|
||||||
int SetNewConnectionHandle(OnAccept );
|
int SetNewConnectionHandle(OnAccept );
|
||||||
friend void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr);
|
friend void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr);
|
||||||
|
@ -109,6 +110,12 @@ public:
|
||||||
friend void write_cb(struct bufferevent *bev, void *arg);
|
friend void write_cb(struct bufferevent *bev, void *arg);
|
||||||
friend void server_run(TcpServerLibevent *p);
|
friend void server_run(TcpServerLibevent *p);
|
||||||
|
|
||||||
|
#ifdef Qt_Support
|
||||||
|
signals:
|
||||||
|
int on_onnected(string ip,uint sock);
|
||||||
|
int on_disconected(string ip,uint sock);
|
||||||
|
|
||||||
|
#endif
|
||||||
private:
|
private:
|
||||||
uint32_t m_port; // 监听端口号
|
uint32_t m_port; // 监听端口号
|
||||||
string m_bind_ip; // 绑定端口号
|
string m_bind_ip; // 绑定端口号
|
||||||
|
|
Loading…
Reference in New Issue