public async Task <Car> GetItem(int id) {//2 return(await Task.Run(async() => { var item = new Car(); item.Id = id; byte[] data = new Converter <Car>().ObjectToByteArray(item); var toSend = new byte[data.Length + 1]; toSend[0] = 32; for (int i = 0; i < data.Length; i++) { toSend[i + 1] = data[i]; } await socket.SendData(toSend); data = new byte[1024]; await socket.ReceiveData(data); try { var ret = new Converter <Car>().ByteArrayToObject(data); return ret; } catch (Exception ex) { MessageBox.Show(ex.Message); } return null; })); }
public static async Task GetFile(string path, string filename) { path += filename; IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings["Ip"].ToString()), int.Parse(ConfigurationManager.AppSettings["Port"].ToString())); using (var socket = new TCPSocket(StaticData.MyAddress, ipPoint)) { await socket.Connect(); var bytePath = ASCIIEncoding.ASCII.GetBytes(filename.ToArray()); var toSend = new byte[bytePath.Length + 1]; toSend[0] = 77; for (int i = 0; i < bytePath.Length; i++) { toSend[i + 1] = bytePath[i]; } await socket.SendData(toSend); var temp = new byte[12]; await socket.ReceiveData(temp); var length = BitConverter.ToInt64(temp, 0); if (length == 0) { MessageBox.Show("File doesn't exist"); return; } int packageSize = BitConverter.ToInt32(temp, 8); var data = new byte[packageSize]; using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write)) { int iter = (int)((length) / packageSize); for (int i = 0; i < iter; i++) { data = new byte[packageSize]; await socket.ReceiveData(data); await file.WriteAsync(data, 0, packageSize); await socket.SendData(new byte[] { 255 }); } var bytes = await socket.ReceiveData(data); await file.WriteAsync(data, 0, bytes); } } }
public async Task Execute(TCPSocket h) { this.handler = h; var buff = new byte[1024]; await handler.ReceiveData(buff); await Respond( await GetData(buff)); handler.Dispose(); }
public p_AllMsg ReceiveData() { byte[] bytes = null; bytes = TCPSocket.ReceiveData(); cache.AddRange(bytes); byte[] data = Decode(ref cache); p_AllMsg p_allmsg = null; if (bytes != null) { p_allmsg = new p_AllMsg(); p_allmsg = Serialization.DeserializeData(data); } return(p_allmsg); }
public void Receive_HandleData() { byte[] data = TCPSocket.ReceiveData(); lock (cache) { if (data != null) { cache.AddRange(data); if (!isReceiving) { isReceiving = true; ReadData(); } } } }
async Task<byte[]> GetData(byte[] data) { int first = 0; first = data[0] % 10; if (first <= 5) return await commands[first - 1].GetData(data); else if (first == 7) { await Task.Run(async () => { var path = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1)); var cleaned = path.Replace("\0", string.Empty); Console.WriteLine(cleaned); if (File.Exists(cleaned)) using (FileStream file = new FileStream(cleaned, FileMode.Open, FileAccess.ReadWrite)) { //max 65535 int packageSize = 16384 * 4; byte[] buff = new byte[packageSize]; var info = new byte[12]; var arr1 = BitConverter.GetBytes(file.Length); var arr2 = BitConverter.GetBytes(packageSize); for (int i = 0; i < 8; i++) info[i] = arr1[i]; for (int i = 8; i < 12; i++) info[i] = arr2[i - 8]; await handler.SendData(info); long persent = 0; int iter = (int)((file.Length) / packageSize); for (int i = 0; i < iter; i++) { await file.ReadAsync(buff, 0, packageSize); if ((int)((file.Position * 100) / file.Length) > persent) { persent = (file.Position * 100) / file.Length; Console.WriteLine(file.Position / 1024 + "kb " + persent + "% done"); } await handler.SendData(buff); await handler.ReceiveData(new byte[10]); } int bytes = await file.ReadAsync(buff, 0, packageSize); await handler.SendData(buff.Take(bytes).ToArray()); Console.WriteLine(file.Position / 1024 + "kb " + ++persent + "% done"); } else { await handler.SendData(BitConverter.GetBytes((long)0)); } }); return null; } else return null; }