Пример #1
0
        /// <summary>
        /// 获取Type类
        /// </summary>
        /// <param name="typeFullName">type的全名</param>
        /// <returns></returns>
        public static Type GetTypeByTypeFullName(string typeFullName, bool isShowErrorLog = true)
        {
            if (string.IsNullOrEmpty(typeFullName))
            {
                return(null);
            }
            Type type = Type.GetType(typeFullName);

            if (type == null)
            {
                Assembly ass = Assembly.GetExecutingAssembly();
                type = ass.GetType(typeFullName);

                if (type == null)
                {
                    Assembly[] assemblys = AppDomain.CurrentDomain.GetAssemblies();
                    for (int i = 0; i < assemblys.Length; i++)
                    {
                        Assembly assembly = assemblys[i];

                        type = assemblys[i].GetType(typeFullName);
                        if (type != null)
                        {
                            break;
                        }
                    }
                }
            }
            if (type == null && isShowErrorLog)
            {
                NetDebug.LogError("无法找到类型:" + typeFullName);
            }
            return(type);
        }
Пример #2
0
        public bool Connect(string networkAddress, int networkPort)
        {
            clientNetStatistics = new ClientNetStatistics();
            if (IsConnected)
            {
                NetDebug.LogError("client is connected!");
                return(false);
            }
            if (ConnectState == NetConnectState.Connecting)
            {
                return(false);
            }
            SetNetConnectState(NetConnectState.Connecting);

            this.networkAddress = networkAddress;
            this.networkPort    = networkPort;
            NetDebug.Log("Client connecting to " + networkAddress + ":" + networkPort);

            if (Transport.Connect(networkAddress, networkPort))
            {
                return(true);
            }
            SetNetConnectState(NetConnectState.DisConnected);
            return(false);
        }
Пример #3
0
        internal void StatisticSendPackets(byte property, int byteSize)
        {
            if (!UseStatistics)
            {
                return;
            }
            try
            {
                statistics.SendAllPackets++;
                statistics.SendAllBytes += byteSize;
                switch ((NetProperty)property)
                {
                case NetProperty.Data:
                    statistics.SendDataPackets++;
                    statistics.SendDataBytes += byteSize;
                    break;

                case NetProperty.HeartBeatClinetSend:
                    statistics.SendHeatBeatPackets++;
                    statistics.SendHeatBeatBytes += byteSize;
                    break;

                case NetProperty.Ping:
                    statistics.SendPingPackets++;
                    statistics.SendPingBytes += byteSize;
                    break;
                }
            }
            catch (System.Exception e)
            {
                NetDebug.LogError(e.ToString());
            }
        }
 public override bool Connect(string address, int port)
 {
     try
     {
         client.Connect(address, port);
     }
     catch (Exception e)
     {
         NetDebug.LogError(e.ToString());
         return(false);
     }
     return(true);
 }
Пример #5
0
        public static object CreateDefultInstance(Type type)
        {
            if (type == null)
            {
                NetDebug.LogError("Type不可为:null");
                return(null);
            }
            object instance = CreateDefultInstanceAll(type, true);;

            if (instance == null)
            {
                NetDebug.LogError("创建默认实例失败!Type:" + type.FullName);
            }
            return(instance);
        }
Пример #6
0
        public bool Send <T>(T messageData)
        {
            //if (isLog)
            //NetDebug.Log("Send Msg:" + SimpleJsonUtils.ToJson(messageData));

            if (IsConnected)
            {
                return(SendData(session, null, messageData));
            }
            else
            {
                NetDebug.LogError("Client not Connected!");
                return(false);
            }
        }
Пример #7
0
        //private NetDataWriter writer = null;
        public byte[] Serialize(object msgType, object data)
        {
            NetDataWriter writer = new NetDataWriter(byteOrder);

            writer.Reset();
            writer.PutValue(msgType);
            INetSerializable serializable = data as INetSerializable;

            if (serializable != null)
            {
                serializable.Serialize(writer);
            }
            else
            {
                NetDebug.LogError("cant change INetSerializable");
            }
            return(writer.CopyData());
        }
