当前位置: 首页>>代码示例>>C#>>正文


C# MessageReader.GetMessageLength方法代码示例

本文整理汇总了C#中MessageReader.GetMessageLength方法的典型用法代码示例。如果您正苦于以下问题:C# MessageReader.GetMessageLength方法的具体用法?C# MessageReader.GetMessageLength怎么用?C# MessageReader.GetMessageLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MessageReader的用法示例。


在下文中一共展示了MessageReader.GetMessageLength方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReceiveCallback

 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         int bytesRead = clientConnection.GetStream().EndRead(ar);
         receiveMessageBytesLeft -= bytesRead;
         if (bytesRead > 0)
         {
             lastReceiveTime = UnityEngine.Time.realtimeSinceStartup;
         }
         if (receiveMessageBytesLeft == 0)
         {
             //We either have the header or the message data, let's do something
             if (!isReceivingMessage)
             {
                 //We have the header
                 using (MessageReader mr = new MessageReader(receiveMessage.data, true))
                 {
                     if (mr.GetMessageType() > (Enum.GetNames(typeof(ServerMessageType)).Length - 1))
                     {
                         //Malformed message, most likely from a non DMP-server.
                         Disconnect("Disconnected from non-DMP server");
                         //Returning from ReceiveCallback will break the receive loop and stop processing any further messages.
                         return;
                     }
                     receiveMessage.type = (ServerMessageType)mr.GetMessageType();
                     int length = mr.GetMessageLength();
                     if (length == 0)
                     {
                         //Null message, handle it.
                         receiveMessage.data = null;
                         HandleMessage(receiveMessage);
                         receiveMessage.type = 0;
                         receiveMessage.data = new byte[8];
                         receiveMessageBytesLeft = receiveMessage.data.Length;
                     }
                     else
                     {
                         if (length < Common.MAX_MESSAGE_SIZE)
                         {
                             isReceivingMessage = true;
                             receiveMessage.data = new byte[length];
                             receiveMessageBytesLeft = receiveMessage.data.Length;
                         }
                         else
                         {
                             //Malformed message, most likely from a non DMP-server.
                             Disconnect("Disconnected from non-DMP server");
                             //Returning from ReceiveCallback will break the receive loop and stop processing any further messages.
                             return;
                         }
                     }
                 }
             }
             else
             {
                 //We have the message data to a non-null message, handle it
                 isReceivingMessage = false;
                 using (MessageReader mr = new MessageReader(receiveMessage.data, false))
                 {
                     receiveMessage.data = mr.Read<byte[]>();
                 }
                 HandleMessage(receiveMessage);
                 receiveMessage.type = 0;
                 receiveMessage.data = new byte[8];
                 receiveMessageBytesLeft = receiveMessage.data.Length;
             }
         }
         if (state >= ClientState.CONNECTED && state != ClientState.DISCONNECTING)
         {
             clientConnection.GetStream().BeginRead(receiveMessage.data, receiveMessage.data.Length - receiveMessageBytesLeft, receiveMessageBytesLeft, new AsyncCallback(ReceiveCallback), null);
         }
     }
     catch (Exception e)
     {
         HandleDisconnectException(e);
     }
 }
开发者ID:GrantTaylor,项目名称:DarkMultiPlayer,代码行数:78,代码来源:NetworkWorker.cs

示例2: ReceiveCallback

 private static void ReceiveCallback(IAsyncResult ar)
 {
     ClientObject client = (ClientObject)ar.AsyncState;
     try
     {
         int bytesRead = client.connection.GetStream().EndRead(ar);
         client.bytesReceived += bytesRead;
         client.receiveMessageBytesLeft -= bytesRead;
         if (client.receiveMessageBytesLeft == 0)
         {
             //We either have the header or the message data, let's do something
             if (!client.isReceivingMessage)
             {
                 //We have the header
                 using (MessageReader mr = new MessageReader(client.receiveMessage.data, true))
                 {
                     if (mr.GetMessageType() > (Enum.GetNames(typeof(ClientMessageType)).Length - 1))
                     {
                         //Malformed message, most likely from a non DMP-client.
                         SendConnectionEnd(client, "Invalid DMP message. Disconnected.");
                         DarkLog.Normal("Invalid DMP message from " + client.endpoint);
                         //Returning from ReceiveCallback will break the receive loop and stop processing any further messages.
                         return;
                     }
                     client.receiveMessage.type = (ClientMessageType)mr.GetMessageType();
                     int length = mr.GetMessageLength();
                     if (length == 0)
                     {
                         //Null message, handle it.
                         client.receiveMessage.data = null;
                         HandleMessage(client, client.receiveMessage);
                         client.receiveMessage.type = 0;
                         client.receiveMessage.data = new byte[8];
                         client.receiveMessageBytesLeft = client.receiveMessage.data.Length;
                     }
                     else
                     {
                         if (length < Common.MAX_MESSAGE_SIZE)
                         {
                             client.isReceivingMessage = true;
                             client.receiveMessage.data = new byte[length];
                             client.receiveMessageBytesLeft = client.receiveMessage.data.Length;
                         }
                         else
                         {
                             //Malformed message, most likely from a non DMP-client.
                             SendConnectionEnd(client, "Invalid DMP message. Disconnected.");
                             DarkLog.Normal("Invalid DMP message from " + client.endpoint);
                             //Returning from ReceiveCallback will break the receive loop and stop processing any further messages.
                             return;
                         }
                     }
                 }
             }
             else
             {
                 //We have the message data to a non-null message, handle it
                 client.isReceivingMessage = false;
                 using (MessageReader mr = new MessageReader(client.receiveMessage.data, false))
                 {
                     client.receiveMessage.data = mr.Read<byte[]>();
                 }
                 HandleMessage(client, client.receiveMessage);
                 client.receiveMessage.type = 0;
                 client.receiveMessage.data = new byte[8];
                 client.receiveMessageBytesLeft = client.receiveMessage.data.Length;
             }
         }
         if (client.connectionStatus == ConnectionStatus.CONNECTED)
         {
             client.lastReceiveTime = Server.serverClock.ElapsedMilliseconds;
             client.connection.GetStream().BeginRead(client.receiveMessage.data, client.receiveMessage.data.Length - client.receiveMessageBytesLeft, client.receiveMessageBytesLeft, new AsyncCallback(ReceiveCallback), client);
         }
     }
     catch (Exception e)
     {
         HandleDisconnectException("ReceiveCallback", client, e);
     }
 }
