p2p2 0.19.0 → 0.20.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: b139ce8cd373285af412d083c4dcbe9a227d882d19d8cb5fdd1389849645c69c
4
- data.tar.gz: 451f47afc5b16d62b7d1c316b00eecc49f947c333abd69e1235539b43a61c1bd
3
+ metadata.gz: 5621af269a434cac452523cfccd1816c1246b37936296df09e5db7a28ed0701b
4
+ data.tar.gz: 718b2f29404d97724b21638fcdd8f683ac1e987cc94b482e38252dc5485afd8e
5
5
  SHA512:
6
- metadata.gz: 3f504248e0c27936e246de5b90705a48d84682a858778bd7ab085f1c846375b0a249a11860a6cc0edc45f8ad7cb66cb66990d8fe3c3d91ea0a397bd40cfc67c6
7
- data.tar.gz: da4849fa322f4e98aa9e44a3f185a0d694e5bbefc11009aa303b94d61ecb113b89b7fba2eb4f5014264b4674236ce863c7f83fac61f64ee8e34e78b4ad572816
6
+ metadata.gz: 680e7ad7a8fbbaf4f5d020d6cbf988db7ba225ff38fce951069c9bbb211923ed54165a05440842ce929861ea32d94caa4683b424a51cedade77ff9bb2ade2d6a
7
+ data.tar.gz: 9a188e36e5f5fa650b8074d263dcc16a7578ea6b9a5135faf9fb73af17695f2c9af33f14d1aaed81fe113b55f37f3653d325826bfb32c3cbfed8797e096d41dc
@@ -6,12 +6,13 @@ module P2p2
6
6
  RESUME_BELOW = 50_000 # 降到多少以下恢复写
7
7
  EXPIRE_NEW = 10 # 创建之后多久没有流量进来,过期
8
8
  EXPIRE_AFTER = 300 # 多久没有新流量,过期
9
- CHECK_EXPIRE_INTERVAL = 30 # 检查过期间隔
9
+ CHECK_EXPIRE_INTERVAL = 60 # 检查过期间隔
10
10
  STATUS_INTERVAL = 0.5 # 发送状态间隔
11
11
  SEND_STATUS_UNTIL = 10 # 持续的告之对面状态,直到没有流量往来,持续多少秒
12
12
  BREAK_SEND_MISS = 10_000 # miss包个数上限,达到上限忽略要后面的段,可控碎片缓存
13
13
  CONFUSE_UNTIL = 5 # 混淆前几个包
14
- UPDATE_ROOM_INTERVAL = 5 # 刷新房间间隔
14
+ HEARTBEAT_INTERVAL = 5 # 心跳间隔
15
+ UPDATE_ROOM_INTERVAL = 60 # 刷新房间间隔
15
16
  PEER_ADDR = 1
16
17
  HEARTBEAT = 2
17
18
  A_NEW_SOURCE = 3
@@ -28,8 +28,7 @@ module P2p2
28
28
  #
29
29
  def looping
30
30
  puts "#{ Time.new } looping"
31
- loop_update_room
32
- loop_check_expire
31
+ loop_heartbeat
33
32
  loop_check_status
34
33
 
35
34
  loop do
@@ -80,76 +79,65 @@ module P2p2
80
79
  private
81
80
 
82
81
  ##
83
- # loop update room
82
+ # loop heartbeat
84
83
  #
85
- def loop_update_room( update_at = Time.new )
84
+ def loop_heartbeat( check_at = Time.new )
86
85
  Thread.new do
87
86
  loop do
88
- sleep UPDATE_ROOM_INTERVAL
87
+ sleep HEARTBEAT_INTERVAL
89
88
 
90
89
  @mutex.synchronize do