Пример #8
0
        private void Send2TransportProcess()
        {
            SendMsgTempData tempData;

            while (sendMsgLoop.TryDequeue(out tempData))
            {
                try
                {
                    Session session = tempData.session;
                    if (session == null)
                    {
                        continue;
                    }
                    byte[] datas = tempData.datas;
                    if (datas == null)
                    {
                        datas = new byte[0];
                    }

                    byte msgProperty = (byte)NetProperty.Data;
                    //必须能msgProperty发送接收一样的才行
                    NetMsgProcessPluginBase plugin = configuration.GetPlugin(msgProperty);
                    if (plugin == null)
                    {
                        NetDebug.LogError("No NetMsgProcessPluginBase:" + msgProperty);
                        continue;
                    }
                    byte[] res = plugin.SendProcess(session, msgProperty, datas);
                    if (res == null)
                    {
                        NetDebug.LogError("No Implement NetMsgProcessPluginBase.SendProcess:" + msgProperty);
                        continue;
                    }
                    //NetDebug.Log("Send byte:" + res.Length );
                    session.StatisticSendPackets(msgProperty, res.Length);
                    Sendbytes(session, res);
                }
                catch (Exception e)
                {
                    NetDebug.LogError("Send error:" + e);
                }
            }
        }
 public override bool Send(long connectionId, byte[] data)
 {
     try
     {
         if (!IsServer)
         {
             client.Send(data);
         }
         else
         {
             server.Send((int)connectionId, data);
         }
     }
     catch (Exception e)
     {
         NetDebug.LogError(e.ToString());
         return(false);
     }
     return(true);
 }
 public override bool Disconnect(long connectionId, EDisconnectReason disconnectReason)
 {
     try
     {
         if (!IsServer)
         {
             client.Disconnect();
         }
         else
         {
             server.Disconnect((int)connectionId);
         }
     }
     catch (Exception e)
     {
         NetDebug.LogError(e.ToString());
         return(false);
     }
     return(true);
 }
Пример #11
0
        //private NetDataReader reader = null;
        public object Deserialize(byte[] datas, out object msgType)
        {
            //NetDebug.Log("Deserialize收到消息:" + datas.Length);
            NetDataReader reader = new NetDataReader(byteOrder);

            reader.SetSource(datas, 0);
            msgType = reader.GetString();
            string msgT = msgType.ToString();

            if (!typeDic.ContainsKey(msgT))
            {
                NetDebug.LogError("No msgType:" + msgType);
                return(null);
            }
            Type             type         = typeDic[msgT];
            INetSerializable serializable = (INetSerializable)ReflectionTool.CreateDefultInstance(type);

            serializable.Deserialize(reader);
            return(serializable);
        }
Пример #12
0
        private void OnHandleMsgPackests()
        {
            if (netMsgPackests.Count == 0)
            {
                return;
            }
            MsgPackest packest;

            while (netMsgPackests.TryDequeue(out packest))
            {
                Session session = packest.session;

                INetMsgSerializer serializer = configuration.GetMsgSerializer();
                object            msgType    = null;
                object            msgData    = null;
                try
                {
                    msgData = serializer.Deserialize(packest.contents, out msgType);
                }
                catch (Exception e)
                {
                    NetDebug.LogError("消息序列化失败:" + e);
                    return;
                }


                IMessageHandler messageHander = configuration.GetMessageHander();
                if (messageHander != null)
                {
                    try
                    {
                        messageHander.DispatchMessage(session, msgType, msgData);
                    }
                    catch (Exception e)
                    {
                        NetDebug.LogError("DispatchMessage error:" + e);
                    }
                }
            }
        }
