示例#1
0
        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());
            }
        }
示例#2
0
        /// <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);
        }
示例#3
0
 private void Accept(DTLSSession session, byte[] message)
 {
     try {
         session.Accept(_udpChannel, message);
     }
     catch (Exception) {
         lock (_sessionList) {
             _sessionList.Remove(session);
         }
     }
 }
示例#4
0
        private void ReceiveData(Object sender, DataReceivedEventArgs e)
        {
            lock (_sessionList) {
                foreach (DTLSSession session in _sessionList)
                {
                    if (e.EndPoint.Equals(session.EndPoint))
                    {
                        session.ReceiveData(sender, e);

                        return;
                    }
                }

                DTLSSession sessionNew = new DTLSSession((IPEndPoint)e.EndPoint, DataReceived, _serverKeys, _userKeys);
                _sessionList.Add(sessionNew);
                new Thread(() => Accept(sessionNew, e.Data)).Start();
            }
        }
        /// <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.

#if SUPPORT_TLS_CWT
                if (CwtTrustKeySet != null)
                {
                    session = new DTLSSession(ipEndPoint, DataReceived, _userKey, CwtTrustKeySet);
                }
                else
                {
#endif
                session = new DTLSSession(ipEndPoint, DataReceived, _userKey);
#if SUPPORT_TLS_CWT
            }
#endif

                session.TlsEventHandler += OnTlsEvent;
                AddSession(session);

                session.Connect(_udpChannel);
            }
            catch {
                _log.Error("Failed to establish a DTLS session");
            }


            return(session);
        }
        /// <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;
            }
        }
示例#7
0
 private static void AddSession(DTLSSession session)
 {
     lock (_sessionList) {
         _sessionList.Add(session);
     }
 }