/*
  * Print out all the timing info
  */
 void DumpTimings()
 {
     if (PerfTimerInstance != null)
     {
         String PerfMessage = PerfTimerInstance.DumpTimings();
         IntPtr PerfMessagePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FInfoMessage)));
         Marshal.StructureToPtr(new FInfoMessage(PerfMessage), PerfMessagePtr, true);
         SendMessage(PerfMessagePtr);
         PerfTimerInstance = null;
     }
 }
		/**
		 * Opens a new connection to the Swarm
		 *
		 * @param CallbackFunc The callback function Swarm will use to communicate back to the Instigator
		 *
		 * @return An INT containing the error code (if < 0) or the handle (>= 0) which is useful for debugging only
		 */
		public virtual Int32 OpenConnection(FConnectionCallback CallbackFunc, IntPtr CallbackData, ELogFlags LoggingFlags, string OptionsFolder)
		{
			// Checked here so we can time OpenConnection
			if ((LoggingFlags & ELogFlags.LOG_TIMINGS) == ELogFlags.LOG_TIMINGS)
			{
				PerfTimerInstance = new PerfTimer();
			}

			StartTiming("OpenConnection-Managed", true);

			// Establish a connection to the local Agent server object
			ConnectionHandle = Constants.INVALID;
			Int32 ReturnValue = Constants.INVALID;
			try
			{
				EditorLog(EVerbosityLevel.Informative, "[OpenConnection] Registering TCP channel ...");

				// Start up network services, by opening a network communication channel
				NetworkChannel = new TcpClientChannel();
				ChannelServices.RegisterChannel(NetworkChannel, false);

				// See if an agent is already running, and if not, launch one
				EnsureAgentIsRunning(OptionsFolder);
				if (AgentProcess != null)
				{
					EditorLog(EVerbosityLevel.Informative, "[OpenConnection] Connecting to agent ...");
					ReturnValue = TryOpenConnection(CallbackFunc, CallbackData, LoggingFlags);
					if (ReturnValue >= 0)
					{
						AgentCacheFolder = ConnectionConfiguration.AgentCachePath;
						if (AgentCacheFolder.Length == 0)
						{
							EditorLog(EVerbosityLevel.Critical, "[OpenConnection] Agent cache folder with 0 length.");
							CloseConnection();
							ReturnValue = Constants.ERROR_FILE_FOUND_NOT;
						}
					}
				}
				else
				{
					EditorLog(EVerbosityLevel.Critical, "[OpenConnection] Failed to find Swarm Agent");
                    ReturnValue = Constants.ERROR_FILE_FOUND_NOT;
				}
			}
			catch (Exception Ex)
			{
				EditorLog(EVerbosityLevel.Critical, "[OpenConnection] Error: " + Ex.Message);
				ReturnValue = Constants.ERROR_EXCEPTION;
			}

			// Finally, if there have been no errors, assign the connection handle 
			if (ReturnValue >= 0)
			{
				ConnectionHandle = ReturnValue;
			}
			else
			{
				// If we've failed for any reason, call the clean up routine
				CleanupClosedConnection();
			}

			StopTiming();
			return ReturnValue;
		}
        FSwarmInterface()
        {
            AgentProcess = null;
            AgentProcessOwner = false;
            Connection = null;
            ConnectionHandle = Constants.INVALID;
            ConnectionMessageThread = null;
            ConnectionMonitorThread = null;
            ConnectionConfiguration = null;
            ConnectionCallback = null;
            BaseChannelHandle = 0;
            PendingTasks = null;
            NetworkChannel = null;
            PerfTimerInstance = null;

            OpenChannels = new ReaderWriterDictionary<Int32, ChannelInfo>();
            FreeChannelWriteBuffers = new Stack<byte[]>();
            CleanupClosedConnectionLock = new Object();

            // TODO: Delete old files
        }