p2p2 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c19c4e83055ee003d93923f5bc75aa21c2cfb8b678dd09f35405f4ccd7e93cb
4
- data.tar.gz: b827a910f9dcbcf48712eb308da98d3231bf8ebe3dc66bf2a07138ef318bbea8
3
+ metadata.gz: 2b2e5798d8439e44d3acbb68a9a8749a1d7dc69c43a5f6effea95f6360185a0a
4
+ data.tar.gz: a306f7dfe4be190712e7317de0f7324135e16075a39af9d7d3408aa3ab978bd5
5
5
  SHA512:
6
- metadata.gz: e1b716246402ff1f8cd6771b0607b78c3d54e754a7ea0189fb9168ad71294569943b7d641c2d3d77046d440cbd50bb22eb4f9cac092741ce6a6555ad3bcb255a
7
- data.tar.gz: 730efff6c7c46ddb64d5f87196be76a47fe4aaf74a6a57bee493c76b83f9accc1c0501c4b15aa88a5bc71bdbf6f4e6441e44174fea25cb29605c474bb5641798
6
+ metadata.gz: 8542ffdd4ed556d9a7d0aa3fbbf3f8da180610bb6a3d8bf04567bb4bc75852f45fd6821c748490e42cf85d51fdaa0b7abf62a49eff50e38565584523de79c07a
7
+ data.tar.gz: a6f98e9fb4116a19e473ed46e4767e54bf4113bb7e91da6970f64ba75f0f5fbf4f1a5f2118d6dff970640aecf9ad39cc34854ed0e317ef740c77589d1b60ba21
@@ -6,7 +6,7 @@ require 'p2p2/version'
6
6
  require 'socket'
7
7
 
8
8
  ##
9
- # P2p2::P1 - 内网里的任意应用,访问另一个内网里的应用服务端。p1端。
9
+ # P2p2::P1 - p2p通道,p1端。
10
10
  #
11
11
  module P2p2
12
12
  class P1
@@ -374,6 +374,25 @@ module P2p2
374
374
  end
375
375
  end
376
376
 
377
+ ##
378
+ # send data
379
+ #
380
+ def send_data( tund, data, to_addr )
381
+ begin
382
+ tund.sendmsg( data, 0, to_addr )
383
+ rescue IO::WaitWritable, Errno::EINTR
384
+ return false
385
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN => e
386
+ puts "#{ Time.new } #{ e.class }, close tund"
387
+ close_tund( tund )
388
+ sleep HEARTBEAT_INTERVAL
389
+ new_a_tund
390
+ return false
391
+ end
392
+
393
+ true
394
+ end
395
+
377
396
  ##
378
397
  # close dst
379
398
  #
@@ -475,15 +494,14 @@ module P2p2
475
494
  #
476
495
  def write_dst( dst )
477
496
  dst_info = @dst_infos[ dst ]
478
- data = dst_info[ :cache ]
479
- from = :cache
497
+ from, data = :cache, dst_info[ :cache ]
480
498
 
481
499
  if data.empty?
482
500
  if dst_info[ :chunks ].any?
483
501
  path = File.join( @dst_chunk_dir, dst_info[ :chunks ].shift )
484
502
 
485
503
  begin
486
- dst_info[ :cache ] = data = IO.binread( path )
504
+ data = dst_info[ :cache ] = IO.binread( path )
487
505
  File.delete( path )
488
506
  rescue Errno::ENOENT => e
489
507
  puts "#{ Time.new } read #{ path } #{ e.class }"
@@ -491,8 +509,7 @@ module P2p2
491
509
  return
492
510
  end
493
511
  else
494
- data = dst_info[ :wbuff ]
495
- from = :wbuff
512
+ from, data = :wbuff, dst_info[ :wbuff ]
496
513
  end
497
514
  end
498
515
 
@@ -532,19 +549,13 @@ module P2p2
532
549
  return
533
550
  end
534
551
 
552
+ now = Time.new
553
+
535
554
  # 传ctlmsg
536
555
  while @tund_info[ :ctlmsgs ].any?
537
556
  to_addr, data = @tund_info[ :ctlmsgs ].first
538
557
 
