コード例 #1
0
ファイル: DataHelper.cs プロジェクト: chenchungit/ky
        /// <summary>
        /// 将对象转为字节流
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <param name="pool"></param>
        /// <param name="cmdID"></param>
        /// <returns></returns>
        public static byte[] ObjectToBytes <T>(T instance)
        {
            try
            {
                byte[] bytesCmd = null;
                if (null == instance)
                {
                    bytesCmd = new byte[0];
                }
                else if (instance is IProtoBuffData)
                {
                    return((instance as IProtoBuffData).toBytes());
                }
                else
                {
                    if (GameManager.FlagOptimizeThreadPool)
                    {
                        TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance();
                        MemoryStream          ms  = tsc.PopMemoryStream();
                        Serializer.Serialize <T>(ms, instance);
                        bytesCmd    = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(bytesCmd, 0, bytesCmd.Length);
                        tsc.PushMemoryStream(ms);
                    }
                    else
                    {
                        MemoryStream ms = new MemoryStream();
                        Serializer.Serialize <T>(ms, instance);
                        bytesCmd    = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(bytesCmd, 0, bytesCmd.Length);
                        ms.Dispose();
                        ms = null;
                    }
                }

                if (bytesCmd.Length > DataHelper.MinZipBytesSize) //大于256字节的才压缩, 节省cpu占用,想一想,每秒10兆小流量的吐出,都在压缩,cpu占用当然会高, 带宽其实不是问题, 不会达到上限(100兆共享)
                {
                    //zlib压缩算法
                    byte[] newBytes = DataHelper.Compress(bytesCmd);
                    if (null != newBytes)
                    {
                        if (newBytes.Length < bytesCmd.Length)
                        {
                            //System.Diagnostics.Debug.WriteLine(string.Format("{0}压缩率: {1}", instance.GetType(), ((double)newBytes.Length / bytesCmd.Length) * 100.0));
                            bytesCmd = newBytes;
                        }
                    }
                }

                return(bytesCmd);
            }
            catch (Exception ex)
            {
                WriteExceptionLogEx(ex, "将对象转为字节流发生异常:");
            }

            return(new byte[0]);
        }
コード例 #2
0
ファイル: SendBufferManager.cs プロジェクト: chenchungit/ky
        /// <summary>
        /// 移除相应套接字的缓存
        /// </summary>
        /// <param name="s"></param>
        public void Remove(TMSKSocket s)
        {
            SendBuffer sendBuffer = null;

            lock (BufferDict)
            {
                if (BufferDict.TryGetValue(s, out sendBuffer))
                {
                    BufferDict.Remove(s);
                    if (GameManager.FlagOptimizeLock)
                    {
                        s._SendBuffer = null;
                    }
                }
            }

            //在外部调用,多线程重复也无所谓
            if (null != sendBuffer)
            {
                if (GameManager.FlagOptimizeThreadPool2)
                {
                    TMSKThreadStaticClass.GetInstance().PushMemoryBlock(sendBuffer.MyMemoryBlock);
                }
                else
                {
                    Global._MemoryManager.Push(sendBuffer.MyMemoryBlock);
                }
            }

            if (!GameManager.FlagOptimizeLockTrace)
            {
                Global._FullBufferManager.Remove(s);
            }
        }
コード例 #3
0
        public static T BytesToObject <T>(byte[] bytesData, int offset, int length)
        {
            T result;

            if (bytesData.Length == 0)
            {
                result = default(T);
            }
            else
            {
                try
                {
                    byte[] copyData = new byte[length];
                    DataHelper.CopyBytes(copyData, 0, bytesData, offset, length);
                    copyData = DataHelper.Uncompress(copyData);
                    TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance();
                    MemoryStream          ms  = tsc.PopMemoryStream();
                    ms.Write(copyData, 0, copyData.Length);
                    ms.Position = 0L;
                    T t = Serializer.Deserialize <T>(ms);
                    tsc.PushMemoryStream(ms);
                    return(t);
                }
                catch (Exception ex)
                {
                    DataHelper.WriteExceptionLogEx(ex, "将字节数据转为对象发生异常:");
                }
                result = default(T);
            }
            return(result);
        }
