public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);

            CFStream.CreatePairWithSocketToHost(ipEndPoint, out readStream, out writeStream);

            inputStream  = ObjCRuntime.Runtime.GetNSObject <NSInputStream>(readStream.Handle);
            outputStream = ObjCRuntime.Runtime.GetNSObject <NSOutputStream>(writeStream.Handle);

            inputStream.ServiceType  = NSStreamServiceType.VoIP;
            outputStream.ServiceType = NSStreamServiceType.VoIP;
//            // or ?
            inputStream[NSStream.NetworkServiceType]  = NSStream.NetworkServiceTypeVoIP;
            outputStream[NSStream.NetworkServiceType] = NSStream.NetworkServiceTypeVoIP;

            inputStream.OnEvent  += HandleInputEvent;
            outputStream.OnEvent += HandleOutputEvent;

            outputStream.Schedule(NSRunLoop.Main, NSRunLoop.NSDefaultRunLoopMode); // TODO check
            inputStream.Schedule(NSRunLoop.Main, NSRunLoop.NSDefaultRunLoopMode);  // TODO check

//            readStream.EnableEvents(CFRunLoop.Main, NSRunLoop.NSDefaultRunLoopMode);
//            writeStream.EnableEvents(CFRunLoop.Main, NSRunLoop.NSDefaultRunLoopMode);

            outputStream.Open();
            inputStream.Open();

//            UIApplication.SharedApplication.SetKeepAliveTimeout(1, null); // Not supported any more.

            return(true);
        }
示例#2
0
        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);
        }
示例#3
0
 public void Memory()
 {
     using (var s = new NSOutputStream()) {
         // initToMemory does not respond (see dontlink.app) but it works
         Assert.That(s.Handle, Is.Not.EqualTo(IntPtr.Zero), "Handle");
     }
 }
示例#4
0
        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]");
                }
            }
        }
示例#5
0
 public void Path()
 {
     using (var s = new NSOutputStream("Info.plist", false)) {
         // initToFileAtPath:append: does not respond (see dontlink.app) but it works
         Assert.That(s.Handle, Is.Not.EqualTo(IntPtr.Zero), "Handle");
     }
 }
示例#6
0
        public void Dispose()
        {
            Input.Dispose();
            Output.Dispose();

            Input  = null;
            Output = null;
        }
示例#7
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         _outputStream?.Close();
         _outputStream?.Dispose();
         _outputStream = null;
     }
 }
        internal ExternalAccessoryNetworkStream(EASession session)
        {
            _delegate     = new EAStreamDelegate(this);
            _inputStream  = session.InputStream;
            _outputStream = session.OutputStream;

            _inputStream.Open();
            _outputStream.Delegate = _delegate;
            _outputStream.Schedule(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
            _outputStream.Open();
        }
示例#9
0
        public void WriteAsync(SocketEventArgs args)
        {
            NSOutputStream stream = GetStreamForConnectionConext(args);

            if (stream == null)
            {
                // OnCompleted called in GetStreamForConnectionConext(), just return here.
                return;
            }

            try
            {
                EventHandler <NSStreamEventArgs> handler = null;
                handler = (_, e1) =>
                {
                    stream.OnEvent -= handler;

                    if (e1.StreamEvent == NSStreamEvent.ErrorOccurred)
                    {
                        args.SocketException = new Exception("Something unexpected happened. " + e1.StreamEvent.ToString());
                        args.Complete();
                    }

                    if (e1.StreamEvent != NSStreamEvent.HasSpaceAvailable)
                    {
                        return;
                    }

                    WriteAsyncInternal(stream, args);
                };

                if (stream.HasSpaceAvailable())
                {
                    WriteAsyncInternal(stream, args);
                }
                else
                {
                    stream.OnEvent += handler;
                }
            }
            catch (ObjectDisposedException)
            {
                // Effectively ignoring this
                args.Complete();
            }
            catch (Exception ex)
            {
                args.SocketException =
                    new Exception("Unable to write to the TCP connection. See inner exception for details.", ex);
                args.Complete();
            }
        }
        protected override void Dispose(bool disposing)
        {
            if (_inputStream is object)
            {
                _inputStream.Close();
                _inputStream.Dispose();
                _inputStream = null;
            }

            if (_outputStream is object)
            {
                _outputStream.Close();
                _outputStream.Unschedule(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
                _outputStream.Dispose();
                _outputStream = null;
            }

            base.Dispose(disposing);
        }
示例#11
0
 public BleOutputStream(NSOutputStream outputStream)
 {
     _outputStream = outputStream;
     _outputStream.WeakDelegate = this;
     _buffer = new List <byte>();
 }
		public override void UserActivityReceivedData (NSUserActivity userActivity, NSInputStream inputStream, NSOutputStream outputStream)
		{
			// Log
			Console.WriteLine ("User Activity Received Data: {0}", userActivity.Title);
		}
示例#13
0
        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;
            });
        }
 public override void UserActivityReceivedData(NSUserActivity userActivity, NSInputStream inputStream, NSOutputStream outputStream)
 {
     // Log
     Console.WriteLine("User Activity Received Data: {0}", userActivity.Title);
 }
示例#15
0
 public NSStreamPair(NSInputStream input, NSOutputStream output)
 {
     Input  = input;
     Output = output;
 }
示例#16
0
        private void SetEncryptionOnStreams(SocketEncryption encryption, NSInputStream inStream, NSOutputStream outStream)
        {
            if (encryption == SocketEncryption.Ssl)
            {
                inStream.SocketSecurityLevel  = NSStreamSocketSecurityLevel.SslV3;
                outStream.SocketSecurityLevel = NSStreamSocketSecurityLevel.SslV3;
            }
            else if (encryption == SocketEncryption.Tls10)
            {
                inStream.SocketSecurityLevel  = NSStreamSocketSecurityLevel.TlsV1;
                outStream.SocketSecurityLevel = NSStreamSocketSecurityLevel.TlsV1;
            }
            else if (encryption == SocketEncryption.Tls11)
            {
                inStream.SocketSecurityLevel  = NSStreamSocketSecurityLevel.TlsV1;
                outStream.SocketSecurityLevel = NSStreamSocketSecurityLevel.TlsV1;
            }
            else if (encryption == SocketEncryption.Tls12)
            {
                inStream.SocketSecurityLevel  = NSStreamSocketSecurityLevel.TlsV1;
                outStream.SocketSecurityLevel = NSStreamSocketSecurityLevel.TlsV1;

                //var key = ObjCRuntime.Dlfcn.GetStringConstant("kCFStreamPropertySSLSettings", Libraries.CoreFoundation.Handle);
            }
        }