Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Modbus
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
AM.WD
Modbus
Commits
bb9dbda3
Commit
bb9dbda3
authored
Jul 15, 2019
by
Andreas Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed some issues using Dispose() after Disconnect() and while reconnecting.
parent
249db101
Pipeline
#31
passed with stage
in 43 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
36 deletions
+55
-36
src/Modbus.Tcp/Client/ModbusClient.cs
src/Modbus.Tcp/Client/ModbusClient.cs
+52
-29
test/UnitTests/ModbusTcpTests.cs
test/UnitTests/ModbusTcpTests.cs
+3
-7
No files found.
src/Modbus.Tcp/Client/ModbusClient.cs
View file @
bb9dbda3
...
...
@@ -170,33 +170,9 @@ namespace AMWD.Modbus.Tcp.Client
/// Disconnects the client.
/// </summary>
/// <returns>An awaitable task.</returns>
public
Task
Disconnect
()
public
async
Task
Disconnect
()
{
if
(
isDisposed
)
{
throw
new
ObjectDisposedException
(
GetType
().
FullName
);
}
if
(!
isStarted
)
{
return
Task
.
CompletedTask
;
}
bool
wasConnected
=
IsConnected
;
reconnectTcs
?.
TrySetResult
(
false
);
mainCts
?.
Cancel
();
reconnectTcs
=
null
;
tcpClient
?.
Close
();
tcpClient
?.
Dispose
();
tcpClient
=
null
;
if
(
wasConnected
)
{
Task
.
Run
(()
=>
Disconnected
?.
Invoke
(
this
,
EventArgs
.
Empty
)).
Forget
();
}
return
Task
.
CompletedTask
;
await
DisconnectInternal
(
false
);
}
#
endregion
Control
...
...
@@ -1051,6 +1027,11 @@ namespace AMWD.Modbus.Tcp.Client
if
(!
IsConnected
)
{
if
(!
isReconnecting
)
{
Task
.
Run
(()
=>
Reconnect
(
mainCts
.
Token
)).
Forget
();
ConnectingTask
=
GetWaitTask
(
mainCts
.
Token
);
}
throw
new
InvalidOperationException
(
"Client is not connected"
);
}
...
...
@@ -1125,7 +1106,7 @@ namespace AMWD.Modbus.Tcp.Client
tcpClient
=
new
TcpClient
(
AddressFamily
.
InterNetworkV6
);
tcpClient
.
Client
.
DualMode
=
true
;
var
task
=
tcpClient
.
ConnectAsync
(
Host
,
Port
);
if
(
await
Task
.
WhenAny
(
task
,
Task
.
Delay
(
TimeSpan
.
FromSeconds
(
timeout
),
ct
))
==
task
)
if
(
await
Task
.
WhenAny
(
task
,
Task
.
Delay
(
TimeSpan
.
FromSeconds
(
timeout
),
ct
))
==
task
&&
tcpClient
?.
Connected
==
true
)
{
logger
?.
LogInformation
(
"ModbusClient.Reconnect connected"
);
Task
.
Run
(()
=>
Connected
?.
Invoke
(
this
,
EventArgs
.
Empty
)).
Forget
();
...
...
@@ -1183,6 +1164,46 @@ namespace AMWD.Modbus.Tcp.Client
}
}
private
async
Task
DisconnectInternal
(
bool
disposing
)
{
if
(
isDisposed
&&
!
disposing
)
{
throw
new
ObjectDisposedException
(
GetType
().
FullName
);
}
if
(!
isStarted
)
{
return
;
}
isStarted
=
false
;
bool
wasConnected
=
IsConnected
;
try
{
reconnectTcs
?.
TrySetResult
(
false
);
mainCts
?.
Cancel
();
reconnectTcs
=
null
;
}
catch
{
}
try
{
tcpClient
?.
Close
();
tcpClient
?.
Dispose
();
tcpClient
=
null
;
}
catch
{
}
if
(
wasConnected
)
{
Task
.
Run
(()
=>
Disconnected
?.
Invoke
(
this
,
EventArgs
.
Empty
)).
Forget
();
}
await
Task
.
CompletedTask
;
}
#
endregion
Private
Methods
#
region
IDisposable
implementation
...
...
@@ -1204,9 +1225,11 @@ namespace AMWD.Modbus.Tcp.Client
{
return
;
}
Disconnect
().
GetAwaiter
().
GetResult
();
isDisposed
=
true
;
DisconnectInternal
(
true
)
.
GetAwaiter
()
.
GetResult
();
}
#
endregion
IDisposable
implementation
...
...
test/UnitTests/ModbusTcpTests.cs
View file @
bb9dbda3
...
...
@@ -49,18 +49,15 @@ namespace UnitTests
using
(
var
client
=
new
ModbusClient
(
IPAddress
.
Loopback
,
server
.
Port
))
{
await
client
.
Connect
();
await
EnsureWait
();
Assert
.
IsTrue
(
client
.
IsConnected
);
await
server
.
Stop
();
await
client
.
ReadHoldingRegisters
(
0
,
0
,
1
);
await
EnsureWait
();
Assert
.
IsFalse
(
client
.
IsConnected
);
server
.
Start
();
await
client
.
ConnectingTask
;
await
EnsureWait
();
Assert
.
IsTrue
(
client
.
IsConnected
);
}
}
...
...
@@ -91,17 +88,16 @@ namespace UnitTests
await
client
.
Connect
();
Assert
.
IsTrue
(
client
.
IsConnected
);
await
EnsureWait
();
await
EnsureWait
();
// get events raised
Assert
.
AreEqual
(
1
,
connectEvents
);
Assert
.
AreEqual
(
0
,
disconnectEvents
);
await
server
.
Stop
();
await
client
.
ReadHoldingRegisters
(
0
,
0
,
1
);
await
EnsureWait
();
Assert
.
IsFalse
(
client
.
IsConnected
);
await
EnsureWait
();
await
EnsureWait
();
// get events raised
Assert
.
AreEqual
(
1
,
connectEvents
);
Assert
.
AreEqual
(
1
,
disconnectEvents
);
...
...
@@ -110,7 +106,7 @@ namespace UnitTests
Assert
.
IsTrue
(
client
.
IsConnected
);
}
await
EnsureWait
();
await
EnsureWait
();
// get events raised
Assert
.
AreEqual
(
2
,
connectEvents
);
Assert
.
AreEqual
(
2
,
disconnectEvents
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment