X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=ucspi.go;h=509e19120da843456415f5301734010157fd6658;hb=022bdcd88f70a8e9e1865bc2195558f2833960dc;hp=74c5b7918afa3e8f7f709c187296c528cecf764a;hpb=88e89726fc19cd0e722d2526cbbf28afc5571127;p=uploader.git diff --git a/ucspi.go b/ucspi.go index 74c5b79..509e191 100644 --- a/ucspi.go +++ b/ucspi.go @@ -1,6 +1,6 @@ /* uploader -- simplest form file uploader -Copyright (C) 2018-2021 Sergey Matveev +Copyright (C) 2018-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,12 +18,15 @@ along with this program. If not, see . package main import ( + "io" "net" "net/http" "os" "time" ) +var aLongTimeAgo = time.Unix(1, 0) + type UCSPIAddr struct { ip string port string @@ -33,9 +36,28 @@ func (addr *UCSPIAddr) Network() string { return "tcp" } func (addr *UCSPIAddr) String() string { return addr.ip + ":" + addr.port } -type UCSPIConn struct{} +type UCSPIConn struct { + eof chan struct{} +} -func (conn *UCSPIConn) Read(b []byte) (int, error) { return os.Stdin.Read(b) } +type ReadResult struct { + n int + err error +} + +func (conn *UCSPIConn) Read(b []byte) (int, error) { + c := make(chan ReadResult) + go func() { + n, err := os.Stdin.Read(b) + c <- ReadResult{n, err} + }() + select { + case res := <-c: + return res.n, res.err + case <-conn.eof: + return 0, io.EOF + } +} func (conn *UCSPIConn) Write(b []byte) (int, error) { return os.Stdout.Write(b) } @@ -43,7 +65,7 @@ func (conn *UCSPIConn) Close() error { if err := os.Stdin.Close(); err != nil { return err } - return os.Stdin.Close() + return os.Stdout.Close() } func (conn *UCSPIConn) LocalAddr() net.Addr { @@ -62,6 +84,12 @@ func (conn *UCSPIConn) SetDeadline(t time.Time) error { } func (conn *UCSPIConn) SetReadDeadline(t time.Time) error { + // An ugly hack to forcefully terminate pending read. + // net/http calls SetReadDeadline(aLongTimeAgo), but file + // descriptors are not capable to exit immediately that way. + if t.Equal(aLongTimeAgo) { + conn.eof <- struct{}{} + } return os.Stdin.SetReadDeadline(t) } @@ -82,7 +110,8 @@ func (ucspi *UCSPI) Accept() (net.Conn, error) { return nil, UCSPIAlreadyAccepted{} } ucspi.accepted = true - return &UCSPIConn{}, nil + conn := UCSPIConn{eof: make(chan struct{}, 1)} + return &conn, nil } func (ucspi *UCSPI) Close() error {