hello,
I think I have found a bug. In my program I create an instance of me.codeninja.spidermonkey.connection.Server. I create a me.codeninja.spidermonkey.connection.Client in my second program and let the Client connect to the Server. This works fine. When the Client is being disconnected from the Server, the Server would notice and it would dispatch an event to the ConnectionListener I have attached. Everything’s fine until here. Now, if I try to close the Server by Server.stop();, an exception is thrown to me.
12.10.2010 13:21:20 me.codeninja.spidermonkey.connection.Server stop
INFO: [Server#1][???] Server is shutting down..
java.nio.channels.ClosedChannelException
at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:126)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:324)
at me.codeninja.spidermonkey.connection.TCPConnection.send(TCPConnection.java:213)
at me.codeninja.spidermonkey.connection.TCPConnection.sendObject(TCPConnection.java:177)
at me.codeninja.spidermonkey.connection.Server.broadcastTCP(Server.java:215)
at me.codeninja.spidermonkey.connection.Server.stop(Server.java:288)
at fl.onlinegame.server.conn.CONNHandler.destroy(CONNHandler.java:70)
at fl.onlinegame.server.general.command.CommandHandler.dispose(CommandHandler.java:53)
at fl.onlinegame.server.general.startandexit.ExitManager.exit(ExitManager.java:40)
at fl.onlinegame.server.exchange.command.sub.XExitCommand$1.run(XExitCommand.java:37)
at java.lang.Thread.run(Thread.java:619)
I believe the following error occurs: When Server.stop(); is called, the server tries to tell all its clients that it is intending to shut down now and therefore sends a DisconnectMessage to all of them. (see: Server.java:288) The problem comes up when the client which connected and disconnected earlier is still regarded as connected by the server-program. I believe the Client-connection was not properly removed from ArrayList Connection.connections (to be more precisely from Server.tcp.connections).
By using the debug function of eclipse, I have found out that when the client disconnects from the server, the ArrayList Server.clientManager.clients is empty while the ArrayList Server.tcp.connections still has one entry. When closing, the server tries to send his DisconnectMessage via Server.broadcastTCP. In TCPConnection.java:177 a for-block leaves through the ArrayList Connection.connections. Although, my client has disconnected, it would find an instance of Client in this ArrayList. The fact, that the Client's channel has already been closed in Connection.java:236 will actually throw the exception. I do not know why the TCPConnection's ArrayList connections would still hold this item. All I know is that when I debug my program and look at what happens in Connection.java:239, the instance of Client, whose Channel was just closed and is now to be removed, is not the same as the one which is in the ArrayList (which is obviously the reason why it's not removed). So for some reason the ArrayList in Connection holds the wrong instance of Client.
I hope you understand what I meant to tell you. I'm sorry, I couldn't work into further detail as I'm not fully understanding every part of SpiderMonkey, yet. I'm not even sure, whether this is a bug in my program or in SpiderMonkey.
Thanks in advance,
Fellkneul