<commit_before>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2011-2013 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.Net;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

using Zongsoft.Services;
using Zongsoft.Services.Composition;

namespace Zongsoft.Communication
{
	/// <summary>
	/// 提供通讯侦听功能的抽象基类。
	/// </summary>
	public abstract class ListenerBase : WorkerBase, IListener, IReceiver
	{
		#region 事件定义
		public event EventHandler<ChannelFailureEventArgs> Failed;
		public event EventHandler<ReceivedEventArgs> Received;
		#endregion

		#region 私有变量
		private readonly object _syncRoot;
		#endregion

		#region 成员变量
		private IReceiver _receiver;
		private IExecutor _executor;
		#endregion

		#region 构造函数
		protected ListenerBase(string name)
		{
			_syncRoot = new object();
		}
		#endregion

		#region 公共属性
		public virtual bool IsListening
		{
			get
			{
				return this.State == WorkerState.Running;
			}
		}

		public IReceiver Receiver
		{
			get
			{
				if(_receiver == null)
				{
					lock(_syncRoot)
					{
						if(_receiver == null)
						{
							_receiver = this.CreateReceiver();

							//绑定接收器的事件
							this.BindReceiver(_receiver, null);
						}
					}
				}

				return _receiver;
			}
			protected set
			{
				if(value == null)
					throw new ArgumentNullException();

				//绑定接收器事件(先取消原有接收器的事件，再挂载新接收器事件)
				this.BindReceiver(value, _receiver);

				_receiver = value;
			}
		}

		public IExecutor Executor
		{
			get
			{
				if(_executor == null)
					System.Threading.Interlocked.CompareExchange(ref _executor, this.CreateExecutor(), null);

				return _executor;
			}
			set
			{
				if(object.ReferenceEquals(_executor, value))
					return;

				//设置属性的成员字段
				_executor = value;
			}
		}<commit_msg>修复Receiver属性不能置空的限制。 :no_good:<commit_after>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2011-2013 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.Net;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

using Zongsoft.Services;
using Zongsoft.Services.Composition;

namespace Zongsoft.Communication
{
	/// <summary>
	/// 提供通讯侦听功能的抽象基类。
	/// </summary>
	public abstract class ListenerBase : WorkerBase, IListener, IReceiver
	{
		#region 事件定义
		public event EventHandler<ChannelFailureEventArgs> Failed;
		public event EventHandler<ReceivedEventArgs> Received;
		#endregion

		#region 私有变量
		private readonly object _syncRoot;
		#endregion

		#region 成员变量
		private IReceiver _receiver;
		private IExecutor _executor;
		#endregion

		#region 构造函数
		protected ListenerBase(string name)
		{
			_syncRoot = new object();
		}
		#endregion

		#region 公共属性
		public virtual bool IsListening
		{
			get
			{
				return this.State == WorkerState.Running;
			}
		}

		public IReceiver Receiver
		{
			get
			{
				if(_receiver == null)
				{
					lock(_syncRoot)
					{
						if(_receiver == null)
						{
							_receiver = this.CreateReceiver();

							//绑定接收器的事件
							this.BindReceiver(_receiver, null);
						}
					}
				}

				return _receiver;
			}
			protected set
			{
				//绑定接收器事件(先取消原有接收器的事件，再挂载新接收器事件)
				this.BindReceiver(value, _receiver);

				_receiver = value;
			}
		}

		public IExecutor Executor
		{
			get
			{
				if(_executor == null)
					System.Threading.Interlocked.CompareExchange(ref _executor, this.CreateExecutor(), null);

				return _executor;
			}
			set
			{
				if(object.ReferenceEquals(_executor, value))
					return;

				//设置属性的成员字段
				_executor = value;
			}
		}
{'fraction_non_alphanumeric': 0.08472727272727272, 'fraction_numerical': 0.0076363636363636364, 'mean_word_length': 3.2380585516178737, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7466367', 'n_tokens_mistral': 2098, 'n_tokens_neox': 1793, 'n_words': 532}