Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UdpClient.cs « System.Net.Sockets « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15e921abfc9ac0b7d61497d9be6f02dbae7c70cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// System.Net.Sockets.UdpClient.cs
//
// Author:
//    Gonzalo Paniagua Javier <gonzalo@ximian.com>
//
// Copyright (C) Ximian, Inc. http://www.ximian.com
//

using System;
using System.Net;

namespace System.Net.Sockets
{
	public class UdpClient : IDisposable
	{
		private bool disposed = false;
		private bool active = false;
		private Socket socket;
		private IPEndPoint localEP;
		
#region Constructors
		public UdpClient ()
		{
			localEP = new IPEndPoint (IPAddress.Any, 0);
			InitSocket ();
		}

		public UdpClient (int port)
		{
			// IPEndPoint throws ArgumentException when port is invalid
			localEP = new IPEndPoint (IPAddress.Any, port);
			InitSocket ();
		}

		public UdpClient (IPEndPoint localEP)
		{
			if (localEP == null)
				throw new ArgumentNullException ("IPEndPoint cannot be null");

			this.localEP = localEP;
			InitSocket ();
		}

		public UdpClient (string hostname, int port)
		{
			if (hostname == null)
				throw new ArgumentNullException ("hostname cannot be null");

			if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
				throw new ArgumentException ("Invalid port");

			localEP = new IPEndPoint (IPAddress.Any, 0);
			InitSocket ();
			Connect (hostname, port);
		}

		private void InitSocket ()
		{
			active = false;
			socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			socket.Bind (localEP);
		}

#endregion // Constructors
#region Public methods
#region Close
		public void Close ()
		{
			((IDisposable) this).Dispose ();	
		}
#endregion
#region Connect
		public void Connect (IPEndPoint endPoint)
		{
			try {
				socket.Connect (endPoint);
				active = true;
			} finally {
				CheckDisposed ();
			}
		}

		public void Connect (IPAddress addr, int port)
		{
			Connect (new IPEndPoint (addr, port));
		}

		public void Connect (string hostname, int port)
		{
			Connect (new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
		}
#endregion
#region Multicast methods
		public void DropMulticastGroup (IPAddress multicastAddr)
		{
			try {
				socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
							new MulticastOption (multicastAddr));
			} finally {
				CheckDisposed ();
			}
		}

		public void JoinMulticastGroup (IPAddress multicastAddr)
		{
			try {
				socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
							new MulticastOption (multicastAddr));
			} finally {
				CheckDisposed ();
			}
		}

		public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
		{
			JoinMulticastGroup (multicastAddr);
			try {
				socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
							timeToLive);
			} finally {
				CheckDisposed ();
			}
		}
#endregion
#region Data I/O
		public byte [] Receive (ref IPEndPoint remoteEP)
		{
			try {
				if (remoteEP == null)
					throw new ArgumentNullException ("remoteEP cannot be null");

				// Length of the array for receiving data??
				byte [] recBuffer;
				int available = socket.Available;

				recBuffer = new byte [1024]; // FIXME: any suggestions?
				EndPoint endPoint = (EndPoint) remoteEP;
				int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
				if (dataRead < recBuffer.Length)
					return CutArray (recBuffer, dataRead);

				return recBuffer;
			} finally {
				CheckDisposed ();
			}
		}

		public int Send (byte [] dgram, int bytes)
		{
			try {
				if (dgram == null)
					throw new ArgumentNullException ("dgram is null");

				byte [] realDgram;
				if (dgram.Length <= bytes)
					realDgram = dgram;
				else
					realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);

				// the socket should be connected already, so I use Send instead of SendTo
				return socket.Send (realDgram);
			} finally {
				CheckDisposed ();
			}
		}

		public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
		{
			try {
				if (dgram == null)
					throw new ArgumentNullException ("dgram is null");

				byte [] realDgram;
				if (dgram.Length <= bytes)
					realDgram = dgram;
				else
					realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);

				// the socket should not be connected
				return socket.SendTo (realDgram, endPoint);
			} finally {
				CheckDisposed ();
			}
		}

		public int Send (byte [] dgram, int bytes, string hostname, int port)
		{
			return Send (dgram, bytes, 
				     new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
		}

		private byte [] CutArray (byte [] orig, int length)
		{
			byte [] newArray = new byte [length];
			Array.Copy (orig, 0, newArray, 0, length);

			return newArray;
		}
#endregion
#region Properties
		protected bool Active {
			get { return active; }
			set { active = value; }
		}

		protected Socket Client {
			get { return socket; }
			set { socket = value; }
		}
#endregion

/* 
// commented because in the ms.net implementation these are not overriden. -- LP
#region Overrides
		public override bool Equals (object obj)
		{
			if (obj is UdpClient)
				return (((UdpClient) obj).socket  == socket &&
					((UdpClient) obj).localEP == localEP);

			return false;
		}

		public override int GetHashCode ()
		{
			return (socket.GetHashCode () + localEP.GetHashCode () + (active ? 1 : 0));
		}
#endregion
*/

#region Disposing
		void IDisposable.Dispose ()
		{
			Dispose (true);
			GC.SuppressFinalize (this);
		}

		protected virtual void Dispose (bool disposing)
		{
			if (disposed) 
				return;
			disposed = true;
			if (disposing) {
				// release managed resources
				localEP = null;
			}			
			// release unmanaged resources
			Socket s = socket;
			socket = null;
			if (s != null)
				s.Close ();
		}
		
		~UdpClient ()
		{
			Dispose (false);
		}
		
		private void CheckDisposed ()
		{
			if (disposed)
				throw new ObjectDisposedException (GetType().FullName);
		}		
#endregion
#endregion
	}
}