Exemplo n.º 1
0
        private async Task <WriteResult> SendInternalAsync(string ipPort, long contentLength, Stream stream)
        {
            ClientMetadata client = null;

            lock (_ClientsLock)
            {
                if (!_Clients.ContainsKey(ipPort))
                {
                    return(new WriteResult(WriteResultStatus.ClientNotFound, 0));
                }

                client = _Clients[ipPort];
            }

            WriteResult result = new WriteResult(WriteResultStatus.Success, 0);

            try
            {
                client.WriteSemaphore.Wait(1);

                if (contentLength > 0 && stream != null && stream.CanRead)
                {
                    long bytesRemaining = contentLength;

                    while (bytesRemaining > 0)
                    {
                        byte[] buffer    = new byte[_StreamBufferSize];
                        int    bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                        if (bytesRead > 0)
                        {
                            byte[] data = null;
                            if (bytesRead == buffer.Length)
                            {
                                data = new byte[buffer.Length];
                                Buffer.BlockCopy(buffer, 0, data, 0, buffer.Length);
                            }
                            else
                            {
                                data = new byte[bytesRead];
                                Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                            }

                            if (!_Ssl)
                            {
                                await client.NetworkStream.WriteAsync(data, 0, data.Length);

                                await client.NetworkStream.FlushAsync();
                            }
                            else
                            {
                                await client.SslStream.WriteAsync(data, 0, data.Length);

                                await client.SslStream.FlushAsync();
                            }

                            result.BytesWritten += bytesRead;
                            Stats.SentBytes     += bytesRead;
                            bytesRemaining      -= bytesRead;
                        }
                    }
                }

                return(result);
            }
            catch (Exception)
            {
                result.Status = WriteResultStatus.Disconnected;
                return(result);
            }
            finally
            {
                client.WriteSemaphore.Release();
            }
        }
Exemplo n.º 2
0
        private async Task <WriteResult> SendInternalAsync(long contentLength, Stream stream)
        {
            WriteResult result = new WriteResult(WriteResultStatus.Success, 0);

            try
            {
                await _WriteSemaphore.WaitAsync(1);

                if (contentLength > 0 && stream != null && stream.CanRead)
                {
                    long bytesRemaining = contentLength;

                    while (bytesRemaining > 0)
                    {
                        byte[] buffer    = new byte[_StreamBufferSize];
                        int    bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                        if (bytesRead > 0)
                        {
                            byte[] data = null;
                            if (bytesRead == buffer.Length)
                            {
                                data = new byte[buffer.Length];
                                Buffer.BlockCopy(buffer, 0, data, 0, buffer.Length);
                            }
                            else
                            {
                                data = new byte[bytesRead];
                                Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                            }

                            if (!_Ssl)
                            {
                                _NetworkStream.Write(data, 0, data.Length);
                                await _NetworkStream.FlushAsync();
                            }
                            else
                            {
                                _SslStream.Write(data, 0, data.Length);
                                await _SslStream.FlushAsync();
                            }

                            result.BytesWritten += bytesRead;
                            Stats.SentBytes     += bytesRead;
                            bytesRemaining      -= bytesRead;
                        }
                    }
                }

                return(result);
            }
            catch (Exception)
            {
                result.Status = WriteResultStatus.Disconnected;
                _IsConnected  = false;
                return(result);
            }
            finally
            {
                _WriteSemaphore.Release();
            }
        }