예제 #1
0
 private void OnConnect()
 {
     mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     mSocket.Connect(mIPAddress, mPort);
     mSocket.ReceiveTimeout = TimeOut;
     mSocket.SendTimeout    = TimeOut;
     if (mBaseNetworkStream != null)
     {
         mBaseNetworkStream.Dispose();
     }
     if (mSslStream != null)
     {
         mSslStream.Dispose();
     }
     mBaseNetworkStream                = new Buffers.PipeStream(ClientBufferPool.Pool, this.LittleEndian, this.Encoding);
     mBaseNetworkStream.Socket         = mSocket;
     mBaseNetworkStream.Encoding       = this.Encoding;
     mBaseNetworkStream.LittleEndian   = this.LittleEndian;
     mBaseNetworkStream.FlashCompleted = OnWriterFlash;
     if (this.Packet != null)
     {
         this.Packet           = this.Packet.Clone();
         this.Packet.Completed = this.OnPacketCompleted;
     }
     if (SSL)
     {
         mSslStream = new SslStreamX(ClientBufferPool.Pool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
         var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
         task.Wait();
     }
     mConnected = true;
 }
예제 #2
0
 public void DisConnect()
 {
     lock (this)
     {
         mConnected = false;
         try
         {
             OnDisconnected();
             Token = null;
             if (mSocket != null)
             {
                 TcpClient.CloseSocket(mSocket);
                 mSocket = null;
             }
             mReceiveEventArgs.Dispose();
             mReceiveEventArgs = null;
             mSendEventArgs.Dispose();
             mSendEventArgs = null;
             mProperties.Clear();
             if (mBaseNetworkStream != null)
             {
                 mBaseNetworkStream.Dispose();
                 mBaseNetworkStream = null;
             }
             if (mSslStream != null)
             {
                 mSslStream.Dispose();
                 mSslStream = null;
             }
             object item = DequeueSendMessage();
             while (item != null)
             {
                 if (item is IBuffer)
                 {
                     Buffers.Buffer.Free(((IBuffer)item));
                 }
                 else if (item is IBuffer[])
                 {
                     foreach (IBuffer b in (IBuffer[])item)
                     {
                         Buffers.Buffer.Free(b);
                     }
                 }
                 item = DequeueSendMessage();
             }
         }
         catch
         {
         }
     }
     if (awaitPipeStream.Pending)
     {
         awaitPipeStream.Error(new SocketException((int)SocketError.ConnectionAborted));
     }
     if (mReadMessageAwait.Pending)
     {
         mReadMessageAwait.Error(new SocketException((int)SocketError.ConnectionAborted));
     }
 }
예제 #3
0
 internal virtual Task Disconnect()
 {
     lock (_lock)
     {
         try { _pipe?.WaitForPipeDrain(); } catch (Exception) { }
         try { _pipe?.Dispose(); } catch (Exception) { }
         _pipe = null;
     }
     return(Task.CompletedTask);
 }
예제 #4
0
 private void OnConnect()
 {
     mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     if (LocalEndPoint != null)
     {
         mSocket.Bind(LocalEndPoint);
     }
     mSocket.Connect(mIPAddress, mPort);
     if (LocalEndPoint == null)
     {
         LocalEndPoint = mSocket.LocalEndPoint;
     }
     mSocket.ReceiveTimeout = TimeOut;
     mSocket.SendTimeout    = TimeOut;
     if (mBaseNetworkStream != null)
     {
         mBaseNetworkStream.Dispose();
         mBaseNetworkStream = null;
     }
     if (mSslStream != null)
     {
         mSslStream.Dispose();
         mSslStream = null;
     }
     mBaseNetworkStream = new PipeStream(BufferPool, this.LittleEndian, this.Encoding)
     {
         Socket         = mSocket,
         Encoding       = this.Encoding,
         LittleEndian   = this.LittleEndian,
         FlashCompleted = OnWriterFlash
     };
     if (this.Packet != null)
     {
         this.Packet           = this.Packet.Clone();
         this.Packet.Completed = this.OnPacketCompleted;
     }
     if (SSL)
     {
         mBaseNetworkStream.SSL = true;
         mSslStream             = new SslStreamX(BufferPool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
         var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
         task.Wait();
         mBaseNetworkStream.SSLConfirmed = true;
         mSslStream.SyncData();
     }
     mConnected = true;
     if (mConnected)
     {
         this.Connected?.Invoke(this);
     }
 }
예제 #5
0
        private async Task OnConnect()
        {
            var task = Task.Run(() => CreateSocket());

            if (!task.Wait(ConnectTimeOut))
            {
                mSocket?.Dispose();
                throw new TimeoutException($"connect {mIPAddress}@{mPort} timeout! task status:{task.Status}");
            }
            //if (LocalEndPoint == null)
            //    LocalEndPoint = mSocket.LocalEndPoint;
            mSocket.ReceiveTimeout = TimeOut;
            mSocket.SendTimeout    = TimeOut;
            if (mBaseNetworkStream != null)
            {
                mBaseNetworkStream.Dispose();
                mBaseNetworkStream = null;
            }
            if (mSslStream != null)
            {
                mSslStream.Dispose();
                mSslStream = null;
            }
            mBaseNetworkStream                = new Buffers.PipeStream(BufferPool, this.LittleEndian, this.Encoding);
            mBaseNetworkStream.Socket         = mSocket;
            mBaseNetworkStream.Encoding       = this.Encoding;
            mBaseNetworkStream.LittleEndian   = this.LittleEndian;
            mBaseNetworkStream.FlashCompleted = OnWriterFlash;
            if (this.Packet != null)
            {
                this.Packet           = this.Packet.Clone();
                this.Packet.Completed = this.OnPacketCompleted;
            }
            if (SSL)
            {
                mBaseNetworkStream.SSL = true;
                mSslStream             = new SslStreamX(BufferPool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
                //var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
                //task.Wait();
                await OnSslAuthenticate(mSslStream);

                mBaseNetworkStream.SSLConfirmed = true;
                mSslStream.SyncData(null);
            }
            mConnected = true;
            if (mConnected)
            {
                this.Connected?.Invoke(this);
            }
        }
예제 #6
0
 protected virtual void CleanUp(bool isDisposing)
 {
     if (isDisposing)
     {
         PipeStream.Dispose();
     }
 }
예제 #7
0
 public void Close()
 {
     pipeStream.WaitForPipeDrain();
     pipeStream.Close();
     pipeStream.Dispose();
     pipeStream = null;
 }
예제 #8
0
        public void Cleanup()
        {
            if (StreamOut != null)
            {
                StreamOut.Dispose();
            }
            if (StreamIn != null)
            {
                StreamIn.Dispose();
            }

            if (pipeOut != null)
            {
                pipeOut.Dispose();
            }
            if (pipeIn != null)
            {
                pipeIn.Dispose();
            }

            if (clientProcess != null)
            {
                try
                {
                    clientProcess.Kill();
                    clientProcess.WaitForExit();
                    clientProcess.Close();
                }
                catch (Exception) { }
            }
        }
예제 #9
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            ManualResetEvent ds = Interlocked.Exchange(ref _disposeSignal, null);

            if (ds != null)
            {
                ds.Set();
                ds.Dispose();
            }

            ManualResetEvent wc = Interlocked.Exchange(ref _writeCompleteSignal, null);

            if (wc != null)
            {
                wc.Set();
                wc.Dispose();
            }

            ManualResetEvent wr = Interlocked.Exchange(ref _writeRequiredSignal, null);

            if (wr != null)
            {
                wr.Dispose();
            }

            PipeStream stream = Interlocked.Exchange(ref Stream, null);

            if (stream == null)
            {
                return;
            }
            stream.Dispose();
        }
예제 #10
0
 protected override void Dispose(bool disposing)
 {
     Try.Swallow(() => { pipe?.WaitForPipeDrain(); });
     Try.Catch(() => { pipe?.Dispose(); });
     Try.Catch(() => { pipeServer?.Dispose(); });
     isDisposed = true;
 }
예제 #11
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         _underlying.Dispose();
     }
 }
