本文整理汇总了C#中Target.ToIPEndPointAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Target.ToIPEndPointAsync方法的具体用法?C# Target.ToIPEndPointAsync怎么用?C# Target.ToIPEndPointAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Target
的用法示例。
在下文中一共展示了Target.ToIPEndPointAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendConnectionlessMessageAsync
public async Task SendConnectionlessMessageAsync (Message message, Target target)
{
if (message == null)
throw new ArgumentNullException ("message");
if (target == null)
throw new ArgumentNullException ("target");
if (message.MustBeReliable)
throw new NotSupportedException ("Reliable messages can not be sent connectionlessly");
if (!this.running)
return;
IPEndPoint endPoint = await target.ToIPEndPointAsync().ConfigureAwait (false);
if (endPoint.AddressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6)
throw new NotSupportedException ("endPoint's AddressFamily not supported on this OS.");
else if (endPoint.AddressFamily != AddressFamily.InterNetwork && endPoint.AddressFamily != AddressFamily.InterNetworkV6)
throw new NotSupportedException ("endPoint's AddressFamily not supported on this provider.");
Socket socket = GetSocket (endPoint);
if (message.Header == null)
message.Header = new MessageHeader();
int length;
byte[] buffer = new byte[512];
buffer = this.connectionlessSerializer.GetBytes (message, out length, buffer);
var tcs = new TaskCompletionSource<bool>();
var args = new SocketAsyncEventArgs();
args.SetBuffer (buffer, 0, length);
args.RemoteEndPoint = endPoint;
args.Completed += (sender, e) => {
tcs.SetResult (e.SocketError == SocketError.Success);
Interlocked.Decrement (ref this.pendingAsync);
e.Dispose();
};
try
{
Interlocked.Increment (ref this.pendingAsync);
if (!socket.SendToAsync (args)) {
Interlocked.Decrement (ref this.pendingAsync);
args.Dispose();
}
}
catch (SocketException)
{
Interlocked.Decrement (ref this.pendingAsync);
}
await tcs.Task.ConfigureAwait (false);
}
示例2: ConnectAsync
public async Task<ClientConnectionResult> ConnectAsync (Target target, MessageTypes messageTypes)
{
if (target == null)
throw new ArgumentNullException ("target");
if (!Enum.IsDefined (typeof (MessageTypes), messageTypes))
throw new ArgumentOutOfRangeException ("messageTypes");
IPEndPoint = await target.ToIPEndPointAsync().ConfigureAwait (false);
if (IPEndPoint.AddressFamily != AddressFamily.InterNetwork && IPEndPoint.AddressFamily != AddressFamily.InterNetworkV6)
throw new ArgumentException ("Unsupported endpoint AddressFamily");
var ntcs = new TaskCompletionSource<ClientConnectionResult>();
ThreadPool.QueueUserWorkItem (s =>
{
Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Waiting for pending ({0}) async..", this.pendingAsync));
while (this.pendingAsync > 0 || Interlocked.CompareExchange (ref this.connectTcs, ntcs, null) != null)
Thread.Sleep (0);
int p = Interlocked.Increment (ref this.pendingAsync);
Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Increment pending: {0}", p));
this.serializer = new ClientMessageSerializer (this, this.originalProtocols);
IEnumerable<string> hashAlgs = null;
if (this.localCrypto != null)
hashAlgs = this.localCrypto.SupportedHashAlgs;
Start (messageTypes);
this.socket = this.listener.GetSocket (IPEndPoint);
Timer dtimer = new Timer (100);
dtimer.TimesUp += OnDeliveryTimer;
dtimer.Start();
this.deliveryTimer = dtimer;
Timer t = new Timer (30000);
Timer previousTimer = Interlocked.Exchange (ref this.connectTimer, t);
if (previousTimer != null)
previousTimer.Dispose();
t.AutoReset = false;
t.TimesUp += (sender, args) =>
{
var tcs = this.connectTcs;
if (tcs != null)
tcs.TrySetResult (new ClientConnectionResult (ConnectionResult.ConnectionFailed, null));
Disconnect (ConnectionResult.ConnectionFailed);
t.Dispose();
};
t.Start();
RemoteTarget = target;
SendAsync (new ConnectMessage
{
Protocols = Protocols,
SignatureHashAlgorithms = hashAlgs
}).ContinueWith (st =>
{
int pa = Interlocked.Decrement (ref this.pendingAsync);
Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Decrement pending: {0}", pa));
});
});
return await ntcs.Task.ConfigureAwait (false);
}