telnet.java
来自「apache推出的net包」· Java 代码 · 共 1,334 行 · 第 1/3 页
JAVA
1,334 行
if (debug || debugoptions) { System.err.println("DO: " + TelnetOption.getOption(option)); } _output_.write(_COMMAND_DO); _output_.write(option); /* Code Section added for sending the negotiation ASAP (start)*/ _output_.flush(); /* Code Section added for sending the negotiation ASAP (end)*/ } /*** * Requests a DO. * <p> * @throws IOException - Exception in I/O. * <p> * @param option - Option code. ***/ final synchronized void _requestDo(int option) throws IOException { if ((_doResponse[option] == 0 && _stateIsDo(option)) || _requestedDo(option)) { return ; } _setWantDo(option); ++_doResponse[option]; _sendDo(option); } /*** * Sends a DONT. * <p> * @throws IOException - Exception in I/O. * <p> * @param option - Option code. ***/ final synchronized void _sendDont(int option) throws IOException { if (debug || debugoptions) { System.err.println("DONT: " + TelnetOption.getOption(option)); } _output_.write(_COMMAND_DONT); _output_.write(option); /* Code Section added for sending the negotiation ASAP (start)*/ _output_.flush(); /* Code Section added for sending the negotiation ASAP (end)*/ } /*** * Requests a DONT. * <p> * @throws IOException - Exception in I/O. * <p> * @param option - Option code. ***/ final synchronized void _requestDont(int option) throws IOException { if ((_doResponse[option] == 0 && _stateIsDont(option)) || _requestedDont(option)) { return ; } _setWantDont(option); ++_doResponse[option]; _sendDont(option); } /*** * Sends a WILL. * <p> * @throws IOException - Exception in I/O. * <p> * @param option - Option code. ***/ final synchronized void _sendWill(int option) throws IOException { if (debug || debugoptions) { System.err.println("WILL: " + TelnetOption.getOption(option)); } _output_.write(_COMMAND_WILL); _output_.write(option); /* Code Section added for sending the negotiation ASAP (start)*/ _output_.flush(); /* Code Section added for sending the negotiation ASAP (end)*/ } /*** * Requests a WILL. * <p> * @throws IOException - Exception in I/O. * <p> * @param option - Option code. ***/ final synchronized void _requestWill(int option) throws IOException { if ((_willResponse[option] == 0 && _stateIsWill(option)) || _requestedWill(option)) { return ; } _setWantWill(option); ++_doResponse[option]; _sendWill(option); } /*** * Sends a WONT. * <p> * @throws IOException - Exception in I/O. * <p> * @param option - Option code. ***/ final synchronized void _sendWont(int option) throws IOException { if (debug || debugoptions) { System.err.println("WONT: " + TelnetOption.getOption(option)); } _output_.write(_COMMAND_WONT); _output_.write(option); /* Code Section added for sending the negotiation ASAP (start)*/ _output_.flush(); /* Code Section added for sending the negotiation ASAP (end)*/ } /*** * Requests a WONT. * <p> * @throws IOException - Exception in I/O. * <p> * @param option - Option code. ***/ final synchronized void _requestWont(int option) throws IOException { if ((_willResponse[option] == 0 && _stateIsWont(option)) || _requestedWont(option)) { return ; } _setWantWont(option); ++_doResponse[option]; _sendWont(option); } /*** * Sends a byte. * <p> * @throws IOException - Exception in I/O. * <p> * @param b - byte to send ***/ final synchronized void _sendByte(int b) throws IOException { _output_.write(b); /* Code Section added for supporting spystreams (start)*/ _spyWrite(b); /* Code Section added for supporting spystreams (end)*/ } /* Code Section added for supporting AYT (start)*/ /*** * Sends an Are You There sequence and waits for the result. * <p> * @throws IOException - Exception in I/O. * @throws IllegalArgumentException - Illegal argument * @throws InterruptedException - Interrupted during wait. * <p> * @param timeout - Time to wait for a response (millis.) * <p> * @return true if AYT received a response, false otherwise ***/ final boolean _sendAYT(long timeout) throws IOException, IllegalArgumentException, InterruptedException { boolean retValue = false; synchronized (aytMonitor) { synchronized (this) { aytFlag = false; _output_.write(_COMMAND_AYT); _output_.flush(); } try { aytMonitor.wait(timeout); if (aytFlag == false) { retValue = false; aytFlag = true; } else { retValue = true; } } catch (IllegalMonitorStateException e) { System.err.println("Exception processing AYT:" + e.getMessage()); } } return (retValue); } /* Code Section added for supporting AYT (end)*/ /* open TelnetOptionHandler functionality (start)*/ /*** * Registers a new TelnetOptionHandler for this telnet to use. * <p> * @throws InvalidTelnetOptionException - The option code is invalid. * <p> * @param opthand - option handler to be registered. ***/ void addOptionHandler(TelnetOptionHandler opthand) throws InvalidTelnetOptionException { int optcode = opthand.getOptionCode(); if (TelnetOption.isValidOption(optcode)) { if (optionHandlers[optcode] == null) { optionHandlers[optcode] = opthand; if (isConnected()) { if (opthand.getInitLocal()) { try { _requestWill(optcode); } catch (IOException e) { System.err.println( "Exception while initializing option: " + e.getMessage()); } } if (opthand.getInitRemote()) { try { _requestDo(optcode); } catch (IOException e) { System.err.println( "Exception while initializing option: " + e.getMessage()); } } } } else { throw (new InvalidTelnetOptionException( "Already registered option", optcode)); } } else { throw (new InvalidTelnetOptionException( "Invalid Option Code", optcode)); } } /*** * Unregisters a TelnetOptionHandler. * <p> * @throws InvalidTelnetOptionException - The option code is invalid. * <p> * @param optcode - Code of the option to be unregistered. ***/ void deleteOptionHandler(int optcode) throws InvalidTelnetOptionException { if (TelnetOption.isValidOption(optcode)) { if (optionHandlers[optcode] == null) { throw (new InvalidTelnetOptionException( "Unregistered option", optcode)); } else { TelnetOptionHandler opthand = optionHandlers[optcode]; optionHandlers[optcode] = null; if (opthand.getWill()) { try { _requestWont(optcode); } catch (IOException e) { System.err.println( "Exception while turning off option: " + e.getMessage()); } } if (opthand.getDo()) { try { _requestDont(optcode); } catch (IOException e) { System.err.println( "Exception while turning off option: " + e.getMessage()); } } } } else { throw (new InvalidTelnetOptionException( "Invalid Option Code", optcode)); } } /* open TelnetOptionHandler functionality (end)*/ /* Code Section added for supporting spystreams (start)*/ /*** * Registers an OutputStream for spying what's going on in * the Telnet session. * <p> * @param spystream - OutputStream on which session activity * will be echoed. ***/ void _registerSpyStream(OutputStream spystream) { spyStream = spystream; } /*** * Stops spying this Telnet. * <p> ***/ void _stopSpyStream() { spyStream = null; } /*** * Sends a read char on the spy stream. * <p> * @param ch - character read from the session ***/ void _spyRead(int ch) { if (spyStream != null) { try { if (ch != (int) '\r') { spyStream.write(ch); if (ch == (int) '\n') { spyStream.write((int) '\r'); } spyStream.flush(); } } catch (Exception e) { spyStream = null; } } } /*** * Sends a written char on the spy stream. * <p> * @param ch - character written to the session ***/ void _spyWrite(int ch) { if (!(_stateIsDo(TelnetOption.ECHO) && _requestedDo(TelnetOption.ECHO))) { if (spyStream != null) { try { spyStream.write(ch); spyStream.flush(); } catch (Exception e) { spyStream = null; } } } } /* Code Section added for supporting spystreams (end)*/ /*** * Registers a notification handler to which will be sent * notifications of received telnet option negotiation commands. * <p> * @param notifhand - TelnetNotificationHandler to be registered ***/ public void registerNotifHandler(TelnetNotificationHandler notifhand) { __notifhand = notifhand; } /*** * Unregisters the current notification handler. * <p> ***/ public void unregisterNotifHandler() { __notifhand = null; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?