예제 #12
0
        private void Shutdown(Exception?shutdownReason)
        {
            lock (_shutdownLock)
            {
                if (_socketDisposed)
                {
                    return;
                }

                // Make sure to close the connection only after the _aborted flag is set.
                // Without this, the RequestsCanBeAbortedMidRead test will sometimes fail when
                // a BadHttpRequestException is thrown instead of a TaskCanceledException.
                _socketDisposed = true;

                // shutdownReason should only be null if the output was completed gracefully, so no one should ever
                // ever observe the nondescript ConnectionAbortedException except for connection middleware attempting
                // to half close the connection which is currently unsupported.
                _shutdownReason = shutdownReason ?? new ConnectionAbortedException("The pipe transport's send loop completed gracefully.");

                _logger.LogInformation("Shutdown connection {0}: {1}", ConnectionId, _shutdownReason.Message);

                try
                {
                    _stream.Close();
                }
                catch
                {
                    // Ignore any errors from _stream.Close() since we're tearing down the connection anyway.
                }

                _stream.Dispose();
            }
        }
예제 #13
0
        public void Close()
        {
            CloseConnection = true;

            PipeStream.Close();
            PipeStream.Dispose();
        }
예제 #14
0
        public static unsafe void WriteUTF(Stream stream, Encoding encoding, string value, bool littleEndian)
        {
            PipeStream pipeStream = stream as PipeStream;

            if (pipeStream != null)
            {
                pipeStream.WriteUTF(value);
            }
            else
            {
                CreateByteCached();
                int maxSubLength = mCachedLength / encoding.GetMaxByteCount(1);
                if (maxSubLength > value.Length)
                {
                    int count = (int)encoding.GetBytes(value, 0, value.Length, mBytesCached, 0);
                    if (!littleEndian)
                    {
                        count = BitHelper.SwapInt32(count);
                    }
                    byte[] lendata = BitConverter.GetBytes(count);
                    stream.Write(lendata, 0, 4);
                    stream.Write(mBytesCached, 0, count);
                }
                else
                {
                    PipeStream pipetream = new PipeStream(BufferPoolGroup.DefaultGroup.Next(), littleEndian, encoding);
                    pipetream.WriteUTF(value);
                    Copy(pipeStream, stream);
                    pipetream.Dispose();
                }
            }
        }
