WEB开发网
开发学院软件开发C语言 P2P网络通讯程序(c#) 阅读

P2P网络通讯程序(c#)

 2009-03-27 08:22:11 来源:WEB开发网   
核心提示: b、消息接受线程/// <summary> /// 接受线程处理 /// </summary> private void RecvThreadProc() { byte[] buffer; while (true) { buffer = client.Recei

b、消息接受线程

/// <summary>
        /// 接受线程处理
        /// </summary>
        private void RecvThreadProc()
        {
            byte[] buffer;
            while (true)
            {
               
               
                buffer = client.Receive(ref remotePoint);
                Packet packet = UtilityHelper.Deserialize(buffer) as Packet;     
                Type msgType = packet.GetType();
                if (msgType == typeof(UserListAckPacket))
                {
                    // 转换消息
                    UserListAckPacket usersMsg = (UserListAckPacket)packet;
                    // 更新用户列表
                    userList.Clear();
                    foreach (PeerEntity user in usersMsg.Users)
                    {
                        userList.Add(user);
                    }
                    bUserListComplete = true;
                }               
                else if (msgType == typeof(UserLoginAckPacket))
                {
                    ProcUserLogAckMsg(packet);
                }        
                else if (msgType == typeof(TextPacket))
                {
                    // 转换消息
                    TextPacket txtPacket = (TextPacket)packet;
                    printf("Receive a message: {0}", txtPacket.Message);
                    // 发送应答消息
                    P2PAckPacket ackMsg = new P2PAckPacket();
                    buffer = UtilityHelper.Serialize(ackMsg);
                    client.Send(buffer, buffer.Length, remotePoint);
                }
                else if (msgType == typeof(MassTextPacket))
                {
                    lock (this)
                    {
                        MassTextPacket fPacket = (MassTextPacket)packet;
                        if (packets.ContainsKey(fPacket.CLSD))
                            packets[fPacket.CLSD].Add(fPacket);
                        else
                            packets.Add(fPacket.CLSD, new List<MassTextPacket>() { fPacket });

                        printf("PacketID:{0}--SeqNo:{1}--progress:{2}%", fPacket.CLSD, fPacket.SeqID, (int)(System.Math.Round(packets[fPacket.CLSD].Count / (decimal)(fPacket as MassTextPacket).SeqCount, 2) * 100));
                        //组包
                        if ((fPacket as MassTextPacket).SeqCount == packets[fPacket.CLSD].Count)
                        {
                            List<MassTextPacket> temp = packets[fPacket.CLSD].OrderBy(c => c.SeqID).ToList();
                            List<byte> values = new List<byte>();
                            foreach (MassTextPacket mt in temp)
                            {
                                byte[] buf = Convert.FromBase64String(mt.Message);       
                                values.AddRange(buf);
                            }

                            MassTextPacket value = UtilityHelper.Deserialize(values.ToArray()) as MassTextPacket;

                            printf("Receive a message: {0}", value.Message);

                          
                            // 发送应答消息
                            P2PAckPacket ackMsg = new P2PAckPacket();
                            buffer = UtilityHelper.Serialize(ackMsg);
                            client.Send(buffer, buffer.Length, remotePoint);
                        }
                    }

                }
                else if (msgType == typeof(FileStreamPacket))
                {                
                    lock (this)                   
                    {
                        FileStreamPacket fPacket = (FileStreamPacket)packet;
                        if (packets.ContainsKey(fPacket.CLSD))
                            packets[fPacket.CLSD].Add(fPacket);
                        else
                            packets.Add(fPacket.CLSD, new List<MassTextPacket>() { fPacket });

                        printf("PacketID:{0}--SeqNo:{1}--progress:{2}%", fPacket.CLSD, fPacket.SeqID, (int)(System.Math.Round(packets[fPacket.CLSD].Count / (decimal)(fPacket as FileStreamPacket).SeqCount, 2) * 100));
                        //组包
                        if ((fPacket as FileStreamPacket).SeqCount == packets[fPacket.CLSD].Count)
                        {
                            List<MassTextPacket> temp = packets[fPacket.CLSD].OrderBy(c => c.SeqID).ToList();

                            System.IO.FileStream fs = new System.IO.FileStream((fPacket as FileStreamPacket).FileName + ".tmp", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
                            foreach (FileStreamPacket mt in temp)
                            {
                                byte[] buf = Convert.FromBase64String(mt.Message);
                                fs.Write(buf, 0, buf.Length);

                            }
                            fs.Flush();
                            fs.Close();
                           printf("Receive a file: {0}", (fPacket as FileStreamPacket).FileName);

                           //清除数据包
                           packets[fPacket.CLSD].Clear();

                           // 发送应答消息
                           P2PAckPacket ackMsg = new P2PAckPacket();
                           buffer = UtilityHelper.Serialize(ackMsg);
                           client.Send(buffer, buffer.Length, remotePoint);
                        }
                    }             
                  
                }
                else if (msgType == typeof(P2PAckPacket))
                {
                    this.ReceivedACK = true;
                }
                else if (msgType == typeof(P2PPurchHolePacket))
                {
                    ProcP2PPurchHoleMsg(packet, remotePoint);
                }
                else if (msgType == typeof(P2PPurchHoleAckPacket))
                {
                    PeerEntity touser = userList.SingleOrDefault(c => c.UserName == (packet as P2PPurchHoleAckPacket).ToUserName);
                    //更改本地的P2P连接时使用的IP地址
                    touser.P2PAddress = touser.RemoteEndPoint;
                }
                Thread.Sleep(100);
            }

        }

上一页  1 2 3 4 5 6  下一页

Tags:PP 网络通讯 程序

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接