@@ -295,38 +295,59 @@ def send_data(host,port)
295
295
end
296
296
sent_data = 0
297
297
sent_blocks = 0
298
+ send_retries = 0
298
299
expected_blocks = data_blocks . size
299
300
expected_size = data_blocks . join . size
300
301
if block_given?
301
302
yield "Source file: #{ self . local_file =~ /^DATA:/ ? "(Data)" : self . remote_file } , destination file: #{ self . remote_file } "
302
303
yield "Sending #{ expected_size } bytes (#{ expected_blocks } blocks)"
303
304
end
304
305
data_blocks . each_with_index do |data_block , idx |
305
- req = [ OpData , ( idx + 1 ) , data_block ] . pack ( "nnA*" )
306
- if self . server_sock . sendto ( req , host , port ) > 0
307
- sent_data += data_block . size
308
- end
309
- res = self . server_sock . recvfrom ( 65535 )
310
- if res
311
- code , type , msg = parse_tftp_response ( res [ 0 ] )
312
- if code == 4
313
- sent_blocks += 1
314
- yield "Sent #{ data_block . size } bytes in block #{ sent_blocks } " if block_given?
315
- else
316
- if block_given?
317
- yield "Got an unexpected response: Code:%d, Type:%d, Message:'%s'. Aborting." % [ code , type , msg ]
306
+ loop do
307
+ req = [ OpData , ( idx + 1 ) , data_block ] . pack ( "nnA*" )
308
+ if self . server_sock . sendto ( req , host , port ) <= 0
309
+ send_retries += 1
310
+ if send_retries > 100
311
+ break
312
+ else
313
+ next
314
+ end
315
+ end
316
+ send_retries = 0
317
+ res = self . server_sock . recvfrom ( 65535 )
318
+ if res
319
+ code , type , msg = parse_tftp_response ( res [ 0 ] )
320
+ if code == 4
321
+ if type == idx + 1
322
+ sent_blocks += 1
323
+ sent_data += data_block . size
324
+ yield "Sent #{ data_block . size } bytes in block #{ idx +1 } " if block_given?
325
+ break
326
+ else
327
+ next
328
+ end
329
+ else
330
+ if block_given?
331
+ yield "Got an unexpected response: Code:%d, Type:%d, Message:'%s'. Aborting." % [ code , type , msg ]
332
+ end
333
+ break
318
334
end
319
- break
320
335
end
321
336
end
322
337
end
338
+
339
+ if send_retries > 100
340
+ yield "Too many send retries, aborted"
341
+ end
342
+
323
343
if block_given?
324
344
if ( sent_data == expected_size )
325
345
yield ( "Transferred #{ sent_data } bytes in #{ sent_blocks } blocks, upload complete!" )
326
346
else
327
347
yield "Upload complete, but with errors."
328
348
end
329
349
end
350
+
330
351
if sent_data == expected_size
331
352
self . status = { :success => [
332
353
self . local_file ,
0 commit comments