예제 #1
0
파일: TLSSession.cs 프로젝트: jimsch/TCP
        public void WriteData()
        {
            if (_queue.Count == 0)
            {
                return;
            }
            lock (_writeLock) {
                if (_writing > 0)
                {
                    return;
                }
                _writing = 1;
            }

            while (Queue.Count > 0)
            {
                QueueItem q;
                int       cbRead;
                byte[]    buffer = new byte[1024];

                if (!_queue.TryDequeue(out q))
                {
                    break;
                }

                if (_tlsClient != null)
                {
                    _tlsClient.OfferOutput(q.Data, 0, q.Data.Length);
                    do
                    {
                        cbRead = _tlsClient.ReadOutput(buffer, 0, buffer.Length);

                        _stm.Write(buffer, 0, cbRead);
                    } while (cbRead > 0);
                }
                else if (_tlsServer != null)
                {
                    _tlsServer.OfferOutput(q.Data, 0, q.Data.Length);
                    do
                    {
                        cbRead = _tlsServer.ReadOutput(buffer, 0, buffer.Length);

                        _stm.Write(buffer, 0, cbRead);
                    } while (cbRead > 0);
                }
            }

            lock (_writeLock) {
                _writing = 0;
                if (_queue.Count > 0)
                {
                    WriteData();
                }
            }
        }
예제 #2
0
파일: TLSSession.cs 프로젝트: jimsch/TCP
        /// <summary>
        /// Start up a session on the server side
        /// </summary>
        public void Accept()
        {
            TlsServerProtocol serverProtocol = new TlsServerProtocol(new SecureRandom());

            TlsServer server = new TlsServer(_signingKeys, _clientKeys);

            //  Make sure we do not startup a listing thread as the correct call is always made
            //  byt the DTLS accept protocol.

            _stm = _client.GetStream();
            serverProtocol.Accept(server);

            bool sleep = true;

            while (!server.HandshakeComplete)
            {
                sleep = true;

                if (_stm.DataAvailable)
                {
                    byte[] data   = new byte[1024];
                    int    cbRead = _stm.Read(data, 0, data.Length);
                    Array.Resize(ref data, cbRead);
                    serverProtocol.OfferInput(data);
                    sleep = false;
                }

                int cbToRead = serverProtocol.GetAvailableOutputBytes();
                if (cbToRead != 0)
                {
                    byte[] data   = new byte[cbToRead];
                    int    cbRead = serverProtocol.ReadOutput(data, 0, cbToRead);
                    _stm.Write(data, 0, cbRead);
                    sleep = false;
                }


                if (sleep)
                {
                    Thread.Sleep(100);
                }
            }

            _tlsServer = serverProtocol;
            _authKey   = server.AuthenticationKey;

            _stm.BeginRead(_buffer, 0, _buffer.Length, ReadCallback, this);
        }