@@ -19,7 +19,7 @@ import (
1919)
2020
2121type gLazyConn struct {
22- tcpConn * gonet. TCPConn
22+ tcpConn * gTCPConn
2323 parentCtx context.Context
2424 stack * stack.Stack
2525 request * tcp.ForwarderRequest
@@ -31,9 +31,6 @@ type gLazyConn struct {
3131}
3232
3333func (c * gLazyConn ) HandshakeContext (ctx context.Context ) error {
34- if c .handshakeDone {
35- return c .handshakeErr
36- }
3734 c .handshakeAccess .Lock ()
3835 defer c .handshakeAccess .Unlock ()
3936 if c .handshakeDone {
@@ -66,15 +63,12 @@ func (c *gLazyConn) HandshakeContext(ctx context.Context) error {
6663 endpoint .SocketOptions ().SetKeepAlive (true )
6764 endpoint .SetSockOpt (common .Ptr (tcpip .KeepaliveIdleOption (15 * time .Second )))
6865 endpoint .SetSockOpt (common .Ptr (tcpip .KeepaliveIntervalOption (15 * time .Second )))
69- tcpConn := gonet . NewTCPConn (& wq , endpoint )
66+ tcpConn := newGTCPConn (& wq , endpoint , c . localAddr , c . remoteAddr )
7067 c .tcpConn = tcpConn
7168 return nil
7269}
7370
7471func (c * gLazyConn ) HandshakeFailure (err error ) error {
75- if c .handshakeDone {
76- return os .ErrInvalid
77- }
7872 c .handshakeAccess .Lock ()
7973 defer c .handshakeAccess .Unlock ()
8074 if c .handshakeDone {
@@ -90,6 +84,18 @@ func (c *gLazyConn) HandshakeSuccess() error {
9084 return c .HandshakeContext (context .Background ())
9185}
9286
87+ func (c * gLazyConn ) NeedHandshakeForRead () bool {
88+ c .handshakeAccess .Lock ()
89+ defer c .handshakeAccess .Unlock ()
90+ return ! c .handshakeDone
91+ }
92+
93+ func (c * gLazyConn ) NeedHandshakeForWrite () bool {
94+ c .handshakeAccess .Lock ()
95+ defer c .handshakeAccess .Unlock ()
96+ return ! c .handshakeDone
97+ }
98+
9399func (c * gLazyConn ) Read (b []byte ) (n int , err error ) {
94100 err = c .HandshakeContext (context .Background ())
95101 if err != nil {
@@ -139,57 +145,38 @@ func (c *gLazyConn) SetWriteDeadline(t time.Time) error {
139145}
140146
141147func (c * gLazyConn ) Close () error {
142- if ! c .handshakeDone {
143- c .handshakeAccess .Lock ()
144- if ! c .handshakeDone {
145- c .request .Complete (true )
146- c .handshakeErr = net .ErrClosed
147- c .handshakeDone = true
148- return nil
149- } else if c .handshakeErr != nil {
150- return nil
151- }
152- c .handshakeAccess .Unlock ()
153- } else if c .handshakeErr != nil {
148+ if c .closeBeforeHandshake () {
154149 return nil
155150 }
156151 return c .tcpConn .Close ()
157152}
158153
159154func (c * gLazyConn ) CloseRead () error {
160- if ! c .handshakeDone {
161- c .handshakeAccess .Lock ()
162- if ! c .handshakeDone {
163- c .request .Complete (true )
164- c .handshakeErr = net .ErrClosed
165- c .handshakeDone = true
166- return nil
167- } else if c .handshakeErr != nil {
168- return nil
169- }
170- c .handshakeAccess .Unlock ()
171- } else if c .handshakeErr != nil {
155+ if c .closeBeforeHandshake () {
172156 return nil
173157 }
174158 return c .tcpConn .CloseRead ()
175159}
176160
177161func (c * gLazyConn ) CloseWrite () error {
162+ if c .closeBeforeHandshake () {
163+ return nil
164+ }
165+ return c .tcpConn .CloseWrite ()
166+ }
167+
168+ func (c * gLazyConn ) closeBeforeHandshake () bool {
169+ c .handshakeAccess .Lock ()
170+ defer c .handshakeAccess .Unlock ()
178171 if ! c .handshakeDone {
179- c .handshakeAccess .Lock ()
180- if ! c .handshakeDone {
172+ if c .request != nil {
181173 c .request .Complete (true )
182- c .handshakeErr = net .ErrClosed
183- c .handshakeDone = true
184- return nil
185- } else if c .handshakeErr != nil {
186- return nil
187174 }
188- c .handshakeAccess . Unlock ()
189- } else if c . handshakeErr != nil {
190- return nil
175+ c .handshakeErr = net . ErrClosed
176+ c . handshakeDone = true
177+ return true
191178 }
192- return c .tcpConn . CloseRead ()
179+ return c .handshakeErr != nil
193180}
194181
195182func (c * gLazyConn ) ReaderReplaceable () bool {
0 commit comments