539
- begin
540
- tund.sendmsg( data, 0, to_addr )
541
- rescue IO::WaitWritable, Errno::EINTR
542
- return
543
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
544
- puts "#{ Time.new } #{ e.class }, close tund"
545
- close_tund( tund )
546
- sleep HEARTBEAT_INTERVAL
547
- new_a_tund
558
+ unless send_data( tund, data, to_addr )
548
559
  return
549
560
  end
550
561
 
@@ -560,22 +571,15 @@ module P2p2
560
571
  data = dst_ext[ :wmems ][ pack_id ]
561
572
 
562
573
  if data
563
- begin
564
- tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
565
- rescue IO::WaitWritable, Errno::EINTR
566
- return
567
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
568
- puts "#{ Time.new } #{ e.class }, close tund"
569
- close_tund( tund )
570
- sleep HEARTBEAT_INTERVAL
571
- new_a_tund
574
+ unless send_data( tund, data, @tund_info[ :tun_addr ] )
572
575
  return
573
576
  end
577
+
578
+ dst_ext[ :last_continue_at ] = now
574
579
  end
575
580
  end
576
581
 
577
582
  @tund_info[ :resendings ].shift
578
- return
579
583
  end
580
584
 
581
585
  # 若写后达到上限,暂停取写前
@@ -591,8 +595,7 @@ module P2p2
591
595
 
592
596
  # 取写前
593
597
  if @tund_info[ :caches ].any?
594
- dst_local_port, pack_id, data = @tund_info[ :caches ].first
595
- from = :caches
598
+ datas = @tund_info[ :caches ]
596
599
  elsif @tund_info[ :chunks ].any?
597
600
  path = File.join( @tund_chunk_dir, @tund_info[ :chunks ].shift )
598
601
 
@@ -613,48 +616,39 @@ module P2p2
613
616
  data = data[ ( 12 + pack_size )..-1 ]
614
617
  end
615
618
 
616
- @tund_info[ :caches ] = caches
617
- dst_local_port, pack_id, data = caches.first
618
- from = :caches
619
+ datas = @tund_info[ :caches ] = caches
619
620
  elsif @tund_info[ :wbuffs ].any?
620
- dst_local_port, pack_id, data = @tund_info[ :wbuffs ].first
621
- from = :wbuffs
621
+ datas = @tund_info[ :wbuffs ]
622
622
  else
623
623
  @writes.delete( tund )
624
624
  return
625
625
  end
626
626
 
627
- dst_ext = @tund_info[ :dst_exts ][ dst_local_port ]
627
+ while datas.any?
628
+ dst_local_port, pack_id, data = datas.first
629
+ dst_ext = @tund_info[ :dst_exts ][ dst_local_port ]
628
630
 
629
- if dst_ext
630
- if pack_id <= CONFUSE_UNTIL
631
- data = @custom.encode( data )
632
- # puts "debug1 encoded pack #{ pack_id }"
633
- end
631
+ if dst_ext
632
+ if pack_id <= CONFUSE_UNTIL
633
+ data = @custom.encode( data )
634
+ # puts "debug1 encoded pack #{ pack_id }"
635
+ end
634
636
 
635
- data = [ [ pack_id, dst_local_port ].pack( 'Q>n' ), data ].join
637
+ data = [ [ pack_id, dst_local_port ].pack( 'Q>n' ), data ].join
636
638
 
637
- begin
638
- tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
639
- rescue IO::WaitWritable, Errno::EINTR
640
- return
641
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
642
- puts "#{ Time.new } #{ e.class }, close tund"
643
- close_tund( tund )
644
- sleep HEARTBEAT_INTERVAL
645
- new_a_tund
646
- return
639
+ unless send_data( tund, data, @tund_info[ :tun_addr ] )
640
+ return
641
+ end
642
+
643
+ # puts "debug2 written pack #{ pack_id }"
644
+ dst_ext[ :relay_pack_id ] = pack_id
645
+ dst_ext[ :wmems ][ pack_id ] = data
646
+ dst_ext[ :send_ats ][ pack_id ] = now
647
+ dst_ext[ :last_continue_at ] = now
647
648
  end
648
649
 