예제 #15
0
 public void Dispose()
 {
     _ready = false;
     npine?.Dispose();
     _mutexc?.Close();
     _mutexs?.Close();
 }
예제 #16
0
        private void FlushProxyStream(bool end)
        {
            IBuffer buffer = mProxyStream.GetWriteCacheBufers();

            if (buffer != null)
            {
                if (Request.Session.SSL)
                {
                    mProxyStream.Import(buffer);
                    var length = (int)mProxyStream.Length;
                    var data   = System.Buffers.ArrayPool <byte> .Shared.Rent(length);

                    mProxyStream.Read(data, 0, length);
                    ProxyDataBuffer proxyData = new ProxyDataBuffer(new ArraySegment <byte>(data, 0, length));
                    Request.Session.Send(proxyData);
                }
                else
                {
                    Request.Session.Send(buffer);
                }
            }
            if (end)
            {
                mProxyStream.Dispose();
            }
        }
예제 #17
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         _stream.Dispose();
         _event.Dispose();
     }
 }
예제 #18
0
 public void Dispose()
 {
     if (readablePipe != null)
     {
         readablePipe.Dispose();
     }
     writeablePipe.Dispose();
 }
예제 #19
0
 public override void Close()
 {
     if (_pipeStream != null)
     {
         _pipeStream.Dispose();
         _pipeStream = null;
         RaiseDisconnectedEvent();
     }
 }
