프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / Rhino.Licensing / Discovery / DiscoveryClient.cs @ 42d49521

이력 | 보기 | 이력해설 | 다운로드 (1.24 KB)

1 42d49521 taeseongkim
using System;
2
using System.Net;
3
using System.Net.Sockets;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
namespace Rhino.Licensing.Discovery
8
{
9
	///<summary>
10
	/// Publish the precense of a client over the network
11
	///</summary>
12
	public class DiscoveryClient : IDisposable
13
	{
14
		private readonly byte[] buffer;
15
		private readonly UdpClient udpClient;
16
		private readonly IPEndPoint allHostsGroup;
17
18
		///<summary>
19
		/// Create a new instance
20
		///</summary>
21
		public DiscoveryClient(Guid senderId, Guid userId, string machineName, string userName)
22
		{
23
			buffer = Encoding.UTF8.GetBytes(senderId + Environment.NewLine + userId + Environment.NewLine + machineName + Environment.NewLine + userName);
24
			udpClient = new UdpClient
25
			{
26
				ExclusiveAddressUse = false
27
			};
28
			allHostsGroup = new IPEndPoint(IPAddress.Parse("224.0.0.1"), 12391);
29
		}
30
31
		///<summary>
32
		/// Publish the presence of this node
33
		///</summary>
34
		public void PublishMyPresence()
35
		{
36
			Task.Factory.FromAsync<byte[], int, IPEndPoint, int>(udpClient.BeginSend, udpClient.EndSend, buffer, buffer.Length, allHostsGroup, null)
37
				.ContinueWith(task =>
38
				{
39
					var _ = task.Exception;
40
					// basically just ignoring this error
41
				});
42
		}
43
44
		void IDisposable.Dispose()
45
		{
46
			udpClient.Close();
47
		}
48
	}
49
}
클립보드 이미지 추가 (최대 크기: 500 MB)