I think I know where was wrong now
the older version mina 1.x didn't handle the TransportMetadata like 2.0.x ....
in the code hasFragmentation is used to handle this situation, seems a
bug in mina 1.x
in the 1.x code the read function is pretty simple, when ret < 0 ,
close the client connect
private void read(T session) {
IoSessionConfig config = session.getConfig();
IoBuffer buf = IoBuffer.allocate(config.getReadBufferSize());
final boolean hasFragmentation =
session.getTransportMetadata().hasFragmentation();
try {
int readBytes = 0;
int ret;
try {
if (hasFragmentation) {
while ((ret = read(session, buf)) > 0) {
readBytes += ret;
if (!buf.hasRemaining()) {
break;
}
}
} else {
ret = read(session, buf);
if (ret > 0) {
readBytes = ret;
}
}
} finally {
buf.flip();
}
if (readBytes > 0) {
session.getFilterChain().fireMessageReceived(buf);
buf = null;
if (hasFragmentation) {
if (readBytes << 1 < config.getReadBufferSize()) {
session.decreaseReadBufferSize();
} else if (readBytes == config.getReadBufferSize()) {
session.increaseReadBufferSize();
}
}
}
if (ret < 0) {
scheduleRemove(session);
}
} catch (Throwable e) {
if (e instanceof IOException) {
scheduleRemove(session);
}
session.getFilterChain().fireExceptionCaught(e);
}
}
but I don't really know what is the fragmentation could be ?
and why the pure nio server code can't work properly, the pure nio code is below
package main;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.Set;
public class ServerMain {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer buffer = ByteBuffer.allocate(512);
Selector selector = Selector.open();
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(new java.net.InetSocketAddress(843));
server.configureBlocking(false);
SelectionKey serverkey = server.register(selector,
SelectionKey.OP_ACCEPT);
for (;;) {
// wait here for event
System.out.println("server started at "
+ server.socket().getLocalPort());
selector.select();
Set<SelectionKey> keys = selector.selectedKeys();
for (Iterator i = keys.iterator(); i.hasNext();) {
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key == serverkey) {
if (key.isAcceptable()) {
SocketChannel client =
server.accept();
client.configureBlocking(false);
SelectionKey clientkey =
client.register(selector,
SelectionKey.OP_READ);
clientkey.attach(new
Integer(0));
}
} else {
SocketChannel client = (SocketChannel)
key.channel();
if (!key.isReadable())
continue;
int bytesread = client.read(buffer);
if (bytesread == -1) {
key.cancel();
client.close();
continue;
}
buffer.flip();
String request =
decoder.decode(buffer).toString();
System.out.println("msg received: " +
request);
client.write(encoder.encode(CharBuffer.wrap(request)));
buffer.clear();
if (request.trim().equals("quit")) {
client.write(encoder.encode(CharBuffer.wrap("Bye.")));
key.cancel();
client.close();
} else {
int num = ((Integer)
key.attachment()).intValue();
String response = num + ": " +
request.toUpperCase();
client.write(encoder.encode(CharBuffer.wrap(response)));
key.attach(new Integer(num +
1));
}
}
}
}
}
}
On Sun, Sep 14, 2008 at 7:34 PM, davy zhang <[EMAIL PROTECTED]> wrote:
> as it well-known flashplayer will request a policy file from the port
> 843(by default)
>
> I set up my server at that port use mina 1.1.7, I can successfully
> receive the message from the flashplayer
>
> it said <policy-file-request/> + "\0"
>
> but the problem is flashplayer disconnected immediately that request
> and I can't send the policy data back.
>
> I've been there for days ... sigh~
>
> I tried the pure nio non-block server way, didn't use any external library
>
> the problem still there!
>
> So I guess it's not the mina's fault, right?
>
> I know the adobe claimed the player will disconnect the server when it
> received a "\0"
>
> cause by the "contract" from adobe that means the end of policy file data.
>
> Is there any possibility the java sent a "\0" after the message
> arrived ?? it's just so wierd~
>
> I managed to solve this problem used the traditional thread-blocking
> way in java, it works
>
> so that is said flashplayer can only use block way ?
>
> but that make no sense~, is there any difference to the client whether
> the server is working on blocking or non-blocking way???
>
> I've been tried use FreePascal to write the policy server before, it
> use non-block and event-driven model too and works fine,
>
> I just so confused about what is going on there....
>
> the reproduct the scene is very easy setup a mina server on localhost:843
>
> write a simple flash code to connect to any port on localhost.
>
> the attachment is the flash swf file and html , it will try to connect
> the 80 port on localhost
>
> you can host this files on your machine and start the mina server and
> click on the flash connect button.
>
> y
>