Ejemplo n.º 1
0
        public void Connect(string connectStr)
        {
            var       ipaddr = connectStr.Split(':')[0];
            int       port   = int.Parse(connectStr.Split(':')[1]);
            IPAddress ip     = IPAddress.Parse(ipaddr);

            clientSocket.Connect(new IPEndPoint(ip, port));

            reader = new BinaryReader(new NetworkStream(clientSocket));
            writer = new BinaryWriter(new NetworkStream(clientSocket));

            _recieveThread = new Thread((o) =>
            {
                try
                {
                    while (true)
                    {
                        int length = reader.ReadInt32();
                        var data   = reader.ReadBytes(length);
                        var msg    = DirectProtoBufTools.Deserialize <GameMessage>(data);
                        MessageHandler(msg);
                    }
                }catch
                {
                }
            });
            _recieveThread.Start();
        }
Ejemplo n.º 2
0
        public void TestDynamicCast()
        {
            SyncMessage msg = GenerateSyncMessage();

            byte[] data = DirectProtoBufTools.Serialize(msg);
            Assert.IsTrue(data.Length > 0);
            var         baseR = DirectProtoBufTools.Deserialize <GameMessage>(data);
            SyncMessage r     = (SyncMessage)baseR;

            Assert.IsTrue(r.stateCode == msg.stateCode);
            Assert.IsTrue(r.players.Count == 1);
            Assert.IsTrue(r.players[0].id == "cg");
            Assert.IsTrue(r.bullets.Count == 2);
            Assert.IsTrue(r.bullets[1].id == 12);
        }
Ejemplo n.º 3
0
        public void TestSerializeAndDeserialize()
        {
            GamePlayer p = new GamePlayer();

            p.r  = 400f;
            p.dx = 0.001f;

            byte[] data = DirectProtoBufTools.Serialize(p);
            Assert.IsTrue(data.Length > 0);

            var sp = DirectProtoBufTools.Deserialize <GamePlayer>(data);

            Assert.IsTrue(sp.r == p.r);
            Assert.IsTrue(sp.dx == p.dx);
        }
Ejemplo n.º 4
0
        public void TestProtobufList()
        {
            SyncMessage msg     = GenerateSyncMessage();
            GameMessage baseMsg = msg;

            byte[] data = DirectProtoBufTools.Serialize(baseMsg);
            Assert.IsTrue(data.Length > 0);

            var r = DirectProtoBufTools.Deserialize <SyncMessage>(data);

            Assert.IsTrue(r.stateCode == msg.stateCode);
            Assert.IsTrue(r.players.Count == 1);
            Assert.IsTrue(r.players[0].id == "cg");
            Assert.IsTrue(r.bullets.Count == 2);
            Assert.IsTrue(r.bullets[1].id == 12);
        }
Ejemplo n.º 5
0
        public void Send(GameMessage msg)
        {
            if (msg == null)
            {
                return;
            }

            var data = DirectProtoBufTools.Serialize(msg);

            if (data.Length == 0)
            {
                return;
            }

            writer.Write(data.Length);
            writer.Write(data);
        }
Ejemplo n.º 6
0
        static public byte[] LoadLua(string path)
        {
            //BY CG:在unity编辑模式下,方便调试,不用repack lua
            if (Application.isEditor)
            {
                return(File.ReadAllBytes("data/lua/" + path + ".lua"));
            }


            if (__luaMapper == null)
            {
                using (var input = BinaryResourceLoader.CreateStreamFromCEBinary(HSUnityEnv.CELuaPath))
                    __luaMapper = DirectProtoBufTools.Deserialize <Dictionary <string, byte[]> >(input);
            }

            //处理文件名格式
            if (!path.Contains("/"))
            {
                path = path.Replace(".", "/");
            }

            if (LuaManager.LUAJIT_ENABLE)
            {
                path = "/luajit/" + path;
            }
            else
            {
                path = "/lua/" + path;
            }

            if (!path.EndsWith(".lua"))
            {
                path += ".lua";
            }

            if (__luaMapper.ContainsKey(path))
            {
                return(__luaMapper[path]);
            }
            else
            {
                _Logger.Error("找不到文件 [{0}]", path);
                return(null);
            }
        }
Ejemplo n.º 7
0
        public TcpNetworkClientSession(TcpClient c)
        {
            client = c;
            writer = new BinaryWriter(c.GetStream());
            reader = new BinaryReader(c.GetStream());

            _recieveThread = new Thread((o) =>
            {
                try
                {
                    while (true)
                    {
                        int length = reader.ReadInt32();
                        var data   = reader.ReadBytes(length);
                        var msg    = DirectProtoBufTools.Deserialize <GameMessage>(data);
                        MessageHandler(msg);
                    }
                }catch
                {
                }
            });
            _recieveThread.Start();
        }