コード例 #4
0
ファイル: SendBuffer.cs プロジェクト: neyma2379294/MuOrigin
        /// <summary>
        /// 重置buffer, 然后可以重新使用,内部调用的函数,构造函数和发送完毕之后调用,外部锁上了,理论上这儿不用锁
        /// </summary>
        protected void Reset(bool init = false)
        {
            //lock (this)
            //{
            if (init || !GameManager.FlagOptimizeThreadPool4)
            {
                _AddFirstPacketTicks = 0;
                _CurrentBufferSize   = 0;
            }

            if (_MaxBufferSize > 0 && _MaxBufferSize <= (4 * 1024 * 1024))
            {
                bool needMemoryBlock = true;
                if (GameManager.FlagOptimizeThreadPool5)
                {
                    if (null != _MemoryBlock)
                    {
                        if (_MemoryBlock.BlockSize < _MaxBufferSize)
                        {
                            if (GameManager.FlagOptimizeThreadPool2)
                            {
                                TMSKThreadStaticClass.GetInstance().PushMemoryBlock(_MemoryBlock);
                            }
                            else
                            {
                                Global._MemoryManager.Push(_MemoryBlock);
                            }
                            _MemoryBlock = null;
                            _Buffer      = null;
                        }
                        else
                        {
                            needMemoryBlock = false;
                        }
                    }
                }
                if (needMemoryBlock)
                {
                    //理论上不会返回null,除非内存耗尽或出错
                    if (GameManager.FlagOptimizeThreadPool2)
                    {
                        _MemoryBlock = TMSKThreadStaticClass.GetInstance().PopMemoryBlock(_MaxBufferSize);
                    }
                    else
                    {
                        _MemoryBlock = Global._MemoryManager.Pop(_MaxBufferSize);
                    }
                    _Buffer = _MemoryBlock.Buffer;
                }
            }
            //}
        }
コード例 #5
0
ファイル: TCPOutPacket.cs プロジェクト: neyma2379294/MuOrigin
        /// <summary>
        /// 重复利用指令包
        /// </summary>
        public void Reset()
        {
            PacketBytes     = null;
            PacketCmdID     = 0;
            _PacketDataSize = 0;

            if (null != _MemoryBlock)
            {
                if (GameManager.FlagOptimizeThreadPool2)
                {
                    TMSKThreadStaticClass.GetInstance().PushMemoryBlock(_MemoryBlock);
                }
                else
                {
                    Global._MemoryManager.Push(_MemoryBlock);
                }
                _MemoryBlock = null;
            }
        }
コード例 #6
0
ファイル: DataHelper.cs プロジェクト: chenchungit/ky
        //public static T BytesToProtocol<T>(byte[] bytesData, int offset, int length) where T : class, IProtoBuffData, new()
        //{
        //    T szProtocol = new T();
        //    try
        //    {
        //        MemoryStream msRecvDatas;
        //        msRecvDatas = new MemoryStream(bytesData, offset, length);
        //        szProtocol = Serializer.Deserialize<T>(msRecvDatas);
        //    }
        //    catch (Exception ex)
        //    {
        //        SysConOut.WriteLine("解析切换地图消息错误");
        //        LogManager.WriteLog(LogTypes.Data, string.Format("解析客户端发上来的数据{0}异常,IP:{1},数据内容:{2}", t.ToString(), socket.RemoteEndPoint.ToString(), Convert.ToBase64String(bytesData, offset, length)));
        //    }
        //    return default(T);
        //}

        /// <summary>
        /// 将字节数据转为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytesData"></param>
        /// <returns></returns>
        public static T BytesToObject <T>(byte[] bytesData, int offset, int length)
        {
            if (bytesData.Length == 0)
            {
                return(default(T));
            }

            try
            {
                //zlib解压缩算法
                byte[] copyData = new byte[length];
                DataHelper.CopyBytes(copyData, 0, bytesData, offset, length);
                copyData = DataHelper.Uncompress(copyData);

                if (GameManager.FlagOptimizeThreadPool)
                {
                    TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance();
                    MemoryStream          ms  = tsc.PopMemoryStream();
                    ms.Write(copyData, 0, copyData.Length);
                    ms.Position = 0;
                    T t = Serializer.Deserialize <T>(ms);
                    tsc.PushMemoryStream(ms);
                    return(t);
                }
                else
                {
                    MemoryStream ms = new MemoryStream();
                    ms.Write(copyData, 0, copyData.Length);
                    ms.Position = 0;
                    T t = Serializer.Deserialize <T>(ms);
                    ms.Dispose();
                    ms = null;
                    return(t);
                }
            }
            catch (Exception ex)
            {
                WriteExceptionLogEx(ex, "将字节数据转为对象发生异常:");
            }

            return(default(T));
        }
