Sending Java Object Over Connection Using Netty

Discussion in 'Programming' started by ReadySetPawn, Jul 22, 2016.

  1. This is my server handler class:

    Code (Java):
    package com.databridge.host;


    import com.databridge.host.io.packets.Packet;
    import com.databridge.host.io.packets.PacketOut;
    import com.databridge.host.io.packets.out.PacketOutAuthResult;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;
    import io.netty.channel.socket.SocketChannel;

    public class DataBridgeServer extends SimpleChannelInboundHandler<Packet> {

        private Main main;
        private String name;
        private SocketChannel channel;
        private ChannelHandlerContext context;

        public DataBridgeServer(Main main, SocketChannel channel){
            super(true);
            this.main = main;
            this.channel = channel;
            this.name = "Test";
            main.getServers().put(name, this);
        }

        public void sendPacket(PacketOut packetOut){
            //TODO
        }

        public void disconnect() {
            try {
                channel.close();
            } catch (Exception e){
                Main.log("Bumpy disconnect for server '" + name + "'");
            }
            main.getServers().remove(name);
        }

        @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, Packet o) throws Exception {
            System.out.println("$$" + o);
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            super.channelActive(ctx);
            this.context = ctx;
            ctx.write(new PacketOutAuthResult(main, true));
            ctx.flush();
            System.out.println("^^^");
            System.out.println(ctx.channel().remoteAddress());
        }



        public String getName() {
            return name;
        }
    }
    And this is what I do to initialize the channel:

    Code (Java):
    @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            ChannelPipeline pipeline = socketChannel.pipeline();
            pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.weakCachingResolver(null)));
            pipeline.addLast("encoder", new ObjectEncoder());
            pipeline.addLast("handler", new DataBridgeServer(main, socketChannel));
            System.out.println("###");
        }
    It worked when I was just sending normal Strings with ByteBufs but I can't seem to make it send an Object (yes, the object is serializable). I have a feeling I need to write my own encoder and decoder. I don't know, I'm really new to netty.
     
    • Like Like x 1
  2. ClassResolvers.weakCachingResolver(DataBridgeServer.class.getClassLoader())

    You can also use
    ClassLoader.getSystemClassLoader() for the classloader instead.
     
  3. Will that fix the problem or is that just another way of doing things?

    EDIT: My bad, I didn't realize I was passing a null value. But yeah, I tried using your method at first and it didn't succeed so I experimented with the null argument and it didn't work also and I just left it there.
     
    #3 ReadySetPawn, Jul 22, 2016
    Last edited: Jul 22, 2016
    • Creative Creative x 1
  4. I was thinking and I wonder if I can just serialize the packet into a ByteBuf and deserialize upon receiving. Seems like it'll work. Any thoughts?