Пример #13
0
        public void Update(float deltaTime)
        {
            if (Transport == null)
            {
                return;
            }

            if (!configuration.UseMultithreading)
            {
                LoopDealwithReceiveData();
                Send2TransportProcess();
            }
            List <NetMsgProcessPluginBase> pluginList = Configuration.GetNetMsgProcessPlugins();

            foreach (var item in pluginList)
            {
                item.Update(deltaTime);
            }

            while (netEventDatas.Count > 0)
            {
                try
                {
                    OnHandleEvent();
                }
                catch (Exception e)
                {
                    NetDebug.LogError(e.ToString());
                }
            }
            try
            {
                OnHandleMsgPackests();
            }
            catch (Exception e)
            {
                NetDebug.LogError(e.ToString());
            }
            OnUpdate(deltaTime);
        }
Пример #14
0
        protected bool SendData <T>(Session session, object msgType, T messageData)
        {
            //if (LogInfo)
            //{
            //NetDebug.Log("Send Msg =>::" + typeof(T).Name + "-->" + JsonUtility.ToJson(messageData));
            //}
            if (Transport == null)
            {
                return(false);
            }

            INetMsgSerializer serializer = configuration.GetMsgSerializer();

            if (serializer == null)
            {
                NetDebug.LogError("No INetMsgSerializer!");
                return(false);
            }
            if (msgType == null)
            {
                msgType = serializer.GetMsgType(messageData);
            }
            byte[] datas = null;
            try
            {
                datas = serializer.Serialize(msgType, messageData);
            }
            catch (Exception e)
            {
                NetDebug.LogError("INetMsgSerializer error:" + e);
                return(false);
            }

            sendMsgLoop.Enqueue(new SendMsgTempData(session, datas));
            return(true);
        }
Пример #15
0
        public override void ReceveProcess(MsgPackest packest)
        {
            Session session = packest.session;

            if (packest.isEncryption == 1)
            {
                MsgEncryptionBase encryption = networkCommon.Configuration.GetMsgEncryption();
                if (encryption == null)
                {
                    NetDebug.LogError("不支持消息解密:" + packest);
                    return;
                }
                else
                {
                    try
                    {
                        packest.contents = encryption.Decryption(packest.session, packest.contents);
                    }
                    catch (System.Exception e)
                    {
                        NetDebug.LogError("消息解密错误:" + packest + " \n" + e);
                        return;
                    }
                }
            }
            if (packest.isCompress == 1)
            {
                MsgCompressBase compress = networkCommon.Configuration.GetCompressFunction(packest.compressType);
                if (compress == null)
                {
                    NetDebug.LogError("不支持的压缩方式:" + packest.compressType);
                    return;
                }
                else
                {
                    packest.contents = compress.Decompress(packest.contents);
                    //NetDebug.Log("解压缩:"+ packest.contents.Length);
                }
            }
            if (bitConverter == null || bitConverter.byteOrder != packest.byteOrder)
            {
                bitConverter = EndianBitConverter.GetBitConverter(packest.byteOrder);
            }
            //收发消息计数器(标明消息序列号)
            uint counter = bitConverter.ToUInt32(packest.contents, 0);

            //if (session.CheckReceiveMsgCounter(counter))
            //{
            //    session.SetReceiveCounter(counter);
            //}
            //else
            //{
            //    NetDebug.LogError("packest.counter error:" + counter + "  session.ReceiveMsgCounter:" + (session.ReceiveMsgCounter + 1));
            //    return;
            //}
            byte[] dataArray = new byte[packest.contents.Length - 4];
            //NetDebug.Log("packest.contents.Length:" + packest.contents.Length + " dataArray:" + dataArray.Length) ;
            Array.Copy(packest.contents, 4, dataArray, 0, dataArray.Length);
            packest.contents = dataArray;
            networkCommon.ReceiveMsgPackest(packest);
        }
