示例#1
0
文件: Program.cs 项目: xxy1991/cozy
 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;
     }
 }
 private void OnMessageMessage(object arg1, object arg2)
 {
     var peer = arg1 as Peer;
     var data = arg2 as byte[];
     if (peer != null && data != null)
     {
         sbuff_[peer.EndPoint.ToString()].Append(data, data.Length);
     }
     PacketTest t = new PacketTest(sbuff_[peer.EndPoint.ToString()].RawData, 0);
     if (t.PacketLength > sbuff_[peer.EndPoint.ToString()].Length)
     {
         //拆包
     }
     else if (t.PacketLength < sbuff_[peer.EndPoint.ToString()].Length)
     {
         //粘包
     }
     else if (t.PacketLength == sbuff_[peer.EndPoint.ToString()].Length)
     {
         //完整包
         MessageDispatch(peer, data, t.PacketId);
     }
 }
示例#3
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;
     }
 }