本文整理汇总了C#中IncomingMessage.AssertPayload方法的典型用法代码示例。如果您正苦于以下问题:C# IncomingMessage.AssertPayload方法的具体用法?C# IncomingMessage.AssertPayload怎么用?C# IncomingMessage.AssertPayload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IncomingMessage
的用法示例。
在下文中一共展示了IncomingMessage.AssertPayload方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessMessageCore
private void ProcessMessageCore(IncomingMessage message)
{
if(message.Message.Payload is VersionPayload)
{
var version = message.AssertPayload<VersionPayload>();
var connectedToSelf = version.Nonce == Nonce;
if(message.Node != null && connectedToSelf)
{
NodeServerTrace.ConnectionToSelfDetected();
message.Node.Disconnect();
return;
}
if(message.Node == null)
{
var remoteEndpoint = version.AddressFrom;
if(!remoteEndpoint.Address.IsRoutable(AllowLocalPeers))
{
//Send his own endpoint
remoteEndpoint = new IPEndPoint(((IPEndPoint)message.Socket.RemoteEndPoint).Address, Network.DefaultPort);
}
if(IsConnectedTo(remoteEndpoint))
{
NodeServerTrace.Warning("Ongoing connection with " + remoteEndpoint + " detected, aborting the incoming connection attempt");
Utils.SafeCloseSocket(message.Socket);
return;
}
var node = new Node(new Peer(PeerOrigin.Advertised, new NetworkAddress()
{
Endpoint = remoteEndpoint,
Time = DateTimeOffset.UtcNow
}), this, message.Socket, version);
if(connectedToSelf)
{
node.SendMessage(CreateVersionPayload(node.Peer, ExternalEndpoint, Version));
NodeServerTrace.ConnectionToSelfDetected();
node.Disconnect();
return;
}
CancellationTokenSource cancel = new CancellationTokenSource();
cancel.CancelAfter(TimeSpan.FromSeconds(10.0));
try
{
node.RespondToHandShake(cancel.Token);
AddNode(node);
}
catch(OperationCanceledException ex)
{
NodeServerTrace.Error("The remote node did not respond fast enough (10 seconds) to the handshake completion, dropping connection", ex);
node.Disconnect();
throw;
}
catch(Exception)
{
node.Disconnect();
throw;
}
}
}
}
示例2: ProcessMessageCore
private void ProcessMessageCore(IncomingMessage message)
{
if(message.Message.Payload is VersionPayload)
{
var version = message.AssertPayload<VersionPayload>();
var connectedToSelf = version.Nonce == Nonce;
if(message.Node != null && connectedToSelf)
{
NodeServerTrace.ConnectionToSelfDetected();
message.Node.DisconnectAsync();
return;
}
if(message.Node == null)
{
var remoteEndpoint = version.AddressFrom;
if(!remoteEndpoint.Address.IsRoutable(AllowLocalPeers))
{
//Send his own endpoint
remoteEndpoint = new IPEndPoint(((IPEndPoint)message.Socket.RemoteEndPoint).Address, Network.DefaultPort);
}
var peer = new NetworkAddress()
{
Endpoint = remoteEndpoint,
Time = DateTimeOffset.UtcNow
};
var node = new Node(peer, Network, CreateNodeConnectionParameters(), message.Socket, version);
if(connectedToSelf)
{
node.SendMessage(CreateNodeConnectionParameters().CreateVersion(node.Peer.Endpoint, Network));
NodeServerTrace.ConnectionToSelfDetected();
node.Disconnect();
return;
}
CancellationTokenSource cancel = new CancellationTokenSource();
cancel.CancelAfter(TimeSpan.FromSeconds(10.0));
try
{
ConnectedNodes.Add(node);
node.StateChanged += node_StateChanged;
node.RespondToHandShake(cancel.Token);
}
catch(OperationCanceledException ex)
{
NodeServerTrace.Error("The remote node did not respond fast enough (10 seconds) to the handshake completion, dropping connection", ex);
node.DisconnectAsync();
throw;
}
catch(Exception)
{
node.DisconnectAsync();
throw;
}
}
}
var messageReceived = MessageReceived;
if(messageReceived != null)
messageReceived(this, message);
}