コード例 #1
0
        /// <summary>
        /// Function that is called when a new connection has been established
        /// </summary>
        /// <param name="socket">Socket of the new connection</param>
        /// <returns>Asynchronous task</returns>
        private static async Task ProcessConnection(Socket socket)
        {
            using Connection connection = new Connection(socket);
            try
            {
                // Check if this connection is permitted
                connection.Logger.Debug("Got new UNIX connection, checking permissions...");
                if (await connection.AssignPermissions())
                {
                    // Send server-side init message to the client
                    await connection.Send(new ServerInitMessage { Id = connection.Id });

                    // Read client-side init message and switch mode
                    Base processor = await GetConnectionProcessor(connection);

                    if (processor != null)
                    {
                        // Send success message
                        await connection.SendResponse();

                        // Let the processor deal with the connection
                        await processor.Process();
                    }
                    else
                    {
                        connection.Logger.Debug("Failed to find processor");
                    }
                }
                else
                {
                    connection.Logger.Warn("Terminating connection due to insufficient permissions");
                    await connection.Send(new UnauthorizedAccessException("Insufficient permissions"));
                }
            }
            catch (Exception e)
            {
                if (!(e is OperationCanceledException) && !(e is SocketException))
                {
                    // Log unexpected errors
                    connection.Logger.Error(e, "Terminating connection due to unexpected exception");
                }
            }
            finally
            {
                connection.Logger.Debug("Connection closed");

                // Unlock the machine model again in case the client application crashed
                LockManager.UnlockMachineModel(connection);
            }
        }
コード例 #2
0
        /// <summary>
        /// Function that is called when a new connection has been established
        /// </summary>
        /// <param name="socket">Socket of the new connection</param>
        /// <returns>Asynchronous task</returns>
        private static async Task ProcessConnection(Socket socket)
        {
            using Connection connection = new Connection(socket);
            try
            {
                // Send server-side init message to the client
                connection.Logger.Debug("Got new UNIX connection, checking mode...");
                await connection.Send(new ServerInitMessage { Id = connection.Id });

                // Read client-side init message and switch mode
                Base processor = await GetConnectionProcessor(connection);

                if (processor != null)
                {
                    // Send success message
                    await connection.SendResponse();

                    // Let the processor deal with the connection
                    await processor.Process();
                }
                else
                {
                    connection.Logger.Debug("Failed to find processor");
                }
            }
            catch (Exception e)
            {
                if (!(e is OperationCanceledException) && !(e is SocketException))
                {
                    // Log unexpected errors
                    connection.Logger.Error(e, "Terminating connection because an unexpected exception was thrown");
                }
            }
            finally
            {
                connection.Logger.Debug("Connection closed");

                // Unlock the machine model again in case the client application crashed
                LockManager.UnlockMachineModel(connection.Id);
            }
        }