Skip to content

Commit aba0171

Browse files
committed
fix: serial port crash when close and reopen
1 parent 553e5ad commit aba0171

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed

mock/FrameworkServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bool FrameworkServer::loadLogFormat(const string& command, const string& params,
7070

7171
sort(vHourlist.begin(),vHourlist.end());
7272

73-
unique(vHourlist.begin(),vHourlist.end());
73+
vHourlist.erase(unique(vHourlist.begin(),vHourlist.end()),vHourlist.end());
7474

7575
result = "loadLogFormat succ:" + sHour;
7676

servant/libservant/StatReport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ void StatReport::resetStatInterv()
170170
_timePoint.push_back(2000);
171171
_timePoint.push_back(3000);
172172

173-
sort(_timePoint.begin(),_timePoint.end());
173+
// sort(_timePoint.begin(),_timePoint.end());
174174

175-
unique(_timePoint.begin(),_timePoint.end());
175+
// unique(_timePoint.begin(),_timePoint.end());
176176
}
177177

178178
string StatReport::trimAndLimitStr(const string& str, uint32_t limitlen)

util/include/util/tc_serialport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class UTIL_DLL_API TC_SerialPort
144144
virtual void onClose() = 0;
145145

146146
/**
147-
* @brief 心跳回调, 在串口通信线程中调用, 每当有收发数据时, 都会回调, 最长不超过
147+
* @brief 心跳回调, 在串口通信线程中调用, 每当有收发数据时, 都会回调, 最长不超过_heartbeatMaxInterval时间没有收发数据, 会回调onHeartbeat
148148
*/
149149
virtual void onHeartbeat() {};
150150
};
@@ -389,7 +389,7 @@ class UTIL_DLL_API TC_SerialPort
389389
/**
390390
* 互斥量
391391
*/
392-
std::mutex _mutex;
392+
std::recursive_mutex _mutex;
393393

