Example #1
0
        public void ConnectTcpClient(NSStreamPair streams, int port, SocketEncryption encryption, string connectionKey)
        {
            var encryptionLevel = SslProtocols.None;

            switch (encryption)
            {
            case SocketEncryption.None:
                encryptionLevel = SslProtocols.None;
                break;

            case SocketEncryption.Ssl:
                encryptionLevel = SslProtocols.Ssl3;
                break;

            case SocketEncryption.Tls10:
                encryptionLevel = SslProtocols.Tls;
                break;

            case SocketEncryption.Tls11:
                encryptionLevel = SslProtocols.Tls11;
                break;

            case SocketEncryption.Tls12:
                encryptionLevel = SslProtocols.Tls12;
                break;
            }

            lock (_connectedClients)
            {
                _logger.LogMessage("Socket", LogLevel.Verbose, string.Format("Adding new TCP client: key={0}", connectionKey));

                var clientInfo = new ConnectedClientInfo
                {
                    //Socket = socket,
                    Stream     = streams,
                    Port       = port,
                    Encryption = encryptionLevel,
                    ClientUid  = connectionKey
                };
                _connectedClients.Add(connectionKey, clientInfo);

                ListeningInputStream(clientInfo);
            }
        }
Example #2
0
        public void ConnectAsync(string ipOrHost, int port, SocketEventArgs args)
        {
            CFReadStream  cfRead;
            CFWriteStream cfWrite;

            CFStream.CreatePairWithSocketToHost(ipOrHost, port, out cfRead, out cfWrite);

            // Toll-Free binding from CFStream to a NSStream.
            var inStream  = (NSInputStream)ObjCRuntime.Runtime.GetNSObject(cfRead.Handle);
            var outStream = (NSOutputStream)ObjCRuntime.Runtime.GetNSObject(cfWrite.Handle);
            var pair      = new NSStreamPair(inStream, outStream);

            inStream.Schedule(_socketWorker.RunLoop, NSRunLoop.NSDefaultRunLoopMode);
            outStream.Schedule(_socketWorker.RunLoop, NSRunLoop.NSDefaultRunLoopMode);

            if (args.EncryptionLevel != SocketEncryption.None)
            {
                SetEncryptionOnStreams(args.EncryptionLevel, inStream, outStream);
            }

            var inReady  = false;
            var outReady = false;

            Action complete = () =>
            {
                _socketWorker.ConnectTcpClient(pair, port, args.EncryptionLevel, args.ClientUid);
                args.Complete();
            };

            EventHandler <NSStreamEventArgs> inReadyHandler = null;

            inReadyHandler = (_, e) =>
            {
                inStream.OnEvent -= inReadyHandler;
                inReady           = true;

                if (inReady && outReady)
                {
                    complete();
                }
            };

            EventHandler <NSStreamEventArgs> outReadyHandler = null;

            outReadyHandler = (_, e) =>
            {
                outStream.OnEvent -= outReadyHandler;
                outReady           = true;

                if (inReady && outReady)
                {
                    complete();
                }
            };

            inStream.OnEvent  += inReadyHandler;
            outStream.OnEvent += outReadyHandler;

            inStream.Open();
            outStream.Open();
        }