private void OnFileBlockDataPacket(Peer peer, byte[] data)
        {
            FileBlockDataPacket d = new FileBlockDataPacket();
            d.Decode(sbuff_[peer.EndPoint.ToString()].RawData, 0, sbuff_[peer.EndPoint.ToString()].Length);

            msgbuff[peer.EndPoint.ToString()].Append(d.data3k, d.len);

            FileBlockBeginPacket begin2 = new FileBlockBeginPacket();
            master.Send(peer, begin2.Encode());
        }
示例#2
0
文件: Program.cs 项目: xxy1991/cozy
        void Download()
        {
            if (task_ != null)
            {
                Console.WriteLine("Download begin task id = " + task_.Id.ToString());
                byte[] data = HttpDownloadRange.Download(task_.RemotePath, task_.from, task_.to);
                Console.WriteLine("Download end");

                Console.WriteLine("trans begin");
                FileBlockBeginPacket begin = new FileBlockBeginPacket();
                autoResetEvent_.WaitOne();
                Program.slave.Send(begin.Encode());
                byte[] b3k = new byte[1024 * 3];
                int c = (data.Length + 3071) / 3072;
                for (int i = 0; i < c - 1; ++i)
                {
                    Array.Copy(data, 1024 * 3 * i, b3k, 0, 1024 * 3);
                    FileBlockDataPacket d = new FileBlockDataPacket(b3k);
                    autoResetEvent_.WaitOne();
                    Program.slave.Send(d.Encode());
                }
                int lastBlockSize = data.Length % 3072;
                if (lastBlockSize == 0)
                    lastBlockSize = 3072;
                {
                    byte[] bxk = new byte[lastBlockSize];
                    Array.Copy(data, 1024 * 3 * (c - 1), bxk, 0, lastBlockSize);
                    FileBlockDataPacket d = new FileBlockDataPacket(bxk);
                    autoResetEvent_.WaitOne();
                    Program.slave.Send(d.Encode());
                }
                FileBlockEndPacket end = new FileBlockEndPacket();
                autoResetEvent_.WaitOne();
                Program.slave.Send(end.Encode());
                Console.WriteLine("trans end");
            }
        }
示例#3
0
文件: Program.cs 项目: liguifa/cozy
        static void Main(string[] args)
        {
            SlavePeer slave = new SlavePeer();
            slave.Start(IPAddress.Any, 48361, new SlavePeerListener());

            MasterPeer master = new MasterPeer();
            master.Start(IPAddress.Any, 48360, new MasterPeerListener());
            var peer = new Peer() { EndPoint = new IPEndPoint(IPAddress.Parse("10.20.208.27"), 48361) };
            master.Connect(peer);

            Console.ReadKey();
            StringPacket sp = new StringPacket("hehe");
            master.Send(peer, sp.Encode());

            Console.ReadKey();
            // 传输3M的数据
            FileBlockBeginPacket begin = new FileBlockBeginPacket();
            master.Send(peer, begin.Encode());
            byte[] b3k = new byte[1024 * 3];
            for (int i = 0; i < 1024; ++i)
            {
                FileBlockDataPacket d = new FileBlockDataPacket(b3k);
                master.Send(peer, d.Encode());
            }
            FileBlockEndPacket end = new FileBlockEndPacket();
            master.Send(peer, end.Encode());

            Console.ReadKey();
        }
示例#4
0
        void Download()
        {
            if (task_ != null)
            {
                LogManager.Instalce.DownloadTaskBeginLog(task_.RemotePath, task_.from, task_.to);
                byte[] data = HttpDownloadRange.Download(task_.RemotePath, task_.from, task_.to);
                LogManager.Instalce.DownloadTaskEndLog(task_.RemotePath, task_.from, task_.to);

                LogManager.Instalce.TransferBegin(task_.RemotePath, task_.from, task_.to);
                FileBlockBeginPacket begin = new FileBlockBeginPacket();
                autoResetEvent_.WaitOne();
                Peer.Send(begin.Encode());
                byte[] b3k = new byte[1024 * 3];
                int c = (data.Length + 3071) / 3072;
                for (int i = 0; i < c - 1; ++i)
                {
                    Array.Copy(data, 1024 * 3 * i, b3k, 0, 1024 * 3);
                    FileBlockDataPacket d = new FileBlockDataPacket(b3k);
                    autoResetEvent_.WaitOne();
                    Peer.Send(d.Encode());
                }
                int lastBlockSize = data.Length % 3072;
                if (lastBlockSize == 0)
                    lastBlockSize = 3072;
                {
                    byte[] bxk = new byte[lastBlockSize];
                    Array.Copy(data, 1024 * 3 * (c - 1), bxk, 0, lastBlockSize);
                    FileBlockDataPacket d = new FileBlockDataPacket(bxk);
                    autoResetEvent_.WaitOne();
                    Peer.Send(d.Encode());
                }
                FileBlockEndPacket end = new FileBlockEndPacket();
                autoResetEvent_.WaitOne();
                Peer.Send(end.Encode());
                LogManager.Instalce.TransferEnd(task_.RemotePath, task_.from, task_.to);
            }
        }
示例#5
0
文件: Program.cs 项目: xxy1991/cozy
 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;
     }
 }