本文整理汇总了C#中Message.GetOptions方法的典型用法代码示例。如果您正苦于以下问题:C# Message.GetOptions方法的具体用法?C# Message.GetOptions怎么用?C# Message.GetOptions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Message
的用法示例。
在下文中一共展示了Message.GetOptions方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Serialize
protected override void Serialize(DatagramWriter writer, Message msg, Int32 code)
{
// create datagram writer to encode options
DatagramWriter optWriter = new DatagramWriter();
Int32 optionCount = 0;
Int32 lastOptionNumber = 0;
IEnumerable<Option> opts = msg.GetOptions();
IList<Option> options = opts as IList<Option> ?? new List<Option>(opts);
if (msg.Token != null && msg.Token.Length > 0 && !msg.HasOption(OptionType.Token))
options.Add(Option.Create(OptionType.Token, msg.Token));
Utils.InsertionSort(options, delegate(Option o1, Option o2)
{
return GetOptionNumber(o1.Type).CompareTo(GetOptionNumber(o2.Type));
});
foreach (Option opt in options)
{
if (opt.IsDefault)
continue;
Int32 optNum = GetOptionNumber(opt.Type);
Int32 optionDelta = optNum - lastOptionNumber;
/*
* The Option Jump mechanism is used when the delta to the next option
* number is larger than 14.
*/
if (optionDelta > MaxOptionDelta)
{
/*
* For the formats that include an Option Jump Value, the actual
* addition to the current Option number is computed as follows:
* Delta = ((Option Jump Value) + N) * 8 where N is 2 for the
* one-byte version and N is 258 for the two-byte version.
*/
if (optionDelta < 30)
{
optWriter.Write(0xF1, SingleOptionJumpBits);
optionDelta -= 15;
}
else if (optionDelta < 2064)
{
Int32 optionJumpValue = (optionDelta / 8) - 2;
optionDelta -= (optionJumpValue + 2) * 8;
optWriter.Write(0xF2, SingleOptionJumpBits);
optWriter.Write(optionJumpValue, SingleOptionJumpBits);
}
else if (optionDelta < 526359)
{
optionDelta = Math.Min(optionDelta, 526344); // Limit to avoid overflow
Int32 optionJumpValue = (optionDelta / 8) - 258;
optionDelta -= (optionJumpValue + 258) * 8;
optWriter.Write(0xF3, SingleOptionJumpBits);
optWriter.Write(optionJumpValue, 2 * SingleOptionJumpBits);
}
else
{
throw new Exception("Option delta too large. Actual delta: " + optionDelta);
}
}
// write option delta
optWriter.Write(optionDelta, OptionDeltaBits);
// write option length
Int32 length = opt.Length;
if (length <= MaxOptionLengthBase)
{
// use option length base field only to encode
// option lengths less or equal than MAX_OPTIONLENGTH_BASE
optWriter.Write(length, OptionLengthBaseBits);
}
else if (length <= 1034)
{
/*
* When the Length field is set to 15, another byte is added as
* an 8-bit unsigned integer whose value is added to the 15,
* allowing option value lengths of 15-270 bytes. For option
* lengths beyond 270 bytes, we reserve the value 255 of an
* extension byte to mean
* "add 255, read another extension byte". Options that are
* longer than 1034 bytes MUST NOT be sent
*/
optWriter.Write(15, OptionLengthBaseBits);
Int32 rounds = (length - 15) / 255;
for (Int32 i = 0; i < rounds; i++)
{
optWriter.Write(255, OptionLengthExtendedBits);
}
Int32 remainingLength = length - ((rounds * 255) + 15);
optWriter.Write(remainingLength, OptionLengthExtendedBits);
}
else
{
throw new Exception("Option length larger than allowed 1034. Actual length: " + length);
}
// write option value
//.........这里部分代码省略.........
示例2: AssembleMessage
private void AssembleMessage(BlockwiseStatus status, Message message, Message last)
{
// The assembled request will contain the options of the last block
message.ID = last.ID;
message.Source = last.Source;
message.Token = last.Token;
message.Type = last.Type;
message.SetOptions(last.GetOptions());
Int32 length = 0;
foreach (Byte[] block in status.Blocks)
length += block.Length;
Byte[] payload = new Byte[length];
Int32 offset = 0;
foreach (Byte[] block in status.Blocks)
{
Array.Copy(block, 0, payload, offset, block.Length);
offset += block.Length;
}
message.Payload = payload;
}
示例3: DisplayMessage
//show messgae
private void DisplayMessage(Message msg)
{
try
{
IList<Option> options = msg.GetOptions();
Boolean ifObserve = false;
string responseCoap;
responseCoap = "HTTP/1.1 200 OK\r\n";
responseCoap += "Content-Type: text/html; charset=UTF-8\r\n\r\n";
//Judge if the resouce can be Observed
foreach (Option o in options)
{
if (o.Type.Equals(OptionType.MaxAge))
{
ifObserve = true;
break;
}
}
//if can be Observed
if (ifObserve)
{
responseCoap += "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
+ "<html><head><title>Observe</title>"
+"<script type=\"text/javascript\"> var ws; function connectSocketServer() {"
+ "if (document.getElementById('btnSend').value == \"Observe\"){"
+ " document.getElementById('messageBoard').innerHTML = \"* Connecting to server ..<br/>\"; document.getElementById('btnSend').value =\"Cancel\";"
+ " ws = new WebSocket('ws://59.64.38.126:912');"
+ " var script_el=document.createElement(\"script\"); script_el.type=\"text/javascript\";"
+ " script_el.src=window.location.href + \"?observehost\";"
+ " document.getElementById('messageBoard').appendChild(script_el);"
+ " ws.onmessage = function (evt) { document.getElementById('messageBoard').innerHTML+=\"# \" + evt.data + \"<br />\"; };"
+ " ws.onopen = function () { document.getElementById('messageBoard').innerHTML +=\"* Connection open<br/>\";};"
+ " ws.onclose = function (){ document.getElementById('messageBoard').innerHTML += \"* Connection closed<br/>\"; };"
+ "} else{ window.location.reload(); }"
+ "}</script>"
+"</head><body><div id=\"messageBoard\">"
+ msg.PayloadString
+ "</div><br/><input type=\"button\" id=\"btnSend\" value=\"Observe\" onclick=\"connectSocketServer();\" /></body></html>";
}
else
{
responseCoap += msg.PayloadString;
}
Byte[] payloadBytes = msg.Payload;
if (MediaType.IsImage(msg.ContentType))
{
// show image
// doesn't work :(
//webBrowserPayload.DocumentStream = new MemoryStream(payloadBytes);
// save to a temp file
File.WriteAllBytes(_tempImagePath, payloadBytes);
String path = "file:///" + _tempImagePath.Replace('\\', '/');
responseCoap = String.Format("<html><head><title></title></head><body><img src=\"{0}\" alt=\"\"></body></html>", path);
}
byte[] bytes = Encoding.UTF8.GetBytes(responseCoap);
this.clientSocket.Send(bytes);
this.clientSocket.Disconnect(false);
this.clientSocket.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}