我正在使用 java NIO 编写一个服务器,但我有几个问题找不到答案。
首先,关于SSLEngine,如何在分离线程中正确处理NEED_TASK
?当我在单独的线程中调用任务时,它们会完成,但我不知道如何返回以执行另一个握手操作。一种选择是从执行委托(delegate)任务的线程调用该操作,但我想这不是实现它的方法。
另一个问题是关于从不同的线程和选择器线程调用 interestOps()
。在尝试写入 channel 但未写入所有数据后,我需要更改关键兴趣。
我考虑过像 ROX NIO 教程中那样使用某种更改队列,但我在这里的另一个线程中读到这不是最好的方法。
最佳答案
first regarding SSLEngine, how to handle NEED_TASK properly in separated thread. When I invoke tasks in separate thread they complete, but I have no idea how to go back to perform another handshake operations.
当引擎处于 NEED_TASK 状态时,它不能做任何其他事情。当任务完成后,您应该重复最初返回 NEED_TASK 的操作,让引擎告诉您下一步该做什么。在任务完成之前,您需要阻止或禁止其他线程使用该引擎,即不要在该 channel 上进行选择。
Another question is about calling interestOps() from different thread then selector thread. I need to change key interests after an attempt to write to channel hadn't written all data. I thought about using some sort of Queue of changes like in ROX NIO tutorial, but I have read in another thread here that it is not the best way.
那就是我。我讨厌那些队列。我只是 wakeup() 选择器并更改 interestOps,从来没有遇到过问题。选择器线程必须正确处理准备就绪的零键,但它已经需要这样做了。
关于Java NIO 和 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14093546/