Class: P2p2::P2Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/p2p2/p2_worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(p2pd_host, p2pd_port, room, sdwd_host, sdwd_port, src_chunk_dir, tun_chunk_dir) ⇒ P2Worker

initialize



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/p2p2/p2_worker.rb', line 7

def initialize( p2pd_host, p2pd_port, room, sdwd_host, sdwd_port, src_chunk_dir, tun_chunk_dir )
  @p2pd_addr = Socket.sockaddr_in( p2pd_port, p2pd_host )
  @room = room
  @sdwd_addr = Socket.sockaddr_in( sdwd_port, sdwd_host )
  @src_chunk_dir = src_chunk_dir
  @tun_chunk_dir = tun_chunk_dir
  @custom = P2p2::P2Custom.new
  @mutex = Mutex.new
  @reads = []
  @writes = []
  @roles = {}     # sock => :dotr / :sdwd / :src / :tun

  @src_infos = {} # src => {}


  dotr, dotw = IO.pipe
  @dotw = dotw
  add_read( dotr, :dotr )
  new_a_sdwd
end

Instance Method Details

#loopingObject

looping



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/p2p2/p2_worker.rb', line 29

def looping
  puts "#{ Time.new } looping"
  loop_heartbeat
  loop_check_status

  loop do
    rs, ws = IO.select( @reads, @writes )

    @mutex.synchronize do
      # 先写,再读

      ws.each do | sock |
        case @roles[ sock ]
        when :src
          write_src( sock )
        when :tun
          write_tun( sock )
        end
      end

      rs.each do | sock |
        case @roles[ sock ]
        when :dotr
          read_dotr( sock )
        when :sdwd
          read_sdwd( sock )
        when :src
          read_src( sock )
        when :tun
          read_tun( sock )
        end
      end
    end
  end
rescue Interrupt => e
  puts e.class
  quit!
end

#quit!Object

quit!



70
71
72
73
74
75
76
77
78
79
# File 'lib/p2p2/p2_worker.rb', line 70

def quit!
  if @tun && !@tun.closed? && @tun_info[ :tund_addr ]
    # puts "debug1 send tun fin"

    data = [ 0, TUN_FIN ].pack( 'Q>C' )
    @tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
  end

  # puts "debug1 exit"

  exit
end