本文整理汇总了C#中Request.HasOption方法的典型用法代码示例。如果您正苦于以下问题:C# Request.HasOption方法的具体用法?C# Request.HasOption怎么用?C# Request.HasOption使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Request
的用法示例。
在下文中一共展示了Request.HasOption方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleRequest
private void HandleRequest(Request request)
{
Exchange exchange = new Exchange(request, Origin.Remote);
exchange.EndPoint = _proxyEndPoint;
Response response = null;
// ignore the request if it is reset or acknowledge
// check if the proxy-uri is defined
if (request.Type != MessageType.RST && request.Type != MessageType.ACK
&& request.HasOption(OptionType.ProxyUri))
{
// get the response from the cache
response = _cacheResource.GetResponse(request);
// TODO update statistics
//_statsResource.updateStatistics(request, response != null);
}
// check if the response is present in the cache
if (response != null)
{
// link the retrieved response with the request to set the
// parameters request-specific (i.e., token, id, etc)
exchange.SendResponse(response);
return;
}
else
{
// edit the request to be correctly forwarded if the proxy-uri is
// set
if (request.HasOption(OptionType.ProxyUri))
{
try
{
ManageProxyUriRequest(request);
}
catch (Exception)
{
if (log.IsWarnEnabled)
log.Warn("Proxy-uri malformed: " + request.GetFirstOption(OptionType.ProxyUri).StringValue);
exchange.SendResponse(new Response(StatusCode.BadOption));
}
}
// handle the request as usual
if (_proxyCoapResolver != null)
_proxyCoapResolver.ForwardRequest(exchange);
}
}
示例2: SendRequest
/// <inheritdoc/>
public override void SendRequest(INextLayer nextLayer, Exchange exchange, Request request)
{
if (request.HasOption(OptionType.Block2) && request.Block2.NUM > 0)
{
// This is the case if the user has explicitly added a block option
// for random access.
// Note: We do not regard it as random access when the block num is
// 0. This is because the user might just want to do early block
// size negotiation but actually wants to receive all blocks.
if (log.IsDebugEnabled)
log.Debug("Request carries explicit defined block2 option: create random access blockwise status");
BlockwiseStatus status = new BlockwiseStatus(request.ContentFormat);
BlockOption block2 = request.Block2;
status.CurrentSZX = block2.SZX;
status.CurrentNUM = block2.NUM;
status.IsRandomAccess = true;
exchange.ResponseBlockStatus = status;
base.SendRequest(nextLayer, exchange, request);
}
else if (RequiresBlockwise(request))
{
// This must be a large POST or PUT request
if (log.IsDebugEnabled)
log.Debug("Request payload " + request.PayloadSize + "/" + _maxMessageSize + " requires Blockwise.");
BlockwiseStatus status = FindRequestBlockStatus(exchange, request);
Request block = GetNextRequestBlock(request, status);
exchange.RequestBlockStatus = status;
exchange.CurrentRequest = block;
base.SendRequest(nextLayer, exchange, block);
}
else
{
exchange.CurrentRequest = request;
base.SendRequest(nextLayer, exchange, request);
}
}
示例3: ReceiveRequest
/// <inheritdoc/>
public Exchange ReceiveRequest(Request request)
{
/*
* This request could be
* - Complete origin request => deliver with new exchange
* - One origin block => deliver with ongoing exchange
* - Complete duplicate request or one duplicate block (because client got no ACK)
* =>
* if ACK got lost => resend ACK
* if ACK+response got lost => resend ACK+response
* if nothing has been sent yet => do nothing
* (Retransmission is supposed to be done by the retransm. layer)
*/
Exchange.KeyID keyId = new Exchange.KeyID(request.ID, request.Source);
/*
* The differentiation between the case where there is a Block1 or
* Block2 option and the case where there is none has the advantage that
* all exchanges that do not need blockwise transfer have simpler and
* faster code than exchanges with blockwise transfer.
*/
if (!request.HasOption(OptionType.Block1) && !request.HasOption(OptionType.Block2))
{
Exchange exchange = new Exchange(request, Origin.Remote);
Exchange previous = _deduplicator.FindPrevious(keyId, exchange);
if (previous == null)
{
exchange.Completed += OnExchangeCompleted;
return exchange;
}
else
{
if (log.IsInfoEnabled)
log.Info("Duplicate request: " + request);
request.Duplicate = true;
return previous;
}
}
else
{
Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, request.Source);
if (log.IsDebugEnabled)
log.Debug("Looking up ongoing exchange for " + keyUri);
Exchange ongoing;
if (_ongoingExchanges.TryGetValue(keyUri, out ongoing))
{
Exchange prev = _deduplicator.FindPrevious(keyId, ongoing);
if (prev != null)
{
if (log.IsInfoEnabled)
log.Info("Duplicate ongoing request: " + request);
request.Duplicate = true;
}
else
{
// the exchange is continuing, we can (i.e., must) clean up the previous response
if (ongoing.CurrentResponse.Type != MessageType.ACK && !ongoing.CurrentResponse.HasOption(OptionType.Observe))
{
keyId = new Exchange.KeyID(ongoing.CurrentResponse.ID, null);
if (log.IsDebugEnabled)
log.Debug("Ongoing exchange got new request, cleaning up " + keyId);
_exchangesByID.Remove(keyId);
}
}
return ongoing;
}
else
{
// We have no ongoing exchange for that request block.
/*
* Note the difficulty of the following code: The first message
* of a blockwise transfer might arrive twice due to a
* retransmission. The new Exchange must be inserted in both the
* hash map 'ongoing' and the deduplicator. They must agree on
* which exchange they store!
*/
Exchange exchange = new Exchange(request, Origin.Remote);
Exchange previous = _deduplicator.FindPrevious(keyId, exchange);
if (previous == null)
{
if (log.IsDebugEnabled)
log.Debug("New ongoing request, storing " + keyUri + " for " + request);
exchange.Completed += OnExchangeCompleted;
_ongoingExchanges[keyUri] = exchange;
return exchange;
}
else
{
if (log.IsInfoEnabled)
log.Info("Duplicate initial request: " + request);
request.Duplicate = true;
return previous;
}
} // if ongoing
} // if blockwise
//.........这里部分代码省略.........
示例4: ReceiveRequest
/// <inheritdoc/>
public override void ReceiveRequest(INextLayer nextLayer, Exchange exchange, Request request)
{
if (request.HasOption(OptionType.Block1))
{
// This must be a large POST or PUT request
BlockOption block1 = request.Block1;
if (log.IsDebugEnabled)
log.Debug("Request contains block1 option " + block1);
BlockwiseStatus status = FindRequestBlockStatus(exchange, request);
if (block1.NUM == 0 && status.CurrentNUM > 0)
{
// reset the blockwise transfer
if (log.IsDebugEnabled)
log.Debug("Block1 num is 0, the client has restarted the blockwise transfer. Reset status.");
status = new BlockwiseStatus(request.ContentType);
exchange.RequestBlockStatus = status;
}
if (block1.NUM == status.CurrentNUM)
{
if (request.ContentType == status.ContentFormat)
{
status.AddBlock(request.Payload);
}
else
{
Response error = Response.CreatePiggybackedResponse(request, StatusCode.RequestEntityIncomplete);
error.AddOption(new BlockOption(OptionType.Block1, block1.NUM, block1.SZX, block1.M));
error.SetPayload("Changed Content-Format");
request.IsAcknowledged = true;
exchange.CurrentResponse = error;
base.SendResponse(nextLayer, exchange, error);
return;
}
status.CurrentNUM = status.CurrentNUM + 1;
if (block1.M)
{
if (log.IsDebugEnabled)
log.Debug("There are more blocks to come. Acknowledge this block.");
if (request.Type == MessageType.CON)
{
Response piggybacked = Response.CreatePiggybackedResponse(request, StatusCode.Continue);
piggybacked.AddOption(new BlockOption(OptionType.Block1, block1.NUM, block1.SZX, true));
piggybacked.Last = false;
request.IsAcknowledged = true;
exchange.CurrentResponse = piggybacked;
base.SendResponse(nextLayer, exchange, piggybacked);
}
// do not assemble and deliver the request yet
}
else
{
if (log.IsDebugEnabled)
log.Debug("This was the last block. Deliver request");
// Remember block to acknowledge. TODO: We might make this a boolean flag in status.
exchange.Block1ToAck = block1;
// Block2 early negotiation
EarlyBlock2Negotiation(exchange, request);
// Assemble and deliver
Request assembled = new Request(request.Method); // getAssembledRequest(status, request);
AssembleMessage(status, assembled, request);
// assembled.setAcknowledged(true); // TODO: prevents accept from sending ACK. Maybe the resource uses separate...
exchange.Request = assembled;
base.ReceiveRequest(nextLayer, exchange, assembled);
}
}
else
{
// ERROR, wrong number, Incomplete
if (log.IsWarnEnabled)
log.Warn("Wrong block number. Expected " + status.CurrentNUM + " but received " + block1.NUM + ". Respond with 4.08 (Request Entity Incomplete).");
Response error = Response.CreatePiggybackedResponse(request, StatusCode.RequestEntityIncomplete);
error.AddOption(new BlockOption(OptionType.Block1, block1.NUM, block1.SZX, block1.M));
error.SetPayload("Wrong block number");
request.IsAcknowledged = true;
exchange.CurrentResponse = error;
base.SendResponse(nextLayer, exchange, error);
}
}
else if (exchange.Response != null && request.HasOption(OptionType.Block2))
{
// The response has already been generated and the client just wants
// the next block of it
BlockOption block2 = request.Block2;
Response response = exchange.Response;
BlockwiseStatus status = FindResponseBlockStatus(exchange, response);
status.CurrentNUM = block2.NUM;
status.CurrentSZX = block2.SZX;
Response block = GetNextResponseBlock(response, status);
block.Token = request.Token;
block.RemoveOptions(OptionType.Observe);
//.........这里部分代码省略.........
示例5: EarlyBlock2Negotiation
private void EarlyBlock2Negotiation(Exchange exchange, Request request)
{
// Call this method when a request has completely arrived (might have
// been sent in one piece without blockwise).
if (request.HasOption(OptionType.Block2))
{
BlockOption block2 = request.Block2;
if (log.IsDebugEnabled)
log.Debug("Request demands blockwise transfer of response with option " + block2 + ". Create and set new block2 status");
BlockwiseStatus status2 = new BlockwiseStatus(request.ContentType, block2.NUM, block2.SZX);
exchange.ResponseBlockStatus = status2;
}
}
示例6: ResponseProduced
protected void ResponseProduced(Request request, Response response)
{
// check if the proxy-uri is defined
if (request.HasOption(OptionType.ProxyUri))
{
// insert the response in the cache
_cacheResource.CacheResponse(request, response);
}
}
示例7: ForwardRequest
protected override Response ForwardRequest(Request incomingRequest)
{
// check the invariant: the request must have the proxy-uri set
if (!incomingRequest.HasOption(OptionType.ProxyUri))
{
if (log.IsWarnEnabled)
log.Warn("Proxy-uri option not set.");
return new Response(StatusCode.BadOption);
}
// remove the fake uri-path
// FIXME: HACK
incomingRequest.RemoveOptions(OptionType.UriPath);
// create a new request to forward to the requested coap server
Request outgoingRequest = null;
try
{
outgoingRequest = CoapTranslator.GetRequest(incomingRequest);
//outgoingRequest.ResponseQueueEnabled = true;
//outgoingRequest.Token = TokenManager.Instance.AcquireToken();
outgoingRequest.Send();
}
catch (TranslationException ex)
{
if (log.IsWarnEnabled)
log.Warn("Proxy-uri option malformed: " + ex.Message);
return new Response(StatusCode.BadOption);
}
catch (System.IO.IOException ex)
{
if (log.IsWarnEnabled)
log.Warn("Failed to execute request: " + ex.Message);
return new Response(StatusCode.InternalServerError);
}
// receive the response
Response receivedResponse = null;
try
{
receivedResponse = outgoingRequest.WaitForResponse();
}
catch (System.Threading.ThreadInterruptedException ex)
{
if (log.IsWarnEnabled)
log.Warn("Receiving of response interrupted: " + ex.Message);
return new Response(StatusCode.InternalServerError);
}
if (receivedResponse == null)
{
return new Response(StatusCode.GatewayTimeout);
}
else
{
// create the real response for the original request
Response outgoingResponse = CoapTranslator.GetResponse(receivedResponse);
return outgoingResponse;
}
}