|
21 | 21 | import java.io.IOException; |
22 | 22 | import java.io.InputStream; |
23 | 23 | import java.net.InetSocketAddress; |
| 24 | +import java.net.SocketTimeoutException; |
24 | 25 | import java.nio.ByteBuffer; |
| 26 | +import java.nio.channels.Channels; |
25 | 27 | import java.nio.channels.ClosedChannelException; |
| 28 | +import java.nio.channels.ReadableByteChannel; |
26 | 29 | import java.nio.channels.SelectionKey; |
27 | 30 | import java.nio.channels.SocketChannel; |
28 | 31 | import java.security.KeyStore; |
@@ -449,6 +452,10 @@ public static void doHandshake(SocketChannel ch, SSLEngine sslEngine, boolean is |
449 | 452 | ByteBuffer out_pkgBuf = ByteBuffer.allocate(sslSession.getPacketBufferSize() + 40); |
450 | 453 | ByteBuffer out_appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize() + 40); |
451 | 454 | int count; |
| 455 | + ch.socket().setSoTimeout(10 * 1000); |
| 456 | + InputStream inStream = ch.socket().getInputStream(); |
| 457 | + // Use readCh to make sure the timeout on reading is working |
| 458 | + ReadableByteChannel readCh = Channels.newChannel(inStream); |
452 | 459 |
|
453 | 460 | if (isClient) { |
454 | 461 | hsStatus = SSLEngineResult.HandshakeStatus.NEED_WRAP; |
@@ -479,7 +486,15 @@ public static void doHandshake(SocketChannel ch, SSLEngine sslEngine, boolean is |
479 | 486 | // One packet may contained multiply operation |
480 | 487 | if (in_pkgBuf.position() == 0 || !in_pkgBuf.hasRemaining()) { |
481 | 488 | in_pkgBuf.clear(); |
482 | | - count = ch.read(in_pkgBuf); |
| 489 | + count = 0; |
| 490 | + try { |
| 491 | + count = readCh.read(in_pkgBuf); |
| 492 | + } catch (SocketTimeoutException ex) { |
| 493 | + if (s_logger.isTraceEnabled()) { |
| 494 | + s_logger.trace("Handshake reading time out! Cut the connection"); |
| 495 | + } |
| 496 | + count = -1; |
| 497 | + } |
483 | 498 | if (count == -1) { |
484 | 499 | throw new IOException("Connection closed with -1 on reading size."); |
485 | 500 | } |
|
0 commit comments