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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime;
using System.Threading;
#if DEBUG
using System.Diagnostics;
#endif
class UdpSocket
{
int openCount;
int timeToLive;
int pendingReceiveCount;
Socket socket;
#if DEBUG
StackTrace disposeStack;
#endif
public UdpSocket(Socket socket, int interfaceIndex)
{
Fx.Assert(socket != null, "Socket argument cannot be null");
this.openCount = 0;
this.pendingReceiveCount = 0;
this.ThisLock = new object();
this.socket = socket;
this.InterfaceIndex = interfaceIndex;
this.timeToLive = socket.Ttl;
}
enum TransferDirection : byte
{
Send,
Receive,
}
public AddressFamily AddressFamily
{
get
{
return this.socket.AddressFamily;
}
}
public int PendingReceiveCount
{
get
{
return this.pendingReceiveCount;
}
}
//value of UdpConstants.Defaults.InterfaceIndex (-1) if not a multicast socket
internal int InterfaceIndex
{
get;
private set;
}
internal bool IsDisposed
{ get { return this.openCount < 0; } }
internal object ThisLock
{
get;
private set;
}
//must be called under a lock in receive loop
public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, ref EndPoint remoteEndPoint, AsyncCallback callback, object state)
{
UdpUtility.ValidateBufferBounds(buffer, offset, size);
ThrowIfNotOpen();
bool success = false;
IAsyncResult asyncResult = null;
try
{
this.pendingReceiveCount++;
asyncResult = new ReceiveFromAsyncResult(
this.socket,
new ArraySegment<byte>(buffer, offset, size),
remoteEndPoint,
size - offset,
this.timeToLive,
callback,
state);
success = true;
return asyncResult;
}
finally
{
if (!success)
{
this.pendingReceiveCount--;
}
}
}
public void Close()
{
bool cleanup = false;
lock (this.ThisLock)
{
if (this.IsDisposed)
{
return;
}
//UdpUtility.CreateListenSocketsOnUniquePort can create a socket and then close it without ever calling
//UdpSocket.Open() if it fails to bind on both IPv4 and IPv6. If this happens, then openCount will still be at zero.
if (this.openCount > 0)
{
this.openCount--;
}
if (this.openCount == 0)
{
cleanup = true;
this.openCount = -1;
}
}
if (cleanup)
{
#if DEBUG
if (!Fx.FastDebug)
{
disposeStack = new StackTrace();
}
#endif
//non-zero sendTimeout causes the socket to block on a receive while looking for an EOF, which will never come
this.socket.Close(0);
}
}
//must be called under a lock in receive loop
public ArraySegment<byte> EndReceiveFrom(IAsyncResult result, ref EndPoint remoteEndPoint)
{
this.pendingReceiveCount--;
return ReceiveFromAsyncResult.End(result, ref remoteEndPoint);
}
internal EndPoint CreateIPAnyEndPoint()
{
if (this.AddressFamily == AddressFamily.InterNetwork)
{
return new IPEndPoint(IPAddress.Any, 0);
}
else
{
return new IPEndPoint(IPAddress.IPv6Any, 0);
}
}
public void Open()
{
lock (this.ThisLock)
{
if (this.IsDisposed)
{
throw FxTrace.Exception.AsError(new ObjectDisposedException("UdpSocket"));
}
this.openCount++;
}
}
public int SendTo(byte[] buffer, int offset, int size, EndPoint remoteEndPoint)
{
ThrowIfNotOpen();
UdpUtility.ValidateBufferBounds(buffer, offset, size);
try
{
int count = this.socket.SendTo(buffer, offset, size, SocketFlags.None, remoteEndPoint);
Fx.Assert(count == size, "Bytes sent on the wire should be the same as the bytes specified");
return count;
}
catch (SocketException socketException)
{
throw FxTrace.Exception.AsError(ConvertNetworkError(socketException, size - offset, TransferDirection.Send, this.timeToLive));
}
}
public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, EndPoint remoteEndPoint, AsyncCallback callback, object state)
{
ThrowIfNotOpen();
UdpUtility.ValidateBufferBounds(buffer, offset, size);
return new SendToAsyncResult(this.socket, buffer, offset, size, remoteEndPoint, this.timeToLive, callback, state);
}
public int EndSendTo(IAsyncResult result)
{
return SendToAsyncResult.End(result);
}
static Exception ConvertNetworkError(SocketException socketException, ReceiveFromAsyncResult result)
{
return ConvertNetworkError(socketException, result.MessageSize, TransferDirection.Receive, result.TimeToLive);
}
// size: sending => the size of the data being sent
// Receiving => the max message size we can receive
// remoteEndPoint: remote endpoint reported when error occured
static Exception ConvertNetworkError(SocketException socketException, int size, TransferDirection direction, int timeToLive)
{
Exception result = null;
if (socketException.ErrorCode == UnsafeNativeMethods.ERROR_INVALID_HANDLE)
{
//This would likely indicate a bug in our ref-counting
//for instance, a channel is closing the socket multiple times...
Fx.Assert("The socket appears to have been closed unexpectedly. This probably indicates incorrect ref counting (i.e. a channel is closing the socket multiple times)");
result = new CommunicationObjectAbortedException(socketException.Message, socketException);
}
else
{
string errorMessage;
switch (socketException.SocketErrorCode)
{
case SocketError.MessageSize: //10040
errorMessage = (direction == TransferDirection.Send ? SR.UdpMaxMessageSendSizeExceeded(size) : SR.MaxReceivedMessageSizeExceeded(size));
Exception inner = new QuotaExceededException(errorMessage, socketException);
result = new ProtocolException(errorMessage, inner);
break;
case SocketError.NetworkReset: //10052
//ICMP: Time Exceeded (TTL expired)
//see http://tools.ietf.org/html/rfc792
result = new CommunicationException(SR.IcmpTimeExpired(timeToLive), socketException);
break;
case SocketError.ConnectionReset: //10054
//ICMP: Destination Unreachable (target host/port/etc not reachable)
//see http://tools.ietf.org/html/rfc792
result = new CommunicationException(SR.IcmpDestinationUnreachable, socketException);
break;
default:
errorMessage = (direction == TransferDirection.Send ? SR.UdpSendException : SR.UdpReceiveException);
result = new CommunicationException(errorMessage, socketException);
break;
}
}
Fx.Assert(result != null, "we should never return null");
return result;
}
void ThrowIfDisposed()
{
if (this.IsDisposed)
{
throw FxTrace.Exception.AsError(new ObjectDisposedException(this.GetType().ToString()));
}
}
void ThrowIfNotOpen()
{
ThrowIfDisposed();
if (this.openCount == 0)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ObjectNotOpen));
}
}
class SendToAsyncResult : TypedAsyncResult<int>
{
Socket socket;
int offset;
int size;
int timeToLive;
static AsyncCallback onSendToComplete = Fx.ThunkCallback(OnSendToComplete);
public SendToAsyncResult(Socket socket, byte[] buffer, int offset, int size, EndPoint remoteEndPoint, int timeToLive, AsyncCallback callback, object state)
: base(callback, state)
{
this.socket = socket;
this.offset = offset;
this.size = size;
this.timeToLive = timeToLive;
int count = 0;
try
{
IAsyncResult socketAsyncResult = this.socket.BeginSendTo(buffer, offset, size, SocketFlags.None, remoteEndPoint, onSendToComplete, this);
if (!socketAsyncResult.CompletedSynchronously)
{
return;
}
count = this.socket.EndSendTo(socketAsyncResult);
}
catch (SocketException socketException)
{
throw FxTrace.Exception.AsError(ConvertNetworkError(socketException, this.size - this.offset, TransferDirection.Send, this.timeToLive));
}
this.Complete(count, true);
}
static void OnSendToComplete(IAsyncResult result)
{
if (result.CompletedSynchronously)
{
return;
}
SendToAsyncResult thisPtr = (SendToAsyncResult)result.AsyncState;
Exception completionException = null;
int count = 0;
try
{
count = thisPtr.socket.EndSendTo(result);
}
catch (SocketException socketException)
{
completionException = ConvertNetworkError(socketException, thisPtr.size - thisPtr.offset, TransferDirection.Send, thisPtr.timeToLive);
}
catch (Exception ex)
{
if (Fx.IsFatal(ex))
{
throw;
}
completionException = ex;
}
if (completionException != null)
{
thisPtr.Complete(false, completionException);
}
else
{
thisPtr.Complete(count, false);
}
}
}
class ReceiveFromAsyncResult : TypedAsyncResult<ArraySegment<byte>>
{
static AsyncCallback onReceiveMessageFromCallback = Fx.ThunkCallback(new AsyncCallback(OnReceiveMessageFrom));
Socket socket;
public ReceiveFromAsyncResult(Socket socket, ArraySegment<byte> buffer, EndPoint remoteEndPoint, int messageSize, int timeToLive, AsyncCallback userCallback, object userState) :
base(userCallback, userState)
{
this.RemoteEndPoint = remoteEndPoint;
this.MessageSize = messageSize;
this.socket = socket;
this.Buffer = buffer;
this.TimeToLive = timeToLive;
ArraySegment<byte> data = default(ArraySegment<byte>);
try
{
IAsyncResult socketAsyncResult = this.socket.BeginReceiveFrom(this.Buffer.Array,
this.Buffer.Offset,
this.Buffer.Count,
SocketFlags.None,
ref remoteEndPoint,
onReceiveMessageFromCallback,
this);
if (!socketAsyncResult.CompletedSynchronously)
{
return;
}
data = EndReceiveFrom(socketAsyncResult);
}
catch (SocketException socketException)
{
throw FxTrace.Exception.AsError(UdpSocket.ConvertNetworkError(socketException, this));
}
Complete(data, true);
}
public EndPoint RemoteEndPoint
{
get;
private set;
}
public int TimeToLive
{
get;
private set;
}
//used when generating error messages for the user...
internal int MessageSize
{
get;
private set;
}
ArraySegment<byte> Buffer
{
get;
set;
}
public static ArraySegment<byte> End(IAsyncResult result, ref EndPoint remoteEndPoint)
{
ArraySegment<byte> data = TypedAsyncResult<ArraySegment<byte>>.End(result);
ReceiveFromAsyncResult receiveFromResult = (ReceiveFromAsyncResult)result;
remoteEndPoint = receiveFromResult.RemoteEndPoint;
return data;
}
static void OnReceiveMessageFrom(IAsyncResult result)
{
if (result.CompletedSynchronously)
{
return;
}
ReceiveFromAsyncResult asyncResult = (ReceiveFromAsyncResult)result.AsyncState;
Exception completionException = null;
ArraySegment<byte> data = default(ArraySegment<byte>);
try
{
data = asyncResult.EndReceiveFrom(result);
}
catch (SocketException socketException)
{
completionException = UdpSocket.ConvertNetworkError(socketException, asyncResult);
}
catch (Exception exception)
{
if (Fx.IsFatal(exception))
{
throw;
}
completionException = exception;
}
if (completionException != null)
{
asyncResult.Complete(false, completionException);
}
else
{
asyncResult.Complete(data, false);
}
}
ArraySegment<byte> EndReceiveFrom(IAsyncResult result)
{
EndPoint remoteEndPoint = this.RemoteEndPoint;
int count = this.socket.EndReceiveFrom(result, ref remoteEndPoint);
this.RemoteEndPoint = remoteEndPoint;
return new ArraySegment<byte>(this.Buffer.Array, this.Buffer.Offset, count);
}
}
}
}
|