zoukankan      html  css  js  c++  java
  • 基于kryonet的RPC,使用kryo进行序列化

    Kryo是一个序列化框架。

    Kryonet是一个基于kryo的RPC框架,它实现了一套高效简洁的API,它通过NIO实现了TCP和UDP通讯,目前还不支持Http。

    自己写了一个测试代码,运行了下,感觉还不错,记录下来。

    1、listener

    package com.mytestcodes.kryonet;  
      
    import com.esotericsoftware.kryonet.Connection;  
    import com.esotericsoftware.kryonet.Listener;  
    import com.mytestcodes.serialization.fulltest.Child;  
      
    public class KListener extends Listener  
    {  
        public KListener()  
        {  
      
        }  
      
        public KListener(String name)  
        {  
            this.name = name;  
        }  
      
        private String name;  
      
        public void received(Connection connection, Object object)  
        {  
            System.out.println(name + " has recive a message");  
      
            if (object instanceof Child)  
            {  
                Child child = (Child) object;  
                System.out.println(child.getChildName());  
                child.setChildName(name + " response");  
                connection.sendTCP(child);  
            }  
        }  
      
        public String getName()  
        {  
            return name;  
        }  
      
        public void setName(String name)  
        {  
            this.name = name;  
        }  
      
    }  

    2、client

    package com.mytestcodes.kryonet;  
      
      
    import java.io.IOException;  
    import java.nio.ByteBuffer;  
      
      
    import com.esotericsoftware.kryonet.Client;  
    import com.mytestcodes.serialization.fulltest.Baby;  
    import com.mytestcodes.serialization.fulltest.Child;  
    import com.mytestcodes.serialization.fulltest.Parent;  
      
      
    public class KClient  
    {  
        public static void main(String[] args)  
        {  
            Client client = new Client();  
            client.addListener(new KListener("Client"));  
            client.start();  
            try  
            {  
                client.connect(5000, "localhost", 54555, 54777);  
            } catch (IOException e)  
            {  
                e.printStackTrace();  
            }  
      
      
            client.getKryo().setRegistrationOptional(true);  
            Child child = new Child();  
      
      
            client.sendTCP(child);  
        }  
      
      
        public static byte[] readBuf(ByteBuffer buf)  
        {  
            int size = buf.position();  
            byte[] newBuf = new byte[size];  
            for (int i = 0; i < size; i++)  
            {  
                newBuf[i] = buf.get(i);  
            }  
            return newBuf;  
        }  
    } 

    3、server

    package com.mytestcodes.kryonet;  
      
      
    import java.io.IOException;  
      
      
    import com.esotericsoftware.kryonet.Server;  
      
      
    public class KServer  
    {  
        public static void main(String[] args)  
        {  
            Server server = new Server();  
            server.addListener(new KListener("server"));  
      
      
            server.getKryo().setRegistrationOptional(true);  
            server.start();  
            try  
            {  
                // TCP port 54555 and UDP port 54777  
                server.bind(54555, 54777);  
            } catch (IOException e)  
            {  
                e.printStackTrace();  
            }  
        }  
    }  

    4、child:

    package com.mytestcodes.serialization.fulltest;  
      
    import java.io.Serializable;  
      
    public class Baby implements Serializable  
    {  
      
        /**  
         *   
         */  
        private static final long serialVersionUID = 8882758100866916676L;  
      
        private String babyName = "baby";  
      
        public String getBabyName()  
        {  
            return babyName;  
        }  
      
        public void setBabyName(String babyName)  
        {  
            this.babyName = babyName;  
        }  
      
    }
    package com.mytestcodes.serialization.fulltest;  
      
    import java.io.Serializable;  
      
    public class Parent implements Serializable  
    {  
      
        /**  
         *   
         */  
        private static final long serialVersionUID = 6933088125784071832L;  
          
        private String parentName="parent";  
      
        public String getParentName()  
        {  
            return parentName;  
        }  
      
        public void setParentName(String parentName)  
        {  
            this.parentName = parentName;  
        }  
          
    }  
  • 相关阅读:
    iOS -Swift 3.0 -for(循环语句用法)
    C++ 中的std::vector介绍(转)
    C++ 中的sort排序用法
    iOS获取当前AppStore版本号与更新
    cocos2dx 3.x(获得父类的node型指针调用父类函数this->getParent())
    cocos2dx 3.x(TexturePacker进行图片加密)
    cocos2dx 3.x(屏幕截图的两种方法)
    cocos2dx 3.x(在Mac平台下利用Eclipse打包安卓apk安装包详细教程)
    cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测
    cocos2dx 3.x(移动修改精灵坐标MoveTo与MoveBy)
  • 原文地址:https://www.cnblogs.com/duanxz/p/4494395.html
Copyright © 2011-2022 走看看