p2p2 0.16.0 → 0.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/p2p2/custom.rb +1 -1
- data/lib/p2p2/head.rb +2 -1
- data/lib/p2p2/p1_worker.rb +51 -51
- data/lib/p2p2/p2_worker.rb +36 -31
- data/lib/p2p2/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57e288ebc806c2661f7fa5314a9be3fa16bbb1dc2deac96cfce9f3ca65c97bea
|
4
|
+
data.tar.gz: e7872fc331f68d07aaf16b02ea95fd3cdcc09f122acc4b16a51eeb49c5cac854
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 665ea51b965d27fdd38fabcdbab4ab7932a9cad6ca70d4503a4c452597979eee6598706b4f8d15670a188742a14375805cb8f9eb2e919c7c48bc8917f2a3a0b4
|
7
|
+
data.tar.gz: 7263370934487b9e3937931cbefb13feedce4b14c30fb3c25f6c3119f41fe9d878e4b18a70cbb733267bcc95b6ba39abe097a4bcb1cf35b474cad04dd1379a92
|
data/lib/p2p2/custom.rb
CHANGED
data/lib/p2p2/head.rb
CHANGED
@@ -6,11 +6,12 @@ module P2p2
|
|
6
6
|
RESUME_BELOW = 50_000 # 降到多少以下恢复写
|
7
7
|
EXPIRE_NEW = 10 # 创建之后多久没有流量进来,过期
|
8
8
|
EXPIRE_AFTER = 300 # 多久没有新流量,过期
|
9
|
-
CHECK_EXPIRE_INTERVAL =
|
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
|
+
HEARTBEAT_INTERVAL = 5 # 心跳间隔
|
14
15
|
UPDATE_ROOM_INTERVAL = 60 # 刷新房间间隔
|
15
16
|
PEER_ADDR = 1
|
16
17
|
HEARTBEAT = 2
|
data/lib/p2p2/p1_worker.rb
CHANGED
@@ -28,8 +28,7 @@ module P2p2
|
|
28
28
|
#
|
29
29
|
def looping
|
30
30
|
puts "#{ Time.new } looping"
|
31
|
-
|
32
|
-
loop_check_expire
|
31
|
+
loop_heartbeat
|
33
32
|
loop_check_status
|
34
33
|
|
35
34
|
loop do
|
@@ -80,67 +79,65 @@ module P2p2
|
|
80
79
|
private
|
81
80
|
|
82
81
|
##
|
83
|
-
# loop
|
82
|
+
# loop heartbeat
|
84
83
|
#
|
85
|
-
def
|
84
|
+
def loop_heartbeat( check_at = Time.new )
|
86
85
|
Thread.new do
|
87
86
|
loop do
|
88
|
-
sleep
|
87
|
+
sleep HEARTBEAT_INTERVAL
|
89
88
|
|
90
89
|
@mutex.synchronize do
|
91
|
-
if !@tund.closed? && @tund_info[ :peer_addr ].nil?
|
92
|
-
add_tund_ctlmsg( @room, @p2pd_addr )
|
93
|
-
next_tick
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
##
|
101
|
-
# loop check expire
|
102
|
-
#
|
103
|
-
def loop_check_expire
|
104
|
-
Thread.new do
|
105
|
-
loop do
|
106
|
-
sleep CHECK_EXPIRE_INTERVAL
|
107
|
-
|
108
|
-
@mutex.synchronize do
|
109
|
-
need_trigger = false
|
110
90
|
now = Time.new
|
111
91
|
|
112
|
-
|
113
|
-
if
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
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
|
124
116
|
end
|
125
|
-
end
|
126
|
-
end
|
127
117
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
118
|
+
# puts "debug2 heartbeat"
|
119
|
+
add_tund_ctlmsg( pack_a_heartbeat )
|
120
|
+
next_tick
|
121
|
+
elsif now - @tund_info[ :created_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
|
133
135
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
need_trigger = true
|
136
|
+
# puts "debug2 update room"
|
137
|
+
add_tund_ctlmsg( data, @p2pd_addr )
|
138
|
+
next_tick
|
138
139
|
end
|
139
140
|
end
|
140
|
-
|
141
|
-
if need_trigger
|
142
|
-
next_tick
|
143
|
-
end
|
144
141
|
end
|
145
142
|
end
|
146
143
|
end
|
@@ -252,6 +249,8 @@ module P2p2
|
|
252
249
|
# is match tun addr
|
253
250
|
#
|
254
251
|
def is_match_tun_addr( addrinfo )
|
252
|
+
return false unless @tund_info[ :tun_addr ]
|
253
|
+
|
255
254
|
from_addr = addrinfo.to_sockaddr
|
256
255
|
|
257
256
|
if from_addr != @tund_info[ :tun_addr ]
|
@@ -260,6 +259,7 @@ module P2p2
|
|
260
259
|
end
|
261
260
|
|
262
261
|
@tund_info[ :last_recv_at ] = Time.new
|
262
|
+
|
263
263
|
true
|
264
264
|
end
|
265
265
|
|
data/lib/p2p2/p2_worker.rb
CHANGED
@@ -28,7 +28,7 @@ module P2p2
|
|
28
28
|
#
|
29
29
|
def looping
|
30
30
|
puts "#{ Time.new } looping"
|
31
|
-
|
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
|
84
|
+
# loop heartbeat
|
85
85
|
#
|
86
|
-
def
|
86
|
+
def loop_heartbeat( check_at = Time.new )
|
87
87
|
Thread.new do
|
88
88
|
loop do
|
89
|
-
sleep
|
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[ :
|
96
|
-
if
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
|
112
|
-
|
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
|
-
|
115
|
-
|
116
|
+
check_at = now
|
117
|
+
end
|
116
118
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
119
|
+
# puts "debug2 heartbeat"
|
120
|
+
add_tun_ctlmsg( pack_a_heartbeat )
|
121
|
+
next_tick
|
122
|
+
elsif now - @tun_info[ :created_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
|
@@ -275,6 +277,8 @@ module P2p2
|
|
275
277
|
# is match tund addr
|
276
278
|
#
|
277
279
|
def is_match_tund_addr( addrinfo )
|
280
|
+
return false unless @tun_info[ :tund_addr ]
|
281
|
+
|
278
282
|
from_addr = addrinfo.to_sockaddr
|
279
283
|
|
280
284
|
if from_addr != @tun_info[ :tund_addr ]
|
@@ -283,6 +287,7 @@ module P2p2
|
|
283
287
|
end
|
284
288
|
|
285
289
|
@tun_info[ :last_recv_at ] = Time.new
|
290
|
+
|
286
291
|
true
|
287
292
|
end
|
288
293
|
|
data/lib/p2p2/version.rb
CHANGED