洞察探索open banking如何通过小程序容器技术助力金融企业实现数据安全和数字化转型
690
2022-10-22
ServerReactor 和 ServerReceiver
public class ServerReactor implements Runnable { /** * Selector to tell the IO event that our server socket channel has. */ private Selector selector; /** * Serve the incoming call for building up connection and dispatch * the request. */ public void run() { try { while (!Thread.interrupted()) { // select the keys to see the channels that are ready // for IO operation selector.select(); // get the selected keys Set selected = selector.selectedKeys(); Iterator it = selected.iterator(); while (it.hasNext()) { // dispatch each selection key dispatch((SelectionKey) (it.next())); } // clear the selection keys selected.clear(); } } catch (Throwable t) { t.printStackTrace(); } } /** * Dispatch the selection key. */ void dispatch(SelectionKey k) throws CloudwaveException { // get the attachment of the selection key Runnable r = (Runnable) (k.attachment()); if (r != null) { if (r instanceof ServerReceiver) { ServerReceiver serverReceiver = (ServerReceiver) r; if (serverReceiver.isClosed()) { return; } } // run the attachment r.run(); } } public void close() throws IOException { Thread.currentThread().interrupt(); serverSocketChannel.close(); selector.close(); }}public abstract class ServerReceiver implements Runnable { public void run() { try { while (true) { // do a single read int read = socketChannel.read(singleReadBuffer); if (read == -1) { // this means we reached the end of socket stream. close(); return; } if (read > 0) { readBytesLength += read; if (readBytesLength > 4) { // flip input buffer singleReadBuffer.flip(); // process input buffer processSingleReadBuffer(read); // clear input buffer singleReadBuffer.clear(); } } if (inputComplete) { // reach the end of input request completeRequest(); inputComplete = false; readBytesLength = 0; processPos = 0; return; } if (read == 0) { // since no more data available, just break to wait for // more in coming data break; } } } catch (Throwable t) { t.printStackTrace(); closeSession(); reinitBuffer(); selectionKey.cancel(); } }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~