예제 #20
0
 /// <summary>
 /// Closing a stream
 /// </summary>
 public void Close()
 {
     if (pipeStream.IsConnected)
     {
         pipeStream.WaitForPipeDrain();
     }
     pipeStream.Close();
     pipeStream.Dispose();
     pipeStream = null;
 }
예제 #21
0
 /// <summary>Close the pipe</summary>
 public void Close()
 {
     if (_pipeStream?.IsConnected ?? false)
     {
         _pipeStream?.WaitForPipeDrain();
     }
     _pipeStream?.Close();
     _pipeStream?.Dispose();
     _pipeStream = null;
 }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _stream?.Dispose();
                _stream = null;

                _speechEngine?.Dispose();
                _speechEngine = null;
            }
        }
예제 #23
0
 public void Stop()
 {
     if (_running)
     {
         _running = false;
         _eventAggregator.GetEvent <LogEvent>().Unsubscribe(ProcessLog);
         _cancellationToken.Cancel(true);
         _stream.Dispose();
         _stream = null;
     }
 }
예제 #24
0
 /// <summary>
 /// Called when a new named pipes client has connected.
 /// </summary>
 /// <param name="pipe">The pipe.</param>
 protected virtual void OnConnectionRequest(PipeStream pipe)
 {
     ConnectionRequest?.Invoke(this, new ResonanceListeningServerConnectionRequestEventArgs <NamedPipesAdapter>(
                                   () =>
     {
         return(new NamedPipesAdapter(pipe));
     }, () =>
     {
         pipe.Dispose();
     }));
 }
예제 #25
0
 public void Cleanup()
 {
     _sslClient.Dispose();
     _sslServer.Dispose();
     _clientIPv4.Dispose();
     _serverIPv4.Dispose();
     _clientIPv6.Dispose();
     _serverIPv6.Dispose();
     _clientPipe.Dispose();
     _serverPipe.Dispose();
 }
예제 #26
0
 public void Dispose()
 {
     try
     {
         readablePipe?.Dispose();
     }
     finally
     {
         writeablePipe.Dispose();
     }
 }
예제 #27
0
        public void ReadOnDisposedReadablePipe_Span_Throws_ObjectDisposedException()
        {
            using (ServerClientPair pair = CreateServerClientPair())
            {
                PipeStream pipe = pair.readablePipe;
                pipe.Dispose();
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                Assert.Throws <ObjectDisposedException>(() => pipe.Read(new Span <byte>(buffer)));
                Assert.Throws <ObjectDisposedException>(() => { pipe.ReadAsync(new Memory <byte>(buffer)); });
            }
        }
예제 #28
0
 public void Disconnect()
 {
     try
     {
         stream.Close();
     }
     finally
     {
         stream.Dispose();
     }
     this.Disconnected.RaiseEvent(this);
 }
예제 #29
0
 /// <inheritdoc/>
 public void Dispose()
 {
     if (!isDisposed)
     {
         if (IsConnected)
         {
             WriteCommand(AADCommand.Disconnect, false);
         }
         stream.Dispose();
         isDisposed = true;
     }
 }
예제 #30
0
 public void DisConnect()
 {
     lock (this)
     {
         mConnected = false;
         try
         {
             if (mSocket != null)
             {
                 TcpClient.CloseSocket(mSocket);
                 mSocket = null;
             }
             mProperties.Clear();
             if (mBaseNetworkStream != null)
             {
                 mBaseNetworkStream.Dispose();
             }
             object item = DequeueSendMessage();
             while (item != null)
             {
                 if (item is IBuffer)
                 {
                     Buffers.Buffer.Free(((IBuffer)item));
                 }
                 else if (item is IBuffer[])
                 {
                     foreach (IBuffer b in (IBuffer[])item)
                     {
                         Buffers.Buffer.Free(b);
                     }
                 }
                 item = DequeueSendMessage();
             }
         }
         catch
         {
         }
     }
 }