Ejemplo n.º 1
0
 public void OnConnect(Peer peer)
 {
     Console.WriteLine("OnConnect - " + peer.EndPoint.ToString());
     lock (lock_)
     {
         subTask_.Add(peer.EndPoint.ToString(), Program.task.GetSubTask());
         i_.Add(peer.EndPoint.ToString(), 0);
         buff_.Add(peer.EndPoint.ToString(), new byte[1024 * 1024 * 3]);
         sbuff_.Add(peer.EndPoint.ToString(), new byte[1024 * 1024 * 4]);
         sbufflen_.Add(peer.EndPoint.ToString(), 0);
     }
     if (subTask_[peer.EndPoint.ToString()] != null)
     {
         FileBlockTask f = new FileBlockTask(subTask_[peer.EndPoint.ToString()]);
         Program.master.Send(peer, f.Encode());
     }
 }
Ejemplo n.º 2
0
 public void OnMessage(byte[] msg)
 {
     Array.Copy(msg, 0,
         sbuff_,
         sbufflen_, msg.Length);
     sbufflen_ += msg.Length;
     PacketTest t = new PacketTest(sbuff_, 0);
     if (t.PacketLength > sbufflen_)
     {
         // string s = "拆包"; ok
     }
     else if (t.PacketLength < sbufflen_)
     {
         string s = "粘包";
     }
     else if (t.PacketLength == sbufflen_)
     {
         switch (t.PacketId)
         {
             case 1:
                 StringPacket packet = new StringPacket();
                 packet.Decode(msg, 0, msg.Length);
                 Console.WriteLine("OnMessage - " + packet.data);
                 break;
             case 10004:
                 FileBlockTask task = new FileBlockTask();
                 task.Decode(msg, 0, msg.Length);
                 task_ = task.task_;
                 Console.WriteLine("OnMessage - FileBlockTask");
                 autoResetEvent_.Set();
                 new Thread(new ThreadStart(Download)).Start();
                 break;
             case 10001:
                 autoResetEvent_.Set();
                 break;
         }
         sbufflen_ = 0;
     }
 }
Ejemplo n.º 3
0
 public void OnMessage(Peer peer, byte[] msg)
 {
     Array.Copy(msg, 0,
         sbuff_[peer.EndPoint.ToString()],
         sbufflen_[peer.EndPoint.ToString()], msg.Length);
     sbufflen_[peer.EndPoint.ToString()] += msg.Length;
     PacketTest t = new PacketTest(sbuff_[peer.EndPoint.ToString()], 0);
     if (t.PacketLength > sbufflen_[peer.EndPoint.ToString()])
     {
         // string s = "拆包"; ok
     }
     else if (t.PacketLength < sbufflen_[peer.EndPoint.ToString()])
     {
         string s = "粘包";
     }
     else if (t.PacketLength == sbufflen_[peer.EndPoint.ToString()])
     {
         switch (t.PacketId)
         {
             case 1:
                 StringPacket packet = new StringPacket();
                 packet.Decode(sbuff_[peer.EndPoint.ToString()], 0, sbufflen_[peer.EndPoint.ToString()]);
                 Console.WriteLine("OnMessage - " + packet.data);
                 break;
             case 10001:
                 Console.WriteLine("OnMessage - FileBlockBeginPacket");
                 FileBlockBeginPacket begin = new FileBlockBeginPacket();
                 Program.master.Send(peer, begin.Encode());
                 break;
             case 10002:
                 FileBlockDataPacket d = new FileBlockDataPacket();
                 d.Decode(sbuff_[peer.EndPoint.ToString()], 0, sbufflen_[peer.EndPoint.ToString()]);
                 Array.Copy(d.data3k, 0, buff_[peer.EndPoint.ToString()], i_[peer.EndPoint.ToString()] * 1024 * 3, d.data3k.Length);
                 i_[peer.EndPoint.ToString()]++;
                 FileBlockBeginPacket begin2 = new FileBlockBeginPacket();
                 Program.master.Send(peer, begin2.Encode());
                 break;
             case 10003:
                 i_[peer.EndPoint.ToString()] = 0;
                 Console.WriteLine("OnMessage - FileBlockEndPacket");
                 subTask_[peer.EndPoint.ToString()].data = buff_[peer.EndPoint.ToString()];
                 var len = subTask_[peer.EndPoint.ToString()].to - subTask_[peer.EndPoint.ToString()].from + 1;
                 if (len != Program.task.BlockSize)
                 {
                     subTask_[peer.EndPoint.ToString()].data = new byte[len];
                     Array.Copy(buff_[peer.EndPoint.ToString()], 0, subTask_[peer.EndPoint.ToString()].data, 0, len);
                 }
                 if (!Program.task.UpdateTaskState(subTask_[peer.EndPoint.ToString()], 2))
                 {
                     subTask_[peer.EndPoint.ToString()] = Program.task.GetSubTask();
                     if (subTask_[peer.EndPoint.ToString()] != null)
                     {
                         FileBlockTask f = new FileBlockTask(subTask_[peer.EndPoint.ToString()]);
                         Program.master.Send(peer, f.Encode());
                     }
                 }
                 break;
         }
         sbufflen_[peer.EndPoint.ToString()] = 0;
     }
 }
Ejemplo n.º 4
0
        private void OnFileBlockEndPacket(Peer peer, byte[] data)
        {
            subTask_[peer.EndPoint.ToString()].data = msgbuff[peer.EndPoint.ToString()].RawData;

            var len = subTask_[peer.EndPoint.ToString()].to - subTask_[peer.EndPoint.ToString()].from + 1;
            if (len != CurrentTask.BlockSize)
            {
                subTask_[peer.EndPoint.ToString()].data = new byte[len];
                Array.Copy(msgbuff[peer.EndPoint.ToString()].RawData, 0, subTask_[peer.EndPoint.ToString()].data, 0, len);
            }

            SetPeerInfoStatus(peer, PeerStatus.Free);
            AddProgress();

            var taskid = subTask_[peer.EndPoint.ToString()].Id;
            Action act = () =>
            {
                this.Blocks[taskid].Status = Controls.Block.BlockStatus.Complete;
            };
            GlobalMessageCenter.Instance.Send("MainWindow.UIThreadInvoke", act);

            if (!CurrentTask.UpdateTaskState(subTask_[peer.EndPoint.ToString()], 2))
            {
                subTask_[peer.EndPoint.ToString()] = CurrentTask.GetSubTask();
                if (subTask_[peer.EndPoint.ToString()] != null)
                {
                    FileBlockTask f = new FileBlockTask(subTask_[peer.EndPoint.ToString()]);
                    master.Send(peer, f.Encode());
                }
            }
        }