Example #1
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;
     }
 }
Example #2
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;
     }
 }
 private void OnStringPacket(Peer peer, byte[] data)
 {
     StringPacket packet = new StringPacket();
     packet.Decode(sbuff_[peer.EndPoint.ToString()].RawData, 0, sbuff_[peer.EndPoint.ToString()].Length);
 }