2828
2929namespace lib60870 . linklayer
3030{
31+
32+ /// <summary>
33+ /// TCP client virtual serial port. Can be used to tunnel CS 101 protocol over TCP/IP.
34+ /// </summary>
3135 public class TcpClientVirtualSerialPort : Stream
3236 {
3337 private int readTimeout = 0 ;
@@ -54,6 +58,17 @@ private void DebugLog(string msg)
5458 }
5559 }
5660
61+
62+ /// <summary>
63+ /// Gets a value indicating whether this <see cref="lib60870.linklayer.TcpClientVirtualSerialPort"/> is connected to a server
64+ /// </summary>
65+ /// <value><c>true</c> if connected; otherwise, <c>false</c>.</value>
66+ public bool Connected {
67+ get {
68+ return this . connected ;
69+ }
70+ }
71+
5772 public bool DebugOutput {
5873 get {
5974 return this . debugOutput ;
@@ -63,6 +78,11 @@ public bool DebugOutput {
6378 }
6479 }
6580
81+ /// <summary>
82+ /// Initializes a new instance of the <see cref="lib60870.linklayer.TcpClientVirtualSerialPort"/> class.
83+ /// </summary>
84+ /// <param name="hostname">IP address of the server</param>
85+ /// <param name="tcpPort">TCP port of the server</param>
6686 public TcpClientVirtualSerialPort ( String hostname , int tcpPort = 2404 )
6787 {
6888 this . hostname = hostname ;
@@ -84,18 +104,22 @@ private void ConnectSocketWithTimeout()
84104 throw new SocketException ( 87 ) ; // wrong argument
85105 }
86106
107+ if ( ! running )
108+ return ;
109+
87110 // Create a TCP/IP socket.
88111 conSocket = new Socket ( AddressFamily . InterNetwork ,
89112 SocketType . Stream , ProtocolType . Tcp ) ;
90113
91114 var result = conSocket . BeginConnect ( remoteEP , null , null ) ;
92115
93- bool success = result . AsyncWaitHandle . WaitOne ( connectTimeoutInMs , true ) ;
94- if ( success )
95- {
96- try
97- {
98- conSocket . EndConnect ( result ) ;
116+ if ( ! running )
117+ return ;
118+
119+ bool success = result . AsyncWaitHandle . WaitOne ( connectTimeoutInMs , true ) ;
120+ if ( success ) {
121+ try {
122+ conSocket . EndConnect ( result ) ;
99123 conSocket . NoDelay = true ;
100124 }
101125 catch ( ObjectDisposedException )
@@ -138,24 +162,46 @@ private void ConnectionThread()
138162 if ( conSocket . Connected == false )
139163 break ;
140164
165+ if ( running == false )
166+ break ;
167+
141168 Thread . Sleep ( 10 ) ;
142169 }
143170
144- connected = false ;
145- socketStream = null ;
146- conSocket . Close ( ) ;
147- conSocket = null ;
171+ connected = false ;
172+
173+ if ( ! this . running )
174+ return ;
175+
176+ if ( socketStream != null ) {
177+ socketStream . Close ( ) ;
178+ conSocket . Dispose ( ) ;
179+ socketStream = null ;
180+ }
181+
182+
183+ if ( conSocket != null ) {
184+ conSocket . Close ( ) ;
185+ conSocket . Dispose ( ) ;
186+ conSocket = null ;
187+
188+ }
148189
149- } catch ( SocketException ) {
190+ } catch ( SocketException e ) {
191+ DebugLog ( "Failed to connect: " + e . Message ) ;
150192 connected = false ;
151193 socketStream = null ;
152194 conSocket = null ;
153195 }
154196
155- Thread . Sleep ( waitRetryConnect ) ;
197+ if ( running )
198+ Thread . Sleep ( waitRetryConnect ) ;
156199 }
157200 }
158201
202+ /// <summary>
203+ /// Start the virtual serial port (connect to server)
204+ /// </summary>
159205 public void Start ( )
160206 {
161207 if ( running == false ) {
@@ -165,14 +211,33 @@ public void Start()
165211 }
166212 }
167213
168-
214+ /// <summary>
215+ /// Stop the virtual serial port
216+ /// </summary>
169217 public void Stop ( )
170218 {
171219 if ( running == true ) {
172220 running = false ;
221+ this . connected = false ;
222+
223+ if ( socketStream != null ) {
224+ socketStream . Close ( ) ;
225+ socketStream . Dispose ( ) ;
226+ socketStream = null ;
227+ }
228+
229+ if ( conSocket != null ) {
230+
231+ try {
232+ conSocket . Shutdown ( SocketShutdown . Both ) ;
233+ }
234+ catch ( SocketException ) {
235+ }
173236
174- if ( conSocket != null )
175237 conSocket . Close ( ) ;
238+ conSocket . Dispose ( ) ;
239+ conSocket = null ;
240+ }
176241
177242 connectionThread . Join ( ) ;
178243 }
@@ -187,13 +252,21 @@ public override int Read (byte[] buffer, int offset, int count)
187252 {
188253 if ( socketStream != null ) {
189254
190- if ( conSocket . Poll ( ReadTimeout , SelectMode . SelectRead ) ) {
191- if ( connected )
192- return socketStream . Read ( buffer , offset , count ) ;
193- else
255+ try {
256+ if ( conSocket . Poll ( ReadTimeout , SelectMode . SelectRead ) ) {
257+ if ( connected )
258+ return socketStream . Read ( buffer , offset , count ) ;
259+ else
260+ return 0 ;
261+ } else
194262 return 0 ;
195- } else
263+ }
264+ catch ( Exception e ) {
265+ Console . WriteLine ( e . ToString ( ) ) ;
266+ this . connected = false ;
196267 return 0 ;
268+ }
269+
197270 }
198271 else
199272 return 0 ;
0 commit comments