本文整理汇总了C#中Response.HasOption方法的典型用法代码示例。如果您正苦于以下问题:C# Response.HasOption方法的具体用法?C# Response.HasOption怎么用?C# Response.HasOption使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Response
的用法示例。
在下文中一共展示了Response.HasOption方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendResponse
/// <inheritdoc/>
public void SendResponse(Exchange exchange, Response response)
{
if (response.ID == Message.None)
response.ID = System.Threading.Interlocked.Increment(ref _currentID) % (1 << 16);
/*
* The response is a CON or NON or ACK and must be prepared for these
* - CON => ACK / RST // we only care to stop retransmission
* - NON => RST // we only care for observe
* - ACK => nothing!
* If this response goes lost, we must be prepared to get the same
* CON/NON request with same MID again. We then find the corresponding
* exchange and the ReliabilityLayer resends this response.
*/
// If this is a CON notification we now can forget all previous NON notifications
if (response.Type == MessageType.CON || response.Type == MessageType.ACK)
{
ObserveRelation relation = exchange.Relation;
if (relation != null)
{
RemoveNotificatoinsOf(relation);
}
}
// Blockwise transfers are identified by URI and remote endpoint
if (response.HasOption(OptionType.Block2))
{
Request request = exchange.CurrentRequest;
Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, response.Destination);
// Observe notifications only send the first block, hence do not store them as ongoing
if (exchange.ResponseBlockStatus != null && !response.HasOption(OptionType.Observe))
{
// Remember ongoing blockwise GET requests
if (Utils.Put(_ongoingExchanges, keyUri, exchange) == null)
{
if (log.IsDebugEnabled)
log.Debug("Ongoing Block2 started late, storing " + keyUri + " for " + request);
}
else
{
if (log.IsDebugEnabled)
log.Debug("Ongoing Block2 continued, storing " + keyUri + " for " + request);
}
}
else
{
if (log.IsDebugEnabled)
log.Debug("Ongoing Block2 completed, cleaning up " + keyUri + " for " + request);
Exchange exc;
_ongoingExchanges.TryRemove(keyUri, out exc);
}
}
// Insert CON and NON to match ACKs and RSTs to the exchange
// Do not insert ACKs and RSTs.
if (response.Type == MessageType.CON || response.Type == MessageType.NON)
{
Exchange.KeyID keyID = new Exchange.KeyID(response.ID, null);
_exchangesByID[keyID] = exchange;
}
// Only CONs and Observe keep the exchange active
if (response.Type != MessageType.CON && response.Last)
{
exchange.Complete = true;
}
}
示例2: ReceiveResponse
/// <inheritdoc/>
public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Response response)
{
if (!response.HasOption(OptionType.Block1) && !response.HasOption(OptionType.Block2))
{
// There is no block1 or block2 option, therefore it is a normal response
exchange.Response = response;
base.ReceiveResponse(nextLayer, exchange, response);
return;
}
BlockOption block1 = response.Block1;
if (block1 != null)
{
// TODO: What if request has not been sent blockwise (server error)
if (log.IsDebugEnabled)
log.Debug("Response acknowledges block " + block1);
BlockwiseStatus status = exchange.RequestBlockStatus;
if (!status.Complete)
{
// TODO: the response code should be CONTINUE. Otherwise deliver
// Send next block
Int32 currentSize = 1 << (4 + status.CurrentSZX);
Int32 nextNum = status.CurrentNUM + currentSize / block1.Size;
if (log.IsDebugEnabled)
log.Debug("Send next block num = " + nextNum);
status.CurrentNUM = nextNum;
status.CurrentSZX = block1.SZX;
Request nextBlock = GetNextRequestBlock(exchange.Request, status);
if (nextBlock.Token == null)
nextBlock.Token = response.Token; // reuse same token
exchange.CurrentRequest = nextBlock;
base.SendRequest(nextLayer, exchange, nextBlock);
// do not deliver response
}
else if (!response.HasOption(OptionType.Block2))
{
// All request block have been acknowledged and we receive a piggy-backed
// response that needs no blockwise transfer. Thus, deliver it.
base.ReceiveResponse(nextLayer, exchange, response);
}
else
{
if (log.IsDebugEnabled)
log.Debug("Response has Block2 option and is therefore sent blockwise");
}
}
BlockOption block2 = response.Block2;
if (block2 != null)
{
BlockwiseStatus status = FindResponseBlockStatus(exchange, response);
if (block2.NUM == status.CurrentNUM)
{
// We got the block we expected :-)
status.AddBlock(response.Payload);
Int32? obs = response.Observe;
if (obs.HasValue)
status.Observe = obs.Value;
// notify blocking progress
exchange.Request.FireResponding(response);
if (status.IsRandomAccess)
{
// The client has requested this specifc block and we deliver it
exchange.Response = response;
base.ReceiveResponse(nextLayer, exchange, response);
}
else if (block2.M)
{
if (log.IsDebugEnabled)
log.Debug("Request the next response block");
// TODO: If this is a notification, do we have to use
// another token now?
Request request = exchange.Request;
Int32 num = block2.NUM + 1;
Int32 szx = block2.SZX;
Boolean m = false;
Request block = new Request(request.Method);
block.Token = response.Token;
block.SetOptions(request.GetOptions());
block.Destination = request.Destination;
block.Type = request.Type; // NON could make sense over SMS or similar transports
block.SetOption(new BlockOption(OptionType.Block2, num, szx, m));
status.CurrentNUM = num;
// to make it easier for Observe, we do not re-use the Token
//if (!response.HasOption(OptionType.Observe))
//{
// block.Token = request.Token;
//}
// make sure not to use Observe for block retrieval
block.RemoveOptions(OptionType.Observe);
//.........这里部分代码省略.........
示例3: SendResponse
/// <inheritdoc/>
public override void SendResponse(INextLayer nextLayer, Exchange exchange, Response response)
{
BlockOption block1 = exchange.Block1ToAck;
if (block1 != null)
exchange.Block1ToAck = null;
if (RequiresBlockwise(exchange, response))
{
// This must be a large response to a GET or POST request (PUT?)
if (log.IsDebugEnabled)
log.Debug("Response payload " + response.PayloadSize + "/" + _maxMessageSize + " requires Blockwise");
BlockwiseStatus status = FindResponseBlockStatus(exchange, response);
Response block = GetNextResponseBlock(response, status);
block.Type = response.Type; // This is only true for the first block
if (block1 != null) // in case we still have to ack the last block1
block.SetOption(block1);
if (block.Token == null)
block.Token = exchange.Request.Token;
if (response.HasOption(OptionType.Observe))
{
// the ACK for the first block should acknowledge the whole notification
exchange.CurrentResponse = response;
}
else
{
exchange.CurrentResponse = block;
}
base.SendResponse(nextLayer, exchange, block);
}
else
{
if (block1 != null)
response.SetOption(block1);
exchange.CurrentResponse = response;
base.SendResponse(nextLayer, exchange, response);
}
}
示例4: SendResponse
/// <inheritdoc/>
public void SendResponse(Exchange exchange, Response response)
{
if (response.ID == Message.None)
response.ID = System.Threading.Interlocked.Increment(ref _currentID) % (1 << 16);
/*
* The response is a CON or NON or ACK and must be prepared for these
* - CON => ACK/RST // we only care to stop retransmission
* - NON => RST // we don't care
* - ACK => nothing!
* If this response goes lost, we must be prepared to get the same
* CON/NON request with same MID again. We then find the corresponding
* exchange and the ReliabilityLayer resends this response.
*/
if (response.Destination == null)
throw new InvalidOperationException("Response has no destination set");
// If this is a CON notification we now can forget all previous NON notifications
if (response.Type == MessageType.CON || response.Type == MessageType.ACK)
{
ObserveRelation relation = exchange.Relation;
if (relation != null)
{
RemoveNotificatoinsOf(relation);
}
}
if (response.HasOption(OptionType.Block2))
{
Request request = exchange.Request;
Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, response.Destination);
if (exchange.ResponseBlockStatus != null && !response.HasOption(OptionType.Observe))
{
// Remember ongoing blockwise GET requests
if (log.IsDebugEnabled)
log.Debug("Ongoing Block2 started, storing " + keyUri + "\nOngoing " + request + "\nOngoing " + response);
_ongoingExchanges[keyUri] = exchange;
}
else
{
if (log.IsDebugEnabled)
log.Debug("Ongoing Block2 completed, cleaning up " + keyUri + "\nOngoing " + request + "\nOngoing " + response);
_ongoingExchanges.Remove(keyUri);
}
}
// Insert CON and NON to match ACKs and RSTs to the exchange
// Do not insert ACKs and RSTs.
if (response.Type == MessageType.CON || response.Type == MessageType.NON)
{
Exchange.KeyID keyID = new Exchange.KeyID(response.ID, response.Destination);
_exchangesByID[keyID] = exchange;
}
if (response.Type == MessageType.ACK || response.Type == MessageType.NON)
{
// Since this is an ACK or NON, the exchange is over with sending this response.
if (response.Last)
{
exchange.Complete = true;
}
} // else this is a CON and we need to wait for the ACK or RST
}
示例5: ReceiveResponse
/// <inheritdoc/>
public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Response response)
{
if (response.HasOption(OptionType.Observe))
{
if (exchange.Request.IsCancelled)
{
// The request was canceled and we no longer want notifications
if (log.IsDebugEnabled)
log.Debug("ObserveLayer rejecting notification for canceled Exchange");
EmptyMessage rst = EmptyMessage.NewRST(response);
SendEmptyMessage(nextLayer, exchange, rst);
// Matcher sets exchange as complete when RST is sent
}
else
{
PrepareReregistration(exchange, response, msg => SendRequest(nextLayer, exchange, msg));
base.ReceiveResponse(nextLayer, exchange, response);
}
}
else
{
// No observe option in response => always deliver
base.ReceiveResponse(nextLayer, exchange, response);
}
}
示例6: GetNextResponseBlock
private Response GetNextResponseBlock(Response response, BlockwiseStatus status)
{
Response block;
Int32 szx = status.CurrentSZX;
Int32 num = status.CurrentNUM;
if (response.HasOption(OptionType.Observe))
{
// a blockwise notification transmits the first block only
block = response;
}
else
{
block = new Response(response.StatusCode);
block.Destination = response.Destination;
block.Token = response.Token;
block.SetOptions(response.GetOptions());
block.TimedOut += (o, e) => response.IsTimedOut = true;
}
Int32 payloadSize = response.PayloadSize;
Int32 currentSize = 1 << (4 + szx);
Int32 from = num * currentSize;
if (payloadSize > 0 && payloadSize > from)
{
Int32 to = Math.Min((num + 1) * currentSize, response.PayloadSize);
Int32 length = to - from;
Byte[] blockPayload = new Byte[length];
Boolean m = to < response.PayloadSize;
block.SetBlock2(szx, m, num);
// crop payload -- do after calculation of m in case block==response
Array.Copy(response.Payload, from, blockPayload, 0, length);
block.Payload = blockPayload;
// do not complete notifications
block.Last = !m && !response.HasOption(OptionType.Observe);
status.Complete = !m;
}
else
{
block.AddOption(new BlockOption(OptionType.Block2, num, szx, false));
block.Last = true;
status.Complete = true;
}
return block;
}