1 17 package org.alfresco.filesys.smb.server; 18 19 import java.io.DataInputStream ; 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import java.net.InetAddress ; 23 import java.net.Socket ; 24 25 28 public abstract class PacketHandler 29 { 30 31 33 private int m_protoType; 34 private String m_protoName; 35 private String m_shortName; 36 37 39 private Socket m_socket; 40 41 43 private DataInputStream m_in; 44 private DataOutputStream m_out; 45 46 48 private String m_clientName; 49 50 59 public PacketHandler(Socket sock, int typ, String name, String shortName) throws IOException 60 { 61 m_socket = sock; 62 m_protoType = typ; 63 m_protoName = name; 64 m_shortName = shortName; 65 66 68 sock.setTcpNoDelay(true); 69 70 72 m_in = new DataInputStream (m_socket.getInputStream()); 73 m_out = new DataOutputStream (m_socket.getOutputStream()); 74 } 75 76 83 public PacketHandler(int typ, String name, String shortName, String clientName) 84 { 85 m_protoType = typ; 86 m_protoName = name; 87 m_shortName = shortName; 88 89 m_clientName = clientName; 90 } 91 92 97 public final int isProtocol() 98 { 99 return m_protoType; 100 } 101 102 107 public final String isProtocolName() 108 { 109 return m_protoName; 110 } 111 112 117 public final String getShortName() 118 { 119 return m_shortName; 120 } 121 122 127 public final boolean hasRemoteAddress() 128 { 129 return m_socket != null ? true : false; 130 } 131 132 137 public final InetAddress getRemoteAddress() 138 { 139 return m_socket != null ? m_socket.getInetAddress() : null; 140 } 141 142 147 public final boolean hasClientName() 148 { 149 return m_clientName != null ? true : false; 150 } 151 152 157 public final String getClientName() 158 { 159 return m_clientName; 160 } 161 162 168 public final int availableBytes() throws IOException 169 { 170 if (m_in != null) 171 return m_in.available(); 172 return 0; 173 } 174 175 184 public final int readPacket(byte[] pkt, int off, int len) throws IOException 185 { 186 187 189 if (m_in != null) 190 return m_in.read(pkt, off, len); 191 return 0; 192 } 193 194 201 public abstract int readPacket(SMBSrvPacket pkt) throws IOException ; 202 203 211 public final void writePacket(byte[] pkt, int off, int len) throws IOException 212 { 213 214 216 if (m_out != null) 217 m_out.write(pkt, off, len); 218 } 219 220 227 public abstract void writePacket(SMBSrvPacket pkt, int len) throws IOException ; 228 229 235 public final void writePacket(SMBSrvPacket pkt) throws IOException 236 { 237 writePacket(pkt, pkt.getLength()); 238 } 239 240 245 public final void flushPacket() throws IOException 246 { 247 if (m_out != null) 248 m_out.flush(); 249 } 250 251 254 public void closeHandler() 255 { 256 257 259 if (m_in != null) 260 { 261 try 262 { 263 m_in.close(); 264 } 265 catch (Exception ex) 266 { 267 } 268 m_in = null; 269 } 270 271 273 if (m_out != null) 274 { 275 try 276 { 277 m_out.close(); 278 } 279 catch (Exception ex) 280 { 281 } 282 m_out = null; 283 } 284 285 287 if (m_socket != null) 288 { 289 try 290 { 291 m_socket.close(); 292 } 293 catch (Exception ex) 294 { 295 } 296 m_socket = null; 297 } 298 } 299 } 300 | Popular Tags |