p2p2 0.6.9 → 0.7.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.rb +3 -3
- data/lib/p2p2/head.rb +9 -9
- data/lib/p2p2/p1.rb +458 -457
- data/lib/p2p2/p2.rb +495 -494
- data/lib/p2p2/p2pd.rb +286 -286
- data/lib/p2p2/version.rb +3 -3
- data/p2p2.gemspec +29 -29
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a02609d47ec49dddcc0200d7bb242470f30289c170bbdada5e5cc950f8b36de2
|
4
|
+
data.tar.gz: 3e0a9f005faaab4d530c3923b01b5cbf898f8c6e81f4d306bad217036aadb5b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce008a49c906c8066fd7345b564cf69f073dc785fedca6107cca02db5f157fdf06596e24a0454942e05ed61abe92880d08bdf90d6f0382f7cf95bdad4d2f7625
|
7
|
+
data.tar.gz: fc6fc4ba37580abec0c97ee57661c012fa76b2a022cd37d9ad5a5e88b7aafb4bc2a6a3de1d8a584796a07684dc6f8b6d89f0300d42bb401ae3bc14ef0864f648
|
data/lib/p2p2.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
require 'p2p2/p1'
|
2
|
-
require 'p2p2/p2'
|
3
|
-
require 'p2p2/p2pd'
|
1
|
+
require 'p2p2/p1'
|
2
|
+
require 'p2p2/p2'
|
3
|
+
require 'p2p2/p2pd'
|
data/lib/p2p2/head.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
module P2p2
|
2
|
-
PACK_SIZE = 1448 # 包大小
|
3
|
-
CHUNK_SIZE = PACK_SIZE * 1000 # 块大小
|
4
|
-
REP2P_LIMIT = 5 # p2p重试次数。到早了另一头还没从洞里出来,会吃ECONNREFUSED,不慌,再来一发。
|
5
|
-
SET_TITLE = 1
|
6
|
-
PAIRING = 2
|
7
|
-
CTL_CLOSE_ROOM = [ 1 ].pack( 'C' )
|
8
|
-
CTL_CLOSE_APP = [ 2 ].pack( 'C' )
|
9
|
-
end
|
1
|
+
module P2p2
|
2
|
+
PACK_SIZE = 1448 # 包大小
|
3
|
+
CHUNK_SIZE = PACK_SIZE * 1000 # 块大小
|
4
|
+
REP2P_LIMIT = 5 # p2p重试次数。到早了另一头还没从洞里出来,会吃ECONNREFUSED,不慌,再来一发。
|
5
|
+
SET_TITLE = 1
|
6
|
+
PAIRING = 2
|
7
|
+
CTL_CLOSE_ROOM = [ 1 ].pack( 'C' )
|
8
|
+
CTL_CLOSE_APP = [ 2 ].pack( 'C' )
|
9
|
+
end
|
data/lib/p2p2/p1.rb
CHANGED
@@ -1,457 +1,458 @@
|
|
1
|
-
require 'p2p2/head'
|
2
|
-
require 'p2p2/hex'
|
3
|
-
require 'p2p2/version'
|
4
|
-
require 'socket'
|
5
|
-
|
6
|
-
##
|
7
|
-
# P2p2::P1 -
|
8
|
-
#
|
9
|
-
module P2p2
|
10
|
-
class P1
|
11
|
-
|
12
|
-
##
|
13
|
-
# roomd_host 配对服务器ip
|
14
|
-
# roomd_port 配对服务器端口
|
15
|
-
# appd_host 任意的一个应用的ip
|
16
|
-
# appd_port 应用端口
|
17
|
-
# title 约定的房间名
|
18
|
-
# app_chunk_dir 文件缓存目录,缓存app来不及写的流量
|
19
|
-
# p1_chunk_dir 文件缓存目录,缓存p1来不及写的流量
|
20
|
-
#
|
21
|
-
def initialize( roomd_host, roomd_port, appd_host, appd_port, title, app_chunk_dir = '/tmp', p1_chunk_dir = '/tmp' )
|
22
|
-
@roomd_sockaddr = Socket.sockaddr_in( roomd_port, roomd_host )
|
23
|
-
@appd_sockaddr = Socket.sockaddr_in( appd_port, appd_host )
|
24
|
-
@title = title
|
25
|
-
@app_chunk_dir = app_chunk_dir
|
26
|
-
@p1_chunk_dir = p1_chunk_dir
|
27
|
-
@hex = P2p2::Hex.new
|
28
|
-
@mutex = Mutex.new
|
29
|
-
@reads = []
|
30
|
-
@writes = []
|
31
|
-
@closings = []
|
32
|
-
@roles = {} # sock => :ctlr / :room / :p1 / :app
|
33
|
-
@infos = {}
|
34
|
-
@
|
35
|
-
|
36
|
-
ctlr, ctlw = IO.pipe
|
37
|
-
@ctlw = ctlw
|
38
|
-
@roles[ ctlr ] = :ctlr
|
39
|
-
@reads << ctlr
|
40
|
-
|
41
|
-
new_room
|
42
|
-
end
|
43
|
-
|
44
|
-
def looping
|
45
|
-
puts 'looping'
|
46
|
-
|
47
|
-
loop_expire
|
48
|
-
|
49
|
-
loop do
|
50
|
-
rs, ws = IO.select( @reads, @writes )
|
51
|
-
|
52
|
-
@mutex.synchronize do
|
53
|
-
rs.each do | sock |
|
54
|
-
case @roles[ sock ]
|
55
|
-
when :ctlr
|
56
|
-
read_ctlr( sock )
|
57
|
-
when :room
|
58
|
-
read_room( sock )
|
59
|
-
when :p1
|
60
|
-
read_p1( sock )
|
61
|
-
when :app
|
62
|
-
read_app( sock )
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
ws.each do | sock |
|
67
|
-
case @roles[ sock ]
|
68
|
-
when :room
|
69
|
-
write_room( sock )
|
70
|
-
when :p1
|
71
|
-
write_p1( sock )
|
72
|
-
when :app
|
73
|
-
write_app( sock )
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
rescue Interrupt => e
|
79
|
-
puts e.class
|
80
|
-
quit!
|
81
|
-
end
|
82
|
-
|
83
|
-
def quit!
|
84
|
-
exit
|
85
|
-
end
|
86
|
-
|
87
|
-
private
|
88
|
-
|
89
|
-
def loop_expire
|
90
|
-
Thread.new do
|
91
|
-
loop do
|
92
|
-
sleep 60
|
93
|
-
|
94
|
-
if Time.new - @room_info[ :updated_at ] >
|
95
|
-
@mutex.synchronize do
|
96
|
-
@ctlw.write( CTL_CLOSE_ROOM )
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
##
|
104
|
-
# read ctlr
|
105
|
-
#
|
106
|
-
def read_ctlr( ctlr )
|
107
|
-
case ctlr.read( 1 )
|
108
|
-
when CTL_CLOSE_ROOM
|
109
|
-
unless @room.closed?
|
110
|
-
add_closing( @room )
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
##
|
116
|
-
# read room
|
117
|
-
#
|
118
|
-
def read_room( room )
|
119
|
-
begin
|
120
|
-
data = room.read_nonblock( PACK_SIZE )
|
121
|
-
rescue IO::WaitReadable, Errno::EINTR, IO::WaitWritable
|
122
|
-
return
|
123
|
-
rescue Errno::ECONNREFUSED, EOFError, Errno::ECONNRESET => e
|
124
|
-
puts "read room #{ e.class } #{ Time.new }"
|
125
|
-
|
126
|
-
if @
|
127
|
-
raise e
|
128
|
-
end
|
129
|
-
|
130
|
-
sleep 5
|
131
|
-
add_closing( room )
|
132
|
-
@
|
133
|
-
return
|
134
|
-
end
|
135
|
-
|
136
|
-
|
137
|
-
info[
|
138
|
-
info[ :
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
#
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
info[
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
#
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
data =
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
#
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
#
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
#
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
@
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
info[
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
info[ :
|
347
|
-
info[ :
|
348
|
-
info[ :
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
@
|
361
|
-
@
|
362
|
-
@
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
room.
|
380
|
-
room.setsockopt( Socket::
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
@
|
396
|
-
@
|
397
|
-
@
|
398
|
-
@
|
399
|
-
@
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
p1.
|
405
|
-
p1.setsockopt( Socket::
|
406
|
-
p1.
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
rescue
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
app.
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
@
|
451
|
-
@
|
452
|
-
@
|
453
|
-
@
|
454
|
-
@
|
455
|
-
|
456
|
-
|
457
|
-
end
|
1
|
+
require 'p2p2/head'
|
2
|
+
require 'p2p2/hex'
|
3
|
+
require 'p2p2/version'
|
4
|
+
require 'socket'
|
5
|
+
|
6
|
+
##
|
7
|
+
# P2p2::P1 - 内网里的任意应用,访问另一个内网里的应用服务端。p1端。
|
8
|
+
#
|
9
|
+
module P2p2
|
10
|
+
class P1
|
11
|
+
|
12
|
+
##
|
13
|
+
# roomd_host 配对服务器ip
|
14
|
+
# roomd_port 配对服务器端口
|
15
|
+
# appd_host 任意的一个应用的ip
|
16
|
+
# appd_port 应用端口
|
17
|
+
# title 约定的房间名
|
18
|
+
# app_chunk_dir 文件缓存目录,缓存app来不及写的流量
|
19
|
+
# p1_chunk_dir 文件缓存目录,缓存p1来不及写的流量
|
20
|
+
#
|
21
|
+
def initialize( roomd_host, roomd_port, appd_host, appd_port, title, app_chunk_dir = '/tmp', p1_chunk_dir = '/tmp' )
|
22
|
+
@roomd_sockaddr = Socket.sockaddr_in( roomd_port, roomd_host )
|
23
|
+
@appd_sockaddr = Socket.sockaddr_in( appd_port, appd_host )
|
24
|
+
@title = title
|
25
|
+
@app_chunk_dir = app_chunk_dir
|
26
|
+
@p1_chunk_dir = p1_chunk_dir
|
27
|
+
@hex = P2p2::Hex.new
|
28
|
+
@mutex = Mutex.new
|
29
|
+
@reads = []
|
30
|
+
@writes = []
|
31
|
+
@closings = []
|
32
|
+
@roles = {} # sock => :ctlr / :room / :p1 / :app
|
33
|
+
@infos = {}
|
34
|
+
@reconn_room_times = 0
|
35
|
+
|
36
|
+
ctlr, ctlw = IO.pipe
|
37
|
+
@ctlw = ctlw
|
38
|
+
@roles[ ctlr ] = :ctlr
|
39
|
+
@reads << ctlr
|
40
|
+
|
41
|
+
new_room
|
42
|
+
end
|
43
|
+
|
44
|
+
def looping
|
45
|
+
puts 'looping'
|
46
|
+
|
47
|
+
loop_expire
|
48
|
+
|
49
|
+
loop do
|
50
|
+
rs, ws = IO.select( @reads, @writes )
|
51
|
+
|
52
|
+
@mutex.synchronize do
|
53
|
+
rs.each do | sock |
|
54
|
+
case @roles[ sock ]
|
55
|
+
when :ctlr
|
56
|
+
read_ctlr( sock )
|
57
|
+
when :room
|
58
|
+
read_room( sock )
|
59
|
+
when :p1
|
60
|
+
read_p1( sock )
|
61
|
+
when :app
|
62
|
+
read_app( sock )
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
ws.each do | sock |
|
67
|
+
case @roles[ sock ]
|
68
|
+
when :room
|
69
|
+
write_room( sock )
|
70
|
+
when :p1
|
71
|
+
write_p1( sock )
|
72
|
+
when :app
|
73
|
+
write_app( sock )
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
rescue Interrupt => e
|
79
|
+
puts e.class
|
80
|
+
quit!
|
81
|
+
end
|
82
|
+
|
83
|
+
def quit!
|
84
|
+
exit
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def loop_expire
|
90
|
+
Thread.new do
|
91
|
+
loop do
|
92
|
+
sleep 60
|
93
|
+
|
94
|
+
if Time.new - @room_info[ :updated_at ] > 1800
|
95
|
+
@mutex.synchronize do
|
96
|
+
@ctlw.write( CTL_CLOSE_ROOM )
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# read ctlr
|
105
|
+
#
|
106
|
+
def read_ctlr( ctlr )
|
107
|
+
case ctlr.read( 1 )
|
108
|
+
when CTL_CLOSE_ROOM
|
109
|
+
unless @room.closed?
|
110
|
+
add_closing( @room )
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
##
|
116
|
+
# read room
|
117
|
+
#
|
118
|
+
def read_room( room )
|
119
|
+
begin
|
120
|
+
data = room.read_nonblock( PACK_SIZE )
|
121
|
+
rescue IO::WaitReadable, Errno::EINTR, IO::WaitWritable
|
122
|
+
return
|
123
|
+
rescue Errno::ECONNREFUSED, EOFError, Errno::ECONNRESET => e
|
124
|
+
puts "read room #{ e.class } #{ Time.new }"
|
125
|
+
|
126
|
+
if @reconn_room_times >= 5
|
127
|
+
raise e
|
128
|
+
end
|
129
|
+
|
130
|
+
sleep 5
|
131
|
+
add_closing( room )
|
132
|
+
@reconn_room_times += 1
|
133
|
+
return
|
134
|
+
end
|
135
|
+
|
136
|
+
@reconn_room_times = 0
|
137
|
+
info = @infos[ room ]
|
138
|
+
info[ :p2_sockaddr ] = data
|
139
|
+
info[ :updated_at ] = Time.new
|
140
|
+
new_p1
|
141
|
+
end
|
142
|
+
|
143
|
+
##
|
144
|
+
# read p1
|
145
|
+
#
|
146
|
+
def read_p1( p1 )
|
147
|
+
begin
|
148
|
+
data = p1.read_nonblock( PACK_SIZE )
|
149
|
+
rescue IO::WaitReadable, Errno::EINTR, IO::WaitWritable
|
150
|
+
return
|
151
|
+
rescue Errno::ECONNREFUSED => e
|
152
|
+
if @room_info[ :renew_p1_times ] >= REP2P_LIMIT
|
153
|
+
raise e
|
154
|
+
end
|
155
|
+
|
156
|
+
sleep 1
|
157
|
+
add_closing( p1 )
|
158
|
+
info = @infos[ p1 ]
|
159
|
+
info[ :need_renew ] = true
|
160
|
+
@room_info[ :renew_p1_times ] += 1
|
161
|
+
return
|
162
|
+
rescue Exception => e
|
163
|
+
add_closing( p1 )
|
164
|
+
return
|
165
|
+
end
|
166
|
+
|
167
|
+
info = @infos[ p1 ]
|
168
|
+
|
169
|
+
if info[ :app ].nil? || info[ :app ].closed?
|
170
|
+
add_closing( p1 )
|
171
|
+
return
|
172
|
+
end
|
173
|
+
|
174
|
+
if info[ :need_decode ]
|
175
|
+
len = data[ 0, 2 ].unpack( 'n' ).first
|
176
|
+
head = @hex.decode( data[ 2, len ] )
|
177
|
+
data = head + data[ ( 2 + len )..-1 ]
|
178
|
+
info[ :need_decode ] = false
|
179
|
+
end
|
180
|
+
|
181
|
+
add_write( info[ :app ], data )
|
182
|
+
@room_info[ :updated_at ] = Time.new
|
183
|
+
end
|
184
|
+
|
185
|
+
##
|
186
|
+
# read app
|
187
|
+
#
|
188
|
+
def read_app( app )
|
189
|
+
begin
|
190
|
+
data = app.read_nonblock( PACK_SIZE )
|
191
|
+
rescue IO::WaitReadable, Errno::EINTR, IO::WaitWritable
|
192
|
+
return
|
193
|
+
rescue Exception => e
|
194
|
+
add_closing( app )
|
195
|
+
return
|
196
|
+
end
|
197
|
+
|
198
|
+
info = @infos[ app ]
|
199
|
+
|
200
|
+
if info[ :p1 ].nil? || info[ :p1 ].closed?
|
201
|
+
add_closing( app )
|
202
|
+
return
|
203
|
+
end
|
204
|
+
|
205
|
+
if info[ :need_encode ]
|
206
|
+
data = @hex.encode( data )
|
207
|
+
data = [ [ data.size ].pack( 'n' ), data ].join
|
208
|
+
info[ :need_encode ] = false
|
209
|
+
end
|
210
|
+
|
211
|
+
add_write( info[ :p1 ], data )
|
212
|
+
@room_info[ :updated_at ] = Time.new
|
213
|
+
end
|
214
|
+
|
215
|
+
##
|
216
|
+
# write room
|
217
|
+
#
|
218
|
+
def write_room( room )
|
219
|
+
if @closings.include?( room )
|
220
|
+
close_sock( room )
|
221
|
+
new_room
|
222
|
+
return
|
223
|
+
end
|
224
|
+
|
225
|
+
info = @infos[ room ]
|
226
|
+
room.write( info[ :wbuff ] )
|
227
|
+
@writes.delete( room )
|
228
|
+
end
|
229
|
+
|
230
|
+
##
|
231
|
+
# write p1
|
232
|
+
#
|
233
|
+
def write_p1( p1 )
|
234
|
+
if @closings.include?( p1 )
|
235
|
+
info = close_sock( p1 )
|
236
|
+
|
237
|
+
if info[ :need_renew ]
|
238
|
+
new_p1
|
239
|
+
return
|
240
|
+
end
|
241
|
+
|
242
|
+
unless info[ :app ].closed?
|
243
|
+
add_closing( info[ :app ] )
|
244
|
+
end
|
245
|
+
|
246
|
+
unless @room.closed?
|
247
|
+
add_closing( @room )
|
248
|
+
end
|
249
|
+
|
250
|
+
return
|
251
|
+
end
|
252
|
+
|
253
|
+
info = @infos[ p1 ]
|
254
|
+
data, from = get_buff( info )
|
255
|
+
|
256
|
+
if data.empty?
|
257
|
+
@writes.delete( p1 )
|
258
|
+
return
|
259
|
+
end
|
260
|
+
|
261
|
+
begin
|
262
|
+
written = p1.write_nonblock( data )
|
263
|
+
rescue IO::WaitWritable, Errno::EINTR, IO::WaitReadable
|
264
|
+
return
|
265
|
+
rescue Exception => e
|
266
|
+
add_closing( p1 )
|
267
|
+
return
|
268
|
+
end
|
269
|
+
|
270
|
+
data = data[ written..-1 ]
|
271
|
+
info[ from ] = data
|
272
|
+
end
|
273
|
+
|
274
|
+
##
|
275
|
+
# write app
|
276
|
+
#
|
277
|
+
def write_app( app )
|
278
|
+
if @closings.include?( app )
|
279
|
+
info = close_sock( app )
|
280
|
+
|
281
|
+
unless info[ :p1 ].closed?
|
282
|
+
add_closing( info[ :p1 ] )
|
283
|
+
end
|
284
|
+
|
285
|
+
return
|
286
|
+
end
|
287
|
+
|
288
|
+
info = @infos[ app ]
|
289
|
+
data, from = get_buff( info )
|
290
|
+
|
291
|
+
if data.empty?
|
292
|
+
@writes.delete( app )
|
293
|
+
return
|
294
|
+
end
|
295
|
+
|
296
|
+
begin
|
297
|
+
written = app.write_nonblock( data )
|
298
|
+
rescue IO::WaitWritable, Errno::EINTR, IO::WaitReadable
|
299
|
+
return
|
300
|
+
rescue Exception => e
|
301
|
+
add_closing( app )
|
302
|
+
return
|
303
|
+
end
|
304
|
+
|
305
|
+
data = data[ written..-1 ]
|
306
|
+
info[ from ] = data
|
307
|
+
end
|
308
|
+
|
309
|
+
def get_buff( info )
|
310
|
+
data, from = info[ :cache ], :cache
|
311
|
+
|
312
|
+
if data.empty?
|
313
|
+
if info[ :chunks ].any?
|
314
|
+
path = File.join( info[ :chunk_dir ], info[ :chunks ].shift )
|
315
|
+
data = info[ :cache ] = IO.binread( path )
|
316
|
+
|
317
|
+
begin
|
318
|
+
File.delete( path )
|
319
|
+
rescue Errno::ENOENT
|
320
|
+
end
|
321
|
+
else
|
322
|
+
data, from = info[ :wbuff ], :wbuff
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
[ data, from ]
|
327
|
+
end
|
328
|
+
|
329
|
+
def add_closing( sock )
|
330
|
+
unless @closings.include?( sock )
|
331
|
+
@reads.delete( sock )
|
332
|
+
@closings << sock
|
333
|
+
end
|
334
|
+
|
335
|
+
add_write( sock )
|
336
|
+
end
|
337
|
+
|
338
|
+
def add_write( sock, data = nil )
|
339
|
+
if data
|
340
|
+
info = @infos[ sock ]
|
341
|
+
info[ :wbuff ] << data
|
342
|
+
|
343
|
+
if info[ :wbuff ].size >= CHUNK_SIZE
|
344
|
+
filename = [ info[ :filename ], info[ :chunk_seed ] ].join( '.' )
|
345
|
+
chunk_path = File.join( info[ :chunk_dir ], filename )
|
346
|
+
IO.binwrite( chunk_path, info[ :wbuff ] )
|
347
|
+
info[ :chunks ] << filename
|
348
|
+
info[ :chunk_seed ] += 1
|
349
|
+
info[ :wbuff ].clear
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
unless @writes.include?( sock )
|
354
|
+
@writes << sock
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
def close_sock( sock )
|
359
|
+
sock.close
|
360
|
+
@reads.delete( sock )
|
361
|
+
@writes.delete( sock )
|
362
|
+
@closings.delete( sock )
|
363
|
+
@roles.delete( sock )
|
364
|
+
info = @infos.delete( sock )
|
365
|
+
|
366
|
+
if info && info[ :chunks ]
|
367
|
+
info[ :chunks ].each do | filename |
|
368
|
+
begin
|
369
|
+
File.delete( File.join( info[ :chunk_dir ], filename ) )
|
370
|
+
rescue Errno::ENOENT
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
info
|
376
|
+
end
|
377
|
+
|
378
|
+
def new_room
|
379
|
+
room = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
380
|
+
room.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
|
381
|
+
room.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 )
|
382
|
+
|
383
|
+
begin
|
384
|
+
room.connect_nonblock( @roomd_sockaddr )
|
385
|
+
rescue IO::WaitWritable, Errno::EINTR
|
386
|
+
end
|
387
|
+
|
388
|
+
title = @title.unpack( "C*" ).map{ | c | c.chr }.join
|
389
|
+
room_info = {
|
390
|
+
wbuff: [ [ SET_TITLE ].pack( 'C' ), title ].join,
|
391
|
+
p2_sockaddr: nil,
|
392
|
+
renew_p1_times: 0,
|
393
|
+
updated_at: Time.new
|
394
|
+
}
|
395
|
+
@room = room
|
396
|
+
@room_info = room_info
|
397
|
+
@roles[ room ] = :room
|
398
|
+
@infos[ room ] = room_info
|
399
|
+
@reads << room
|
400
|
+
@writes << room
|
401
|
+
end
|
402
|
+
|
403
|
+
def new_p1
|
404
|
+
p1 = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
405
|
+
p1.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
|
406
|
+
p1.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 )
|
407
|
+
p1.bind( @room.local_address ) # use the hole
|
408
|
+
|
409
|
+
begin
|
410
|
+
p1.connect_nonblock( @room_info[ :p2_sockaddr ] )
|
411
|
+
rescue IO::WaitWritable, Errno::EINTR
|
412
|
+
rescue Exception => e
|
413
|
+
puts "connect p2 #{ e.class } #{ Time.new }"
|
414
|
+
p1.close
|
415
|
+
add_closing( @room )
|
416
|
+
return
|
417
|
+
end
|
418
|
+
|
419
|
+
app = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
420
|
+
app.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 )
|
421
|
+
|
422
|
+
begin
|
423
|
+
app.connect_nonblock( @appd_sockaddr )
|
424
|
+
rescue IO::WaitWritable, Errno::EINTR
|
425
|
+
end
|
426
|
+
|
427
|
+
p1_info = {
|
428
|
+
wbuff: '',
|
429
|
+
cache: '',
|
430
|
+
filename: [ Process.pid, p1.object_id ].join( '-' ),
|
431
|
+
chunk_dir: @p1_chunk_dir,
|
432
|
+
chunks: [],
|
433
|
+
chunk_seed: 0,
|
434
|
+
need_decode: true,
|
435
|
+
need_renew: false,
|
436
|
+
app: app
|
437
|
+
}
|
438
|
+
|
439
|
+
app_info = {
|
440
|
+
wbuff: '',
|
441
|
+
cache: '',
|
442
|
+
filename: [ Process.pid, app.object_id ].join( '-' ),
|
443
|
+
chunk_dir: @app_chunk_dir,
|
444
|
+
chunks: [],
|
445
|
+
chunk_seed: 0,
|
446
|
+
need_encode: true,
|
447
|
+
p1: p1
|
448
|
+
}
|
449
|
+
|
450
|
+
@roles[ p1 ] = :p1
|
451
|
+
@infos[ p1 ] = p1_info
|
452
|
+
@reads << p1
|
453
|
+
@roles[ app ] = :app
|
454
|
+
@infos[ app ] = app_info
|
455
|
+
@reads << app
|
456
|
+
end
|
457
|
+
end
|
458
|
+
end
|