Class: Baykit::BayServer::Docker::Terminal::HijackedDataSender

Inherits:
Object
  • Object
show all
Includes:
Agent, Agent::NonBlockingHandler::ChannelListener, Http::H1, Protocol, Util
Defined in:
lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb

Overview

Send data of hijacked response

Constant Summary collapse

DEFAULT_FREAD_BUF_SIZE =
8192

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tur, fully) ⇒ HijackedDataSender



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 40

def initialize(tur, fully)
  @tour = tur
  @tour_id = @tour.tour_id
  @fully = fully
  @file_buf_list = []
  @read_buf_size = tour.ship.protocol_handler.max_res_packet_data_size

  if @fully
    @packet_store = PacketStore.new(tur.ship, H1PacketFactory.new)
    @command_unpacker = H1CommandUnPacker.new(self, false)
    @packet_unpacker = H1PacketUnPacker.new(@command_unpacker, @packet_store)
  end
  reset
end

Instance Attribute Details

#command_unpackerObject (readonly)

Returns the value of attribute command_unpacker.



36
37
38
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 36

def command_unpacker
  @command_unpacker
end

#cur_file_idxObject (readonly)

Returns the value of attribute cur_file_idx.



29
30
31
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 29

def cur_file_idx
  @cur_file_idx
end

#file_buf_listObject (readonly)

Returns the value of attribute file_buf_list.



28
29
30
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 28

def file_buf_list
  @file_buf_list
end

#file_wrote_lenObject (readonly)

Returns the value of attribute file_wrote_len.



27
28
29
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 27

def file_wrote_len
  @file_wrote_len
end

#fullyObject (readonly)

Returns the value of attribute fully.



25
26
27
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 25

def fully
  @fully
end

#packet_storeObject (readonly)

Returns the value of attribute packet_store.



34
35
36
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 34

def packet_store
  @packet_store
end

#packet_unpackerObject (readonly)

Returns the value of attribute packet_unpacker.



35
36
37
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 35

def packet_unpacker
  @packet_unpacker
end

#pipe_ioObject (readonly)

Returns the value of attribute pipe_io.



32
33
34
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 32

def pipe_io
  @pipe_io
end

#read_buf_sizeObject (readonly)

Returns the value of attribute read_buf_size.



30
31
32
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 30

def read_buf_size
  @read_buf_size
end

#tourObject (readonly)

Returns the value of attribute tour.



23
24
25
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 23

def tour
  @tour
end

#tour_idObject (readonly)

Returns the value of attribute tour_id.



24
25
26
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 24

def tour_id
  @tour_id
end

Instance Method Details

#check_timeout(chk_fd, duration) ⇒ Object



94
95
96
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 94

def check_timeout(chk_fd, duration)
  BayLog.debug "#{self} Hijack timeout(Ignore)"
end

#handle_content(cmd) ⇒ Object

Implements H1CommandHandler Fully hijacked mode



133
134
135
136
137
138
139
140
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 133

def handle_content(cmd)
  available = @tour.res.send_content(@tour_id, cmd.buf, cmd.start, cmd.len)
  if !available
    NextSocketAction::SUSPEND
  else
    NextSocketAction::CONTINUE
  end
end

#handle_header(cmd) ⇒ Object

Implements H1CommandHandler Fully hijacked mode



120
121
122
123
124
125
126
127
128
129
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 120

def handle_header(cmd)
  cmd.headers.each do |nv|
    @tour.res.headers.add(nv[0], nv[1])
  end

  @tour.res.headers.status = cmd.status != nil ? cmd.status : HttpStatus::OK
  @tour.send_headers(@tour_id)

  return NextSocketAction::CONTINUE
end

#on_closed(chk_fd) ⇒ Object



105
106
107
108
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 105

def on_closed(chk_fd)
  BayLog.debug "#{self} Hijack Closed(Ignore)"
  check_socket(chk_fd)
end

#on_error(chk_fd, e) ⇒ Object



98
99
100
101
102
103
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 98

def on_error(chk_fd, e)
  BayLog.debug "#{self} Hijack Error"
  check_socket(chk_fd)

  BayLog.error_e e
end

#on_readable(chk_fd) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 64

def on_readable(chk_fd)
  BayLog.debug "#{self} Hijack Readable"
  check_socket(chk_fd)

  buf = new_file_buffer
  begin
    @pipe_io.read_nonblock(@read_buf_size, buf)
  rescue EOFError => e
    BayLog.debug "#{self} Hijack EOF"
    @tour.res.end_content(@tour_id)
    return NextSocketAction::CLOSE
  end

  @file_wrote_len += buf.length

  BayLog.debug "#{self} read hijack #{buf.length} bytes: total=#{@file_wrote_len}"

  if @fully
    return @packet_unpacker.bytes_received(buf)
  else
    available = @tour.res.send_content(@tour_id, buf, 0, buf.length)
    if !available
      NextSocketAction::SUSPEND
    else
      NextSocketAction::CONTINUE
    end
  end

end

#resetObject



55
56
57
58
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 55

def reset
  @file_wrote_len = 0
  @cur_file_idx = -1
end

#resumeObject



142
143
144
145
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 142

def resume
  BayLog.debug("#{self} resume")
  @tour.ship.agent.non_blocking_handler.ask_to_read(@pipe_io)
end

#send_pipe_data(io) ⇒ Object



111
112
113
114
115
116
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 111

def send_pipe_data(io)
  BayLog.debug("#{self} Send hijacked data #{io.inspect}")
  @pipe_io = io
  @file_wrote_len = 0
  @tour.ship.agent.non_blocking_handler.ask_to_read(@pipe_io)
end

#shipObject



60
61
62
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 60

def ship
  @tour.ship
end

#to_sObject



147
148
149
# File 'lib/baykit/bayserver/docker/terminal/hijacked_data_sender.rb', line 147

def to_s
  "hijack[#{@pipe_io.inspect}]"
end