- Added `setTokenLength(ulong)` which returns `false` if the token length is above 15, else sets the token length (<=15) and returns `true`
- Added `setToken(ubyte[])` which sets the token (and length) and checks that the length is fine

Exceptions

- Added new exception time `CoapException`
This commit is contained in:
Tristan B. Velloza Kildaire 2023-09-12 11:09:14 +02:00
parent 64e22741dd
commit f7f928023d
2 changed files with 35 additions and 0 deletions

9
source/doap/exceptions.d Normal file
View File

@ -0,0 +1,9 @@
module doap.exceptions;
public class CoapException : Exception
{
this(string msg)
{
super(msg);
}
}

View File

@ -2,6 +2,7 @@ module doap.packet;
import doap.types : MessageType;
import doap.codes : Code;
import doap.exceptions : CoapException;
/**
* Represents a CoAP packet
@ -47,6 +48,31 @@ public class CoapPacket
this.type = type;
}
public void setToken(ubyte[] token)
{
if(setTokenLength(token.length))
{
this.token = token;
}
else
{
throw new CoapException("Token length above 15 bytes not allowed");
}
}
private bool setTokenLength(ulong tkl)
{
if(tkl > 15)
{
return false;
}
else
{
this.tokenLen = cast(ubyte)tkl;
return true;
}
}
// public ubyte getVersion()
// {