91
- if !@tund.closed? && @tund_info[ :peer_addr ].nil?
92
- now = Time.new
93
-
94
- if now - update_at >= 60
95
- data = @room
96
- update_at = now
97
- else
98
- data = [ rand( 128 ) ].pack( 'C' )
99
- end
100
-
101
- add_tund_ctlmsg( data, @p2pd_addr )
102
- next_tick
103
- end
104
- end
105
- end
106
- end
107
- end
108
-
109
- ##
110
- # loop check expire
111
- #
112
- def loop_check_expire
113
- Thread.new do
114
- loop do
115
- sleep CHECK_EXPIRE_INTERVAL
116
-
117
- @mutex.synchronize do
118
- need_trigger = false
119
90
  now = Time.new
120
91
 
121
- if !@tund.closed? && @tund_info[ :tun_addr ]
122
- if now - @tund_info[ :last_recv_at ] > EXPIRE_AFTER
123
- puts "#{ Time.new } expire tund"
124
- set_is_closing( @tund )
125
- else
126
- # puts "debug1 #{ Time.new } heartbeat"
127
- add_tund_ctlmsg( pack_a_heartbeat )
128
-
129
- @tund_info[ :dst_exts ].each do | dst_local_port, dst_ext |
130
- if dst_ext[ :dst ].closed? && ( now - dst_ext[ :last_continue_at ] > EXPIRE_AFTER )
131
- puts "#{ Time.new } expire dst ext #{ dst_local_port }"
132
- del_dst_ext( dst_local_port )
92
+ unless @tund.closed?
93
+ if @tund_info[ :peer_addr ]
94
+ if @tund_info[ :tun_addr ]
95
+ if now - check_at >= CHECK_EXPIRE_INTERVAL
96
+ if now - @tund_info[ :last_recv_at ] > EXPIRE_AFTER
97
+ puts "#{ Time.new } expire tund"
98
+ set_is_closing( @tund )
99
+ else
100
+ @tund_info[ :dst_exts ].each do | dst_local_port, dst_ext |
101
+ if dst_ext[ :dst ].closed? && ( now - dst_ext[ :last_continue_at ] > EXPIRE_AFTER )
102
+ puts "#{ Time.new } expire dst ext #{ dst_local_port }"
103
+ del_dst_ext( dst_local_port )
104
+ end
105
+ end
106
+ end
107
+
108
+ @dst_infos.each do | dst, dst_info |
109
+ if dst_info[ :last_recv_at ].nil? && ( now - dst_info[ :created_at ] > EXPIRE_NEW )
110
+ puts "#{ Time.new } expire dst"
111
+ set_is_closing( dst )
112
+ end
113
+ end
114
+
115
+ check_at = now
133
116
  end
134
- end
135
- end
136
-
137
- need_trigger = true
138
- end
139
117
 
140
- @dst_infos.each do | dst, dst_info |
141
- is_expired = dst_info[ :last_recv_at ].nil? && ( now - dst_info[ :created_at ] > EXPIRE_NEW )
118
+ # puts "debug2 heartbeat"
119
+ add_tund_ctlmsg( pack_a_heartbeat )
120
+ next_tick
121
+ elsif now - @tund_info[ :last_recv_at ] > EXPIRE_NEW
122
+ # no tun addr
123
+ puts "#{ Time.new } expire new tund"
124
+ set_is_closing( @tund )
125
+ next_tick
126
+ end
127
+ else
128
+ # no peer addr
129
+ if now - check_at >= UPDATE_ROOM_INTERVAL
130
+ data = @room
131
+ check_at = now
132
+ else
133
+ data = [ rand( 128 ) ].pack( 'C' )
134
+ end
142
135
 
143
- if is_expired
144
- puts "#{ Time.new } expire dst"
145
- set_is_closing( dst )
146
- need_trigger = true
136
+ # puts "debug2 update room"
137
+ add_tund_ctlmsg( data, @p2pd_addr )
138
+ next_tick
147
139
  end
148
140
  end
149
-
150
- if need_trigger
151
- next_tick
152
- end
153
141
  end
