/
conn.go
96 lines (83 loc) · 2.4 KB
/
conn.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2018 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package xmpp
import (
"io"
"net"
"time"
)
// conn is a net.Conn created for the purpose of establishing an XMPP session.
type conn struct {
c net.Conn
rw io.ReadWriter
}
// newConn wraps an io.ReadWriter in a Conn.
// If rw is already a net.Conn, it is returned without modification.
// If rw is not a conn but prev is, the various Conn methods that are not part
// of io.ReadWriter proxy through to prev.
func newConn(rw io.ReadWriter, prev net.Conn) net.Conn {
if c, ok := rw.(net.Conn); ok {
return c
}
nc := &conn{rw: rw, c: prev}
return nc
}
// Close closes the connection.
func (c *conn) Close() error {
if c.c != nil {
return c.c.Close()
}
if closer, ok := c.rw.(io.Closer); ok {
return closer.Close()
}
return nil
}
// LocalAddr returns the local network address.
func (c *conn) LocalAddr() net.Addr {
return c.c.LocalAddr()
}
// Read can be made to time out and return a net.Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetReadDeadline.
func (c *conn) Read(b []byte) (n int, err error) {
return c.rw.Read(b)
}
// RemoteAddr returns the remote network address.
func (c *conn) RemoteAddr() net.Addr {
if c.c == nil {
return nil
}
return c.c.RemoteAddr()
}
// SetDeadline sets the read and write deadlines associated with the connection.
// A zero value for t means Read and Write will not time out.
// After a Write has timed out, the TLS state is corrupt and all future writes
// will return the same error.
func (c *conn) SetDeadline(t time.Time) error {
if c.c == nil {
return nil
}
return c.c.SetDeadline(t)
}
// SetReadDeadline sets the read deadline on the underlying connection.
// A zero value for t means Read will not time out.
func (c *conn) SetReadDeadline(t time.Time) error {
if c.c == nil {
return nil
}
return c.c.SetReadDeadline(t)
}
// SetWriteDeadline sets the write deadline on the underlying connection.
// A zero value for t means Write will not time out.
// After a Write has timed out, the TLS state is corrupt and all future writes
// will return the same error.
func (c *conn) SetWriteDeadline(t time.Time) error {
if c.c == nil {
return nil
}
return c.c.SetWriteDeadline(t)
}
// Write writes data to the connection.
func (c *conn) Write(b []byte) (int, error) {
return c.rw.Write(b)
}