Esempio n. 1
0
 public bool Connect(Peer peer)
 {
     var connector = new MasterConnector();
     connectorList_.Add(peer.EndPoint.ToString(), connector);
     connector.Connect(peer, this);
     return true;
 }
Esempio n. 2
0
        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();
        }
        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());
                }
            }
        }
Esempio n. 4
0
 public bool Connect(Peer peer, IMasterConnectorListener listener)
 {
     peer_ = peer;
     listener_ = listener;
     socket_ = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     socket_.BeginConnect(peer.EndPoint.Address, peer.EndPoint.Port, ConnectCallback, socket_);
     return true;
 }
Esempio n. 5
0
 public bool AddPeer(Peer peer, bool force = false)
 {
     if (force || !peers_.ContainsKey(peer.EndPoint.ToString()))
     {
         peers_.Add(peer.EndPoint.ToString(), peer);
         return true;
     }
     return false;
 }
Esempio n. 6
0
 internal void SetPeerInfoStatus(Peer peer, PeerStatus status)
 {
     var addr = peer.EndPoint.Address.ToString();
     var port = peer.EndPoint.Port;
     var info = PeerInfoList.Where(x => x.Address == addr && x.Port == port).First();
     if (info != null)
     {
         info.Status = status;
     }
 }
Esempio n. 7
0
 public bool Send(Peer peer, byte[] msg)
 {
     MasterConnector connector;
     if (connectorList_.TryGetValue(peer.EndPoint.ToString(), out connector))
     {
         connector.Send(msg);
         return true;
     }
     return false;
 }
Esempio n. 8
0
 public bool DisConnect(Peer peer)
 {
     MasterConnector connector;
     if (connectorList_.TryRemove(peer.EndPoint.ToString(), out connector))
     {
         connector.DisConnect();
         return true;
     }
     return false;
 }
        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());
        }
Esempio n. 10
0
 public bool Connect(Peer peer)
 {
     var connector = new MasterConnector();
     if(connector.Connect(peer, this))
     {
         if(connectorList_.TryAdd(peer.EndPoint.ToString(), connector))
         {
             return true;
         }
     }
     return false;
 }
Esempio n. 11
0
 public bool Connect(Peer peer, IMasterConnectorListener listener)
 {
     peer_ = peer;
     listener_ = listener;
     socket_ = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     socket_.SendTimeout = 1000 * 10;
     socket_.ReceiveTimeout = 1000 * 10;
     socket_.SendBufferSize = 1024 * 4;
     socket_.ReceiveBufferSize = 1024 * 4;
     socket_.BeginConnect(peer.EndPoint.Address, peer.EndPoint.Port, ConnectCallback, socket_);
     return true;
 }
Esempio n. 12
0
        private void OnFileBlockBeginPacket(Peer peer, byte[] data)
        {
            FileBlockBeginPacket begin = new FileBlockBeginPacket();
            master.Send(peer, begin.Encode());

            var taskid = subTask_[peer.EndPoint.ToString()].Id;
            Action act = () =>
            {
                this.Blocks[taskid].Status = Controls.Block.BlockStatus.Downloading;
            };
            GlobalMessageCenter.Instance.Send("MainWindow.UIThreadInvoke", act);
        }
Esempio n. 13
0
 static void Main(string[] args)
 {
     var r = Console.ReadLine();
     if (r == "master")
     {
         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();
         master.Send(peer, "kingwl");
         Console.ReadKey();
         master.Stop();
     }
     else if (r == "master2")
     {
         MasterPeer master = new MasterPeer();
         master.Start(IPAddress.Any, 48360, new MasterPeerListener());
         var peer1 = new Peer() { EndPoint = new IPEndPoint(IPAddress.Parse("10.20.208.27"), 48361) };
         var peer2 = new Peer() { EndPoint = new IPEndPoint(IPAddress.Parse("10.20.208.27"), 48362) };
         var peer3 = new Peer() { EndPoint = new IPEndPoint(IPAddress.Parse("10.20.208.27"), 48363) };
         master.Connect(peer1);
         master.Connect(peer2);
         master.Connect(peer3);
         Console.ReadKey();
         master.Send(peer1, "kingwl");
         master.Send(peer2, "kingwlkingwl");
         master.Send(peer3, "kingwlkingwlkingwl");
         Console.ReadKey();
         master.Stop();
     }
     else if (r == "slave")
     {
         SlavePeer slave = new SlavePeer();
         slave.Start(IPAddress.Any, 48361, new SlavePeerListener());
         Console.ReadKey();
         slave.Send("hehe");
         Console.ReadKey();
         slave.Stop();
     }
     else if (r == "slave2")
     {
         SlavePeer slave = new SlavePeer();
         slave.Start(IPAddress.Any, 48362, new SlavePeerListener());
         SlavePeer slave2 = new SlavePeer();
         slave2.Start(IPAddress.Any, 48363, new SlavePeerListener());
         Console.ReadKey();
         slave.Stop();
         slave2.Stop();
     }
 }
