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