コード例 #1
0
ファイル: DTLSChannel.cs プロジェクト: lulzzz/CoAP-CSharp
        /// <summary>
        /// Get an existing session.  If one does not exist then create it and try
        /// to make a connection.
        /// </summary>
        /// <returns>session to use</returns>
        public ISession GetSession(System.Net.EndPoint ep)
        {
            DTLSSession session = null;

            try {
                IPEndPoint ipEndPoint = (IPEndPoint)ep;

                //  Do we already have a session setup for this?

                session = FindSession(ipEndPoint);
                if (session != null)
                {
                    return(session);
                }

                //  No session - create a new one.

                session = new DTLSSession(ipEndPoint, DataReceived, _serverKeys, _userKeys);
                AddSession(session);
                session.TlsEventHandler += MyTlsEventHandler;


                session.Connect(_udpChannel);
            }
            catch {
                ;
            }

            return(session);
        }
コード例 #2
0
ファイル: DTLSChannel.cs プロジェクト: lulzzz/CoAP-CSharp
        public void Send(byte[] data, ISession sessionReceive, System.Net.EndPoint ep)
        {
            try {
                IPEndPoint ipEP = (IPEndPoint)ep;

                DTLSSession session = FindSession(ipEP);
                if (session == null)
                {
                    session = new DTLSSession(ipEP, DataReceived, _serverKeys, _userKeys);
                    session.TlsEventHandler += MyTlsEventHandler;
                    AddSession(session);
                    session.Connect(_udpChannel);
                }
                else if (session != sessionReceive)
                {
                    //  Don't send it
                    return;
                }
                session.Queue.Enqueue(new QueueItem(/*null, */ data));
                session.WriteData();
            }
            catch (Exception e) {
                Console.WriteLine("Error in DTLSClientChannel Sending - " + e.ToString());
            }
        }
コード例 #3
0
        /// <summary>
        /// Send data through the DTLS channel to other side
        /// </summary>
        /// <param name="data">Data to be sent</param>
        /// <param name="sessionReceive">What session was the request on</param>
        /// <param name="ep">Where to send it</param>
        public void Send(byte[] data, ISession sessionReceive, System.Net.EndPoint ep)
        {
            try {
                //  We currently only support IP addresses with this channel.
                //  This is a restriction is enforce from BouncyCastle where
                //  that is the only end point that can be used.

                IPEndPoint ipEndPoint = (IPEndPoint)ep;

                DTLSSession session = FindSession(ipEndPoint);
                if (session == null)
                {
                    _log.Warn("Setup a new session - ");

                    //  Create a new session to send with if we don't already have one

                    session = new DTLSSession(ipEndPoint, DataReceived, _userKey);
                    AddSession(session);

                    session.Connect(_udpChannel);
                }
                else if (session != sessionReceive)
                {
                    _log.Warn("Don't send because the sessions are different");
                    return;
                }

                //  Queue the data onto the session.

                session.Queue.Enqueue(new QueueItem(data));
                session.WriteData();
            }
            catch (Exception e) {
                _log.Error($"Error in DTLSClientChannel Sending - {e}");
                throw;
            }
        }