@@ -128,12 +128,13 @@ private[tls] object TLSEngine {
128
128
129
129
private def read0 (maxBytes : Int , timeout : Option [FiniteDuration ]): F [Option [Chunk [Byte ]]] =
130
130
// Check if the initial handshake has finished -- if so, read; otherwise, handshake and then read
131
- dequeueUnwrap (maxBytes).flatMap { out =>
131
+ unwrapThenTakeUnwrapped (maxBytes, timeout ).flatMap { out =>
132
132
if (out.isEmpty)
133
133
initialHandshakeDone.ifM(
134
134
read1(maxBytes, timeout),
135
- write(Chunk .empty, None ) >> dequeueUnwrap(maxBytes).flatMap { out =>
136
- if (out.isEmpty) read1(maxBytes, timeout) else Applicative [F ].pure(out)
135
+ write(Chunk .empty, None ) >> unwrapThenTakeUnwrapped(maxBytes, timeout).flatMap {
136
+ out =>
137
+ if (out.isEmpty) read1(maxBytes, timeout) else Applicative [F ].pure(out)
137
138
}
138
139
)
139
140
else Applicative [F ].pure(out)
@@ -161,7 +162,7 @@ private[tls] object TLSEngine {
161
162
case SSLEngineResult .HandshakeStatus .NOT_HANDSHAKING =>
162
163
unwrapBuffer.inputRemains
163
164
.map(_ > 0 && result.bytesConsumed > 0 )
164
- .ifM(unwrap(maxBytes, timeout), dequeueUnwrap (maxBytes))
165
+ .ifM(unwrap(maxBytes, timeout), takeUnwrapped (maxBytes))
165
166
case SSLEngineResult .HandshakeStatus .FINISHED =>
166
167
unwrap(maxBytes, timeout)
167
168
case _ =>
@@ -172,17 +173,23 @@ private[tls] object TLSEngine {
172
173
)
173
174
}
174
175
case SSLEngineResult .Status .BUFFER_UNDERFLOW =>
175
- dequeueUnwrap (maxBytes)
176
+ takeUnwrapped (maxBytes)
176
177
case SSLEngineResult .Status .BUFFER_OVERFLOW =>
177
178
unwrapBuffer.expandOutput >> unwrap(maxBytes, timeout)
178
179
case SSLEngineResult .Status .CLOSED =>
179
- stopWrap >> stopUnwrap >> dequeueUnwrap (maxBytes)
180
+ stopWrap >> stopUnwrap >> takeUnwrapped (maxBytes)
180
181
}
181
182
}
182
183
183
- private def dequeueUnwrap (maxBytes : Int ): F [Option [Chunk [Byte ]]] =
184
+ private def takeUnwrapped (maxBytes : Int ): F [Option [Chunk [Byte ]]] =
184
185
unwrapBuffer.output(maxBytes).map(out => if (out.isEmpty) None else Some (out))
185
186
187
+ private def unwrapThenTakeUnwrapped (
188
+ maxBytes : Int ,
189
+ timeout : Option [FiniteDuration ]
190
+ ): F [Option [Chunk [Byte ]]] =
191
+ unwrapBuffer.inputRemains.map(_ > 0 ).ifM(unwrap(maxBytes, timeout), takeUnwrapped(maxBytes))
192
+
186
193
/** Determines what to do next given the result of a handshake operation.
187
194
* Must be called with `handshakeSem`.
188
195
*/
0 commit comments