154
142
  end
155
143
  end
@@ -28,7 +28,7 @@ module P2p2
28
28
  #
29
29
  def looping
30
30
  puts "#{ Time.new } looping"
31
- loop_check_expire
31
+ loop_heartbeat
32
32
  loop_check_status
33
33
 
34
34
  loop do
@@ -81,49 +81,51 @@ module P2p2
81
81
  private
82
82
 
83
83
  ##
84
- # loop check expire
84
+ # loop heartbeat
85
85
  #
86
- def loop_check_expire
86
+ def loop_heartbeat( check_at = Time.new )
87
87
  Thread.new do
88
88
  loop do
89
- sleep CHECK_EXPIRE_INTERVAL
89
+ sleep HEARTBEAT_INTERVAL
90
90
 
91
91
  @mutex.synchronize do
92
- need_trigger = false
93
92
  now = Time.new
94
93
 
95
- if @tun && !@tun.closed? && @tun_info[ :tund_addr ]
96
- if now - @tun_info[ :last_recv_at ] > EXPIRE_AFTER
97
- puts "#{ Time.new } expire tun"
98
- set_is_closing( @tun )
99
- else
100
- # puts "debug1 #{ Time.new } heartbeat"
101
- add_tun_ctlmsg( pack_a_heartbeat )
102
-
103
- @tun_info[ :src_exts ].each do | src_id, src_ext |
104
- if src_ext[ :src ].closed? && ( now - src_ext[ :last_continue_at ] > EXPIRE_AFTER )
105
- puts "#{ Time.new } expire src ext #{ src_id }"
106
- del_src_ext( src_id )
94
+ if @tun && !@tun.closed? && @tun_info[ :peer_addr ]
95
+ if @tun_info[ :tund_addr ]
96
+ if now - check_at >= CHECK_EXPIRE_INTERVAL
97
+ if now - @tun_info[ :last_recv_at ] > EXPIRE_AFTER
98
+ puts "#{ Time.new } expire tun"
99
+ set_is_closing( @tun )
100
+ else
101
+ @tun_info[ :src_exts ].each do | src_id, src_ext |
102
+ if src_ext[ :src ].closed? && ( now - src_ext[ :last_continue_at ] > EXPIRE_AFTER )
103
+ puts "#{ Time.new } expire src ext #{ src_id }"
104
+ del_src_ext( src_id )
105
+ end
106
+ end
107
107
  end
108
- end
109
- end
110
108
 
111
- need_trigger = true
112
- end
109
+ @src_infos.each do | src, src_info |
110
+ if src_info[ :last_recv_at ].nil? && ( now - src_info[ :created_at ] > EXPIRE_NEW )
111
+ puts "#{ Time.new } expire src"
112
+ set_is_closing( src )
113
+ end
114
+ end
113
115
 
114
- @src_infos.each do | src, src_info |
115
- is_expired = src_info[ :last_recv_at ].nil? && ( now - src_info[ :created_at ] > EXPIRE_NEW )
116
+ check_at = now
117
+ end
116
118
 
117
- if is_expired
118
- puts "#{ Time.new } expire src"
119
- set_is_closing( src )
120
- need_trigger = true
119
+ # puts "debug2 heartbeat"
120
+ add_tun_ctlmsg( pack_a_heartbeat )
121
+ next_tick
122
+ elsif now - @tun_info[ :last_recv_at ] > EXPIRE_NEW
123
+ # no tund addr
124
+ puts "#{ Time.new } expire new tun"
125
+ set_is_closing( @tun )
126
+ next_tick
121
127
  end
122
128
  end
123
-
124
- if need_trigger
125
- next_tick
126
- end
127
129
  end
128
130
  end
129
131
  end
@@ -1,3 +1,3 @@
1
1
  module P2p2
2
- VERSION = '0.19.0'.freeze
2
+ VERSION = '0.20.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p2p2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takafan