649
- # puts "debug2 written pack #{ pack_id }"
650
- now = Time.new
651
- dst_ext[ :relay_pack_id ] = pack_id
652
- dst_ext[ :wmems ][ pack_id ] = data
653
- dst_ext[ :send_ats ][ pack_id ] = now
654
- dst_ext[ :last_continue_at ] = now
650
+ datas.shift
655
651
  end
656
-
657
- @tund_info[ from ].shift
658
652
  end
659
653
 
660
654
  ##
@@ -6,7 +6,7 @@ require 'p2p2/version'
6
6
  require 'socket'
7
7
 
8
8
  ##
9
- # P2p2::P2 - 内网里的任意应用,访问另一个内网里的应用服务端。p2端。
9
+ # P2p2::P2 - p2p通道,p2端。
10
10
  #
11
11
  module P2p2
12
12
  class P2
@@ -402,6 +402,23 @@ module P2p2
402
402
  end
403
403
  end
404
404
 
405
+ ##
406
+ # send data
407
+ #
408
+ def send_data( tun, data, to_addr )
409
+ begin
410
+ tun.sendmsg( data, 0, to_addr )
411
+ rescue IO::WaitWritable, Errno::EINTR
412
+ return false
413
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN => e
414
+ puts "#{ Time.new } #{ e.class }, close tun"
415
+ close_tun( tun )
416
+ return false
417
+ end
418
+
419
+ true
420
+ end
421
+
405
422
  ##
406
423
  # close src
407
424
  #
@@ -503,15 +520,14 @@ module P2p2
503
520
  #
504
521
  def write_src( src )
505
522
  src_info = @src_infos[ src ]
506
- data = src_info[ :cache ]
507
- from = :cache
523
+ from, data = :cache, src_info[ :cache ]
508
524
 
509
525
  if data.empty?
510
526
  if src_info[ :chunks ].any?
511
527
  path = File.join( @src_chunk_dir, src_info[ :chunks ].shift )
512
528
 
513
529
  begin
514
- src_info[ :cache ] = data = IO.binread( path )
530
+ data = src_info[ :cache ] = IO.binread( path )
515
531
  File.delete( path )
516
532
  rescue Errno::ENOENT => e
517
533
  puts "#{ Time.new } read #{ path } #{ e.class }"
@@ -519,8 +535,7 @@ module P2p2
519
535
  return
520
536
  end
521
537
  else
522
- data = src_info[ :wbuff ]
523
- from = :wbuff
538
+ from, data = :wbuff, src_info[ :wbuff ]
524
539
  end
525
540
  end
526
541
 
@@ -558,17 +573,13 @@ module P2p2
558
573
  return
559
574
  end
560
575
 
576
+ now = Time.new
577
+
561
578
  # 传ctlmsg
562
579
  while @tun_info[ :ctlmsgs ].any?
563
580
  to_addr, data = @tun_info[ :ctlmsgs ].first
564
581
 
565
- begin
566
- tun.sendmsg( data, 0, to_addr )
567
- rescue IO::WaitWritable, Errno::EINTR
568
- return
569
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
570
- puts "#{ Time.new } #{ e.class }, close tun"
571
- close_tun( tun )
582
+ unless send_data( tun, data, to_addr )
572
583
  return
573
584
  end
574
585
 
@@ -584,20 +595,15 @@ module P2p2
584
595
  data = src_ext[ :wmems ][ pack_id ]
585
596
 
586
597
  if data
587
- begin
588
- tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
589
- rescue IO::WaitWritable, Errno::EINTR
590
- return
591
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
592
- puts "#{ Time.new } #{ e.class }, close tun"
593
- close_tun( tun )
598
+ unless send_data( tun, data, @tun_info[ :tund_addr ] )
594
599
  return
595
600
  end
601
+
602
+ src_ext[ :last_continue_at ] = now
596
603
  end
597
604
  end
598
605
 
599
606
  @tun_info[ :resendings ].shift
600
- return
601
607
  end
602
608
 
603
609
  # 若写后达到上限,暂停取写前
@@ -613,8 +619,7 @@ module P2p2
613
619
 
614
620
  # 取写前
615
621
  if @tun_info[ :caches ].any?
616
- src_id, pack_id, data = @tun_info[ :caches ].first
617
- from = :caches
622
+ datas = @tun_info[ :caches ]
618
623
  elsif @tun_info[ :chunks ].any?
