55using System . Threading ;
66using System . Threading . Tasks ;
77using FluentFTP ;
8+ using static System . Net . WebRequestMethods ;
89
910namespace _1RM . Model . Protocol . FileTransmit . Transmitters
1011{
@@ -39,7 +40,12 @@ public async Task Conn()
3940
4041 public bool IsConnected ( )
4142 {
42- return _ftp ? . IsConnected == true ;
43+ bool isConnected = true ;
44+ lock ( this )
45+ {
46+ isConnected = _ftp ? . IsConnected == true ;
47+ }
48+ return isConnected ;
4349 }
4450
4551 public ITransmitter Clone ( )
@@ -50,35 +56,42 @@ public ITransmitter Clone()
5056 public async Task < RemoteItem ? > Get ( string path )
5157 {
5258 await FtpConnection ;
53- if ( _ftp == null ) return null ;
59+ lock ( this )
60+ {
61+ if ( _ftp == null || _ftp . IsDisposed ) return null ;
62+ }
5463 return await Exists ( path ) ? FtpListItem2RemoteItem ( await _ftp . GetObjectInfo ( path ) ) : null ;
5564 }
5665
5766 public async Task < List < RemoteItem > > ListDirectoryItems ( string path )
5867 {
5968 await FtpConnection ;
6069 var ret = new List < RemoteItem > ( ) ;
61- if ( _ftp != null )
70+ lock ( this )
6271 {
63- IEnumerable < FtpListItem > items = await _ftp . GetListing ( path ) ;
64- if ( ! items . Any ( ) )
65- return ret ;
72+ if ( _ftp == null || _ftp . IsDisposed ) return ret ;
73+ }
74+ IEnumerable < FtpListItem > items = await _ftp . GetListing ( path ) ;
75+ if ( ! items . Any ( ) )
76+ return ret ;
6677
67- items = items . OrderBy ( x => x . Name ) ;
68- foreach ( var item in items )
69- {
70- if ( item . Name == "." || item . Name == ".." )
71- continue ;
72- ret . Add ( FtpListItem2RemoteItem ( item ) ) ;
73- }
78+ items = items . OrderBy ( x => x . Name ) ;
79+ foreach ( var item in items )
80+ {
81+ if ( item . Name == "." || item . Name == ".." )
82+ continue ;
83+ ret . Add ( FtpListItem2RemoteItem ( item ) ) ;
7484 }
7585 return ret ;
7686 }
7787
7888 public async Task < bool > Exists ( string path )
7989 {
8090 await FtpConnection ;
81- if ( _ftp == null ) return false ;
91+ lock ( this )
92+ {
93+ if ( _ftp == null || _ftp . IsDisposed ) return false ;
94+ }
8295 if ( await _ftp . FileExists ( path ) )
8396 return true ;
8497 if ( await _ftp . DirectoryExists ( path ) )
@@ -129,7 +142,10 @@ private RemoteItem FtpListItem2RemoteItem(FtpListItem item)
129142 public async Task Delete ( string path )
130143 {
131144 await FtpConnection ;
132- if ( _ftp == null ) return ;
145+ lock ( this )
146+ {
147+ if ( _ftp == null || _ftp . IsDisposed ) return ;
148+ }
133149 var item = await Get ( path ) ;
134150 if ( item != null )
135151 {
@@ -152,14 +168,21 @@ public async Task Delete(RemoteItem item)
152168 public async Task CreateDirectory ( string path )
153169 {
154170 await FtpConnection ;
155- if ( _ftp == null ) return ;
171+ lock ( this )
172+ {
173+ if ( _ftp == null || _ftp . IsDisposed ) return ;
174+ }
156175 if ( await _ftp . DirectoryExists ( path ) == false )
157176 await _ftp . CreateDirectory ( path ) ;
158177 }
159178
160179 public async Task RenameFile ( string path , string newPath )
161180 {
162181 await FtpConnection ;
182+ lock ( this )
183+ {
184+ if ( _ftp == null || _ftp . IsDisposed ) return ;
185+ }
163186 if ( _ftp != null && path != newPath && await Exists ( path ) )
164187 await _ftp . Rename ( path , newPath ) ;
165188 }
@@ -171,7 +194,10 @@ public async Task UploadFile(string localFilePath, string saveToRemotePath, Acti
171194 return ;
172195
173196 await FtpConnection ;
174- if ( _ftp == null ) return ;
197+ lock ( this )
198+ {
199+ if ( _ftp == null || _ftp . IsDisposed ) return ;
200+ }
175201 await FtpSemaphoe . WaitAsync ( ) . ConfigureAwait ( false ) ;
176202 try
177203 {
@@ -204,7 +230,10 @@ await _ftp.UploadFile(localFilePath, saveToRemotePath, FtpRemoteExists.Overwrite
204230 public async Task DownloadFile ( string remoteFilePath , string saveToLocalPath , Action < ulong > readCallBack , CancellationToken cancellationToken )
205231 {
206232 await FtpConnection ;
207- if ( _ftp == null ) return ;
233+ lock ( this )
234+ {
235+ if ( _ftp == null || _ftp . IsDisposed ) return ;
236+ }
208237 await FtpSemaphoe . WaitAsync ( ) . ConfigureAwait ( false ) ;
209238 try
210239 {
@@ -227,23 +256,33 @@ public void Release()
227256 {
228257 FtpSemaphoe ? . Dispose ( ) ;
229258 FtpConnection ? . Dispose ( ) ;
230- var ftp = _ftp ;
231- ftp ? . Disconnect ( ) ;
232- ftp ? . Dispose ( ) ;
233- _ftp = null ;
259+ lock ( this )
260+ {
261+ _ftp ? . Disconnect ( ) ;
262+ }
263+ ReleaseFtp ( ) ;
264+ }
265+
266+ private void ReleaseFtp ( )
267+ {
268+ lock ( this )
269+ {
270+ _ftp ? . Dispose ( ) ;
271+ _ftp = null ;
272+ }
234273 }
235274
236275 private async Task InitClient ( )
237276 {
238- _ftp ? . Dispose ( ) ;
277+ await _ftp ? . Disconnect ( ) ;
278+ ReleaseFtp ( ) ;
239279 _ftp = new AsyncFtpClient ( Hostname , new System . Net . NetworkCredential ( Username , Password ) , Port ) ;
240280 _ftp . Config . Noop = true ;
241281 await _ftp . AutoConnect ( ) ;
242282 if ( ! _ftp . IsConnected )
243283 {
244284 await _ftp . Disconnect ( ) ;
245- _ftp . Dispose ( ) ;
246- _ftp = null ;
285+ ReleaseFtp ( ) ;
247286 throw new Exception ( "Couldn't connect to the server." ) ;
248287 }
249288 }
0 commit comments