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
|
// <copyright>
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
namespace System.ServiceModel.Channels
{
using System;
using System.Diagnostics;
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Diagnostics;
internal class UdpRequestContext : RequestContextBase
{
private NetworkInterfaceMessageProperty networkInterfaceMessageProperty;
private UdpOutputChannel outputChannel;
private Uri via;
public UdpRequestContext(UdpOutputChannel outputChannel, Message requestMessage)
: base(requestMessage, outputChannel.InternalCloseTimeout, outputChannel.InternalSendTimeout)
{
Fx.Assert(outputChannel != null, "replyChannel can't be null");
this.outputChannel = outputChannel;
if (!NetworkInterfaceMessageProperty.TryGet(requestMessage, out this.networkInterfaceMessageProperty))
{
Fx.Assert("requestMessage must always contain NetworkInterfaceMessageProperty");
}
RemoteEndpointMessageProperty remoteEndpointMessageProperty;
if (!requestMessage.Properties.TryGetValue(RemoteEndpointMessageProperty.Name, out remoteEndpointMessageProperty))
{
Fx.Assert("requestMessage must always contain RemoteEndpointMessageProperty");
}
UriBuilder uriBuilder = new UriBuilder(UdpConstants.Scheme, remoteEndpointMessageProperty.Address, remoteEndpointMessageProperty.Port);
this.via = uriBuilder.Uri;
}
protected override void OnAbort()
{
}
protected override IAsyncResult OnBeginReply(Message message, TimeSpan timeout, AsyncCallback callback, object state)
{
if (message != null)
{
this.SetAddressingInformation(message);
return this.outputChannel.BeginSend(message, timeout, callback, state);
}
else
{
return new CompletedAsyncResult(callback, state);
}
}
protected override void OnClose(TimeSpan timeout)
{
}
protected override void OnEndReply(IAsyncResult result)
{
this.outputChannel.EndSend(result);
}
protected override void OnReply(Message message, TimeSpan timeout)
{
if (message != null)
{
this.SetAddressingInformation(message);
this.outputChannel.Send(message, timeout);
}
}
private void SetAddressingInformation(Message message)
{
this.networkInterfaceMessageProperty.AddTo(message);
message.Properties.Via = this.via;
}
}
}
|