@@ -89,8 +89,23 @@ class Connector
8989 std::set<Connection<BUFFER, NetProvider>> m_ReadyToSend;
9090 void close (Connection<BUFFER, NetProvider> &conn);
9191 void close (ConnectionImpl<BUFFER, NetProvider> &conn);
92+
93+ private:
94+ /* *
95+ * A helper to decode responses of a connection.
96+ * Can be called when the connection is not ready to decode - it's just no-op.
97+ * Returns -1 in the case of any error, 0 on success.
98+ */
99+ int connectionDecodeResponses (Connection<BUFFER, NetProvider> &conn,
100+ Response<BUFFER> *result);
101+
92102private:
93103 NetProvider m_NetProvider;
104+ /* *
105+ * Set of connections that are ready to decode.
106+ * Shouldn't be modified directly - is managed by methods `readyToDecode`
107+ * and `connectionDecodeResponses`.
108+ */
94109 std::set<Connection<BUFFER, NetProvider>> m_ReadyToDecode;
95110};
96111
@@ -157,21 +172,35 @@ Connector<BUFFER, NetProvider>::close(ConnectionImpl<BUFFER, NetProvider> &conn)
157172
158173template <class BUFFER , class NetProvider >
159174int
160- connectionDecodeResponses (Connection<BUFFER, NetProvider> &conn,
161- Response<BUFFER> *result)
175+ Connector<BUFFER, NetProvider>:: connectionDecodeResponses(Connection<BUFFER, NetProvider> &conn,
176+ Response<BUFFER> *result)
162177{
178+ if (!hasDataToDecode (conn))
179+ return 0 ;
180+
181+ /* Ready to decode connection must be in the corresponding set. */
182+ assert (m_ReadyToDecode.find (conn) != m_ReadyToDecode.end ());
183+
184+ int rc = 0 ;
163185 while (hasDataToDecode (conn)) {
164- DecodeStatus rc = processResponse (conn, result);
165- if (rc == DECODE_ERR)
166- return -1 ;
186+ DecodeStatus status = processResponse (conn, result);
187+ if (status == DECODE_ERR) {
188+ rc = -1 ;
189+ break ;
190+ }
167191 // In case we've received only a part of response
168192 // we should wait until the rest arrives - otherwise
169193 // we can't properly decode response. */
170- if (rc == DECODE_NEEDMORE)
171- return 0 ;
172- assert (rc == DECODE_SUCC);
194+ if (status == DECODE_NEEDMORE) {
195+ rc = 0 ;
196+ break ;
197+ }
198+ assert (status == DECODE_SUCC);
173199 }
174- return 0 ;
200+ /* A connection that has no data to decode must not be left in the set. */
201+ if (!hasDataToDecode (conn))
202+ m_ReadyToDecode.erase (conn);
203+ return rc;
175204}
176205
177206template <class BUFFER , class NetProvider >
@@ -191,17 +220,8 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
191220 strerror (errno), errno);
192221 return -1 ;
193222 }
194- if (hasDataToDecode (conn)) {
195- assert (m_ReadyToDecode.find (conn) != m_ReadyToDecode.end ());
196- if (connectionDecodeResponses (conn, result) != 0 )
197- return -1 ;
198- /*
199- * In case we've handled whole data in input buffer -
200- * mark connection as completed.
201- */
202- if (!hasDataToDecode (conn))
203- m_ReadyToDecode.erase (conn);
204- }
223+ if (connectionDecodeResponses (conn, result) != 0 )
224+ return -1 ;
205225 if (timer.isExpired ())
206226 break ;
207227 }
@@ -233,13 +253,8 @@ Connector<BUFFER, NetProvider>::waitAll(Connection<BUFFER, NetProvider> &conn,
233253 strerror (errno), errno);
234254 return -1 ;
235255 }
236- if (hasDataToDecode (conn)) {
237- assert (m_ReadyToDecode.find (conn) != m_ReadyToDecode.end ());
238- if (connectionDecodeResponses (conn, static_cast <Response<BUFFER>*>(nullptr )) != 0 )
239- return -1 ;
240- if (!hasDataToDecode (conn))
241- m_ReadyToDecode.erase (conn);
242- }
256+ if (connectionDecodeResponses (conn, static_cast <Response<BUFFER>*>(nullptr )) != 0 )
257+ return -1 ;
243258 bool finish = true ;
244259 for (size_t i = last_not_ready; i < futures.size (); ++i) {
245260 if (!conn.futureIsReady (futures[i])) {
@@ -280,8 +295,6 @@ Connector<BUFFER, NetProvider>::waitAny(int timeout)
280295 assert (hasDataToDecode (conn));
281296 if (connectionDecodeResponses (conn, static_cast <Response<BUFFER>*>(nullptr )) != 0 )
282297 return std::nullopt ;
283- if (!hasDataToDecode (conn))
284- m_ReadyToDecode.erase (conn);
285298 return conn;
286299}
287300
@@ -299,13 +312,8 @@ Connector<BUFFER, NetProvider>::waitCount(Connection<BUFFER, NetProvider> &conn,
299312 strerror (errno), errno);
300313 return -1 ;
301314 }
302- if (hasDataToDecode (conn)) {
303- assert (m_ReadyToDecode.find (conn) != m_ReadyToDecode.end ());
304- if (connectionDecodeResponses (conn, static_cast <Response<BUFFER>*>(nullptr )) != 0 )
305- return -1 ;
306- if (!hasDataToDecode (conn))
307- m_ReadyToDecode.erase (conn);
308- }
315+ if (connectionDecodeResponses (conn, static_cast <Response<BUFFER>*>(nullptr )) != 0 )
316+ return -1 ;
309317 if ((conn.getFutureCount () - ready_futures) >= future_count)
310318 return 0 ;
311319 if (timer.isExpired ())
0 commit comments