2 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, version 3 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 type SingleConn struct {
31 func (conn *SingleConn) Read(b []byte) (int, error) { return conn.conn.Read(b) }
33 func (conn *SingleConn) Write(b []byte) (int, error) { return conn.conn.Write(b) }
35 func (conn *SingleConn) Close() error {
36 conn.once.Do(conn.ln.Unlock)
37 return conn.conn.Close()
40 func (conn *SingleConn) LocalAddr() net.Addr { return conn.conn.LocalAddr() }
42 func (conn *SingleConn) RemoteAddr() net.Addr { return conn.conn.RemoteAddr() }
44 func (conn *SingleConn) SetDeadline(t time.Time) error { return conn.conn.SetDeadline(t) }
46 func (conn *SingleConn) SetReadDeadline(t time.Time) error { return conn.conn.SetReadDeadline(t) }
48 func (conn *SingleConn) SetWriteDeadline(t time.Time) error { return conn.conn.SetWriteDeadline(t) }
50 type AlreadyAccepted struct{}
52 func (err AlreadyAccepted) Error() string { return "already accepted" }
54 type SingleListener struct {
60 func (ln *SingleListener) Accept() (net.Conn, error) {
63 return nil, AlreadyAccepted{}
66 return &SingleConn{conn: ln.conn, ln: ln}, nil
69 func (ln *SingleListener) Close() error { return nil }
71 func (ln *SingleListener) Addr() net.Addr { return ln.conn.LocalAddr() }