コード例 #7
0
 public static byte[] ObjectToBytes <T>(T instance)
 {
     try
     {
         byte[] bytesCmd;
         if (null == instance)
         {
             bytesCmd = new byte[0];
         }
         else
         {
             if (instance is IProtoBuffData)
             {
                 return((instance as IProtoBuffData).toBytes());
             }
             TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance();
             MemoryStream          ms  = tsc.PopMemoryStream();
             Serializer.Serialize <T>(ms, instance);
             bytesCmd    = new byte[ms.Length];
             ms.Position = 0L;
             ms.Read(bytesCmd, 0, bytesCmd.Length);
             tsc.PushMemoryStream(ms);
         }
         if (bytesCmd.Length > DataHelper.MinZipBytesSize && instance is ICompressed)
         {
             byte[] newBytes = DataHelper.Compress(bytesCmd);
             if (null != newBytes)
             {
                 if (newBytes.Length < bytesCmd.Length)
                 {
                     bytesCmd = newBytes;
                 }
             }
         }
         return(bytesCmd);
     }
     catch (Exception ex)
     {
         DataHelper.WriteExceptionLogEx(ex, "将对象转为字节流发生异常:");
     }
     return(new byte[0]);
 }
コード例 #8
0
ファイル: TCPManager.cs プロジェクト: chenchungit/ky
        /// <summary>
        /// 发送数据通知函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SocketSended(object sender, SocketAsyncEventArgs e)
        {
            ////SocketListener sl = sender as SocketListener;
            ////TMSKSocket s = (e.UserToken as AsyncUserToken).CurrentSocket;
            //TCPOutPacket tcpOutPacket = (e.UserToken as AsyncUserToken).Tag as TCPOutPacket;
            ////采用发送数据缓存后Tag被绑定为null
            //if (null != tcpOutPacket)
            //{
            //    tcpOutPacketPool.Push(tcpOutPacket);
            //}

            AsyncUserToken userToken = (e.UserToken as AsyncUserToken);

            if (!GameManager.FlagOptimizeThreadPool5 && !GameManager.FlagOptimizeThreadPool4)
            {
                MemoryBlock item = userToken.Tag as MemoryBlock;
                if (null != item)
                {
                    if (GameManager.FlagOptimizeThreadPool2)
                    {
                        TMSKThreadStaticClass.GetInstance().PushMemoryBlock(item);
                    }
                    else
                    {
                        Global._MemoryManager.Push(item);
                    }
                }
            }

            if (GameManager.FlagOptimizeThreadPool4)
            {
                SendBuffer sendBuffer = userToken._SendBuffer;
                if (null != sendBuffer)
                {
                    sendBuffer.Reset2();
                }
            }

            TMSKSocket s = userToken.CurrentSocket;

            Global._SendBufferManager.OnSendBufferOK(s);
        }
コード例 #9
0
ファイル: TCPOutPacket.cs プロジェクト: neyma2379294/MuOrigin
        /// <summary>
        /// 数据写入
        /// </summary>
        /// <param name="buffer">数据缓存</param>
        /// <param name="offset">从字节偏移处拷贝</param>
        /// <param name="count">写入的长度</param>
        public bool FinalWriteData(byte[] buffer, int offset, int count)
        {
            if (null != PacketBytes)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("TCP发出命令包不能被重复写入数据, 命令ID: {0}", PacketCmdID));
                return(false);
            }

            //先判断是否超出的最大包的大小
            if ((6 + count) >= (int)TCPCmdPacketSize.MAX_SIZE)
            {
                //throw new Exception(string.Format("TCP命令包长度最大不能超过: {0}", (int)TCPCmdPacketSize.MAX_SIZE));
                LogManager.WriteLog(LogTypes.Error, string.Format("TCP命令包长度:{0}, 最大不能超过: {1}, 命令ID: {2}", count, (int)TCPCmdPacketSize.MAX_SIZE, PacketCmdID));
                return(false);
            }

            //理论上这儿是不会返回NULL的,如果返回,直接崩溃,外部会接收到异常
            if (GameManager.FlagOptimizeThreadPool2)
            {
                _MemoryBlock = TMSKThreadStaticClass.GetInstance().PopMemoryBlock(count + 6);
            }
            else
            {
                _MemoryBlock = Global._MemoryManager.Pop(count + 6);
            }
            PacketBytes = _MemoryBlock.Buffer;

            //写入数据
            int offsetTo = (int)6;

            DataHelper.CopyBytes(PacketBytes, offsetTo, buffer, offset, count);
            _PacketDataSize = count;
            Final();

            return(true);
        }