コード例 #1
0
ファイル: Message.cs プロジェクト: ayanamists/P2PDownload
 public static List<P2PMessage> GenFromMS(MemoryStream ms)
 {
     var now_pos = 0;
     List<P2PMessage> ret = new List<P2PMessage>();
     var max_pos = ms.Position;
     ms.Position = 0;
     while (now_pos < max_pos) {
         var buffer_head = new byte[8];
         ms.Read(buffer_head, 0, 8);
         var length = BitConverter.ToInt32(buffer_head, 4);
         var type_value = BitConverter.ToInt32(buffer_head);
         var buffer_tail = new byte[length];
         ms.Read(buffer_tail, 0, length);
         var now_item = new P2PMessage();
         switch (type_value)
         {
             case 0:
                 now_item = new GetDataMessage(buffer_tail);
                 break;
             case 1:
                 now_item = new GetFileInfoMessage(buffer_tail);
                 break;
             case 2:
                 now_item = new GetBlockInfoMessage(buffer_tail);
                 break;
             case 3:
                 now_item =  new DataMessage(buffer_tail);
                 break;
             case 4:
                 now_item = new ErrorMessage(buffer_tail);
                 break;
             case 5:
                 now_item =  new FileInfoMessage(buffer_tail);
                 break;
             case 6:
                 now_item = new BlockInfoMessage(buffer_tail);
                 break;
             case 7:
                 now_item = new OKMessage(buffer_tail);
                 break;
             case 8:
                 now_item = new CloseMessage();
                 break;
             case 9:
                 now_item = new AliveMessage();
                 break;
             default:
                 throw new System.Exception("not allow");
         }
         now_item.type = (P2PMessageType)type_value;
         ret.Add(now_item);
         now_pos += 8 + length;
     }
     Debug.Assert(now_pos == max_pos);
     return ret;
 }
コード例 #2
0
ファイル: Message.cs プロジェクト: ayanamists/P2PDownload
        public static void Test(string[] args)
        {
            Int32[] temp = { 1, 2, 3, 4};
            var hash = SHA256.Create().ComputeHash(new byte[1]);
            GetDataMessage getData = new GetDataMessage(hash, temp);
            serializeAndWrite(getData);
            TestParse(getData);

            GetFileInfoMessage getFileInfo = new GetFileInfoMessage(hash);
            serializeAndWrite(getFileInfo);
            TestParse(getFileInfo);

            GetBlockInfoMessage getBlockInfo = new GetBlockInfoMessage(hash, 10);
            serializeAndWrite(getBlockInfo);
            TestParse(getBlockInfo);

            var block = new byte[4096];
            block[1] = 0; block[2] = 2;
            DataMessage data = new DataMessage(block, hash, 10);
            serializeAndWrite(data);
            TestParse(data);

            var error = new ErrorMessage(hash, 10);
            serializeAndWrite(error);
            TestParse(error);

            IPAddress a = IPAddress.Parse("192.168.1.190");
            int[] array = { 1, 3, 4 };
            FileLocateInfo fileLocateInfo = new FileLocateInfo(a, hash, array);
            FileLocateInfo[] fileLocateInfos = new FileLocateInfo[1];
            fileLocateInfos[0] = fileLocateInfo;
            var fileInfo = new FileInfoMessage(fileLocateInfos);
            serializeAndWrite(fileInfo);
            TestParse(fileInfo);

            BlockInfoMessage blockInfo = new BlockInfoMessage(hash, 10);
            serializeAndWrite(blockInfo);
            TestParse(blockInfo);

            OKMessage oK = new OKMessage(hash, 10);
            serializeAndWrite(oK);
            TestParse(oK);
        }
コード例 #3
0
ファイル: Message.cs プロジェクト: ayanamists/P2PDownload
 public static P2PMessage GenFromSocket(System.Net.Sockets.NetworkStream stream, int type_value, int length)
 {
     P2PMessage ret;
     var buffer = new byte[length];
     stream.Read(buffer, 0, length);
     switch (type_value)
     {
         case 0:
             ret = new GetDataMessage(buffer);
             break;
         case 1:
             ret = new GetFileInfoMessage(buffer);
             break;
         case 2:
             ret = new GetBlockInfoMessage(buffer);
             break;
         case 3:
             ret = new DataMessage(buffer);
             break;
         case 4:
             ret = new ErrorMessage(buffer);
             break;
         case 5:
             ret = new FileInfoMessage(buffer);
             break;
         case 6:
             ret = new BlockInfoMessage(buffer);
             break;
         case 7:
             ret = new OKMessage(buffer);
             break;
         case 8:
             ret = new CloseMessage();
             break;
         default:
             throw new System.Exception("not allow");
     }
     return ret;
 }
コード例 #4
0
        public void SendOkMessage(byte[] hash, int index)
        {
            var Ok = new OKMessage(hash, index);

            send(Ok);
        }