/*
 * IDK Game Server by Steve Winfield
 * https://github.com/WinfieldSteve
 */
package org.stevewinfield.suja.idk.network.codec;

import org.apache.log4j.Logger;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.stevewinfield.suja.idk.communication.MessageWriter;
import org.stevewinfield.suja.idk.communication.QueuedMessageWriter;

import java.nio.charset.Charset;

public class NetworkEncoder extends SimpleChannelHandler {
    private static final Logger logger = Logger.getLogger(NetworkDecoder.class);

    @Override
    public void writeRequested(final ChannelHandlerContext ctx, final MessageEvent e) {
        try {
            if (e.getMessage() instanceof String) {
                Channels.write(ctx, e.getFuture(), ChannelBuffers.copiedBuffer((String) e.getMessage(), Charset.forName("UTF-8")));
            } else if (e.getMessage() instanceof MessageWriter) {
                final MessageWriter msg = (MessageWriter) e.getMessage();
                Channels.write(ctx, e.getFuture(), msg.getBytes());
            } else if (e.getMessage() instanceof QueuedMessageWriter) {
                final QueuedMessageWriter msg = (QueuedMessageWriter) e.getMessage();
                Channels.write(ctx, e.getFuture(), msg.getBytes());
            }
        } catch (final Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}

@@ -819,16 +819,70 @@
   try %7B%0A
+            if (ctx.getChannel().isConnected()) %7B%0A    
         
@@ -921,24 +921,28 @@
 f String) %7B%0A
+    
             
@@ -1065,32 +1065,36 @@
 ));%0A            
+    
 %7D else if (e.get
@@ -1143,24 +1143,28 @@
             
+ 
+   
 final Messag
@@ -1217,32 +1217,36 @@
                 
+   
+ 
 Channels.write(c
@@ -1273,32 +1273,36 @@
 sg.getBytes());%0A
+    
             %7D el
@@ -1369,24 +1369,28 @@
             
+ 
+   
 final Queued
@@ -1455,32 +1455,36 @@
                 
+    
 Channels.write(c
@@ -1511,32 +1511,50 @@
 sg.getBytes());%0A
+                %7D%0A
             %7D%0A  

Fix exception in NetworkEncoder