619
624
  path = File.join( @tun_chunk_dir, @tun_info[ :chunks ].shift )
620
625
 
@@ -635,46 +640,39 @@ module P2p2
635
640
  data = data[ ( 18 + pack_size )..-1 ]
636
641
  end
637
642
 
638
- @tun_info[ :caches ] = caches
639
- src_id, pack_id, data = caches.first
640
- from = :caches
643
+ datas = @tun_info[ :caches ] = caches
641
644
  elsif @tun_info[ :wbuffs ].any?
642
- src_id, pack_id, data = @tun_info[ :wbuffs ].first
643
- from = :wbuffs
645
+ datas = @tun_info[ :wbuffs ]
644
646
  else
645
647
  @writes.delete( tun )
646
648
  return
647
649
  end
648
650
 
649
- src_ext = @tun_info[ :src_exts ][ src_id ]
651
+ while datas.any?
652
+ src_id, pack_id, data = datas.first
653
+ src_ext = @tun_info[ :src_exts ][ src_id ]
650
654
 
651
- if src_ext
652
- if pack_id <= CONFUSE_UNTIL
653
- data = @custom.encode( data )
654
- # puts "debug1 encoded pack #{ pack_id }"
655
- end
655
+ if src_ext
656
+ if pack_id <= CONFUSE_UNTIL
657
+ data = @custom.encode( data )
658
+ # puts "debug1 encoded pack #{ pack_id }"
659
+ end
656
660
 
657
- data = [ [ pack_id, src_id ].pack( 'Q>Q>' ), data ].join
661
+ data = [ [ pack_id, src_id ].pack( 'Q>Q>' ), data ].join
658
662
 
659
- begin
660
- tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
661
- rescue IO::WaitWritable, Errno::EINTR
662
- return
663
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
664
- puts "#{ Time.new } #{ e.class }, close tun"
665
- close_tun( tun )
666
- return
663
+ unless send_data( tun, data, @tun_info[ :tund_addr ] )
664
+ return
665
+ end
666
+
667
+ # puts "debug2 written pack #{ pack_id }"
668
+ src_ext[ :relay_pack_id ] = pack_id
669
+ src_ext[ :wmems ][ pack_id ] = data
670
+ src_ext[ :send_ats ][ pack_id ] = now
671
+ src_ext[ :last_continue_at ] = now
667
672
  end
668
673
 
669
- # puts "debug2 written pack #{ pack_id }"
670
- now = Time.new
671
- src_ext[ :relay_pack_id ] = pack_id
672
- src_ext[ :wmems ][ pack_id ] = data
673
- src_ext[ :send_ats ][ pack_id ] = now
674
- src_ext[ :last_continue_at ] = now
674
+ datas.shift
675
675
  end
676
-
677
- @tun_info[ from ].shift
678
676
  end
679
677
 
680
678
  ##
@@ -5,7 +5,7 @@ require 'p2p2/version'
5
5
  require 'socket'
6
6
 
7
7
  ##
8
- # P2p2::P2pd - 内网里的任意应用,访问另一个内网里的应用服务端。配对服务器端。
8
+ # P2p2::P2pd - 配对服务。
9
9
  #
10
10
  # 包结构
11
11
  # ======
@@ -1,3 +1,3 @@
1
1
  module P2p2
2
- VERSION = '0.24.0'.freeze
2
+ VERSION = '0.25.0'.freeze
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["qqtakafan@gmail.com"]
11
11
 
12
12
  spec.summary = %q{p2p}
13
- spec.description = %q{内网里的任意应用,访问另一个内网里的应用服务端。}
13
+ spec.description = %q{p2p通道。nat穿透。}
14
14
  spec.homepage = "https://github.com/takafan/p2p2"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p2p2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.24.0
4
+ version: 0.25.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takafan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-21 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: 内网里的任意应用,访问另一个内网里的应用服务端。
13
+ description: p2p通道。nat穿透。
14
14
  email:
15
15
  - qqtakafan@gmail.com
16
16
  executables: []
@@ -49,8 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0'
51
51
  requirements: []
52
- rubyforge_project:
53
- rubygems_version: 2.7.7
52
+ rubygems_version: 3.1.2
54
53
  signing_key:
55
54
  specification_version: 4
56
55
  summary: p2p