Skip to content

Commit bda56fb

Browse files
committed
deps: snap7 -> 1.3.1
1 parent 40b11de commit bda56fb

6 files changed

Lines changed: 74 additions & 27 deletions

File tree

deps/snap7/src/core/s7_client.cpp

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@ TSnap7Client::TSnap7Client()
3030
{
3131
FThread = 0;
3232
CliCompletion = 0;
33-
EvtJob = new TSnapEvent(false);
34-
EvtComplete = new TSnapEvent(false);
35-
OpenThread();
33+
EvtJob = NULL;
34+
EvtComplete = NULL;
35+
FThread=NULL;
36+
ThreadCreated = false;
3637
}
3738
//---------------------------------------------------------------------------
3839
TSnap7Client::~TSnap7Client()
3940
{
4041
Destroying=true;
4142
Disconnect();
4243
CliCompletion=NULL;
43-
CloseThread();
44-
delete EvtComplete;
45-
delete EvtJob;
44+
if (ThreadCreated)
45+
{
46+
CloseThread();
47+
delete EvtComplete;
48+
delete EvtJob;
49+
ThreadCreated=false;
50+
}
4651
}
4752
//---------------------------------------------------------------------------
4853
void TSnap7Client::CloseThread()
@@ -57,13 +62,14 @@ void TSnap7Client::CloseThread()
5762
else
5863
Timeout=1000;
5964
EvtJob->Set();
60-
if (FThread->WaitFor(Timeout)!=WAIT_OBJECT_0)
65+
if (FThread->WaitFor(Timeout)!=WAIT_OBJECT_0)
6166
FThread->Kill();
6267
try {
6368
delete FThread;
6469
}
6570
catch (...){
6671
}
72+
6773
FThread=0;
6874
}
6975
}
@@ -77,10 +83,16 @@ void TSnap7Client::OpenThread()
7783
int TSnap7Client::Reset(bool DoReconnect)
7884
{
7985
bool WasConnected = Connected;
80-
CloseThread();
81-
Disconnect();
82-
OpenThread();
83-
if (DoReconnect || WasConnected)
86+
if (ThreadCreated)
87+
{
88+
CloseThread();
89+
Disconnect();
90+
OpenThread();
91+
}
92+
else
93+
Disconnect();
94+
95+
if (DoReconnect || WasConnected)
8496
return Connect();
8597
else
8698
return 0;
@@ -440,23 +452,35 @@ int TSnap7Client::AsDBFill(int DBNumber, int FillChar)
440452
void TSnap7Client::StartAsyncJob()
441453
{
442454
ClrError();
443-
EvtComplete->Reset(); // reset if previously was not called WaitAsCompletion
455+
if (!ThreadCreated)
456+
{
457+
EvtJob = new TSnapEvent(false);
458+
EvtComplete = new TSnapEvent(false);
459+
OpenThread();
460+
ThreadCreated=true;
461+
}
462+
EvtComplete->Reset(); // reset if previously was not called WaitAsCompletion
444463
EvtJob->Set();
445464
}
446465
//---------------------------------------------------------------------------
447466
int TSnap7Client::WaitAsCompletion(unsigned long Timeout)
448467
{
449468
if (Job.Pending)
450469
{
451-
if (EvtComplete->WaitFor(Timeout)==WAIT_OBJECT_0)
452-
return Job.Result;
453-
else
454-
{
455-
if (Destroying)
456-
return errCliDestroying;
457-
else
458-
return SetError(errCliJobTimeout);
459-
}
470+
if (ThreadCreated)
471+
{
472+
if (EvtComplete->WaitFor(Timeout)==WAIT_OBJECT_0)
473+
return Job.Result;
474+
else
475+
{
476+
if (Destroying)
477+
return errCliDestroying;
478+
else
479+
return SetError(errCliJobTimeout);
480+
}
481+
}
482+
else
483+
return SetError(errCliJobTimeout);
460484
}
461485
else
462486
return Job.Result;

deps/snap7/src/core/s7_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TSnap7Client: public TSnap7MicroClient
5151
{
5252
private:
5353
TClientThread *FThread;
54+
bool ThreadCreated;
5455
void CloseThread();
5556
void OpenThread();
5657
void StartAsyncJob();

deps/snap7/src/core/s7_micro_client.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ int TSnap7MicroClient::opReadArea()
7373
// First check : params bounds
7474
if ((Job.Number<0) || (Job.Number>65535) || (Job.Start<0) || (Job.Amount<1))
7575
return errCliInvalidParams;
76+
// Second check : transport size
77+
if ((Job.WordLen==S7WLBit) && (Job.Amount>1))
78+
return errCliInvalidTransportSize;
7679
// Request Params size
7780
RPSize =sizeof(TReqFunReadItem)+2; // 1 item + FunRead + ItemsCount
7881
// Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
@@ -181,6 +184,9 @@ int TSnap7MicroClient::opWriteArea()
181184
// First check : params bounds
182185
if ((Job.Number<0) || (Job.Number>65535) || (Job.Start<0) || (Job.Amount<1))
183186
return errCliInvalidParams;
187+
// Second check : transport size
188+
if ((Job.WordLen==S7WLBit) && (Job.Amount>1))
189+
return errCliInvalidTransportSize;
184190

185191
RHSize =sizeof(TS7ReqHeader)+ // Request header
186192
2+ // FunWrite+ItemCount (of TReqFunWriteParams)
@@ -382,7 +388,11 @@ int TSnap7MicroClient::opReadMultiVars()
382388
IsoSize=RPSize+sizeof(TS7ReqHeader);
383389
if (IsoSize>PDULength)
384390
return errCliSizeOverPDU;
385-
Result=isoExchangeBuffer(0,IsoSize);
391+
Result=isoExchangeBuffer(0,IsoSize);
392+
393+
if (Result!=0)
394+
return Result;
395+
386396
// Function level error
387397
if (Answer->Error!=0)
388398
return CpuError(SwapWord(Answer->Error));
@@ -550,9 +560,13 @@ int TSnap7MicroClient::opWriteMultiVars()
550560
if (IsoSize>PDULength)
551561
return errCliSizeOverPDU;
552562
Result=isoExchangeBuffer(0,IsoSize);
553-
// Function level error
554-
if (Answer->Error!=0)
555-
return CpuError(SwapWord(Answer->Error));
563+
564+
if (Result!=0)
565+
return Result;
566+
567+
// Function level error
568+
if (Answer->Error!=0)
569+
return CpuError(SwapWord(Answer->Error));
556570

557571
if (ResParams->ItemCount!=ItemsCount)
558572
return errCliInvalidPlcAnswer;
@@ -1198,7 +1212,7 @@ int TSnap7MicroClient::opUpload()
11981212
if (Full)
11991213
{
12001214
opSize=int(Offset);
1201-
if (opSize<92)
1215+
if (opSize<78)
12021216
Result=errCliInvalidDataSizeRecvd;
12031217
}
12041218
else

deps/snap7/src/core/s7_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class TSnap7Server : public TCustomMsgServer
212212
protected:
213213
int DBCount;
214214
int DBLimit;
215-
PS7Area DB[MaxDB-1]; // DB
215+
PS7Area DB[MaxDB]; // DB
216216
PS7Area HA[5]; // MK,PE,PA,TM,CT
217217
PS7Area FindDB(word DBNumber);
218218
PWorkerSocket CreateWorkerSocket(socket_t Sock);

deps/snap7/src/sys/snap_msgsock.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,12 @@ int TMsgSocket::WaitForData(int Size, int Timeout)
267267
void TMsgSocket::SetSocketOptions()
268268
{
269269
int NoDelay = 1;
270+
int KeepAlive = 1;
270271
LastTcpError=0;
271272
SockCheck(setsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY,(char*)&NoDelay, sizeof(NoDelay)));
273+
274+
if (LastTcpError==0)
275+
SockCheck(setsockopt(FSocket, SOL_SOCKET, SO_KEEPALIVE,(char*)&KeepAlive, sizeof(KeepAlive)));
272276
}
273277
//---------------------------------------------------------------------------
274278
int TMsgSocket::SockCheck(int SockResult)

deps/snap7/src/sys/snap_threads.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ TSnapThread::~TSnapThread()
8484
Terminate();
8585
Join();
8686
};
87+
#ifdef OS_WINDOWS
88+
if (Started)
89+
CloseHandle(th);
90+
#endif
8791
}
8892
//---------------------------------------------------------------------------
8993
void TSnapThread::ThreadCreate()

0 commit comments

Comments
 (0)