开发者ID:nux17,项目名称:DarkMultiPlayer,代码行数:79,代码来源:ClientHandler.cs

示例3: Main

        public static void Main()
        {
            Random r = new Random();
            int messageType = 1;
            short shortTest = 2;
            int intTest = r.Next();
            long longTest = DateTime.UtcNow.Ticks;
            bool boolTest = true;
            bool bool2Test = false;
            byte byteTest = (byte)6;
            float floatTest = (float)Math.PI;
            double doubleTest = Math.PI;
            float[] floatArrayTest = new float[4];
            floatArrayTest[0] = (float)Math.PI;
            floatArrayTest[1] = (float)Math.PI + 1;
            floatArrayTest[2] = (float)Math.PI + 2;
            floatArrayTest[3] = (float)Math.PI + 3;
            double[] doubleArrayTest = new double[3];
            doubleArrayTest[0] = Math.PI;
            doubleArrayTest[1] = Math.PI + 1;
            doubleArrayTest[2] = Math.PI + 2;
            string stringTest = "This is a string";
            string[] stringArrayTest = new string[2];
            stringArrayTest[0] = "This is a";
            stringArrayTest[1] = "string array";
            byte[] byteArrayTest = new byte[1024];
            r.NextBytes(byteArrayTest);
            byte[] messageBytes;

            using (MessageWriter mw = new MessageWriter(messageType))
            {
                mw.Write<short>(shortTest);
                mw.Write<int>(intTest);
                mw.Write<long>(longTest);
                mw.Write<bool>(boolTest);
                mw.Write<bool>(bool2Test);
                mw.Write<byte>(byteTest);
                mw.Write<float>(floatTest);
                mw.Write<double>(doubleTest);
                mw.Write<float[]>(floatArrayTest);
                mw.Write<double[]>(doubleArrayTest);
                mw.Write<string>(stringTest);
                mw.Write<string[]>(stringArrayTest);
                mw.Write<byte[]>(byteArrayTest);
                mw.Write<string>(null);
                messageBytes = mw.GetMessageBytes();
            }
            Console.WriteLine(messageBytes.Length);
            using (MessageReader mr = new MessageReader(messageBytes, true))
            {
                short shortReturn = mr.Read<short>();
                int intReturn = mr.Read<int>();
                long longReturn = mr.Read<long>();
                bool boolReturn = mr.Read<bool>();
                bool bool2Return = mr.Read<bool>();
                byte byteReturn = mr.Read<byte>();
                float floatReturn = mr.Read<float>();
                double doubleReturn = mr.Read<double>();
                float[] floatArrayReturn = mr.Read<float[]>();
                double[] doubleArrayReturn = mr.Read<double[]>();
                string stringReturn = mr.Read<string>();
                string[] stringArrayReturn = mr.Read<string[]>();
                byte[] byteArrayReturn = mr.Read<byte[]>();
                string testEmptyString = mr.Read<string>();
                //Uncomment this to make it throw an exception.
                //mr.Read<byte>();
                Console.WriteLine("Message Type: " + mr.GetMessageType());
                Console.WriteLine("Message Length: " + mr.GetMessageLength());
                //===Tests===
                //Short test
                if (shortReturn == shortTest)
                {
                    Console.WriteLine("Short is correct: " + shortReturn);
                }
                else
                {
                    Console.WriteLine("WARNING: Short failed: " + shortReturn);
                }
                //Int test
                if (intReturn == intTest)
                {
                    Console.WriteLine("Int is correct: " + intReturn);
                }
                else
                {
                    Console.WriteLine("WARNING: Int failed:" + intReturn);
                }
                //Long test
                if (longReturn == longTest)
                {
                    Console.WriteLine("Long is correct: " + longReturn);
                }
                else
                {
                    Console.WriteLine("WARNING: Long failed: " + longReturn);
                }
                //Bool test
                if (boolReturn == boolTest)
                {
                    Console.WriteLine("Bool is correct: " + boolReturn);
//.........这里部分代码省略.........
开发者ID:JoshBlake,项目名称:BinarySerialiser,代码行数:101,代码来源:MessageStreamTester.cs


注:本文中的MessageReader.GetMessageLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。