/// <summary> /// Task that uploads queue to server /// </summary> private async void UploadTask(Page sender) { try { while (ConnectionStatus) { if (TransferQueue.Count != 0) { TransferQueue[0].Stream.Position = 0; BinaryReader r = new BinaryReader(TransferQueue[0].Stream); //Message format (datasize FILENAME/DATASIZE DATA) without spaces and brackets //Byte representation of filename byte[] name = Encoding.ASCII.GetBytes(TransferQueue[0].Name + '/'); //Byte respresentation of image data byte[] imageData; using (var memoryStream = new MemoryStream()) { TransferQueue[0].Stream.CopyTo(memoryStream); imageData = memoryStream.ToArray(); } //byte representation of package size byte[] totalLen = BitConverter.GetBytes(name.Length + imageData.Length + 4); byte[] package = new byte[name.Length + imageData.Length + 4]; System.Array.Copy(totalLen, 0, package, 0, totalLen.Length); System.Array.Copy(name, 0, package, totalLen.Length, name.Length); System.Array.Copy(imageData, 0, package, totalLen.Length + name.Length, imageData.Length); NetworkStream s = DataClient.GetStream(); int totalSentBytes = 0; int packetSize = 4096; while (totalSentBytes != package.Length) { int leftToSend = package.Length - totalSentBytes; if (leftToSend > packetSize) { s.Write(package, totalSentBytes, packetSize); totalSentBytes += packetSize; } else { s.Write(package, totalSentBytes, leftToSend); totalSentBytes += leftToSend; } } string n = TransferQueue[0].Stream.Name; TransferQueue.RemoveAt(0); File.Delete(n); } await Task.Delay(20); } } catch { Device.BeginInvokeOnMainThread(async() => { await sender.DisplayAlert("Error", "Server is not responding", "OK"); ConnectionStatus = false; }); } }