📄 serversocketthread.java
字号:
return 1024; // default for binary packets } } // ------------------------------------------------------------------------ public void setMinimumPacketLength(int len) { this.minReadLength = len; } public int getMinimumPacketLength() { if (this.minReadLength > 0) { return this.minReadLength; } else if (this.isTextPackets()) { return 1; // at least '\r' (however, this isn't used for text packets) } else { return this.getMaximumPacketLength(); } } // ------------------------------------------------------------------------ public void setLineTerminatorChar(int term) { this.setLineTerminatorChar(new int[] { term }); } public void setLineTerminatorChar(int term[]) { this.lineTerminatorChar = term; } public int[] getLineTerminatorChar() { return this.lineTerminatorChar; } public boolean isLineTerminatorChar(int ch) { int termChar[] = this.getLineTerminatorChar(); if ((termChar != null) && (ch >= 0)) { for (int i = 0; i < termChar.length; i++) { if (termChar[i] == ch) { return true; } } } return false; } // ------------------------------------------------------------------------ public void setBackspaceChar(int bs) { this.setBackspaceChar(new int[] { bs }); } public void setBackspaceChar(int bs[]) { this.backspaceChar = bs; } public int[] getBackspaceChar() { return this.backspaceChar; } public boolean isBackspaceChar(int ch) { if (this.hasPrompt() && (this.backspaceChar != null) && (ch >= 0)) { for (int i = 0; i < this.backspaceChar.length; i++) { if (this.backspaceChar[i] == ch) { return true; } } } return false; } // ------------------------------------------------------------------------ public void setIgnoreChar(int bs[]) { this.ignoreChar = bs; } public int[] getIgnoreChar() { return this.ignoreChar; } public boolean isIgnoreChar(int ch) { if ((this.ignoreChar != null) && (ch >= 0)) { for (int i = 0; i < this.ignoreChar.length; i++) { if (this.ignoreChar[i] == ch) { return true; } } } return false; } // ------------------------------------------------------------------------ public void setAutoPrompt(boolean auto) { if (auto) { this.prompt = null; this.autoPrompt = true; } else { this.autoPrompt = false; } } public void setPrompt(byte prompt[]) { this.prompt = prompt; this.autoPrompt = false; } public void setPrompt(String prompt) { this.setPrompt(StringTools.getBytes(prompt)); } protected byte[] getPrompt(int ndx) { this.promptIndex = ndx; if (this.prompt != null) { return this.prompt; } else if (this.autoPrompt && this.isTextPackets()) { return StringTools.getBytes("" + (this.promptIndex+1) + "> "); } else { return null; } } public boolean hasPrompt() { return (this.prompt != null) || (this.autoPrompt && this.isTextPackets()); } protected int getPromptIndex() { return this.promptIndex; } // ------------------------------------------------------------------------ private static class ClientSocket { private Socket tcpClient = null; private DatagramPacket udpClient = null; private InputStream bais = null; public ClientSocket(Socket tcpClient) { this.tcpClient = tcpClient; } public ClientSocket(DatagramPacket udpClient) { this.udpClient = udpClient; } public boolean isTCP() { return (this.tcpClient != null); } public boolean isUDP() { return (this.udpClient != null); } public int available() { try { return this.getInputStream().available(); } catch (Throwable t) { return 0; } } public InetAddress getInetAddress() { if (this.tcpClient != null) { return this.tcpClient.getInetAddress(); } else if (this.udpClient != null) { SocketAddress sa = this.udpClient.getSocketAddress(); if (sa instanceof InetSocketAddress) { return ((InetSocketAddress)sa).getAddress(); } else { return null; } } else { return null; } } public OutputStream getOutputStream() throws IOException { if (this.tcpClient != null) { return this.tcpClient.getOutputStream(); } else { return null; } } public InputStream getInputStream() throws IOException { if (this.tcpClient != null) { return this.tcpClient.getInputStream(); } else if (this.udpClient != null) { if (bais == null) { bais = new ByteArrayInputStream(this.udpClient.getData(), 0, this.udpClient.getLength()); } return bais; } else { return null; } } public void setSoTimeout(int timeoutSec) throws SocketException { if (this.tcpClient != null) { this.tcpClient.setSoTimeout(timeoutSec); } } public void setSoLinger(int timeoutSec) throws SocketException { if (this.tcpClient != null) { if (timeoutSec <= 0) { this.tcpClient.setSoLinger(false, 0); // no linger } else { this.tcpClient.setSoLinger(true, timeoutSec); } } } public void setSoLinger(boolean on, int timeoutSec) throws SocketException { if (this.tcpClient != null) { if (timeoutSec <= 0) { on = false; } this.tcpClient.setSoLinger(on, timeoutSec); } } public void close() throws IOException { if (this.tcpClient != null) { this.tcpClient.close(); } } } // ------------------------------------------------------------------------ public class ServerSessionThread extends Thread { private Object runLock = new Object(); private ClientSocket client = null; private long readByteCount = 0L; private long writeByteCount = 0L; public ServerSessionThread(Socket client) { super("ClientSession"); this.client = new ClientSocket(client); this.start(); } public ServerSessionThread(ClientSocket client) { super("ClientSession"); this.client = client; this.start(); } public boolean setClientIfAvailable(ClientSocket clientSocket) { boolean rtn = false; synchronized (this.runLock) { if (this.client != null) { rtn = false; // not available } else { this.client = clientSocket; this.runLock.notify(); rtn = true; } } return rtn; } public void run() { /* loop forever */ while (true) { /* wait for client (if necessary) */ synchronized (this.runLock) { while (this.client == null) { try { this.runLock.wait(); } catch (InterruptedException ie) {} } } /* reset byte counts */ this.readByteCount = 0L; this.writeByteCount = 0L; /* IP address */ InetAddress inetAddr = this.client.getInetAddress(); /* session timeout */ long sessionStartTime = DateTime.getCurrentTimeMillis(); long sessionTimeoutMS = ServerSocketThread.this.getSessionTimeout(); long sessionTimeoutAt = (sessionTimeoutMS > 0L)? (sessionStartTime + sessionTimeoutMS) : -1L; /* client session handler */ ClientPacketHandler clientHandler = ServerSocketThread.this.getClientPacketHandler(); if (clientHandler != null) { clientHandler.sessionStarted(inetAddr, this.client.isTCP(), ServerSocketThread.this.isTextPackets()); } /* process client requests */ OutputStream output = null; Throwable termError = null; try { /* get output stream */ output = this.client.getOutputStream(); /* write initial packet from server */ if (clientHandler != null) { byte initialPacket[] = clientHandler.getInitialPacket(); this.writeBytes(output, initialPacket); } /* loop until timeout, error, client terminate */ for (int i = 0;; i++) { /* session timeout? */ if (sessionTimeoutAt > 0L) { long currentTimeMS = DateTime.getCurrentTimeMillis(); if (currentTimeMS >= sessionTimeoutAt) { throw new SSSessionTimeoutException("Session timeout"); } } /* display prompt */ byte prompt[] = ServerSocketThread.this.getPrompt(i); this.writeBytes(output, prompt); /* read packet */ byte line[] = null; if (ServerSocketThread.this.isTextPackets()) { // ASCII: read until packet EOL line = this.readLine(this.client, clientHandler); } else { // Binary: read until packet length or timeout line = this.readPacket(this.client, clientHandler); } /* send packet to listeners */ if ((line != null) && ServerSocketThread.this.hasListeners()) { try { ServerSocketThread.this.invokeListeners(line); } catch (Throwable t) { // a listener can terminate this session break; } } /* get response */ if ((line != null) && (clientHandler != null)) { try { byte response[] = clientHandler.getHandlePacket(line); this.writeBytes(output, response); if (clientHandler.terminateSession()) { break; } } catch (Throwable t) { // the ClientPacketHandler can terminate this session Print.logException("Unexpected exception: ", t); break; } } /* terminate now if we're reading a Datagram and we're out of data */ if (this.client.isUDP() && (this.client.available() <= 0)) { // Normal end of UDP connection break; } } // socket read loop } catch (SSSessionTimeoutException ste) { Print.logError(ste.getMessage()); termError = ste; } catch (SSReadTimeoutException rte) { Print.logError(rte.getMessage()); termError = rte; } catch (SSEndOfStreamException eos) { if (this.client.isTCP()) { // run Print.logError(eos.getMessage()); termError = eos; } else { // We're at the end of the UDP datastream } } catch (SocketException se) { Print.logError("Connection closed"); termError = se; } catch (Throwable t) { Print.logException("?", t); termError = t; } /* client session terminated */ if (clientHandler != null) { try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -