Newbie Spellweaver
- Joined
- Nov 21, 2011
- Messages
- 68
- Reaction score
- 12
// OnMessage:
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
try {
Player player = (Player) ctx.getChannel().getAttachment();
NettyRequest request = (NettyRequest) e.getMessage();
if (request == null) {
return;
}
if (Util.getConfiguration().get("Logging", "log.packets", Boolean.class)) {
Log.println("Received: " + request.getMessageId() + " / " + request.getMessageBody());
}
if (player != null){
Icarus.getServer().getMessageHandler().handleRequest(player, request);
}
} catch (Exception ex) {
Log.exception(ex);
}
}
public void handleRequest(Player player, ClientMessage message) {
if (messages.containsKey(message.getMessageId())) {
messages.get(message.getMessageId()).handle(player, message);
}
}
If you don't want active waits and/or sync issues, you should probably be using monitors. Multithreading isn't really simple, but it is easy once you get the hang of it. This way, you'd put the thread to sleep, and once it recieves something you notify it to get going. No need to actively wait for anything, nor to waste processing cycles.
Here's the documentation onYou must be registered to see links, which give you even more flexibility on Java monitors.
Is this a question or something?
There are multiple ways you can handle messages, queue them to a thread pool, handle them straight away or do whatever you want.
In the example you showed regarding Icarus, it uses Netty. Under the hood, Netty processes events in a queue. You can define whether you want to use multiple queues for user events, IO events etc (aka loop groups) so it would be pretty damn pointless pushing those events to another queue (Comet supports handling messages in the current thread, handling them in another pool etc, feel free to take a look.. I don't recommend using any other method of handling messages other than the default method in production though; after much testing, I figured letting the Netty event loop handle it was the best way).
In other cases, if the message is initially read in the IO threads, you want to be pushing those messages to another queue to take the load away from the IO threads.
So yeah.. It honestly depends on so many other things. You might want to do some research into how your networking stack works if you want to squeeze every ounce of performance out of your code.