private void WriteAsyncInternal(NSOutputStream stream, SocketEventArgs args) { byte[] sendBuffer = args.MessageToSend.Serialize(); EventHandler <NSStreamEventArgs> completedHandler = null; completedHandler = (sender, e) => { stream.OnEvent -= completedHandler; if (args.MessageToSend is IMqttIdMessage) { var msgWithId = args.MessageToSend as IMqttIdMessage; _logger.LogMessage("Socket", LogLevel.Verbose, string.Format("Sent message type '{0}', ID={1}.", msgWithId.MessageType, msgWithId.MessageId)); } else { _logger.LogMessage("Socket", LogLevel.Verbose, string.Format("Sent message type '{0}'.", args.MessageToSend.MessageType)); } if (e.StreamEvent == NSStreamEvent.ErrorOccurred) { args.SocketException = new Exception("Socket error occured: " + e.StreamEvent.ToString()); } args.Complete(); }; stream.OnEvent += completedHandler; stream.Write(sendBuffer, (nuint)sendBuffer.Length); }
public unsafe void Write() { using (var s = (NSOutputStream)NSOutputStream.OutputStreamToMemory()) { s.Open(); s.Write(new byte[] { 1, 2, 3 }, 3); using (var obj = s [NSStream.DataWrittenToMemoryStreamKey] as NSData) { Assert.IsNotNull(obj, "a"); Assert.AreEqual(1, Marshal.ReadByte(obj.Bytes, 0), "a[0]"); Assert.AreEqual(2, Marshal.ReadByte(obj.Bytes, 1), "a[1]"); Assert.AreEqual(3, Marshal.ReadByte(obj.Bytes, 2), "a[2]"); } } using (var s = new NSOutputStream()) { s.Open(); s.Write(new byte[] { 1, 2, 3 }); using (var obj = s [NSStream.DataWrittenToMemoryStreamKey] as NSData) { Assert.IsNotNull(obj, "a"); Assert.AreEqual(1, Marshal.ReadByte(obj.Bytes, 0), "b[0]"); Assert.AreEqual(2, Marshal.ReadByte(obj.Bytes, 1), "b[1]"); Assert.AreEqual(3, Marshal.ReadByte(obj.Bytes, 2), "b[2]"); } } using (var s = (NSOutputStream)NSOutputStream.OutputStreamToMemory()) { s.Open(); s.Write(new byte[] { 1, 2, 3 }, 2, 1); using (var obj = s [NSStream.DataWrittenToMemoryStreamKey] as NSData) { Assert.IsNotNull(obj, "a"); Assert.AreEqual(3, Marshal.ReadByte(obj.Bytes, 0), "c[0]"); } } }
public void Write(byte[] buffer, int offset, int count) { if (offset < 0) { throw new ArgumentException("Offset must be greater than or equal to 0"); } var bytesSent = 0; var bytesToBeSent = new byte[buffer.Length - offset]; var length = count; buffer.CopyTo(bytesToBeSent, offset); Console.WriteLine($"Start writing to stream - length: {buffer.LongLength}"); while (length > 0) { if (_outputStream.HasSpaceAvailable()) { var bytesWritten = _outputStream.Write(buffer, (uint)count); if (bytesWritten == -1) { Console.WriteLine($"{buffer.LongLength} bytes failed to write to stream"); break; } if (bytesWritten > 0) { Console.WriteLine($"Bytes written to stream: {bytesWritten}"); length -= (int)bytesWritten; if (0 == length) { break; } var temp = new List <byte>(); for (var i = bytesWritten; i < bytesToBeSent.Length; i++) { temp.Add(bytesToBeSent[i]); } bytesToBeSent = temp.ToArray(); } } else { Console.WriteLine("No more space left in output stream"); } } }
IObservable<Unit> SaveFullResolutionImageToFile (FileRequest request) { return Observable.Create<Unit> (o => { var description = request.DescriptionAs<AssetDescription> (); var disp = new CancellationDisposable (); var token = disp.Token; GetAsset (description, token).ContinueWith (t => { using (File.Create (request.Filename)) using (var asset = t.Result) using (var representation = asset.DefaultRepresentation) using (var stream = new NSOutputStream (request.Filename, true)) { stream.Open (); long offset = 0; uint bytesRead = 0; NSError err; // A large enough buffer that shouldn't cause memory warnings byte [] buffer = new byte [131072]; GCHandle handle = GCHandle.Alloc (buffer, GCHandleType.Pinned); IntPtr pointer = handle.AddrOfPinnedObject (); unsafe { while (offset < representation.Size && stream.HasSpaceAvailable ()) { bytesRead = representation.GetBytes (pointer, offset, (uint)buffer.Length, out err); stream.Write (buffer, bytesRead); offset += bytesRead; } } stream.Close (); handle.Free (); } o.OnCompleted (); }, token).RouteExceptions (o); return disp; }); }