コード例 #1
0
        static void Main(string[] args)
        {
            SqlConn.ConnectDatabase();
            TypeDo.AddType();
            Server srv = new Server();

            srv.Start("192.168.1.5", 62345); //127.0.0.1
            SqlConn.InitializePersenInfo();
            while (true)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                FrameUpdate();

                stopwatch.Stop();

                long sleepTime = FrameTimeMs - stopwatch.ElapsedMilliseconds;
                // System.Console.WriteLine(stopwatch.ElapsedMilliseconds);
                if (sleepTime > 0)//执行时间测试
                {
                    Thread.Sleep((int)sleepTime);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 粘包处理
        /// </summary>
        private void ProcessData()
        {
            //小于长度字节
            if (buffCount < HEADLENGTH)//命令+长度=2 int
            {
                Console.WriteLine("长度:" + buffCount);
                Console.WriteLine("buffCount < 8");
                return;
            }

            //消息长度
            // Array.Copy(readBuff, 4, lenBytes, 0, sizeof(Int32));
            msgLength      = BitConverter.ToInt32(readBuff, 4);//根据从数组第4位开始获取int(4字节长度)的对应int值获取长度值
            msgTotalLength = msgLength + HEADLENGTH;

            if (buffCount < msgTotalLength)
            {
                Console.WriteLine("长度:" + buffCount);
                Console.WriteLine("buffCount < msgLength + 8");
                return;
            }

            //处理消息
            whole = new byte[msgTotalLength];//完整的消息长度
            Array.Copy(readBuff, whole, msgTotalLength);
            //命令处理
            TypeDo.DoType(whole, this);

            //清除已处理的消息
            int count = buffCount - msgTotalLength;

            Array.Copy(readBuff, msgTotalLength, readBuff, 0, count);
            buffCount = count;
            if (buffCount > 0)
            {
                ProcessData();//如果消息过大还可以继续处理看能不能再找出完整的消息
            }
        }
コード例 #3
0
 /// <summary>
 /// 执行方法
 /// 执行命令处理(所有(处理消息)命令放到主线程处理,接收消息放到多线程处理)
 /// </summary>
 /// <param name="heartTime"></param>
 static void FrameUpdate()
 {
     g_heartTime.Update();
     TypeDo.ProcessAllCommand();
 }