| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package jsonrpc2 |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"io" |
| 10 |
"net" |
| 11 |
"os" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
type NetListenOptions struct { |
| 20 |
NetListenConfig net.ListenConfig |
| 21 |
NetDialer net.Dialer |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func NetListener(ctx context.Context, network, address string, options NetListenOptions) (Listener, error) { |
| 26 |
ln, err := options.NetListenConfig.Listen(ctx, network, address) |
| 27 |
if err != nil { |
| 28 |
return nil, err |
| 29 |
} |
| 30 |
return &netListener{net: ln}, nil |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
type netListener struct { |
| 35 |
net net.Listener |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func (l *netListener) Accept(ctx context.Context) (io.ReadWriteCloser, error) { |
| 40 |
return l.net.Accept() |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func (l *netListener) Close() error { |
| 46 |
addr := l.net.Addr() |
| 47 |
err := l.net.Close() |
| 48 |
if addr.Network() == "unix" { |
| 49 |
rerr := os.Remove(addr.String()) |
| 50 |
if rerr != nil && err == nil { |
| 51 |
err = rerr |
| 52 |
} |
| 53 |
} |
| 54 |
return err |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (l *netListener) Dialer() Dialer { |
| 59 |
return NetDialer(l.net.Addr().Network(), l.net.Addr().String(), net.Dialer{ |
| 60 |
Timeout: 5 * time.Second, |
| 61 |
}) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func NetDialer(network, address string, nd net.Dialer) Dialer { |
| 66 |
return &netDialer{ |
| 67 |
network: network, |
| 68 |
address: address, |
| 69 |
dialer: nd, |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
type netDialer struct { |
| 74 |
network string |
| 75 |
address string |
| 76 |
dialer net.Dialer |
| 77 |
} |
| 78 |
|
| 79 |
func (n *netDialer) Dial(ctx context.Context) (io.ReadWriteCloser, error) { |
| 80 |
return n.dialer.DialContext(ctx, n.network, n.address) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func NetPipe(ctx context.Context) (Listener, error) { |
| 88 |
return &netPiper{ |
| 89 |
done: make(chan struct{}), |
| 90 |
dialed: make(chan io.ReadWriteCloser), |
| 91 |
}, nil |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
type netPiper struct { |
| 96 |
done chan struct{} |
| 97 |
dialed chan io.ReadWriteCloser |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func (l *netPiper) Accept(ctx context.Context) (io.ReadWriteCloser, error) { |
| 102 |
|
| 103 |
select { |
| 104 |
case rwc := <-l.dialed: |
| 105 |
return rwc, nil |
| 106 |
case <-l.done: |
| 107 |
return nil, io.EOF |
| 108 |
case <-ctx.Done(): |
| 109 |
return nil, ctx.Err() |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func (l *netPiper) Close() error { |
| 116 |
|
| 117 |
close(l.done) |
| 118 |
return nil |
| 119 |
} |
| 120 |
|
| 121 |
func (l *netPiper) Dialer() Dialer { |
| 122 |
return l |
| 123 |
} |
| 124 |
|
| 125 |
func (l *netPiper) Dial(ctx context.Context) (io.ReadWriteCloser, error) { |
| 126 |
client, server := net.Pipe() |
| 127 |
l.dialed <- server |
| 128 |
return client, nil |
| 129 |
} |