Esempio n. 14
0
 Peer currentSelect()
 {
     var ss = listView1.SelectedItems;
     if (ss.Count > 0)
     {
         var s = ss[0];
         var ts = s.Text.Split(':');
         if (ts.Count() == 2)
         {
             var peer = new Peer() { EndPoint = new IPEndPoint(IPAddress.Parse(ts[0]), int.Parse(ts[1])) };
             return peer;
         }
     }
     return null;
 }
Esempio n. 15
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());
     }
 }
Esempio n. 16
0
 public void MessageDispatch(Peer peer, byte[] data, int id)
 {
     switch (id)
     {
         case 1:
             OnStringPacket(peer, data);
             break;
         case 10001:
             OnFileBlockBeginPacket(peer, data);
             break;
         case 10002:
             OnFileBlockDataPacket(peer, data);
             break;
         case 10003:
             OnFileBlockEndPacket(peer, data);
             break;
     }
     sbuff_[peer.EndPoint.ToString()].Clear();
 }
Esempio n. 17
0
 public void OnMessage(Peer peer, byte[] msg)
 {
     listener_?.OnMessage(peer, msg);
 }
Esempio n. 18
0
 public void OnMessage(Peer peer, byte[] msg)
 {
     Console.WriteLine("OnMessage - " + peer.EndPoint.ToString() + " - " + Encoding.ASCII.GetString(msg, 0, msg.Length));
 }
Esempio n. 19
0
 public void OnDisConnect(Peer peer)
 {
     GlobalMessageCenter.Instance.Send("PeerListener.Disconnect", peer);
 }
Esempio n. 20
0
 public void OnMessage(Peer peer, byte[] msg)
 {
     GlobalMessageCenter.Instance.Send("PeerListener.Message", peer, msg);
 }
Esempio n. 21
0
 public void OnDisConnect(Peer peer)
 {
     Console.WriteLine("OnDisConnect - " + peer.EndPoint.ToString());
 }
Esempio n. 22
0
 public bool AddPeer(Peer peer)
 {
     return peerList_.AddPeer(peer);
 }
Esempio n. 23
0
 public bool DelPeer(Peer peer)
 {
     return peers_.Remove(peer.EndPoint.ToString());
 }
Esempio n. 24
0
 public bool DelPeer(Peer peer)
 {
     return peerList_.DelPeer(peer);
 }
Esempio n. 25
0
 public void OnDisConnect(Peer peer)
 {
     listBox1.Items.Add("OnDisConnect - " + peer.EndPoint.ToString());
     listBox1.SelectedIndex = listBox1.Items.Count - 1;
 }
Esempio n. 26
0
 public void OnMessage(Peer peer, byte[] msg)
 {
     listBox1.Items.Add("OnMessage - " + peer.EndPoint.ToString());
     listBox1.Items.Add(msg);
     listBox1.SelectedIndex = listBox1.Items.Count - 1;
 }
Esempio n. 27
0
 public void OnDisConnect(Peer peer)
 {
     listener_?.OnDisConnect(peer);
 }
Esempio n. 28
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;
     }
 }
Esempio n. 29
0
 private void OnStringPacket(Peer peer, byte[] data)
 {
     StringPacket packet = new StringPacket();
     packet.Decode(sbuff_[peer.EndPoint.ToString()].RawData, 0, sbuff_[peer.EndPoint.ToString()].Length);
 }
Esempio n. 30
0
        static void Main(string[] args)
        {
            task = new DownloadTask();
            task.RemotePath = @"http://speed.myzone.cn/pc_elive_1.1.rar"; //60M
            //task.RemotePath = @"http://cd002.www.duba.net/duba/install/2011/ever/duba160406_100_50.exe"; //17M
            task.LocalPath = @"d:\hehe.rar";
            task.BlockSize = 1024 * 1024 * 3;
            if (task.InitTask())
            {
                Console.WriteLine("begin download");
            }

            Console.ReadKey();

            master = new MasterPeer();
            master.Start(IPAddress.Any, 48360, new MasterPeerListener());
            List<KeyValuePair<string, int>> peerList = new List<KeyValuePair<string, int>>()
            {
                new KeyValuePair<string, int>("127.0.0.1", 48360),
                //new KeyValuePair<string, int>("10.20.208.27", 48235),
                //new KeyValuePair<string, int>("10.20.208.27", 48362),
                //new KeyValuePair<string, int>("10.20.221.119", 48360),
                //new KeyValuePair<string, int>("10.20.208.30", 48360), //wyf
                //new KeyValuePair<string, int>("10.20.208.38", 48390), //hym
                //new KeyValuePair<string, int>("10.20.208.55", 48390), //hsj
            };
            foreach (var i in peerList)
            {
                var peer = new Peer() { EndPoint = new IPEndPoint(IPAddress.Parse(i.Key), i.Value) };
                master.Connect(peer);
            }
            Console.ReadKey();
        }