dont close until they close themself

master
bel 2023-04-15 17:36:06 -06:00
parent 7d82bdcdfe
commit 5fccea247e
1 changed files with 6 additions and 15 deletions

21
tcp.go
View File

@ -1,7 +1,6 @@
package main
import (
"context"
"crypto/tls"
"io"
"log"
@ -44,27 +43,19 @@ func (tcp TCP) Listen() error {
}
defer conn2.Close()
ctx, can := context.WithCancel(context.Background())
defer can()
errc := make(chan error)
go func() {
_, err := io.Copy(conn, conn2)
select {
case errc <- err:
case <-ctx.Done():
close(errc)
}
errc <- err
}()
go func() {
_, err := io.Copy(conn2, conn)
select {
case errc <- err:
case <-ctx.Done():
close(errc)
}
errc <- err
}()
<-errc
for i := 0; i < 2; i++ {
<-errc
}
close(errc)
}()
}
}