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); }
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; }
public void SendP2PData(byte[] data, byte[] hash, int index, TcpClient client) { DataMessage p2PData = new DataMessage(data, hash, index); send(p2PData, client); }