@@ -2,7 +2,6 @@ package tcpudp
2
2
3
3
import (
4
4
"bufio"
5
- "bytes"
6
5
"context"
7
6
"errors"
8
7
"io"
@@ -111,9 +110,7 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
111
110
112
111
//Gather connection list for later cleanup
113
112
t .connections = append (t .connections , conn )
114
-
115
113
for {
116
-
117
114
if t .settings .TimeOut > 0 {
118
115
t .logger .Info ("Setting timeout: " , t .settings .TimeOut )
119
116
conn .SetDeadline (time .Now ().Add (time .Duration (t .settings .TimeOut ) * time .Millisecond ))
@@ -123,9 +120,13 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
123
120
124
121
if t .delimiter != 0 {
125
122
data , err := bufio .NewReader (conn ).ReadBytes (t .delimiter )
123
+ if len (data ) > 0 {
124
+ output .Data = string (data )
125
+ t .triggerFlow (conn , output )
126
+ }
126
127
if err != nil {
127
128
errString := err .Error ()
128
- if ! strings .Contains (errString , "use of closed network connection" ) {
129
+ if ! strings .Contains (errString , "use of closed network connection" ) && err != io . EOF {
129
130
t .logger .Error ("Error reading data from connection: " , err .Error ())
130
131
} else {
131
132
t .logger .Info ("Connection is closed." )
@@ -134,16 +135,16 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
134
135
// Return if not timeout error
135
136
return
136
137
}
137
-
138
- } else {
139
- output .Data = string (data [:len (data )- 1 ])
140
138
}
141
139
} else {
142
- var buf bytes.Buffer
143
- _ , err := io .Copy (& buf , conn )
140
+ data , err := bufio .NewReader (conn ).ReadBytes ('\n' )
141
+ if len (data ) > 0 {
142
+ output .Data = string (data )
143
+ t .triggerFlow (conn , output )
144
+ }
144
145
if err != nil {
145
146
errString := err .Error ()
146
- if ! strings .Contains (errString , "use of closed network connection" ) {
147
+ if ! strings .Contains (errString , "use of closed network connection" ) && err != io . EOF {
147
148
t .logger .Error ("Error reading data from connection: " , err .Error ())
148
149
} else {
149
150
t .logger .Info ("Connection is closed." )
@@ -152,38 +153,38 @@ func (t *Trigger) handleNewConnection(conn net.Conn) {
152
153
// Return if not timeout error
153
154
return
154
155
}
155
- } else {
156
- output .Data = string (buf .Bytes ())
157
156
}
158
157
}
158
+ }
159
+ }
159
160
160
- if output .Data != "" {
161
- var replyData []string
162
- for i := 0 ; i < len (t .handlers ); i ++ {
163
- results , err := t .handlers [i ].Handle (context .Background (), output )
164
- if err != nil {
165
- t .logger .Error ("Error invoking action : " , err .Error ())
166
- continue
167
- }
161
+ func (t * Trigger ) triggerFlow (conn net.Conn , output * Output ) {
162
+ if output .Data != "" {
163
+ var replyData []string
164
+ for i := 0 ; i < len (t .handlers ); i ++ {
165
+ results , err := t .handlers [i ].Handle (context .Background (), output )
166
+ if err != nil {
167
+ t .logger .Error ("Error invoking action : " , err .Error ())
168
+ continue
169
+ }
168
170
169
- reply := & Reply {}
170
- err = reply .FromMap (results )
171
- if err != nil {
172
- t .logger .Error ("Failed to convert flow output : " , err .Error ())
173
- continue
174
- }
175
- if reply .Reply != "" {
176
- replyData = append (replyData , reply .Reply )
177
- }
171
+ reply := & Reply {}
172
+ err = reply .FromMap (results )
173
+ if err != nil {
174
+ t .logger .Error ("Failed to convert flow output : " , err .Error ())
175
+ continue
178
176
}
177
+ if reply .Reply != "" {
178
+ replyData = append (replyData , reply .Reply )
179
+ }
180
+ }
179
181
180
- if len (replyData ) > 0 {
181
- replyToSend := strings .Join (replyData , string (t .delimiter ))
182
- // Send a response back to client contacting us.
183
- _ , err := conn .Write ([]byte (replyToSend + "\n " ))
184
- if err != nil {
185
- t .logger .Error ("Failed to write to connection : " , err .Error ())
186
- }
182
+ if len (replyData ) > 0 {
183
+ replyToSend := strings .Join (replyData , string (t .delimiter ))
184
+ // Send a response back to client contacting us.
185
+ _ , err := conn .Write ([]byte (replyToSend + "\n " ))
186
+ if err != nil {
187
+ t .logger .Error ("Failed to write to connection : " , err .Error ())
187
188
}
188
189
}
189
190
}
0 commit comments