Пример #16
0
        public override byte[] SendProcess(Session session, byte msgProperty, byte[] datas)
        {
            ByteOrder byteOrder    = networkCommon.Configuration.byteOrder;
            byte      compressType = 0;
            byte      isEncryption = 0;
            byte      isCompress   = 0;

            if (bitConverter == null || bitConverter.byteOrder != byteOrder)
            {
                bitConverter = EndianBitConverter.GetBitConverter(byteOrder);
            }
            byte[] pBytes   = bitConverter.GetBytes(session.AddSendCounter());
            byte[] allDatas = new byte[pBytes.Length + datas.Length];
            pBytes.CopyTo(allDatas, 0);
            datas.CopyTo(allDatas, pBytes.Length);
            datas = allDatas;

            MsgCompressBase compress = networkCommon.Configuration.GetSendCompressFunction();

            if (compress != null)
            {
                isCompress = 1;
                try
                {
                    //NetDebug.Log("压缩消息:" + datas.Length);
                    datas = compress.Compress(datas);
                    //NetDebug.Log("压缩后消息:" + datas.Length);
                }
                catch (Exception e)
                {
                    NetDebug.LogError("压缩错误:" + e);
                    return(null);
                }
                compressType = compress.CompressType;
            }
            else
            {
                isCompress = 0;
            }


            MsgEncryptionBase encryption = networkCommon.Configuration.GetMsgEncryption();

            if (networkCommon.Configuration.IsEncryption && encryption != null)
            {
                isEncryption = 1;
                try
                {
                    datas = encryption.Encryption(session, datas);
                }
                catch (Exception e)
                {
                    NetDebug.LogError("加密错误:" + e);
                    return(null);
                }
            }
            else
            {
                isEncryption = 0;
            }


            byte[] res = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, isEncryption, isCompress, compressType, msgProperty, datas);
            return(res);
        }
Пример #17
0
        private void LoopDealwithReceiveData()
        {
            if (Transport == null)
            {
                return;
            }

            TransportEventData eventData;

            while (Transport.Receive(out eventData))
            {
                Session session = null;

                sessions.TryGetValue(eventData.connectionId, out session);

                if (eventData.type == ENetworkEvent.DataEvent)
                {
                    if (session == null)
                    {
                        NetDebug.LogError("session is null, connectionId :" + session + " is not Connnected");
                        return;
                    }
                    MsgPackest packest;
                    try
                    {
                        packest = new MsgPackest(configuration.byteOrder, session, eventData.data);
                    }
                    catch (Exception e)
                    {
                        NetDebug.LogError("Parse MsgPackest Error :" + e);
                        continue;
                    }
                    if (eventData.data != null)
                    {
                        session.StatisticReceivePackets(packest.msgProperty, eventData.data.Length);
                    }
                    //NetDebug.Log("接收到消息 connectionId:" + packest.connectionId);
                    NetMsgProcessPluginBase plugin = configuration.GetPlugin(packest.msgProperty);
                    if (plugin == null)
                    {
                        NetDebug.LogError("No NetMsgProcessPluginBase By msgProperty:" + packest.msgProperty);
                    }
                    else
                    {
                        plugin.ReceveProcess(packest);
                    }
                }
                else
                {
                    if (eventData.type == ENetworkEvent.ConnectEvent)
                    {
                        session = new Session(eventData.connectionId);
                        session.OpenNetStatistics(Configuration.UseStatistics);
                        session.SetConnectTimeInStatistic();
                        sessions.Add(session.ConnectionId, session);

                        foreach (var plugin in configuration.GetNetMsgProcessPlugins())
                        {
                            plugin.PeerConnectedEvent(session);
                        }
                    }
                    else if (eventData.type == ENetworkEvent.DisconnectEvent)
                    {
                        if (session != null)
                        {
                            session.SetDisconnectTimeInStatistic();
                        }
                        sessions.Remove(eventData.connectionId);

                        foreach (var plugin in configuration.GetNetMsgProcessPlugins())
                        {
                            plugin.DisconnectedEvent(session, eventData.disconnectInfo);
                        }
                    }

                    eventData.session = session;
                    //NetDebug.Log("循环接收事件:" + eventData.type);
                    AddNetEvent(eventData);
                }
            }
        }