public string Upload_File(List <string> file_name) { try { // file_list part // need to add // Packet_type = 0x20 byte Packet_type = 0x20; byte Device_id = 0x02; byte[] file_data; // 0xffff - 300 - 2 int MessageBody_Length_Max = 65233; for (int i = 0; i < file_name.Count; i++) { //read file //for test FileStream file = new FileStream(PATH + file_name[i], FileMode.Open); file.Seek(0, SeekOrigin.Begin); long file_length = file.Length; file_data = null; // MessageBody Length int MessageBody_Length = 0; if (file_length <= MessageBody_Length_Max) { MessageBody_Length = (int)file_length; // 8 + 2 + 1 + 1 + 300 + len(MessageBody) + 16 +2 byte[] MessageBodyByte = new byte[MessageBody_Length + 30 + 300]; // Packet Initiator[ 8 bytes] 0x11 0xff 0x6c 0x6f 0x6e 0x64 0x6f 0x6e byte[] Initiator = { 0x11, 0xff, 0x6c, 0x6f, 0x6e, 0x64, 0x6f, 0x6e }; Buffer.BlockCopy(Initiator, 0, MessageBodyByte, 0, Initiator.Length); // Packet Content Length [ 2 bytes ] // Context_Length = 2 + 300 <file_index_json> + len(MessageBody) int Context_Length = (2 + 300 + MessageBody_Length); byte[] length = { (byte)(Context_Length >> 8), (byte)(Context_Length & 0xff) }; Buffer.BlockCopy(length, 0, MessageBodyByte, 8, length.Length); // Packet Type [ 1 byte ] MessageBodyByte[10] = Packet_type; // Packet ID [ 1 byte ] MessageBodyByte[11] = Device_id; byte[] file_json = new byte[300]; file_index_json f_js = new file_index_json() { Name = file_name[i], Num = 0x1, Index = 0 }; string f_js_str = JsonConvert.SerializeObject(f_js); byte[] f_js_bytes = System.Text.Encoding.Default.GetBytes(f_js_str); Buffer.BlockCopy(f_js_bytes, 0, file_json, 0, f_js_str.Length); /** for 1.txt file (cmd = 1) * demo * { * "Name": "1.txt", * "Num": 2, * "Index": 0 * } * { * "Name": "1.txt", * "Num": 2, * "Index": 1 * } * */ //copy json data Buffer.BlockCopy(file_json, 0, MessageBodyByte, 12, file_json.Length); //read file data file_data = new byte[MessageBody_Length]; file.Read(file_data, 0, (int)MessageBody_Length); //copy file data from index = 312 Buffer.BlockCopy(file_data, 0, MessageBodyByte, 12 + 300, MessageBody_Length); // Check_sum byte[] md5 = GetCheck_sum(Packet_type, Device_id, file_data, true); Buffer.BlockCopy(md5, 0, MessageBodyByte, 12 + 300 + MessageBody_Length, md5.Length); // end with 0xff 0xee MessageBodyByte[MessageBody_Length + 28 + 300] = 0xff; MessageBodyByte[MessageBody_Length + 29 + 300] = 0xee; SendMessage(MessageBodyByte); byte[] res_flag = ReceiveMessage(); // result == "ok" if (res_flag[0] == 0x6f && res_flag[1] == 0x6b) { file.Close(); continue; } else { file.Close(); return("failed upload" + file_name[i]); } } else { // Rounds double Packet_num = Math.Ceiling((double)file_length / (double)MessageBody_Length_Max); // Json - Packet Num byte Packet_Num = (byte)Packet_num; // Json - Packet Index byte index = 0; file_data = null; byte[] MessageBodyByte; while (Packet_num > 0) { if (Packet_num != 1) { MessageBody_Length = MessageBody_Length_Max; } else { MessageBody_Length = (int)(file_length - MessageBody_Length_Max * index); } MessageBodyByte = new byte[MessageBody_Length + 30 + 300]; // Packet Initiator[ 8 bytes] 0x11 0xff 0x6c 0x6f 0x6e 0x64 0x6f 0x6e byte[] Initiator = { 0x11, 0xff, 0x6c, 0x6f, 0x6e, 0x64, 0x6f, 0x6e }; Buffer.BlockCopy(Initiator, 0, MessageBodyByte, 0, Initiator.Length); // Packet Content Length [ 2 bytes ] // Context_Length = 2 + 300 <file_index_json> + len(MessageBody) int Context_Length = (2 + 300 + MessageBody_Length); byte[] length = { (byte)(Context_Length >> 8), (byte)(Context_Length & 0xff) }; Buffer.BlockCopy(length, 0, MessageBodyByte, 8, length.Length); // Packet Type [ 1 byte ] MessageBodyByte[10] = Packet_type; // Packet ID [ 1 byte ] MessageBodyByte[11] = Device_id; byte[] file_json = new byte[300]; file_index_json f_js = new file_index_json() { Name = file_name[i], Num = Packet_Num, Index = index }; string f_js_str = JsonConvert.SerializeObject(f_js); byte[] f_js_bytes = System.Text.Encoding.Default.GetBytes(f_js_str); Buffer.BlockCopy(f_js_bytes, 0, file_json, 0, f_js_str.Length); //copy json data Buffer.BlockCopy(file_json, 0, MessageBodyByte, 12, file_json.Length); //read file file_data = new byte[MessageBody_Length]; //it will continue reading from last position file.Read(file_data, 0, MessageBody_Length); //copy file data Buffer.BlockCopy(file_data, 0, MessageBodyByte, 12 + 300, MessageBody_Length); // Check_sum /* * ** FILE data */ byte[] md5 = GetCheck_sum(Packet_type, Device_id, file_data, true); Buffer.BlockCopy(md5, 0, MessageBodyByte, 12 + 300 + MessageBody_Length, md5.Length); // end with 0xff 0xee MessageBodyByte[MessageBody_Length + 28 + 300] = 0xff; MessageBodyByte[MessageBody_Length + 29 + 300] = 0xee; Packet_num--; index++; SendMessage(MessageBodyByte); } file.Close(); byte[] res_flag = ReceiveMessage(); if (res_flag[0] == 0x6f && res_flag[1] == 0x6b) { continue; } else { return("failed upload" + file_name[i]); } } } // socketClient.Close(); return("success upload"); } catch (Exception ex) { listMessage.Add(ex.ToString()); throw new Exception(ex.Message); } }
public void Upload_File_differ(byte[] file_id, string file_name) { SendMessage(BuildDataPackage_For_Pull(file_id, 0x22, Device_id)); byte[] result = ReceiveMessage(); byte[] hash_json = new byte[300]; Buffer.BlockCopy(result, 0, hash_json, 0, hash_json.Length); int index = hash_json.ToList().IndexOf(0); byte[] hash_json_new = new byte[index]; Buffer.BlockCopy(hash_json, 0, hash_json_new, 0, hash_json_new.Length); string str = System.Text.Encoding.Default.GetString(hash_json_new); file_index_json File_index_json = JsonConvert.DeserializeObject <file_index_json>(str); string Path = PATH + File_index_json.Name; int hash_list_num = (result.Length - 300) / 16; List <string> md5_list = new List <string>(); // bool flag = true; while (flag) { index = 0; if (hash_list_num != 0) { byte[] md5 = new byte[16]; Buffer.BlockCopy(result, 300 + index * 16, md5, 0, md5.Length); md5_list.Add(BitConverter.ToString(md5, 0).Replace("-", string.Empty).ToLower()); hash_list_num--; } else { flag = false; } } differ_info_json_list diff_json = new differ_info_json_list(); byte[] filedata = null; lib.Search_block_index(Path, md5_list, out diff_json, out filedata, file_name); byte[] Diff_json = new byte[1000]; int filedata_lenth = filedata.Length; double num = Math.Ceiling((double)filedata_lenth / (double)(0xffff - 2 - 1000)); if (filedata_lenth > (0xffff - 2 - 1000)) { int idx = 0; while (true) { if (filedata_lenth > 0) { diff_json.Idx = idx; diff_json.Num = (int)num; byte[] Diff_json_new = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(diff_json)); Buffer.BlockCopy(Diff_json_new, 0, Diff_json, 0, Diff_json_new.Length); byte[] Msg = new byte[1000 + filedata_lenth]; Buffer.BlockCopy(Diff_json, 0, Msg, 0, 1000); Buffer.BlockCopy(filedata, (0xffff - 2 - 1000) * idx, Msg, 0, filedata_lenth); SendMessage(BuildDataPackage_For_Pull(Msg, 0x22, Device_id)); } else { break; } filedata_lenth -= (0xffff - 1000 - 2); idx++; } } else { byte[] Diff_json_new = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(diff_json)); Buffer.BlockCopy(Diff_json_new, 0, Diff_json, 0, Diff_json_new.Length); byte[] Msg = new byte[1000 + filedata.Length]; Buffer.BlockCopy(Diff_json, 0, Msg, 0, 1000); Buffer.BlockCopy(filedata, 0, Msg, 1000, filedata.Length); SendMessage(BuildDataPackage_For_Pull(Msg, 0x22, Device_id)); } }
public string ReceiveMessage_For_download() { // timer System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // start byte[] recvBytes; byte Packet_Num; string File_Name; // 8 + 2 int Tcp_header_length = 10; int Tcp_body_length = 0; byte[] Tcp_header = new byte[Tcp_header_length]; socketClient.Receive(Tcp_header, 0, Tcp_header_length, 0); Tcp_body_length = ((int)Tcp_header[8] << 8) + ((int)Tcp_header[9]); // Context Length byte[] Packet_type = new byte[1]; int File_data_Length; byte[] file_json = new byte[300]; // 65234 File_data_Length = Tcp_body_length - 300 - 1; recvBytes = new byte[File_data_Length]; socketClient.Receive(Packet_type, 0, 1, 0); socketClient.Receive(file_json, 0, 300, 0); // get Packet Num int End_file_json_flag = Array.IndexOf(file_json, (byte)0x00); byte[] File_json = new byte[End_file_json_flag]; Buffer.BlockCopy(file_json, 0, File_json, 0, End_file_json_flag); file_index_json F_json = JsonConvert.DeserializeObject <file_index_json>(System.Text.Encoding.Default.GetString(File_json)); // return File_Name + Packet_Num File_Name = F_json.Name; Packet_Num = F_json.Num; //create file or find file string filePath = PATH + File_Name; FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); // receive file data int index = 0; // set Buffer size = 1024 while (true) { if (File_data_Length > Buffer_Length_Max) { socketClient.Receive(recvBytes, index * Buffer_Length_Max, Buffer_Length_Max, 0); File_data_Length -= Buffer_Length_Max; } else { socketClient.Receive(recvBytes, index * Buffer_Length_Max, File_data_Length, 0); break; } index++; // wair for 1ms // .Net have the speed limit // control received speed in order not to loss packet System.Threading.Thread.Sleep(1); } byte[] md5 = new byte[16]; socketClient.Receive(md5, 0, 16, 0); byte[] End_flag = new byte[2]; socketClient.Receive(End_flag, 0, 2, 0); // check End_flag if (End_flag[0] == 0xff && End_flag[1] == 0xee) { // socketClient.Close(); fs.Write(recvBytes, 0, recvBytes.Length); fs.Position = fs.Length; if (Packet_Num == 1) { stopwatch.Stop(); // stop watch TimeSpan timespan = stopwatch.Elapsed; // Get the elapsed time as a TimeSpan value. // double hours = timespan.TotalHours; // hours // double minutes = timespan.TotalMinutes; // Minutes // double seconds = timespan.TotalSeconds; // Seconds double milliseconds = timespan.TotalMilliseconds; // Milliseconds fs.Flush(); double fs_size = fs.Length; double speed = fs_size / milliseconds * 1000 / 1024; // kb/s fs.Close(); return(File_Name + ": download successfully"); } else { while (Packet_Num > 1) { byte[] file_data = UnpackData(ReceiveMessage()); fs.Write(file_data, 0, file_data.Length); fs.Position = fs.Length; Packet_Num--; } stopwatch.Stop(); // stop watch TimeSpan timespan = stopwatch.Elapsed; // Get the elapsed time as a TimeSpan value. // double hours = timespan.TotalHours; // hours // double minutes = timespan.TotalMinutes; // Minutes // double seconds = timespan.TotalSeconds; // Seconds double milliseconds = timespan.TotalMilliseconds; // Milliseconds fs.Flush(); double fs_size = fs.Length; double speed = fs_size / milliseconds * 1000 / 1024; // kb/s fs.Close(); return(File_Name + ": download successfully"); } } else { stopwatch.Stop(); // stop watch // socketClient.Close(); return(null); } //listMessage.Add(ex.ToString()); //throw new Exception(ex.Message); }
public double Upload_File(string file_name, int upload_type) { try { // upload the whole file -> 20 // differ upload -> 22 byte Packet_type; if (upload_type == 0) { Packet_type = 0x20; } else { Packet_type = 0x22; } byte Device_id = 0x02; byte[] file_data; // 0xffff - 300 - 2 int MessageBody_Length_Max = 0xffff - 300 - 2; // read file // for test FileStream file = new FileStream(PATH + file_name, FileMode.Open); file.Seek(0, SeekOrigin.Begin); long file_length = file.Length; file_data = null; // MessageBody Length int MessageBody_Length = 0; if (file_length <= MessageBody_Length_Max) { MessageBody_Length = (int)file_length; // 8 + 2 + 1 + 1 + 300 + len(MessageBody) + 16 +2 byte[] MessageBodyByte = new byte[MessageBody_Length + 30 + 300]; // Packet Initiator[ 8 bytes] 0x11 0xff 0x6c 0x6f 0x6e 0x64 0x6f 0x6e byte[] Initiator = { 0x11, 0xff, 0x6c, 0x6f, 0x6e, 0x64, 0x6f, 0x6e }; Buffer.BlockCopy(Initiator, 0, MessageBodyByte, 0, Initiator.Length); // Packet Content Length [ 2 bytes ] // Context_Length = 2 + 300 <file_index_json> + len(MessageBody) int Context_Length = (2 + 300 + MessageBody_Length); byte[] length = { (byte)(Context_Length >> 8), (byte)(Context_Length & 0xff) }; Buffer.BlockCopy(length, 0, MessageBodyByte, 8, length.Length); // Packet Type [ 1 byte ] MessageBodyByte[10] = Packet_type; // Packet ID [ 1 byte ] MessageBodyByte[11] = Device_id; byte[] file_json = new byte[300]; file_index_json f_js = new file_index_json() { Name = file_name, Num = 0x1, Index = 0 }; string f_js_str = JsonConvert.SerializeObject(f_js); byte[] f_js_bytes = System.Text.Encoding.Default.GetBytes(f_js_str); Buffer.BlockCopy(f_js_bytes, 0, file_json, 0, f_js_str.Length); /** for 1.txt file (cmd = 1) * demo * { * "Name": "1.txt", * "Num": 2, * "Index": 0 * } * { * "Name": "1.txt", * "Num": 2, * "Index": 1 * } * */ //copy json data Buffer.BlockCopy(file_json, 0, MessageBodyByte, 12, file_json.Length); switch (upload_type) { case 0: //read file data file_data = new byte[MessageBody_Length]; file.Read(file_data, 0, (int)MessageBody_Length); //copy file data from index = 312 Buffer.BlockCopy(file_data, 0, MessageBodyByte, 12 + 300, MessageBody_Length); break; case 1: //read file data -- rsync file_data = new byte[MessageBody_Length]; file.Read(file_data, 0, (int)MessageBody_Length); //copy file data from index = 312 Buffer.BlockCopy(file_data, 0, MessageBodyByte, 12 + 300, MessageBody_Length); break; } // Check_sum byte[] all_data = new byte[file_data.Length + file_json.Length]; Buffer.BlockCopy(file_json, 0, all_data, 0, file_json.Length); Buffer.BlockCopy(file_data, 0, all_data, file_json.Length, file_data.Length); byte[] md5 = GetCheck_sum(Packet_type, Device_id, all_data, true); Buffer.BlockCopy(md5, 0, MessageBodyByte, 12 + 300 + MessageBody_Length, md5.Length); // end with 0xff 0xee MessageBodyByte[MessageBody_Length + 28 + 300] = 0xff; MessageBodyByte[MessageBody_Length + 29 + 300] = 0xee; // timer System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // start SendMessage(MessageBodyByte); byte[] res_flag = ReceiveMessage(); // result == "ok" if (res_flag[0] == 0x6f && res_flag[1] == 0x6b) { stopwatch.Stop(); // stop watch TimeSpan timespan = stopwatch.Elapsed; // Get the elapsed time as a TimeSpan value. // double hours = timespan.TotalHours; // hours // double minutes = timespan.TotalMinutes; // Minutes // double seconds = timespan.TotalSeconds; // Seconds double milliseconds = timespan.TotalMilliseconds; // Milliseconds double fs_size = file_length; double speed = fs_size / milliseconds * 1000 / 1024; // kb/s file.Close(); return(speed); } else { file.Close(); return(0); } } else { // Rounds double Packet_num = Math.Ceiling((double)file_length / (double)MessageBody_Length_Max); // Json - Packet Num byte Packet_Num = (byte)Packet_num; // Json - Packet Index byte index = 0; file_data = null; byte[] MessageBodyByte; // timer System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // start while (Packet_num > 0) { if (Packet_num != 1) { MessageBody_Length = MessageBody_Length_Max; } else { MessageBody_Length = (int)(file_length - MessageBody_Length_Max * index); } MessageBodyByte = new byte[MessageBody_Length + 30 + 300]; // Packet Initiator[ 8 bytes] 0x11 0xff 0x6c 0x6f 0x6e 0x64 0x6f 0x6e byte[] Initiator = { 0x11, 0xff, 0x6c, 0x6f, 0x6e, 0x64, 0x6f, 0x6e }; Buffer.BlockCopy(Initiator, 0, MessageBodyByte, 0, Initiator.Length); // Packet Content Length [ 2 bytes ] // Context_Length = 2 + 300 <file_index_json> + len(MessageBody) int Context_Length = (2 + 300 + MessageBody_Length); byte[] length = { (byte)(Context_Length >> 8), (byte)(Context_Length & 0xff) }; Buffer.BlockCopy(length, 0, MessageBodyByte, 8, length.Length); // Packet Type [ 1 byte ] MessageBodyByte[10] = Packet_type; // Packet ID [ 1 byte ] MessageBodyByte[11] = Device_id; byte[] file_json = new byte[300]; file_index_json f_js = new file_index_json() { Name = file_name, Num = Packet_Num, Index = index }; string f_js_str = JsonConvert.SerializeObject(f_js); byte[] f_js_bytes = System.Text.Encoding.Default.GetBytes(f_js_str); Buffer.BlockCopy(f_js_bytes, 0, file_json, 0, f_js_str.Length); //copy json data Buffer.BlockCopy(file_json, 0, MessageBodyByte, 12, file_json.Length); //read file file_data = new byte[MessageBody_Length]; //it will continue reading from last position file.Read(file_data, 0, MessageBody_Length); //copy file data Buffer.BlockCopy(file_data, 0, MessageBodyByte, 12 + 300, MessageBody_Length); // Check_sum /* * ** FILE data */ // Check_sum byte[] all_data = new byte[file_data.Length + file_json.Length]; Buffer.BlockCopy(file_json, 0, all_data, 0, file_json.Length); Buffer.BlockCopy(file_data, 0, all_data, file_json.Length, file_data.Length); byte[] md5 = GetCheck_sum(Packet_type, Device_id, all_data, true); Buffer.BlockCopy(md5, 0, MessageBodyByte, 12 + 300 + MessageBody_Length, md5.Length); // end with 0xff 0xee MessageBodyByte[MessageBody_Length + 28 + 300] = 0xff; MessageBodyByte[MessageBody_Length + 29 + 300] = 0xee; Packet_num--; index++; SendMessage(MessageBodyByte); } byte[] res_flag = ReceiveMessage(); if (res_flag[0] == 0x6f && res_flag[1] == 0x6b) { TimeSpan timespan = stopwatch.Elapsed; // Get the elapsed time as a TimeSpan value. // double hours = timespan.TotalHours; // hours // double minutes = timespan.TotalMinutes; // Minutes // double seconds = timespan.TotalSeconds; // Seconds double milliseconds = timespan.TotalMilliseconds; // Milliseconds double fs_size = file_length; double speed = fs_size / milliseconds * 1000 / 1024; // kb/s file.Close(); return(speed); } else { file.Close(); return(0); } } } catch (Exception ex) { listMessage.Add(ex.ToString()); return(0); } }