Пример #1
0
        public override void OnReceiveData(ChoConnectionState state)
        {
            while (state.AvailableData > 0)
            {
                int readBytes = state.Read(state.Buffer, 0, state.Buffer.Length);

                byte[] buffer = state.Buffer.Take(readBytes).ToArray();
                if (_inbuffer == null)
                {
                    _inbuffer    = buffer;
                    _headerFound = false;
                }
                else
                {
                    _inbuffer = ChoByteArrayEx.Combine(_inbuffer, buffer);
                }

                try
                {
                    if (!_headerFound)
                    {
                        _msgBodySize = ProcessHeader(out _headerFound);
                    }

                    if (_headerFound)
                    {
                        ProcessBody(state, _msgBodySize);
                    }
                }
                catch (Exception ex)
                {
                    ChoTrace.Write(ex);
                }
            }
        }
Пример #2
0
        public void Load(Type[] types)
        {
            if (types == null)
            {
                return;
            }

            foreach (Type type in types)
            {
                if (type == null)
                {
                    continue;
                }
                try
                {
                    IChoPlugIn <T> plugIn = ChoActivator.CreateInstance <IChoPlugIn <T> >();
                    if (plugIn != null)
                    {
                        _plugInsObjs.Add(plugIn);
                    }
                }
                catch (Exception ex)
                {
                    ChoTrace.Write(ex);
                }
            }
        }
Пример #3
0
        private bool ProcessBody(ChoConnectionState state, int msgBodySize)
        {
            if (_inbuffer.Length < msgBodySize)
            {
                return(false);
            }

            try
            {
                MemoryStream instream = new MemoryStream();
                instream.Write(_inbuffer, 0, msgBodySize);
                _inbuffer = ChoByteArrayEx.SubArray(_inbuffer, msgBodySize);
                instream.Flush();
                instream.Position = 0;

                InvokeObjectReceived(state, instream.GetBuffer());
            }
            catch (Exception ex)
            {
                ChoTrace.Write(ex);
            }
            finally
            {
                //_inbuffer = null;
                _headerFound = false;
            }

            return(true);
        }
Пример #4
0
        /// <summary>
        /// Execute the work item
        /// </summary>
        private void ExecuteWorkItem()
        {
            ChoCallerThreadContext ctc = null;

            if (null != _callerContext)
            {
                ctc = ChoCallerThreadContext.Capture();
                ChoCallerThreadContext.Apply(_callerContext);
            }

            Exception exception = null;
            object    result    = null;

            try
            {
                result = _callback(_state);
            }
            catch (Exception e)
            {
                // Save the exception so we can rethrow it later
                ChoTrace.Write(e);
                exception = e;
            }

            if (null != _callerContext)
            {
                ChoCallerThreadContext.Apply(ctc);
            }

            SetResult(result, exception);
        }
		/// <summary>
		/// <para>Raises the <see cref="ConfigurationChanged"/> event.</para>
		/// </summary>
		public virtual void OnConfigurationChanged()
		{
			try
			{
				ChoConfigurationChangedEventArgs eventData = EventData;
				OrderedDictionary eventHandlerList = _eventHandlerList;
				if (eventHandlerList != null)
				{
					lock (_eventHandlerListLock)
					{
						//int counter = 0;
						//WaitHandle[] handles = new WaitHandle[_eventHandlerList.Count];
						foreach (ChoConfigurationChangedEventHandler callback in eventHandlerList.Values)
						{
							if (callback != null)
							{
								callback(this, eventData);

								//handles[counter++] = callback.BeginInvoke(this, eventData, null, null).AsyncWaitHandle;
							}
						}
						//WaitHandle.WaitAll(handles);
					}
				}
			}
			catch (ChoFatalApplicationException)
			{
			}
			catch (Exception ex)
			{
				ChoTrace.Write(ex);
			}
		}
