Пример #1
0
        protected override void OnInit()
        {
            if (networkCommon.Configuration.UseMultithreading)
            {
                heardBeatThread = new Thread(HeardBeatThreadFun);
                heardBeatThread.Start();
                heardBeatThread.IsBackground = true;
            }

            sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder,
                                               0, 0, 0, (byte)NetProperty.HeartBeatClinetSend, new byte[0]);
        }
Пример #2
0
        private void ClientPingUpdate(object obj)
        {
            int tempTime = pingDelayTime;

            while (true)
            {
                if (enable && isConnect)
                {
                    if (msgQueue.Count > 0)
                    {
                        MsgPackest eventData = msgQueue.Dequeue();
                        long       lastTime  = bitConverter.ToInt64(eventData.contents, 0);
                        //NetDebug.Log("接收到时间:" + lastTime + " eventData.contents"+eventData.contents.Length);
                        ping = (int)((DateTime.Now.Ticks - lastTime) / 20000);
                        msgQueue.Clear();
                    }

                    if (tempTime <= 0)
                    {
                        tempTime = pingDelayTime;
                        //Debug.Log("发送时间:" + DateTime.Now.Ticks);
                        byte[] contents = bitConverter.GetBytes(DateTime.Now.Ticks);
                        //long temp = bitConverter.ToInt64(contents, 0);
                        //NetDebug.Log("发送Ping:" + temp);

                        byte[] sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, 0, 0, 0, property, contents);
                        session.StatisticSendPackets(property, sendBytes.Length);
                        networkCommon.Sendbytes(session, sendBytes);
                    }
                    else
                    {
                        tempTime -= 50;
                    }
                }
                Thread.Sleep(50);
            }
        }
Пример #3
0
 public override void ReceveProcess(MsgPackest packest)
 {
     networkCommon.Sendbytes(packest.session, sendBytes);
 }
Пример #4
0
 protected override void OnInit()
 {
     sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, 0, 0, 0, (byte)NetProperty.HeartBeatServerSend, new byte[0]);
 }
Пример #5
0
 public override void ReceveProcess(MsgPackest packest)
 {
     byte[] sendBytes = MsgPackest.Write2Bytes(networkCommon.Configuration.byteOrder, 0, 0, 0, (byte)NetProperty.Pong, packest.contents);
     networkCommon.Sendbytes(packest.session, sendBytes);
 }
 public override void ReceveProcess(MsgPackest packest)
 {
     msgQueue.Enqueue(packest);
     OnMsgReceve();
 }
 public virtual void ReceveProcess(MsgPackest packest)
 {
 }
Пример #8
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);
        }
Пример #9
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);
        }
Пример #10
0
 internal void ReceiveMsgPackest(MsgPackest packest)
 {
     //NetDebug.Log("收到处理消息包:"+packest.counter);
     netMsgPackests.Enqueue(packest);
 }
Пример #11
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);
                }
            }
        }