37 |
|
|
38 |
package gnu.java.nio; |
package gnu.java.nio; |
39 |
|
|
40 |
import java.net.*; |
import java.io.IOException; |
41 |
import java.io.*; |
import java.net.InetAddress; |
42 |
import java.nio.*; |
import java.net.InetSocketAddress; |
43 |
import java.nio.channels.*; |
import java.net.Socket; |
44 |
import java.nio.channels.spi.*; |
import java.net.SocketAddress; |
45 |
|
import java.nio.ByteBuffer; |
46 |
|
import java.nio.channels.AlreadyConnectedException; |
47 |
|
import java.nio.channels.SocketChannel; |
48 |
|
import java.nio.channels.spi.SelectorProvider; |
49 |
import gnu.classpath.Configuration; |
import gnu.classpath.Configuration; |
50 |
|
|
51 |
public class SocketChannelImpl extends SocketChannel |
public class SocketChannelImpl extends SocketChannel |
52 |
{ |
{ |
53 |
Socket sock_object; |
Socket sock_object; |
54 |
int fd; |
int fd; |
55 |
int local_port; |
int local_port; |
56 |
boolean blocking = true; |
boolean blocking = true; |
57 |
boolean connected = false; |
boolean connected = false; |
58 |
InetSocketAddress sa; |
InetSocketAddress sa; |
59 |
|
|
60 |
static native int SocketCreate(); |
/* |
61 |
static native int SocketConnect(int fd, InetAddress a, int port); |
static native int SocketCreate(); |
62 |
static native int SocketBind(int fd, InetAddress host, int port); |
static native int SocketConnect(int fd, InetAddress a, int port); |
63 |
static native int SocketListen(int fd, int backlog); |
static native int SocketBind(int fd, InetAddress host, int port); |
64 |
static native int SocketAvailable(int fd); |
static native int SocketListen(int fd, int backlog); |
65 |
static native int SocketClose(int fd); |
static native int SocketAvailable(int fd); |
66 |
static native int SocketRead(int fd, byte b[], int off, int len); |
static native int SocketClose(int fd); |
67 |
static native int SocketWrite(int fd, byte b[], int off, int len); |
static native int SocketRead(int fd, byte b[], int off, int len); |
68 |
|
static native int SocketWrite(int fd, byte b[], int off, int len); |
69 |
public SocketChannelImpl(SelectorProvider provider) |
*/ |
70 |
{ |
|
71 |
super(provider); |
static int SocketCreate() { return 0; }; |
72 |
|
static int SocketConnect(int fd, InetAddress a, int port) { return 0; }; |
73 |
fd = SocketCreate(); |
static int SocketBind(int fd, InetAddress host, int port) { return 0; }; |
74 |
|
static int SocketListen(int fd, int backlog) { return 0; }; |
75 |
if (fd == -1) |
static int SocketAvailable(int fd) { return 0; }; |
76 |
{ |
static int SocketClose(int fd) { return 0; }; |
77 |
System.err.println("failed to create socket:"+fd); |
static int SocketRead(int fd, byte b[], int off, int len) { return 0; }; |
78 |
} |
static int SocketWrite(int fd, byte b[], int off, int len) { return 0; }; |
79 |
|
|
80 |
//System.out.println("socket-channel:"+fd); |
public SocketChannelImpl(SelectorProvider provider) |
81 |
} |
{ |
82 |
|
super(provider); |
83 |
public void finalizer() |
|
84 |
{ |
fd = SocketCreate(); |
85 |
if (connected) |
|
86 |
{ |
if (fd == -1) |
87 |
try { |
{ |
88 |
close(); |
System.err.println("failed to create socket:"+fd); |
89 |
} catch (Exception e) { |
} |
90 |
} |
} |
91 |
} |
|
92 |
} |
public void finalizer() |
93 |
|
{ |
94 |
protected void implCloseSelectableChannel() |
if (connected) |
95 |
{ |
{ |
96 |
connected = false; |
try |
97 |
SocketClose(fd); |
{ |
98 |
fd = SocketCreate(); |
close(); |
99 |
} |
} |
100 |
|
catch (Exception e) |
101 |
protected void implConfigureBlocking(boolean block) |
{ |
102 |
{ |
} |
103 |
if (blocking == block) |
} |
104 |
|
} |
105 |
|
|
106 |
|
protected void implCloseSelectableChannel() |
107 |
|
{ |
108 |
|
connected = false; |
109 |
|
SocketClose(fd); |
110 |
|
fd = SocketCreate(); |
111 |
|
} |
112 |
|
|
113 |
|
protected void implConfigureBlocking(boolean block) |
114 |
|
{ |
115 |
|
if (blocking == block) |
116 |
return; |
return; |
117 |
} |
} |
118 |
|
|
119 |
public boolean connect(SocketAddress remote) |
public boolean connect(SocketAddress remote) |
120 |
throws IOException |
throws IOException |
121 |
{ |
{ |
122 |
if (connected) |
if (connected) |
123 |
{ |
{ |
124 |
throw new AlreadyConnectedException(); |
throw new AlreadyConnectedException(); |
125 |
} |
} |
126 |
|
|
127 |
// ok, lets connect ! |
// ok, lets connect ! |
128 |
|
|
129 |
sa = (InetSocketAddress) remote; |
sa = (InetSocketAddress) remote; |
130 |
|
|
131 |
InetAddress addr = sa.getAddress(); |
InetAddress addr = sa.getAddress(); |
132 |
int port = sa.getPort(); |
int port = sa.getPort(); |
133 |
|
int err = SocketConnect(fd, addr, port); |
|
// System.out.println("CONNECT: " + addr + ","+port); |
|
|
|
|
|
int err = SocketConnect(fd, addr, port); |
|
134 |
|
|
135 |
if (err < 0) |
if (err < 0) |
136 |
{ |
{ |
137 |
throw new IOException("Connection refused:"+err + ", connect="+err); |
throw new IOException("Connection refused:"+err + ", connect="+err); |
138 |
} |
} |
139 |
|
|
140 |
local_port = err; |
local_port = err; |
141 |
|
|
142 |
connected = true; |
connected = true; |
143 |
|
|
144 |
return blocking; |
return blocking; |
145 |
} |
} |
146 |
|
|
147 |
public boolean finishConnect() |
public boolean finishConnect() |
148 |
{ |
{ |
149 |
return false; |
return false; |
150 |
} |
} |
151 |
|
|
152 |
public boolean isConnected() |
public boolean isConnected() |
153 |
{ |
{ |
154 |
return connected; |
return connected; |
155 |
} |
} |
156 |
|
|
157 |
public boolean isConnectionPending() |
public boolean isConnectionPending() |
158 |
{ |
{ |
159 |
if (blocking) |
if (blocking) |
160 |
return false; |
return false; |
161 |
return false; |
|
162 |
} |
return false; |
163 |
|
} |
164 |
|
|
165 |
public Socket socket() |
public Socket socket() |
166 |
{ |
{ |
167 |
if (sock_object != null) |
if (sock_object != null) |
168 |
{ |
{ |
169 |
//sock_object.ch = this; |
//sock_object.ch = this; |
170 |
} |
} |
|
return sock_object; |
|
|
} |
|
171 |
|
|
172 |
|
return sock_object; |
173 |
|
} |
174 |
|
|
175 |
public int read(ByteBuffer dst) |
public int read(ByteBuffer dst) |
176 |
{ |
{ |
177 |
int bytes = 0; |
int bytes = 0; |
178 |
|
int len = 1024; |
179 |
int len = 1024; |
byte[]b = new byte[len]; |
|
byte[]b = new byte[len]; |
|
180 |
|
|
181 |
bytes = SocketRead(fd, b, 0, len); |
bytes = SocketRead(fd, b, 0, len); |
|
//System.out.println("readbytes:"+bytes +",len" +len); |
|
182 |
|
|
183 |
dst.put(b, 0, bytes); |
dst.put(b, 0, bytes); |
184 |
|
|
185 |
if (bytes == 0) |
if (bytes == 0) |
186 |
{ |
{ |
187 |
// we've hit eof ? |
// we've hit eof ? |
188 |
return -1; |
return -1; |
189 |
} |
} |
190 |
|
|
191 |
return bytes; |
return bytes; |
192 |
} |
} |
193 |
|
|
194 |
|
|
195 |
public long read(ByteBuffer[] dsts, int offset, int length) |
public long read(ByteBuffer[] dsts, int offset, int length) |
196 |
{ |
{ |
197 |
long bytes = 0; |
long bytes = 0; |
198 |
for (int i=offset; i<length; i++) |
|
199 |
|
for (int i=offset; i<length; i++) |
200 |
{ |
{ |
201 |
bytes += read(dsts[i]); |
bytes += read(dsts[i]); |
202 |
} |
} |
203 |
return bytes; |
|
204 |
} |
return bytes; |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
public int write(ByteBuffer src) |
public int write(ByteBuffer src) |
209 |
{ |
{ |
210 |
int bytes = 0; |
int bytes = 0; |
211 |
|
int len = src.position(); |
212 |
int len = src.position(); |
|
213 |
|
if (src instanceof ByteBufferImpl) |
214 |
if (src instanceof ByteBufferImpl) |
{ |
215 |
{ |
ByteBufferImpl bi = (ByteBufferImpl) src; |
216 |
ByteBufferImpl bi = (ByteBufferImpl) src; |
byte[]b = bi.array(); |
217 |
byte[]b = bi.array(); |
bytes = SocketWrite(fd, b, 0, len); |
218 |
bytes = SocketWrite(fd, b, 0, len); |
} |
219 |
|
else |
220 |
//System.out.println("reused memory buffer...."); |
{ |
221 |
} |
byte[]b = new byte[len]; |
222 |
else |
src.get(b, 0, len); |
223 |
{ |
bytes = SocketWrite(fd, b, 0, len); |
|
byte[]b = new byte[len]; |
|
|
src.get(b, 0, len); |
|
|
bytes = SocketWrite(fd, b, 0, len); |
|
224 |
} |
} |
225 |
|
|
226 |
|
return bytes; |
227 |
|
} |
|
//System.out.println("WRITEN #bytes="+bytes +",fd=" +fd+","+(char)b[0]+(char)b[1]+(char)b[2]); |
|
|
|
|
|
return bytes; |
|
|
} |
|
228 |
|
|
229 |
public long write(ByteBuffer[] srcs, int offset, int length) |
public long write (ByteBuffer[] srcs, int offset, int length) |
230 |
{ |
{ |
231 |
long bytes = 0; |
long bytes = 0; |
232 |
for (int i=offset; i<length; i++) |
for (int i=offset; i<length; i++) |
233 |
{ |
{ |
234 |
bytes += write(srcs[i]); |
bytes += write(srcs[i]); |
235 |
} |
} |
236 |
return bytes; |
return bytes; |
237 |
} |
} |
238 |
} |
} |