示例#1
0
        private static void HandleMessage(NetMessageType type, NetConnection source, NetBuffer buffer)
        {
            switch (type)
            {
            case NetMessageType.DebugMessage:
                WriteToConsole(buffer.ReadString());
                break;

            case NetMessageType.StatusChanged:
                string statusMessage          = buffer.ReadString();
                NetConnectionStatus newStatus = (NetConnectionStatus)buffer.ReadByte();

                WriteToConsole("New status: " + newStatus + " (" + statusMessage + ")");
                UpdateStatisticsDisplay(source);
                break;

            case NetMessageType.Data:
                //System.IO.File.AppendAllText("C:\\receivedpackets.txt", s_userMessagesReceived.ToString() + ": " + msg.ReadString() + " (" + msg.m_sequenceNumber + ")" + Environment.NewLine);
                s_userMessagesReceived++;

                // simulate some processing of the message here
                for (int i = 0; i < buffer.LengthBytes - 2; i++)
                {
                    buffer.ReadByte();
                }

                // check checksum
                ushort checksum = NetChecksum.Adler16(buffer.Data, 0, buffer.LengthBytes - 2);
                ushort given    = buffer.ReadUInt16();
                if (checksum != given)
                {
                    WriteToConsole("Wrong checksum! Expected " + checksum + " found given " + given);
                }

                double b = s_userMessagesReceived;
                for (int i = 0; i < 1000; i++)
                {
                    b += Math.Sqrt((double)i) / Math.Sin(s_tmp);
                }
                s_tmp += b / 10000.0;
                break;

            default:
                break;
            }
        }
示例#2
0
        static void OnAppIdle(object sender, EventArgs e)
        {
            while (NativeMethods.AppStillIdle)
            {
                double now = NetTime.Now;

                if (s_client.Status != NetConnectionStatus.Disconnected)
                {
                    NetMessageType type;
                    while (s_client.ReadMessage(s_readBuffer, out type))
                    {
                        HandleMessage(type, s_readBuffer);
                    }

                    // send stressing packets
                    if (s_client.Status == NetConnectionStatus.Connected)
                    {
                        while (s_client.Status == NetConnectionStatus.Connected && s_sentUntil < now)
                        {
                            NetBuffer sendBuffer = s_client.CreateBuffer();
                            sendBuffer.Reset();
                            NetRandom.Instance.NextBytes(s_randomData);
                            sendBuffer.Write(s_randomData);

                            // calculate and append checksum
                            ushort checksum = NetChecksum.Adler16(s_randomData, 0, s_randomData.Length);
                            sendBuffer.Write(checksum);

                            //m_buffer.Write("Nr: " + s_messagesSent);
                            s_client.SendMessage(sendBuffer, m_sendOnChannel);
                            s_messagesSent++;
                            s_sentUntil += s_oneMessageTime;
                        }
                    }
                }

                if (now > s_nextDisplay)
                {
                    UpdateStatisticsDisplay(s_client.ServerConnection);
                    s_nextDisplay = now + 0.2;                     // five times per second
                }

                System.Threading.Thread.Sleep(1);
            }
        }