394394
/**
395395
* 等待互斥量

util/src/tc_serialport.cpp

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void TC_SerialPortGroup::erase(const shared_ptr<TC_SerialPort> & sp)
7474
{
7575
if(sp)
7676
{
77-
// sp->close();
77+
sp->close();
7878

7979
std::lock_guard<std::recursive_mutex> lock(_mutex);
8080
_serialPorts.erase(sp->options().portName);
@@ -273,6 +273,8 @@ TC_SerialPort::~TC_SerialPort()
273273

274274
void TC_SerialPort::close()
275275
{
276+
std::lock_guard<std::recursive_mutex> lock(_mutex);
277+
276278
if(!isValid())
277279
{
278280
return;
@@ -304,6 +306,7 @@ void TC_SerialPort::close()
304306

305307
if(_serialFd != INVALID_HANDLE_VALUE)
306308
{
309+
CreateIoCompletionPort(_serialFd, NULL, 0, 0);
307310

308311
CloseHandle(_serialFd);
309312
_serialFd = INVALID_HANDLE_VALUE;
@@ -509,19 +512,19 @@ void TC_SerialPort::initialize()
509512

510513
void TC_SerialPort::setParserCallback(const onparser_callback & onparser)
511514
{
512-
std::lock_guard<std::mutex> lock(_mutex);
515+
std::lock_guard<std::recursive_mutex> lock(_mutex);
513516
_onParserCallback = onparser;
514517
}
515518

516519
void TC_SerialPort::setRequestCallback(const RequestCallbackPtr & callbackPtr)
517520
{
518-
std::lock_guard<std::mutex> lock(_mutex);
521+
std::lock_guard<std::recursive_mutex> lock(_mutex);
519522
_callbackPtr = callbackPtr;
520523
}
521524

522525
TC_SerialPort::RequestCallbackPtr TC_SerialPort::getRequestCallbackPtr()
523526
{
524-
std::lock_guard<std::mutex> lock(_mutex);
527+
std::lock_guard<std::recursive_mutex> lock(_mutex);
525528
return _callbackPtr;
526529
}
527530

@@ -534,9 +537,13 @@ void TC_SerialPort::sendRequest(const char* sBuffer, size_t length, bool header)
534537

535538
buff->addBuffer(sBuffer, length);
536539

537-
if(!isValid())
538540
{
539-
initialize();
541+
std::lock_guard<std::recursive_mutex> lock(_mutex);
542+
543+
if(!isValid())
544+
{
545+
initialize();
546+
}
540547
}
541548
addSendReqBuffer(buff, header);
542549
}
@@ -546,9 +553,13 @@ void TC_SerialPort::sendRequest(const shared_ptr<TC_NetWorkBuffer::Buffer> & buf
546553
if (buff && buff->empty())
547554
return;
548555

549-
if(!isValid())
550556
{
551-
initialize();
557+
std::lock_guard<std::recursive_mutex> lock(_mutex);
558+
559+
if(!isValid())
560+
{
561+
initialize();
562+
}
552563
}
553564
addSendReqBuffer(buff, header);
554565
}
@@ -595,7 +606,7 @@ void TC_SerialPort::notify(vector<char> && response)
595606

596607
void TC_SerialPort::addSendReqBuffer(const shared_ptr<TC_NetWorkBuffer::Buffer> & reqBuffer, bool header)
597608
{
598-
std::lock_guard<std::mutex> lock(_mutex);
609+
std::lock_guard<std::recursive_mutex> lock(_mutex);
599610

600611
if (header)
601612
{
@@ -612,7 +623,6 @@ void TC_SerialPort::addSendReqBuffer(const shared_ptr<TC_NetWorkBuffer::Buffer>
612623
#else
613624
_serialPortGroup->getEpoller().notify();
614625
#endif
615-
616626
}
617627

618628
int TC_SerialPort::doProtocolAnalysis(TC_NetWorkBuffer *buff)
@@ -630,10 +640,7 @@ int TC_SerialPort::doProtocolAnalysis(TC_NetWorkBuffer *buff)
630640
vector<char> out;
631641
ioriginal = buff->getBufferLength();
632642

633-
{
634-
std::lock_guard<std::mutex> lock(_mutex);
635-
ret = _onParserCallback(*buff, out);
636-
}
643+
ret = _onParserCallback(*buff, out);
637644
isurplus = buff->getBufferLength();
638645

639646
if (ret == TC_NetWorkBuffer::PACKET_FULL || ret == TC_NetWorkBuffer::PACKET_FULL_CLOSE)
@@ -691,6 +698,11 @@ bool TC_SerialPort::handleCloseImp()
691698

692699
bool TC_SerialPort::handleInputImp(const shared_ptr<TC_Epoller::EpollInfo> & epollInfo)
693700
{
701+
std::lock_guard<std::recursive_mutex> lock(_mutex);
702+
if(!isValid())
703+
{
704+
return false;
705+
}
694706
try
695707
{
696708
//串口读取数据
@@ -748,6 +760,11 @@ bool TC_SerialPort::handleInputImp()
748760
{
749761
try
750762
{
763+
std::lock_guard<std::recursive_mutex> lock(_mutex);
764+
if(!isValid())
765+
{
766+
return false;
767+
}
751768
this->recv();
752769
}
753770
catch (exception & ex)
@@ -769,9 +786,9 @@ void TC_SerialPort::onRequestCallback()
769786
for (;;)
770787
{
771788
decltype(_messages)::iterator it;
789+
std::lock_guard<std::recursive_mutex> lock(_mutex);
772790

773791
{
774-
std::lock_guard<std::mutex> lock(_mutex);
775792
if (_messages.empty())
776793
{
777794
return;
@@ -788,8 +805,6 @@ void TC_SerialPort::onRequestCallback()
788805

789806
if (iRet != eRetNotSend)
790807
{
791-
std::lock_guard<std::mutex> lock(_mutex);
792-
793808
_messages.erase(it);
794809
}
795810

@@ -828,13 +843,25 @@ bool TC_SerialPort::handleOutputImp()
828843
#if TARGET_PLATFORM_WINDOWS
829844
void TC_SerialPort::sendSucc(uint32_t len)
830845
{
846+
std::lock_guard<std::recursive_mutex> lock(_mutex);
847+
if(!isValid())
848+
{
849+
return;
850+
}
851+
831852
_sendBuffer.moveHeader(len);
832853
}
833854

834855
void TC_SerialPort::recvSucc(uint32_t len)
835856
{
836857
try
837858
{
859+
std::lock_guard<std::recursive_mutex> lock(_mutex);
860+
if(!isValid())
861+
{
862+
return;
863+
}
864+
838865
assert(!_buffRecv.empty());
839866

840867
auto it = _buffRecv.begin();
@@ -860,7 +887,6 @@ void TC_SerialPort::recvSucc(uint32_t len)
860887
if (callback)
861888
{
862889
callback->onFailed(ex.what());
863-
// callback->onClose();
864890
}
865891
}
866892

@@ -919,6 +945,7 @@ TC_SerialPort::ReturnStatus TC_SerialPort::writeBuffer(const shared_ptr<TC_NetWo
919945

920946
void TC_SerialPort::doRequest()
921947
{
948+
std::lock_guard<std::recursive_mutex> lock(_mutex);
922949
//buf不为空,先发送buffer的内容
923950
while (!_sendBuffer.empty())
924951
{
@@ -944,11 +971,7 @@ void TC_SerialPort::doRequest()
944971
int TC_SerialPort::send(const void *buf, uint32_t len)
945972
{
946973
#if TARGET_PLATFORM_WINDOWS
947-
if(!isValid())
948-
{
949-
return 0;
950-
}
951-
974+
952975
if(len == 0)
953976
{
954977
return 0;
@@ -1005,10 +1028,7 @@ int TC_SerialPort::send(const void *buf, uint32_t len)
10051028

10061029
int TC_SerialPort::recv()
10071030
{
1008-
if(!isValid())
1009-
{
1010-
return 0;
1011-
}
1031+
10121032
DWORD dwErrorFlags;
10131033
COMSTAT ComStat;
10141034
DWORD dwBytesRead;

0 commit comments

Comments
 (0)