p2p2 0.16.0 → 0.21.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: 0ff46f98fa6ec31663730e3a2f79fa49b0196823c2b99688daaf4f34a8c91bd3
4
- data.tar.gz: 24b9449407dc43bc37a23b77e37e39548ad6313a52eaacea359fec96da786ef1
3
+ metadata.gz: 57e288ebc806c2661f7fa5314a9be3fa16bbb1dc2deac96cfce9f3ca65c97bea
4
+ data.tar.gz: e7872fc331f68d07aaf16b02ea95fd3cdcc09f122acc4b16a51eeb49c5cac854
5
5
  SHA512:
6
- metadata.gz: b86a9e22e55b4b31339877b3fdfc294b24720a9f906347019aa84f59a3274ea93f20ad0552cfd4203dcdc9510bc0920eb7096af490f2397d1b016f795255ef4e
7
- data.tar.gz: 724486226b02372d27b5b32534141e0e4952e5825bf35ab7ef3cc13f5cdea8181f28cf2446fbf71b4e4b5f82d20f6cfe585ba30ce0cb42e582167e6a47e89b7b
6
+ metadata.gz: 665ea51b965d27fdd38fabcdbab4ab7932a9cad6ca70d4503a4c452597979eee6598706b4f8d15670a188742a14375805cb8f9eb2e919c7c48bc8917f2a3a0b4
7
+ data.tar.gz: 7263370934487b9e3937931cbefb13feedce4b14c30fb3c25f6c3119f41fe9d878e4b18a70cbb733267bcc95b6ba39abe097a4bcb1cf35b474cad04dd1379a92
@@ -1,4 +1,4 @@
1
- module P2pd
1
+ module P2p2
2
2
  module Custom
3
3
  def encode( data )
4
4
  # overwrite me, you'll be free
@@ -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 = 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
+ HEARTBEAT_INTERVAL = 5 # 心跳间隔
14
15
  UPDATE_ROOM_INTERVAL = 60 # 刷新房间间隔
15
16
  PEER_ADDR = 1
16
17
  HEARTBEAT = 2
@@ -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,67 +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
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
- 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
- if !@tund.closed? && @tund_info[ :tun_addr ]
113
- if now - @tund_info[ :last_recv_at ] > EXPIRE_AFTER
114
- puts "#{ Time.new } expire tund"
115
- set_is_closing( @tund )
116
- else
117
- # puts "debug1 #{ Time.new } heartbeat"
118
- add_tund_ctlmsg( pack_a_heartbeat )
119
-
120
- @tund_info[ :dst_exts ].each do | dst_local_port, dst_ext |
121
- if dst_ext[ :dst ].closed? && ( now - dst_ext[ :last_continue_at ] > EXPIRE_AFTER )
122
- puts "#{ Time.new } expire dst ext #{ dst_local_port }"
123
- 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
124
116
  end
125
- end
126
- end
127
117
 
128
- need_trigger = true
129
- end
130
-
131
- @dst_infos.each do | dst, dst_info |
132
- 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[ :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
- if is_expired
135
- puts "#{ Time.new } expire dst"
136
- set_is_closing( dst )
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
 
@@ -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[ :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
 
@@ -1,3 +1,3 @@
1
1
  module P2p2
2
- VERSION = '0.16.0'.freeze
2
+ VERSION = '0.21.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.16.0
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takafan