Class: Cybertrain::Sink
- Inherits:
-
Object
- Object
- Cybertrain::Sink
- Defined in:
- cybertrain/http/body_writer.rb
Overview
The socket of a streaming response, as a BodyWriter sees it. #write
sends one chunk (with the Transfer-Encoding: chunked framing when the
response is chunked, HTTP/1.1; as it is for HTTP/1.0 and for an
upgraded connection); it never raises: once a write fails (EPIPE,
ECONNRESET) or a read sees EOF, #closed? is true and later writes do
nothing, so a writer’s loop only has to look at #closed?.
Instance Method Summary collapse
-
#closed? ⇒ Boolean
True once the client went away: a write failed or a read saw EOF (also when the server shut the read side because it is stopping).
-
#socket ⇒ TCPSocket
The connection’s socket, for a writer that speaks its own protocol on it (the WebSocket session after a 101).
-
#wait_closed(seconds) ⇒ Boolean
Waits up to
secondsfor the client to close its end; true if it did (or already had). -
#write(bytes) ⇒ nil
Sends one chunk; “” sends nothing (an empty chunk would end a chunked body).
Instance Method Details
#closed? ⇒ Boolean
True once the client went away: a write failed or a read saw EOF (also when the server shut the read side because it is stopping).
127 128 129 |
# File 'cybertrain/http/body_writer.rb', line 127 def closed? @closed end |
#socket ⇒ TCPSocket
The connection’s socket, for a writer that speaks its own protocol on
it (the WebSocket session after a 101). Every read on it must be
preceded by wait_readable (spikes/NOTES.md rule 20).
93 94 95 |
# File 'cybertrain/http/body_writer.rb', line 93 def socket @socket end |
#wait_closed(seconds) ⇒ Boolean
Waits up to seconds for the client to close its end; true if it did
(or already had). Anything else the client sends is read and dropped
(an SSE client sends nothing). wait_readable before the read
(spikes/NOTES.md rule 20); 0 only looks.
138 139 140 141 142 143 144 145 146 |
# File 'cybertrain/http/body_writer.rb', line 138 def wait_closed(seconds) return true if @closed sock = @socket return false if sock.wait_readable(seconds).nil? sink_drain(sock) @closed end |
#write(bytes) ⇒ nil
Sends one chunk; “” sends nothing (an empty chunk would end a chunked body). Returns nil, also when the client is gone.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'cybertrain/http/body_writer.rb', line 102 def write(bytes) return nil if @closed # A new local (spikes/NOTES.md rule 62); appended with << so bytes # after a NUL survive (rule 60). data = bytes.to_s n = data.bytesize return nil if n == 0 frame = +"" if @chunked frame << n.to_s(16) frame << "\r\n" frame << data frame << "\r\n" else frame << data end sink_send(frame) end |