forked from tg123/sshpiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
40 lines (33 loc) · 905 Bytes
/
listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ioconn
import (
"io"
"net"
)
type singleConnListener struct {
conn
used chan int
}
// Accept implements net.Listener
func (l *singleConnListener) Accept() (net.Conn, error) {
<-l.used
return &l.conn, nil
}
// Addr implements net.Listener
func (l *singleConnListener) Addr() net.Addr {
return l.conn.LocalAddr()
}
// Close implements net.Listener
func (l *singleConnListener) Close() error {
return l.conn.Close()
}
// ListenFromSingleIO creates a net.Listener from a single input/output connection.
// It takes an io.ReadCloser and an io.WriteCloser as parameters and returns a net.Listener and an error.
// The returned net.Listener can be used to accept incoming connections.
func ListenFromSingleIO(in io.ReadCloser, out io.WriteCloser) (net.Listener, error) {
l := &singleConnListener{
conn{in, out},
make(chan int, 1),
}
l.used <- 1 // ready for accept
return l, nil
}