Esempio n. 1
0
        public ClientServerTester(RequestHandler servercallback, RequestHandler clientcallback)
        {
            S1 = new BlockingPipeStream("serverInput/clientOutput");
            S2 = new BlockingPipeStream("clientInput/serverOutput");

            Server = new InterProcessConnection(S1, S2);
            Client = new InterProcessConnection(S2, S1);

            ServerTask = Task.Run(() => Server.RunMainLoopAsync(servercallback, false));
            ClientTask = Task.Run(() => Client.RunMainLoopAsync(clientcallback, true));
        }
Esempio n. 2
0
        /// <summary>
        /// Starts the connection after configuration has completed
        /// </summary>
        /// <param name="asclient">If set to <c>true</c>, connect as a client.</param>
        /// <param name="authenticationHandler">The optional authentication handler.</param>
        /// <returns>The peer instance</returns>
        public virtual RPCPeer Start(bool asclient, IAuthenticationHandler authenticationHandler = null)
        {
            if (MainTask != null)
            {
                throw new InvalidOperationException("Cannot start a RPC peer more than once");
            }

            MainTask = m_connection.RunMainLoopAsync(asclient, authenticationHandler);
            m_connection.WaitForHandshakeAsync().Wait();
            return(this);
        }
Esempio n. 3
0
 /// <summary>
 /// Runs the main loop, parsing input messages, reporting error for unknown messages.
 /// </summary>
 /// <returns>An awaitable task.</returns>
 /// <param name="asClient">A value indicating if this instance is connecting as a client</param>
 /// <param name="authenticationHandler">An optional authentication handler</param>
 public Task RunMainLoopAsync(bool asClient, IAuthenticationHandler authenticationHandler = null)
 {
     return(m_connection.RunMainLoopAsync(asClient, authenticationHandler));
 }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ceen.Httpd.Cli.SubProcess.SpawnRemoteInstance"/> class.
        /// </summary>
        /// <param name="path">The path to the config file</param>
        /// <param name="usessl">If set to <c>true</c> use ssl.</param>
        public SpawnRemoteInstance(string path, bool usessl, IStorageCreator storage, CancellationToken token)
        {
            m_path    = path;
            m_useSSL  = usessl;
            m_storage = storage;

            var prefix = m_hiddenSocketPath ? "\0" : string.Empty;
            var sockid = string.Format("ceen-socket-{0}", new Random().Next().ToString("x8"));

            if (m_hiddenSocketPath)
            {
                m_socketpath = sockid;
            }
            else
            {
                var pathPrefix = Environment.GetEnvironmentVariable("CEEN_SOCKET_FOLDER");
                if (string.IsNullOrWhiteSpace(pathPrefix))
                {
                    pathPrefix = System.IO.Path.GetTempPath();
                }

                m_socketpath = System.IO.Path.GetFullPath(System.IO.Path.Combine(pathPrefix, sockid));
            }

            // Setup a socket server, start the child, and stop the server
            using ((SystemHelper.IsCurrentOSPosix && !m_hiddenSocketPath) ? new DeleteFilesHelper(m_socketpath, m_socketpath + "_fd") : null)
                using (var serveripcsocket = SystemHelper.IsCurrentOSPosix ? new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) : null)
                    using (var serverfdsocket = SystemHelper.IsCurrentOSPosix ? new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP) : null)
                    {
                        if (SystemHelper.IsCurrentOSPosix)
                        {
                            serveripcsocket.Bind(new SockRock.UnixEndPoint(prefix + m_socketpath));
                            serveripcsocket.Listen(1);

                            serverfdsocket.Bind(new SockRock.UnixEndPoint(prefix + m_socketpath + "_fd"));
                            serverfdsocket.Listen(1);
                        }

                        // Launch the child process
                        m_proc = StartRemoteProcess();

                        // TODO: Consider some multiplexer to allow multiple outputs without mixing the contents
                        var tasks = Task.WhenAll(
                            m_proc.StandardOutput.BaseStream.CopyToAsync(Console.OpenStandardOutput()),
                            m_proc.StandardError.BaseStream.CopyToAsync(Console.OpenStandardError()),
                            Console.OpenStandardInput().CopyToAsync(m_proc.StandardInput.BaseStream)
                            );

                        if (SystemHelper.IsCurrentOSPosix)
                        {
                            var ct = new CancellationTokenSource();

                            // Prepare cancellation after 5 seconds
                            Task.Delay(5000, ct.Token).ContinueWith(_ =>
                            {
                                serveripcsocket.Dispose();
                                serverfdsocket.Dispose();
                            });

                            // Get the first connection
                            m_ipcSocket = serveripcsocket.Accept();
                            m_fdSocket  = serverfdsocket.Accept();

                            // Stop the timer
                            ct.Cancel();
                        }

                        //and then don't listen anymore
                    }


            var ipc = new InterProcessConnection(new NetworkStream(m_ipcSocket, true));

            m_peer = new RPCPeer(
                ipc,
                typeof(IStorageEntry),
                typeof(IStorageCreator)
                );

            // We pass these by reference
            m_peer.TypeSerializer.RegisterSerializationAction(typeof(IStorageEntry), SerializationAction.Reference);
            m_peer.TypeSerializer.RegisterSerializationAction(typeof(IStorageCreator), SerializationAction.Reference);

            // Set up special handling for serializing an endpoint instance
            m_peer.TypeSerializer.RegisterEndPointSerializers();
            m_peer.TypeSerializer.RegisterIPEndPointSerializers();

            m_main = Task.Run(async() => {
                try
                {
                    using (m_fdSocket)
                        using (m_ipcSocket)
                            await ipc.RunMainLoopAsync(false);
                }
                finally
                {
                    Program.DebugConsoleOutput("{0}: Finished main loop, no longer connected to: {1}", System.Diagnostics.Process.GetCurrentProcess().Id, m_proc.Id);
                    m_proc.WaitForExit((int)TimeSpan.FromMinutes(1).TotalMilliseconds);
                    if (!m_proc.HasExited)
                    {
                        m_proc.Kill();
                    }
                    Program.DebugConsoleOutput("{0}: Target now stopped: {1}", System.Diagnostics.Process.GetCurrentProcess().Id, m_proc.Id);
                }
            });

            if (token.CanBeCanceled)
            {
                token.Register(() => this.Stop());
            }

            // Register the storage item, since it is a different concrete class (i.e. not the interface)
            if (storage != null)
            {
                m_peer.TypeSerializer.RegisterSerializationAction(storage.GetType(), SerializationAction.Reference);
                m_peer.RegisterLocalObjectOnRemote(storage, typeof(IStorageCreator)).Wait();
            }

            m_peer.AddAutomaticProxy(typeof(SpawnedServer), typeof(ISpawnedServerProxy));
            m_proxy = m_peer.CreateAsync <ISpawnedServerProxy>(typeof(SpawnedServer), usessl, path, storage).Result;

            if (SystemHelper.IsCurrentOSPosix)
            {
                // Prepare the file descriptor socket
                m_fdSocketTypeSerializer = new TypeSerializer(false, false);
                SetupDescriptorSocket().Wait();
            }
        }