private async Task <object> Get(string Uri, IDtlsCredentials Credentials, params CoapOption[] Options)
        {
            ManualResetEvent Done   = new ManualResetEvent(false);
            ManualResetEvent Error  = new ManualResetEvent(false);
            object           Result = null;

            await this.client.GET(Uri, true, Credentials, (sender, e) =>
            {
                if (e.Ok)
                {
                    Result = e.Message.Decode();
                    Done.Set();
                }
                else
                {
                    Error.Set();
                }
            }, null, Options);

            Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 30000));
            Assert.IsNotNull(Result);

            Console.Out.WriteLine(Result.ToString());

            return(Result);
        }
Exemple #2
0
        /// <summary>
        /// Sends a packet to a remote endpoint.
        /// </summary>
        /// <param name="Packet">Packet to send.</param>
        /// <param name="RemoteEndpoint">Remote endpoint.</param>
        /// <param name="Credentials">Optional credentials.</param>
        /// <param name="Callback">Method to call when operation concludes.</param>
        /// <param name="State">State object to pass on to callback method.</param>
        public void Send(byte[] Packet, IPEndPoint RemoteEndpoint, IDtlsCredentials Credentials,
                         UdpTransmissionEventHandler Callback, object State)
        {
            if (this.dtlsStates.TryGetValue(RemoteEndpoint, out DtlsOverUdpState DtlsState))
            {
                switch (DtlsState.CurrentState)
                {
                case Security.DTLS.DtlsState.SessionEstablished:
                    this.dtls.SendApplicationData(Packet, RemoteEndpoint);
                    break;

                case Security.DTLS.DtlsState.Handshake:
                    DtlsState.AddToQueue(Packet, Callback, State);
                    break;

                case Security.DTLS.DtlsState.Closed:
                case Security.DTLS.DtlsState.Failed:
                case Security.DTLS.DtlsState.Created:
                default:
                    DtlsState.AddToQueue(Packet, Callback, State);
                    this.dtls.StartHandshake(RemoteEndpoint, Credentials);
                    break;
                }
            }
            else
            {
                DtlsState = new DtlsOverUdpState()
                {
                    RemoteEndpoint = RemoteEndpoint,
                    Queue          = new LinkedList <Tuple <byte[], UdpTransmissionEventHandler, object> >(),
                    CurrentState   = Security.DTLS.DtlsState.Handshake
                };

                DtlsState.AddToQueue(Packet, Callback, State);
                this.dtlsStates.Add(RemoteEndpoint, DtlsState);

                this.dtls.StartHandshake(RemoteEndpoint, Credentials);
            }
        }
        /// <summary>
        /// Contains a reference to an LWM2M Server.
        /// </summary>
        /// <param name="RemoteEndpoint">Host name or IP address of Remote endpoint.</param>
        /// <param name="Port">Port number.</param>
        /// <param name="Credentials">Optional credentials to use to establish DTLS session, if required.</param>
        public Lwm2mServerReference(string RemoteEndpoint, int Port, IDtlsCredentials Credentials)
        {
            this.remoteEndpoint = RemoteEndpoint;
            this.credentials    = Credentials;

            StringBuilder sb = new StringBuilder();

            sb.Append("coap");
            if (this.credentials != null)
            {
                sb.Append('s');
            }
            sb.Append("://");
            sb.Append(this.remoteEndpoint);

            if (this.credentials == null)
            {
                if (Port != CoapEndpoint.DefaultCoapPort)
                {
                    sb.Append(':');
                    sb.Append(Port.ToString());
                }
            }
            else
            {
                if (Port != CoapEndpoint.DefaultCoapsPort)
                {
                    sb.Append(':');
                    sb.Append(Port.ToString());
                }
            }

            sb.Append('/');

            this.uri = sb.ToString();
        }
 /// <summary>
 /// Contains a reference to an LWM2M Server.
 /// </summary>
 /// <param name="RemoteEndpoint">Host name or IP address of Remote endpoint.</param>
 /// <param name="Credentials">Optional credentials to use to establish DTLS session, if required.</param>
 public Lwm2mServerReference(string RemoteEndpoint, IDtlsCredentials Credentials)
     : this(RemoteEndpoint, Credentials == null ? CoapEndpoint.DefaultCoapPort : CoapEndpoint.DefaultCoapsPort, Credentials)
 {
 }
 protected virtual void SetupClientServer()
 {
     this.server            = new CoapEndpoint(new int[] { CoapEndpoint.DefaultCoapPort }, null, null, null, false, true, new TextWriterSniffer(Console.Out, BinaryPresentationMethod.Hexadecimal));
     this.client            = new CoapEndpoint(new int[] { CoapEndpoint.DefaultCoapPort + 2 }, null, null, null, true, false);
     this.clientCredentials = null;
 }