public static PMM loadFile(String file) { PMM a = new PMM(); int position = 3; var reader = new BinaryReader(new FileStream(file, FileMode.Open)); if (reader.ReadChar() != 'P') { return(null); } String method = "P" + reader.ReadChar(); //Eat number reader.ReadChar(); //Eat newline string widths = "", heights = ""; char temp = reader.ReadChar(); while (temp != ' ' && temp != '\n') { widths += temp; position++; temp = reader.ReadChar(); } position++; while ((temp = reader.ReadChar()) >= '0' && temp <= '9') { heights += temp; position++; } position++; if (reader.ReadChar() != '2' || reader.ReadChar() != '5' || reader.ReadChar() != '5') { return(null); } reader.ReadChar(); //Eat the last newline position += 4; int width = int.Parse(widths), height = int.Parse(heights); reader.Close(); //set PMM object a.width = width; a.height = height; a.method = method; byte[] data = File.ReadAllBytes(file); a.data = new byte[data.Length - position]; Buffer.BlockCopy(data, position, a.data, 0, a.data.Length); return(a); }
public void listen_pmm_resp() { while (true) { Package pack = conn.recv(); if (pack == null) { continue; } if (pack.type == "ask_pmm") { if (Local.exist(pack.header[0])) { pmm = PMM.loadFile(pack.header[0]); Package pack_resp = new Package("has_pmm"); Connector conn_resp = Client.find_conn(Client.conn_pmm_ask, pack.from); pack_resp.header.Add("" + pmm.width); pack_resp.header.Add("" + pmm.height); pack_resp.header.Add(pmm.method); pmm_data = new List <byte[]>(); int position = 0; int len = pmm.data.Length / 10; for (int i = 0; i < 9; i++) { pmm_data.Add(new byte[len]); Buffer.BlockCopy(pmm.data, position, pmm_data[i], 0, len); position += len; } pmm_data.Add(new byte[pmm.data.Length - position]); Buffer.BlockCopy(pmm.data, position, pmm_data[9], 0, pmm.data.Length - position); conn_resp.send(pack_resp); } else { Package pack_resp = new Package("no_pmm"); Connector conn_resp = Client.find_conn(Client.conn_pmm_ask, pack.from); conn_resp.send(pack_resp); } } else if (pack.type == "request_pmm") { Package pack_resp = new Package("resp_pmm"); Connector conn_resp = Client.find_conn(Client.conn_pmm_ask, pack.from); pack_resp.header.Add(pack.header[0]); pack_resp.data = pmm_data[Int32.Parse(pack.header[0])]; conn_resp.send(pack_resp); } } }
public void askPMM(String filename) { pmm_no = 0; pmm_has = 0; pmm_finish = 0; pmm_writing = 0; pmm_data = new List <byte[]>(); pmm_haha = new List <int>(); pmm_client = new List <string>(); count = 0; for (int i = 0; i < 10; i++) { pmm_data.Add(new byte[0]); pmm_haha.Add(0); } int pmm_asked = 0; for (int i = 0; i < ip_list.Count; i++) { if (ip_list[i] != self_ip && ip_list[i] != "") { video_asked++; Connector conn = find_conn(conn_pmm_resp, ip_list[i]); Package pack = new Package("ask_pmm"); pack.header.Add(filename); conn.send(pack); pmm_asked++; } } while (pmm_has + pmm_no < pmm_asked) { } if (pmm_no == pmm_asked) { return; } PMM pmm = new PMM(Int32.Parse(pmm_header[0]), Int32.Parse(pmm_header[1]), pmm_header[2]); while (count < 10) { Connector conn = find_conn(conn_pmm_resp, pmm_client[count % pmm_client.Count]); Package pack = new Package("request_pmm"); pack.header.Add("" + count); conn.send(pack); while (pmm_haha[count] == 0) { } count++; } while (true) { int tag = 0; for (int i = 0; i < 10; i++) { if (pmm_haha[i] == 0) { tag = 1; continue; } } //finish receiving all data if (tag == 0) { int length = 0; for (int i = 0; i < 10; i++) { length += pmm_data[i].Length; } pmm.data = new byte[length]; int position = 0; for (int i = 0; i < 10; i++) { Buffer.BlockCopy(pmm_data[i], 0, pmm.data, position, pmm_data[i].Length); position += pmm_data[i].Length; } pmm.writeToFile(Local.ref_addr + filename); return; } } }