@@ -74,7 +74,7 @@ void TC_SerialPortGroup::erase(const shared_ptr<TC_SerialPort> & sp)
74
74
{
75
75
if (sp)
76
76
{
77
- // sp->close();
77
+ sp->close ();
78
78
79
79
std::lock_guard<std::recursive_mutex> lock (_mutex);
80
80
_serialPorts.erase (sp->options ().portName );
@@ -273,6 +273,8 @@ TC_SerialPort::~TC_SerialPort()
273
273
274
274
void TC_SerialPort::close ()
275
275
{
276
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
277
+
276
278
if (!isValid ())
277
279
{
278
280
return ;
@@ -304,6 +306,7 @@ void TC_SerialPort::close()
304
306
305
307
if (_serialFd != INVALID_HANDLE_VALUE)
306
308
{
309
+ CreateIoCompletionPort (_serialFd, NULL , 0 , 0 );
307
310
308
311
CloseHandle (_serialFd);
309
312
_serialFd = INVALID_HANDLE_VALUE;
@@ -509,19 +512,19 @@ void TC_SerialPort::initialize()
509
512
510
513
void TC_SerialPort::setParserCallback (const onparser_callback & onparser)
511
514
{
512
- std::lock_guard<std::mutex > lock (_mutex);
515
+ std::lock_guard<std::recursive_mutex > lock (_mutex);
513
516
_onParserCallback = onparser;
514
517
}
515
518
516
519
void TC_SerialPort::setRequestCallback (const RequestCallbackPtr & callbackPtr)
517
520
{
518
- std::lock_guard<std::mutex > lock (_mutex);
521
+ std::lock_guard<std::recursive_mutex > lock (_mutex);
519
522
_callbackPtr = callbackPtr;
520
523
}
521
524
522
525
TC_SerialPort::RequestCallbackPtr TC_SerialPort::getRequestCallbackPtr ()
523
526
{
524
- std::lock_guard<std::mutex > lock (_mutex);
527
+ std::lock_guard<std::recursive_mutex > lock (_mutex);
525
528
return _callbackPtr;
526
529
}
527
530
@@ -534,9 +537,13 @@ void TC_SerialPort::sendRequest(const char* sBuffer, size_t length, bool header)
534
537
535
538
buff->addBuffer (sBuffer , length);
536
539
537
- if (!isValid ())
538
540
{
539
- initialize ();
541
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
542
+
543
+ if (!isValid ())
544
+ {
545
+ initialize ();
546
+ }
540
547
}
541
548
addSendReqBuffer (buff, header);
542
549
}
@@ -546,9 +553,13 @@ void TC_SerialPort::sendRequest(const shared_ptr<TC_NetWorkBuffer::Buffer> & buf
546
553
if (buff && buff->empty ())
547
554
return ;
548
555
549
- if (!isValid ())
550
556
{
551
- initialize ();
557
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
558
+
559
+ if (!isValid ())
560
+ {
561
+ initialize ();
562
+ }
552
563
}
553
564
addSendReqBuffer (buff, header);
554
565
}
@@ -595,7 +606,7 @@ void TC_SerialPort::notify(vector<char> && response)
595
606
596
607
void TC_SerialPort::addSendReqBuffer (const shared_ptr<TC_NetWorkBuffer::Buffer> & reqBuffer, bool header)
597
608
{
598
- std::lock_guard<std::mutex > lock (_mutex);
609
+ std::lock_guard<std::recursive_mutex > lock (_mutex);
599
610
600
611
if (header)
601
612
{
@@ -612,7 +623,6 @@ void TC_SerialPort::addSendReqBuffer(const shared_ptr<TC_NetWorkBuffer::Buffer>
612
623
#else
613
624
_serialPortGroup->getEpoller ().notify ();
614
625
#endif
615
-
616
626
}
617
627
618
628
int TC_SerialPort::doProtocolAnalysis (TC_NetWorkBuffer *buff)
@@ -630,10 +640,7 @@ int TC_SerialPort::doProtocolAnalysis(TC_NetWorkBuffer *buff)
630
640
vector<char > out;
631
641
ioriginal = buff->getBufferLength ();
632
642
633
- {
634
- std::lock_guard<std::mutex> lock (_mutex);
635
- ret = _onParserCallback (*buff, out);
636
- }
643
+ ret = _onParserCallback (*buff, out);
637
644
isurplus = buff->getBufferLength ();
638
645
639
646
if (ret == TC_NetWorkBuffer::PACKET_FULL || ret == TC_NetWorkBuffer::PACKET_FULL_CLOSE)
@@ -691,6 +698,11 @@ bool TC_SerialPort::handleCloseImp()
691
698
692
699
bool TC_SerialPort::handleInputImp (const shared_ptr<TC_Epoller::EpollInfo> & epollInfo)
693
700
{
701
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
702
+ if (!isValid ())
703
+ {
704
+ return false ;
705
+ }
694
706
try
695
707
{
696
708
// 串口读取数据
@@ -748,6 +760,11 @@ bool TC_SerialPort::handleInputImp()
748
760
{
749
761
try
750
762
{
763
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
764
+ if (!isValid ())
765
+ {
766
+ return false ;
767
+ }
751
768
this ->recv ();
752
769
}
753
770
catch (exception & ex)
@@ -769,9 +786,9 @@ void TC_SerialPort::onRequestCallback()
769
786
for (;;)
770
787
{
771
788
decltype (_messages)::iterator it;
789
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
772
790
773
791
{
774
- std::lock_guard<std::mutex> lock (_mutex);
775
792
if (_messages.empty ())
776
793
{
777
794
return ;
@@ -788,8 +805,6 @@ void TC_SerialPort::onRequestCallback()
788
805
789
806
if (iRet != eRetNotSend)
790
807
{
791
- std::lock_guard<std::mutex> lock (_mutex);
792
-
793
808
_messages.erase (it);
794
809
}
795
810
@@ -828,13 +843,25 @@ bool TC_SerialPort::handleOutputImp()
828
843
#if TARGET_PLATFORM_WINDOWS
829
844
void TC_SerialPort::sendSucc (uint32_t len)
830
845
{
846
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
847
+ if (!isValid ())
848
+ {
849
+ return ;
850
+ }
851
+
831
852
_sendBuffer.moveHeader (len);
832
853
}
833
854
834
855
void TC_SerialPort::recvSucc (uint32_t len)
835
856
{
836
857
try
837
858
{
859
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
860
+ if (!isValid ())
861
+ {
862
+ return ;
863
+ }
864
+
838
865
assert (!_buffRecv.empty ());
839
866
840
867
auto it = _buffRecv.begin ();
@@ -860,7 +887,6 @@ void TC_SerialPort::recvSucc(uint32_t len)
860
887
if (callback)
861
888
{
862
889
callback->onFailed (ex.what ());
863
- // callback->onClose();
864
890
}
865
891
}
866
892
@@ -919,6 +945,7 @@ TC_SerialPort::ReturnStatus TC_SerialPort::writeBuffer(const shared_ptr<TC_NetWo
919
945
920
946
void TC_SerialPort::doRequest ()
921
947
{
948
+ std::lock_guard<std::recursive_mutex> lock (_mutex);
922
949
// buf不为空,先发送buffer的内容
923
950
while (!_sendBuffer.empty ())
924
951
{
@@ -944,11 +971,7 @@ void TC_SerialPort::doRequest()
944
971
int TC_SerialPort::send (const void *buf, uint32_t len)
945
972
{
946
973
#if TARGET_PLATFORM_WINDOWS
947
- if (!isValid ())
948
- {
949
- return 0 ;
950
- }
951
-
974
+
952
975
if (len == 0 )
953
976
{
954
977
return 0 ;
@@ -1005,10 +1028,7 @@ int TC_SerialPort::send(const void *buf, uint32_t len)
1005
1028
1006
1029
int TC_SerialPort::recv ()
1007
1030
{
1008
- if (!isValid ())
1009
- {
1010
- return 0 ;
1011
- }
1031
+
1012
1032
DWORD dwErrorFlags;
1013
1033
COMSTAT ComStat;
1014
1034
DWORD dwBytesRead;
0 commit comments