/
outbound_connection.go
155 lines (145 loc) · 4.29 KB
/
outbound_connection.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package connection
import (
"context"
"fmt"
"io"
"net"
"time"
"github.com/ihciah/rabbit-tcp/block"
"github.com/ihciah/rabbit-tcp/logger"
"go.uber.org/atomic"
)
type OutboundConnection struct {
baseConnection
HalfOpenConn
ctx context.Context
cancel context.CancelFunc
}
func NewOutboundConnection(connectionID uint32, sendQueue chan<- block.Block, ctx context.Context, removeFromPool context.CancelFunc) Connection {
c := OutboundConnection{
baseConnection: baseConnection{
blockProcessor: newBlockProcessor(ctx, removeFromPool),
connectionID: connectionID,
closed: atomic.NewBool(true),
sendQueue: sendQueue,
recvQueue: make(chan block.Block, RecvQueueSize),
orderedRecvQueue: make(chan block.Block, OrderedRecvQueueSize),
logger: logger.NewLogger(fmt.Sprintf("[OutboundConnection-%d]", connectionID)),
},
ctx: ctx,
cancel: removeFromPool,
}
c.logger.Infof("OutboundConnection %d created.\n", connectionID)
return &c
}
func (oc *OutboundConnection) closeThenCancelWithOnceSend() {
oc.HalfOpenConn.Close()
oc.cancel()
if oc.closed.CAS(false, true) {
oc.SendDisconnect(block.ShutdownBoth)
}
}
func (oc *OutboundConnection) closeThenCancel() {
oc.HalfOpenConn.Close()
oc.cancel()
}
// real connection -> ConnectionPool's SendQueue -> TunnelPool
func (oc *OutboundConnection) RecvRelay() {
recvBuffer := make([]byte, OutboundRecvBuffer)
for {
oc.HalfOpenConn.SetReadDeadline(time.Now().Add(OutboundBlockTimeoutSec * time.Second))
n, err := oc.HalfOpenConn.Read(recvBuffer)
if err == nil {
oc.sendData(recvBuffer[:n])
oc.HalfOpenConn.SetReadDeadline(time.Time{})
} else if err == io.EOF {
oc.logger.Debugln("EOF received from outbound connection.")
oc.closeThenCancelWithOnceSend()
return
} else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
oc.logger.Debugln("Receive timeout from outbound connection.")
} else {
oc.logger.Errorf("Error when recv relay outbound connection: %v\n.", err)
oc.closeThenCancelWithOnceSend()
return
}
select {
case <-oc.ctx.Done():
// Should read all before leave, or packet will be lost
for {
n, err := oc.HalfOpenConn.Read(recvBuffer)
if err == nil {
oc.logger.Debugln("Data received from outbound connection successfully after close.")
oc.sendData(recvBuffer[:n])
} else {
oc.logger.Debugf("Error when receiving data from outbound connection after close: %v.\n", err)
break
}
}
return
default:
continue
}
}
}
// orderedRecvQueue -> real connection
func (oc *OutboundConnection) SendRelay() {
for {
select {
case blk := <-oc.orderedRecvQueue:
switch blk.Type {
case block.TypeConnect:
// Will do nothing!
continue
case block.TypeData:
oc.logger.Debugln("Send out DATA bytes.")
oc.HalfOpenConn.SetWriteDeadline(time.Now().Add(OutboundBlockTimeoutSec * time.Second))
_, err := oc.HalfOpenConn.Write(blk.BlockData)
if err == nil {
oc.HalfOpenConn.SetWriteDeadline(time.Time{})
} else {
oc.logger.Errorf("Error when send relay outbound connection: %v\n.", err)
oc.closeThenCancelWithOnceSend()
}
case block.TypeDisconnect:
if blk.BlockData[0] == block.ShutdownRead {
oc.logger.Debugf("CloseRead for remote connection\n")
oc.HalfOpenConn.CloseRead()
} else if blk.BlockData[0] == block.ShutdownWrite {
oc.logger.Debugf("CloseWrite for remote connection\n")
oc.HalfOpenConn.CloseWrite()
} else {
oc.logger.Debugln("Send out DISCONNECT action.")
oc.closeThenCancel()
}
}
case <-oc.ctx.Done():
oc.closeThenCancelWithOnceSend()
return
}
}
}
func (oc *OutboundConnection) RecvBlock(blk block.Block) {
if blk.Type == block.TypeConnect {
address := string(blk.BlockData)
go oc.connect(address)
}
oc.recvQueue <- blk
}
func (oc *OutboundConnection) connect(address string) {
oc.logger.Debugln("Send out CONNECTION action.")
if !oc.closed.Load() || oc.HalfOpenConn != nil {
return
}
rawConn, err := net.Dial("tcp", address)
if err == nil {
oc.logger.Infof("Dial to %s successfully.\n", address)
oc.HalfOpenConn = rawConn.(*net.TCPConn)
oc.closed.Toggle()
go oc.RecvRelay()
go oc.SendRelay()
} else {
oc.logger.Warnf("Error when dial to %s: %v.\n", address, err)
oc.SendDisconnect(block.ShutdownBoth)
}
}