Skip to content

Commit

Permalink
show pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Jan 23, 2021
1 parent bd17ddd commit 4a4220b
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions pipe.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package warp

import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"net"
"net/textproto"
"strings"
"sync"
)

Expand All @@ -18,25 +22,54 @@ type Pipe struct {
func (p *Pipe) Do() {
p.Req = new(bytes.Buffer)
p.Res = new(bytes.Buffer)

close := func() {
defer p.Dst.Close()
defer p.Src.Close()
defer log.Print("connection closed")
}
var once sync.Once

// src ===> dst
go func() {
w := io.MultiWriter(p.Dst, p.Req)
io.Copy(w, p.Src)
once.Do(close)
once.Do(p.close())
}()

// src <=== dst
go func() {
w := io.MultiWriter(p.Src, p.Res)
io.Copy(w, p.Dst)
once.Do(close)
once.Do(p.close())
}()
}

func (p *Pipe) data(b *bytes.Buffer) ([]string, error) {
var data []string
r := textproto.NewReader(bufio.NewReader(b))
for {
line, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
return data, err
}
data = append(data, line)
}
return data, nil
}

func (p *Pipe) close() func() {
return func() {
defer p.Dst.Close()
defer p.Src.Close()
defer log.Print("connection closed")

req, err := p.data(p.Req)
if err != nil {
fmt.Printf("%#v\n", err)
}
fmt.Printf("%s\n", strings.Join(req, "\n"))
res, err := p.data(p.Res)
if err != nil {
fmt.Printf("%#v\n", err)
}
fmt.Printf("%s\n", strings.Join(res, "\n"))
}
}

0 comments on commit 4a4220b

Please sign in to comment.