Пример #6
0
        public static void OnEnvironmentChanged()
        {
            try
            {
                OrderedDictionary eventHandlerList = _eventHandlerList;
                if (eventHandlerList != null)
                {
                    lock (_eventHandlerListLock)
                    {
                        //int counter = 0;
                        //WaitHandle[] handles = new WaitHandle[_eventHandlerList.Count];
                        foreach (EventHandler callback in eventHandlerList.Values)
                        {
                            if (callback != null)
                            {
                                callback(null, null);

                                //handles[counter++] = callback.BeginInvoke(this, eventData, null, null).AsyncWaitHandle;
                            }
                        }
                        //WaitHandle.WaitAll(handles);
                    }
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ChoTrace.Write(ex);
            }
        }
Пример #7
0
        public virtual void TraceOutput(object configObject)
        {
            ChoFileProfile.Clean(TraceOutputDirectory, _traceOutputFileName);

            if (!AutoTrace)
            {
                ChoFileProfile.WriteLine(TraceOutputDirectory, _traceOutputFileName, "AutoTrace is turned off.");
                return;
            }

            try
            {
                if (ErrMsg != null)
                {
                    if (this.DisplayOnConsole)
                    {
                        Console.WriteLine(ErrMsg);
                    }
                    ChoFileProfile.WriteLine(TraceOutputDirectory, _traceOutputFileName, ErrMsg);
                }

                string msg = ChoConfigurationManagementFactory.ToString(configObject);
                if (this.DisplayOnConsole)
                {
                    Console.WriteLine(msg);
                }
                ChoFileProfile.WriteLine(TraceOutputDirectory, _traceOutputFileName, msg);
            }
            catch (Exception ex)
            {
                ChoTrace.Write(ex);
                throw;
            }
        }
Пример #8
0
 static ChoFileProfile()
 {
     try
     {
         _fileWriterThread = new Thread(new ParameterizedThreadStart(SyncFileWriter));
         _fileWriterThread.IsBackground = true;
         _fileWriterThread.Start();
     }
     catch (Exception ex)
     {
         ChoTrace.Write(ex);
     }
 }
Пример #9
0
        public void StartFileCopy(string sourceDirectory = null, string destDirectory = null)
        {
            try
            {
                ChoAppSettings appSettings = new ChoAppSettings();
                if (!SettingsFilePath.IsNullOrWhiteSpace())
                {
                    if (!File.Exists(SettingsFilePath))
                    {
                        throw new ArgumentException("Can't find '{0}' settings file.".FormatString(SettingsFilePath));
                    }

                    appSettings.LoadXml(File.ReadAllText(SettingsFilePath));
                }

                ChoConsole.WriteLine();

                ChoRoboCopyManager _roboCopyManager = new ChoRoboCopyManager(SettingsFilePath);
                _roboCopyManager.Status += (sender, e) =>
                {
                    ChoTrace.Write(e.Message);
                    ChoConsole.Write(e.Message, ConsoleColor.Yellow);
                };
                _roboCopyManager.AppStatus += (sender, e) =>
                {
                    ChoTrace.Write(e.Message);
                    ChoConsole.Write(e.Message, ConsoleColor.Yellow);
                };

                _roboCopyManager.Process(appSettings.RoboCopyFilePath, appSettings.GetCmdLineParams(),
                                         appSettings, true);
            }
            catch (ThreadAbortException)
            {
                Console.WriteLine("RoboCopy operation cancelled by user.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("RoboCopy operation failed." + Environment.NewLine + ChoApplicationException.ToString(ex));
            }
        }
Пример #10
0
        public void Load(string[] types)
        {
            if (types == null)
            {
                return;
            }

            foreach (string typeString in types)
            {
                if (typeString.IsNullOrWhiteSpace())
                {
                    continue;
                }

                foreach (string typeText in typeString.SplitNTrim())
                {
                    try
                    {
                        Type type = ChoType.GetType(typeText);
                        if (type == null)
                        {
                            continue;
                        }

                        IChoPlugIn <T> plugIn = ChoActivator.CreateInstance <IChoPlugIn <T> >();
                        if (plugIn != null)
                        {
                            _plugInsObjs.Add(plugIn);
                        }
                    }
                    catch (Exception ex)
                    {
                        ChoTrace.Write(ex);
                    }
                }
            }
        }
Пример #11
0
 public virtual void OnConfigurationStateChanged(object cmdInstruction, object tag)
 {
     try
     {
         if (_eventHandlerList != null)
         {
             int          counter = 0;
             WaitHandle[] handles = new WaitHandle[_eventHandlerList.Count];
             ChoConfigurationStateChangedEventArgs eventData = EventData;
             foreach (ChoConfigurationStateChangedEventHandler callback in _eventHandlerList.ToValuesArray())
             {
                 if (callback != null)
                 {
                     handles[counter++] = callback.BeginInvoke(this, new ChoConfigurationStateChangedEventArgs(_configsectionName, cmdInstruction, tag), null, null).AsyncWaitHandle;
                 }
             }
             WaitHandle.WaitAll(handles);
         }
     }
     catch (Exception ex)
     {
         ChoTrace.Write(ex);
     }
 }
Пример #12
0
        private static void SyncFileWriter(object state)
        {
            FileProfileItem item;

            while (true)
            {
                try
                {
                    object queueObject = null;
                    if (_messageQ.Count > 0)
                    {
                        queueObject = _messageQ.Dequeue();
                    }
                    else
                    {
                        _newMsgArrived.WaitOne();
                        continue;
                    }

                    item = (FileProfileItem)queueObject;
                    if (item.IsFlushMsg)
                    {
                        break;
                    }

                    switch (item.Operation)
                    {
                    case FileOperation.WriteLine:
                    {
                        using (StreamWriter Writer = new StreamWriter(new FileStream(item.FileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)))
                            Writer.WriteLine(item.Msg);
                        break;
                    }

                    case FileOperation.Write:
                    {
                        using (StreamWriter Writer = new StreamWriter(new FileStream(item.FileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)))
                            Writer.Write(item.Msg);
                        break;
                    }

                    case FileOperation.Clean:
                        if (File.Exists(item.FileName))
                        {
                            using (StreamWriter logFile = new StreamWriter(item.FileName))
                            {
                            }
                        }
                        break;

                    case FileOperation.Move:
                        if (File.Exists(item.TargetFileName))
                        {
                            File.Delete(item.TargetFileName);
                        }
                        if (File.Exists(item.FileName))
                        {
                            File.Move(item.FileName, item.TargetFileName);
                        }
                        break;
                    }
                }
                catch (ThreadAbortException)
                {
                    Thread.ResetAbort();
                }
                catch (Exception ex)
                {
                    ChoTrace.Write(ex);
                }
            }
        }