Exemplo n.º 1
0
        /// <summary>
        /// This method first reads the objectId as an int from the stream,
        /// this int should be found in the "ObjectType" enumeration. This
        /// objectId informs the method what kind of object should be
        /// deserialized and returned from the method. The objectId is an
        /// output parameter. This parameter is also returned so it can be
        /// used in the read and write methods to determine if
        /// a frame or end marker was found.
        /// </summary>
        private object DeserializeFromStream(out int objectId)
        {
            object objectRead = null;

            objectId = readStream.ReadByte();
            switch ((ObjectType)objectId)
            {
            case ObjectType.NetSerialization:
                objectRead = binaryFormatter.Deserialize(readStream);
                break;

            case ObjectType.FrameMarker:
                objectRead = binaryReader.ReadInt32();
                break;

            case ObjectType.PostBuildResult:
                objectRead = new LocalCallDescriptorForPostBuildResult();
                ((LocalCallDescriptorForPostBuildResult)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.PostBuildRequests:
                objectRead = new LocalCallDescriptorForPostBuildRequests();
                ((LocalCallDescriptorForPostBuildRequests)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.PostLoggingMessagesToHost:
                objectRead = new LocalCallDescriptorForPostLoggingMessagesToHost();
                ((LocalCallDescriptorForPostLoggingMessagesToHost)objectRead).CreateFromStream(binaryReader, loggingTypeCache);
                break;

            case ObjectType.InitializeNode:
                objectRead = new LocalCallDescriptorForInitializeNode();
                ((LocalCallDescriptorForInitializeNode)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.InitializationComplete:
                objectRead = new LocalCallDescriptorForInitializationComplete();
                ((LocalCallDescriptorForInitializationComplete)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.UpdateNodeSettings:
                objectRead = new LocalCallDescriptorForUpdateNodeSettings();
                ((LocalCallDescriptorForUpdateNodeSettings)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.RequestStatus:
                objectRead = new LocalCallDescriptorForRequestStatus();
                ((LocalCallDescriptorForRequestStatus)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.PostCacheEntriesToHost:
                objectRead = new LocalCallDescriptorForPostingCacheEntriesToHost();
                ((LocalCallDescriptorForPostingCacheEntriesToHost)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.GetCacheEntriesFromHost:
                objectRead = new LocalCallDescriptorForGettingCacheEntriesFromHost();
                ((LocalCallDescriptorForGettingCacheEntriesFromHost)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.ShutdownComplete:
                objectRead = new LocalCallDescriptorForShutdownComplete();
                ((LocalCallDescriptorForShutdownComplete)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.ShutdownNode:
                objectRead = new LocalCallDescriptorForShutdownNode();
                ((LocalCallDescriptorForShutdownNode)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.PostIntrospectorCommand:
                objectRead = new LocalCallDescriptorForPostIntrospectorCommand(null, null);
                ((LocalCallDescriptorForPostIntrospectorCommand)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.GenericSingleObjectReply:
                objectRead = new LocalReplyCallDescriptor();
                ((LocalReplyCallDescriptor)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.PostStatus:
                objectRead = new LocalCallDescriptorForPostStatus();
                ((LocalCallDescriptorForPostStatus)objectRead).CreateFromStream(binaryReader);
                break;

            case ObjectType.EndMarker:
                return(null);

            default:
                ErrorUtilities.VerifyThrow(false, "Should not be here, ObjectId:" + objectId + "Next:" + readStream.ReadByte());
                break;
            }
            return(objectRead);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function establishes communication with a node given an index. If a node
        /// is not running it is launched.
        /// </summary>
        private void InitializeNode(int nodeIndex)
        {
            bool nodeConnected = false;
            int  restartCount  = 0;

            try
            {
                IncreaseActiveNodeCount();

                while (!nodeConnected && restartCount < maximumNodeRestartCount)
                {
                    if (!checkIfNodeActive(nodeData[nodeIndex].NodeNumber))
                    {
                        // Attempt to launch a new node process
                        LaunchNode(nodeIndex);
                        // If we could not launch the node there is no reason to continue
                        if (nodeData[nodeIndex].CommunicationFailed)
                        {
                            break;
                        }
                    }

                    if (checkIfNodeActive(nodeData[nodeIndex].NodeNumber))
                    {
                        nodeData[nodeIndex].SharedMemoryToNode.Reset();
                        nodeData[nodeIndex].SharedMemoryFromNode.Reset();

                        // Activate the initiation event to prove to the child that we have the same level of privilege as it does. This operation will not fail because each privilege level creates
                        // events in different namespaces
                        EventWaitHandle nodeInitiateActivationEvent = new EventWaitHandle(false, EventResetMode.ManualReset, LocalNodeProviderGlobalNames.NodeInitiateActivationEventName(nodeData[nodeIndex].NodeNumber));
                        nodeInitiateActivationEvent.Set();
                        nodeInitiateActivationEvent.Close();

                        // Wait for node to indicate that it is activated
                        EventWaitHandle nodeActivatedEvent = new EventWaitHandle(false, EventResetMode.ManualReset, LocalNodeProviderGlobalNames.NodeActivedEventName(nodeData[nodeIndex].NodeNumber));
                        nodeActivatedEvent.WaitOne(initializationTimeout, false);
                        nodeActivatedEvent.Close();

                        // Looked in Environment.cs the IDictionary is a HashTable
                        IDictionary variableDictionary        = Environment.GetEnvironmentVariables();
                        Hashtable   environmentVariablesTable = new Hashtable(variableDictionary);

                        LocalCallDescriptorForInitializeNode callDescriptorInit =
                            new LocalCallDescriptorForInitializeNode(environmentVariablesTable, nodeLoggers.ToArray(), nodeData[nodeIndex].NodeId, parentGlobalProperties, toolsetSearchLocations, Process.GetCurrentProcess().Id, startupDirectory);
                        nodeData[nodeIndex].NodeCommandQueue.Enqueue(callDescriptorInit);

                        EventWaitHandle nodeInUseEvent = new EventWaitHandle(false, EventResetMode.ManualReset, LocalNodeProviderGlobalNames.NodeInUseEventName(nodeData[nodeIndex].NodeNumber));

                        // Wait for node to indicate that it is ready. The node may time out and exit, between
                        // when we check that it is active and before the initialization messages reaches it.
                        // In that rare case we have to restart the node.
                        if (nodeInUseEvent.WaitOne(initializationTimeout, false))
                        {
                            UpdateSettings(nodeIndex);
                            nodeConnected = true;
                        }
                        nodeInUseEvent.Close();

                        // If the node is still active and has not replied to the initialization message it must
                        // be in bad state - try to get that node to exit
                        if (!nodeConnected && checkIfNodeActive(nodeData[nodeIndex].NodeNumber))
                        {
                            EventWaitHandle nodeShutdownEvent = new EventWaitHandle(false, EventResetMode.ManualReset, LocalNodeProviderGlobalNames.NodeErrorShutdownEventName(nodeData[nodeIndex].NodeNumber));
                            nodeShutdownEvent.Set();
                            nodeShutdownEvent.Close();

                            restartCount = maximumNodeRestartCount;
                        }

                        restartCount++;
                    }
                }
            }
            finally
            {
                // Make sure to decrement the active node count if the communication has failed
                if (nodeConnected != true)
                {
                    DecreaseActiveNodeCount(nodeData[nodeIndex].NodeId);
                    nodeData[nodeIndex].CommunicationFailed = true;
                }
            }
        }