Esempio n. 1
0
        void ReadPacketQueueProcess(object sender, EventArgs e)
        {
            ProcessLog();

            try
            {
                JsonPacketData packet = null;
                lock (((System.Collections.ICollection)RecvPacketQueue).SyncRoot)
                {
                    if (RecvPacketQueue.Count() > 0)
                    {
                        packet = RecvPacketQueue.Dequeue();
                    }
                }

                if (packet != null)
                {
                    switch (packet.PacketID)
                    {
                    case PACKET_ID_DISCONNECTED:
                        SetDisconnectd();
                        break;

                    case PACKET_ID_ECHO:
                    {
                        string jsonstring = System.Text.Encoding.GetEncoding("utf-8").GetString(packet.JsonFormatData);
                        var    resData    = Newtonsoft.Json.JsonConvert.DeserializeObject <JsonPacketNoticeEcho>(jsonstring);

                        textBoxSendChat.Text = "";
                        var msg = string.Format("[ECHO]: {0}", resData.Msg);
                        listBoxChat.Items.Add(msg);
                    }
                    break;

                    case PACKET_ID_CHAT:
                    {
                        string jsonstring = System.Text.Encoding.GetEncoding("utf-8").GetString(packet.JsonFormatData);
                        var    resData    = Newtonsoft.Json.JsonConvert.DeserializeObject <JsonPacketResponseChat>(jsonstring);

                        textBoxSendChat.Text = "";
                        var msg = string.Format("[{0}]: {1}", resData.who, resData.chat);
                        listBoxChat.Items.Add(msg);
                    }
                    break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("ReadPacketQueueProcess. error:{0}", ex.Message));
            }
        }
Esempio n. 2
0
        void NetworkReadProcess()
        {
            while (IsNetworkThreadRunning)
            {
                if (Network.IsConnected() == false)
                {
                    continue;
                }

                var recvData = Network.Receive();

                if (recvData.Count > 0)
                {
                    PacketBuffer.Write(recvData.Array, recvData.Offset, recvData.Count);

                    while (true)
                    {
                        var data = PacketBuffer.Read();
                        if (data.Count < 1)
                        {
                            break;
                        }

                        var packet = new JsonPacketData();
                        packet.PacketID       = BitConverter.ToUInt16(data.Array, data.Offset);
                        packet.PacketOption   = BitConverter.ToUInt32(data.Array, data.Offset + 2);
                        packet.DataCRC        = BitConverter.ToUInt32(data.Array, data.Offset + 6);
                        packet.DataSize       = BitConverter.ToUInt16(data.Array, data.Offset + 10);
                        packet.JsonFormatData = new byte[packet.DataSize];
                        Buffer.BlockCopy(recvData.Array, (recvData.Offset + 12), packet.JsonFormatData, 0, (data.Count - 12));

                        lock (((System.Collections.ICollection)RecvPacketQueue).SyncRoot)
                        {
                            RecvPacketQueue.Enqueue(packet);
                        }
                    }
                }
                else
                {
                    var packet = new JsonPacketData();
                    packet.PacketID = PACKET_ID_DISCONNECTED;
                    packet.DataSize = 0;

                    lock (((System.Collections.ICollection)RecvPacketQueue).SyncRoot)
                    {
                        RecvPacketQueue.Enqueue(packet);
                    }
                }
            }
        }
Esempio n. 3
0
        void ReadPacketQueueProcess(object sender, EventArgs e)
        {
            ProcessLog();

            try
            {
                JsonPacketData packet = null;
                lock (((System.Collections.ICollection)RecvPacketQueue).SyncRoot)
                {
                    if (RecvPacketQueue.Count() > 0)
                    {
                        packet = RecvPacketQueue.Dequeue();
                    }
                }

                if (packet != null)
                {
                    switch (packet.PacketID)
                    {
                    case PACKET_ID_DISCONNECTED:
                        SetDisconnectd();
                        break;

                    case PACKET_ID_ECHO:
                    {
                        var resData = JsonEnDecode.Decode <JsonPacketNoticeEcho>(packet.JsonFormatData);

                        textBoxSendChat.Text = "";
                        var msg = string.Format("[ECHO]: {0}", resData.Msg);
                        listBoxLog.Items.Add(msg);
                    }
                    break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("ReadPacketQueueProcess. error:{0}", ex.Message));
            }
        }
Esempio n. 4
0
        void NetworkReadProcess()
        {
            while (IsNetworkThreadRunning)
            {
                if (Network.IsConnected() == false)
                {
                    continue;
                }

                var recvData = Network.Receive();

                if (recvData.Count > 0)
                {
                    PacketBuffer.Write(recvData.Array, recvData.Offset, recvData.Count);

                    while (true)
                    {
                        var data = PacketBuffer.Read();
                        if (data.Count < 1)
                        {
                            break;
                        }

                        var packet = new JsonPacketData();
                        packet.PacketID = BitConverter.ToUInt16(data.Array, data.Offset);
                        packet.PacketOption = BitConverter.ToUInt32(data.Array, data.Offset+2);
                        packet.DataCRC = BitConverter.ToUInt32(data.Array, data.Offset+6);
                        packet.DataSize = BitConverter.ToUInt16(data.Array, data.Offset+10);
                        packet.JsonFormatData = new byte[packet.DataSize];
                        Buffer.BlockCopy(recvData.Array, (recvData.Offset + 12), packet.JsonFormatData, 0, (data.Count - 12));

                        lock (((System.Collections.ICollection)RecvPacketQueue).SyncRoot)
                        {
                            RecvPacketQueue.Enqueue(packet);
                        }
                    }
                }
                else
                {
                    var packet = new JsonPacketData();
                    packet.PacketID = PACKET_ID_DISCONNECTED;
                    packet.DataSize = 0;

                    lock (((System.Collections.ICollection)RecvPacketQueue).SyncRoot)
                    {
                        RecvPacketQueue.Enqueue(packet);
                    }
                }
            }
        }