|
| 1 | +# frozen_string_literal: true |
| 2 | +# |
| 3 | +# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com> |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +require_relative 'writable' |
| 24 | + |
| 25 | +require 'forwardable' |
| 26 | + |
| 27 | +module Async |
| 28 | + module HTTP |
| 29 | + module Body |
| 30 | + class Pipe |
| 31 | + extend Forwardable |
| 32 | + |
| 33 | + def initialize(input, output = Writable.new, task: Task.current) |
| 34 | + @input = input |
| 35 | + @output = output |
| 36 | + |
| 37 | + head, tail = IO::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM) |
| 38 | + |
| 39 | + @head = Async::IO::Stream.new(head) |
| 40 | + @tail = tail |
| 41 | + |
| 42 | + @reader = nil |
| 43 | + @writer = nil |
| 44 | + |
| 45 | + task.async(&self.method(:reader)) |
| 46 | + task.async(&self.method(:writer)) |
| 47 | + end |
| 48 | + |
| 49 | + def to_io |
| 50 | + @tail |
| 51 | + end |
| 52 | + |
| 53 | + def close |
| 54 | + @reader&.stop |
| 55 | + @writer&.stop |
| 56 | + |
| 57 | + @tail.close |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + # Read from the @input stream and write to the head of the pipe. |
| 63 | + def reader(task) |
| 64 | + @reader = task |
| 65 | + |
| 66 | + task.annotate "pipe reader" |
| 67 | + |
| 68 | + while chunk = @input.read |
| 69 | + @head.write(chunk) |
| 70 | + @head.flush |
| 71 | + end |
| 72 | + |
| 73 | + @head.close_write |
| 74 | + ensure |
| 75 | + @reader = nil |
| 76 | + @input.close($!) |
| 77 | + |
| 78 | + @head.close if @writer.nil? |
| 79 | + end |
| 80 | + |
| 81 | + # Read from the head of the pipe and write to the @output stream. |
| 82 | + # If the @tail is closed, this will cause chunk to be nil, which in turn will call `@output.close` and `@head.close` |
| 83 | + def writer(task) |
| 84 | + @writer = task |
| 85 | + |
| 86 | + task.annotate "pipe writer" |
| 87 | + |
| 88 | + while chunk = @head.read_partial |
| 89 | + @output.write(chunk) |
| 90 | + end |
| 91 | + ensure |
| 92 | + @writer = nil |
| 93 | + @output.close($!) |
| 94 | + |
| 95 | + @head.close if @reader.nil? |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments