1 29 30 package com.caucho.quercus.lib.file; 31 32 import com.caucho.quercus.env.Env; 33 import com.caucho.vfs.ReadStream; 34 import com.caucho.vfs.SocketStream; 35 import com.caucho.vfs.WriteStream; 36 37 import java.io.IOException ; 38 import java.net.Socket ; 39 import java.net.SocketAddress ; 40 41 44 public class SocketReadWrite extends AbstractBinaryInputOutput 45 { 46 public enum Domain { AF_INET, AF_INET6, AF_UNIX }; 47 48 private int _lastError; 49 private Domain _domain; 50 private Socket _socket; 51 52 public SocketReadWrite(Env env, Socket socket, Domain domain) 53 { 54 env.addClose(this); 55 56 _socket = socket; 57 _domain = domain; 58 } 59 60 public void bind(SocketAddress address) 61 throws IOException 62 { 63 _socket.bind(address); 64 } 65 66 public void connect(SocketAddress address) 67 throws IOException 68 { 69 _socket.connect(address); 70 71 init(); 72 } 73 74 public void setError(int error) 75 { 76 _lastError = error; 77 } 78 79 public void init() 80 { 81 SocketStream sock = new SocketStream(_socket); 82 83 WriteStream os = new WriteStream(sock); 84 ReadStream is = new ReadStream(sock, os); 85 86 init(is, os); 87 } 88 89 92 public void close() 93 { 94 super.close(); 95 96 Socket s = _socket; 97 _socket = null; 98 99 try { 100 if (s != null) 101 s.close(); 102 } catch (IOException e) { 103 } 104 } 105 106 public String toString() 107 { 108 if (_socket != null) 109 return "SocketReadWrite[" + _socket.getInetAddress() + "," + _socket.getPort() + "]"; 110 else 111 return "SocketReadWrite[closed]"; 112 } 113 } 114 115 | Popular Tags |