1 29 30 package com.caucho.vfs; 31 32 import com.caucho.log.Log; 33 34 import java.io.IOException ; 35 import java.net.InetAddress ; 36 import java.net.ServerSocket ; 37 import java.net.Socket ; 38 import java.nio.channels.Selector ; 39 import java.nio.channels.ServerSocketChannel ; 40 import java.nio.channels.spi.SelectorProvider ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class QServerSocketWrapper extends QServerSocket { 48 private static final Logger log = Log.open(QServerSocketWrapper.class); 49 50 private ServerSocket _ss; 51 private boolean _tcpNoDelay = true; 52 private int _connectionSocketTimeout = 65000; 53 54 public QServerSocketWrapper() 55 { 56 } 57 58 public QServerSocketWrapper(ServerSocket ss) 59 { 60 init(ss); 61 } 62 63 public void init(ServerSocket ss) 64 { 65 _ss = ss; 66 } 67 68 public void setTcpNoDelay(boolean delay) 69 { 70 _tcpNoDelay = delay; 71 } 72 73 public boolean getTcpNoDelay() 74 { 75 return _tcpNoDelay; 76 } 77 78 public void setConnectionSocketTimeout(int socketTimeout) 79 { 80 _connectionSocketTimeout = socketTimeout; 81 } 82 83 86 public boolean accept(QSocket qSocket) 87 throws IOException 88 { 89 QSocketWrapper s = (QSocketWrapper) qSocket; 90 91 Socket socket = _ss.accept(); 92 93 if (socket == null) 94 return false; 95 96 if (_tcpNoDelay) 98 socket.setTcpNoDelay(true); 99 100 if (_connectionSocketTimeout > 0) 101 socket.setSoTimeout(_connectionSocketTimeout); 102 103 s.init(socket); 104 105 return true; 106 } 107 108 111 public QSocket createSocket() 112 throws IOException 113 { 114 return new QSocketWrapper(); 115 } 116 117 public InetAddress getLocalAddress() 118 { 119 return _ss.getInetAddress(); 120 } 121 122 public int getLocalPort() 123 { 124 return _ss.getLocalPort(); 125 } 126 127 public Selector getSelector() 128 { 129 try { 130 ServerSocketChannel channel = _ss.getChannel(); 131 132 if (channel == null) 133 return null; 134 135 SelectorProvider provider = channel.provider(); 136 137 if (provider != null) 138 return provider.openSelector(); 139 else 140 return null; 141 } catch (Throwable e) { 142 log.log(Level.WARNING, e.toString(), e); 143 return null; 144 } 145 } 146 147 150 public void close() 151 throws IOException 152 { 153 ServerSocket ss = _ss; 154 _ss = ss; 155 156 if (ss != null) { 157 try { 158 ss.close(); 159 } catch (Exception e) { 160 } 161 } 162 } 163 164 public String toString() 165 { 166 return "ServerSocketWrapper[" + getLocalAddress() + ":" + getLocalPort() + "]"; 167 } 168 } 169 170 | Popular Tags |