Ejemplo n.º 1
0
        private bool MessageWrite(ClientMetadata client, WatsonMessage msg, byte[] data)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            int dataLen = 0;

            if (data != null)
            {
                dataLen = data.Length;
            }

            byte[] headerBytes = msg.ToHeaderBytes(dataLen);

            _SendLock.Wait();

            try
            {
                if (_Mode == Mode.Tcp)
                {
                    client.NetworkStream.Write(headerBytes, 0, headerBytes.Length);
                    if (dataLen > 0)
                    {
                        client.NetworkStream.Write(data, 0, dataLen);
                    }
                    client.NetworkStream.Flush();
                }
                else if (_Mode == Mode.Ssl)
                {
                    client.SslStream.Write(headerBytes, 0, headerBytes.Length);
                    if (dataLen > 0)
                    {
                        client.SslStream.Write(data, 0, dataLen);
                    }
                    client.SslStream.Flush();
                }
                else
                {
                    throw new ArgumentException("Unknown mode: " + _Mode.ToString());
                }

                return(true);
            }
            catch (Exception)
            {
                Log("*** MessageWrite " + client.IpPort + " disconnected due to exception");
                return(false);
            }
            finally
            {
                _SendLock.Release();
            }
        }
Ejemplo n.º 2
0
        internal async Task <bool> MessageWriteAsync(WatsonMessage msg, int readStreamBufferSize)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            if (msg.ContentLength > 0)
            {
                if (msg.DataStream == null || !msg.DataStream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            byte[] headerBytes = msg.ToHeaderBytes(msg.ContentLength);

            int  bytesRead;
            long bytesRemaining = msg.ContentLength;

            byte[] buffer = new byte[readStreamBufferSize];

            try
            {
                await _WriteLock.WaitAsync();

                await _TrafficStream.WriteAsync(headerBytes, 0, headerBytes.Length);

                if (msg.ContentLength > 0)
                {
                    while (bytesRemaining > 0)
                    {
                        bytesRead = await msg.DataStream.ReadAsync(buffer, 0, buffer.Length);

                        if (bytesRead > 0)
                        {
                            await _TrafficStream.WriteAsync(buffer, 0, bytesRead);

                            bytesRemaining -= bytesRead;
                        }
                    }
                }

                await _TrafficStream.FlushAsync();

                Common.Log($"MessageWriteAsync sent {Encoding.UTF8.GetString(headerBytes)}");
                return(true);
            }
            catch (Exception e)
            {
                Common.Log($"*** MessageWriteAsync {_IpPort} disconnected due to exception: {e.Message}");
                return(false);
            }
            finally
            {
                _WriteLock.Release();
            }
        }
Ejemplo n.º 3
0
        internal bool MessageWrite(WatsonMessage msg, int readStreamBufferSize)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            if (msg.ContentLength > 0)
            {
                if (msg.DataStream == null || !msg.DataStream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            byte[] headerBytes = msg.ToHeaderBytes(msg.ContentLength);

            int  bytesRead;
            long bytesRemaining = msg.ContentLength;

            byte[] buffer = new byte[readStreamBufferSize];

            try
            {
                _WriteLock.Wait(1);
                _TrafficStream.Write(headerBytes, 0, headerBytes.Length);

                if (msg.ContentLength > 0)
                {
                    while (bytesRemaining > 0)
                    {
                        bytesRead = msg.DataStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead > 0)
                        {
                            _TrafficStream.Write(buffer, 0, bytesRead);
                            bytesRemaining -= bytesRead;
                        }
                    }
                }

                _TrafficStream.Flush();

                return(true);
            }
            catch (Exception e)
            {
                Common.Log($"*** MessageWrite {_IpPort} disconnected due to exception: {e.Message}");
                return(false);
            }
            finally
            {
                _WriteLock.Release();
            }
        }
Ejemplo n.º 4
0
        private bool MessageWrite(WatsonMessage msg)
        {
            bool disconnectDetected = false;
            long dataLen            = 0;

            if (msg.Data != null)
            {
                dataLen = msg.Data.Length;
            }

            try
            {
                if (_Client == null ||
                    !_Client.Connected)
                {
                    disconnectDetected = true;
                    return(false);
                }

                byte[] headerBytes = msg.ToHeaderBytes(dataLen);

                _WriteLock.Wait(1);

                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        _TcpStream.Write(headerBytes, 0, headerBytes.Length);
                        if (msg.Data != null && msg.Data.Length > 0)
                        {
                            _TcpStream.Write(msg.Data, 0, msg.Data.Length);
                        }
                        _TcpStream.Flush();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        _SslStream.Write(headerBytes, 0, headerBytes.Length);
                        if (msg.Data != null && msg.Data.Length > 0)
                        {
                            _SslStream.Write(msg.Data, 0, msg.Data.Length);
                        }
                        _SslStream.Flush();
                    }
                }
                finally
                {
                    _WriteLock.Release();
                }

                return(true);
            }
            catch (Exception e)
            {
                Log(Environment.NewLine +
                    "MessageWrite exception encountered:" +
                    Environment.NewLine +
                    e.ToString() +
                    Environment.NewLine);

                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 5
0
        private bool MessageWrite(long contentLength, Stream stream)
        {
            if (contentLength < 0)
            {
                throw new ArgumentException("Content length must be zero or greater bytes.");
            }
            if (contentLength > 0)
            {
                if (stream == null || !stream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            bool disconnectDetected = false;

            try
            {
                if (_Client == null ||
                    !_Client.Connected)
                {
                    disconnectDetected = true;
                    return(false);
                }

                WatsonMessage msg         = new WatsonMessage(contentLength, stream, Debug);
                byte[]        headerBytes = msg.ToHeaderBytes(contentLength);

                int    bytesRead      = 0;
                long   bytesRemaining = contentLength;
                byte[] buffer         = new byte[_ReadStreamBufferSize];

                _WriteLock.Wait(1);

                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        _TcpStream.Write(headerBytes, 0, headerBytes.Length);

                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = stream.Read(buffer, 0, buffer.Length);
                                if (bytesRead > 0)
                                {
                                    _TcpStream.Write(buffer, 0, bytesRead);
                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        _TcpStream.Flush();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        _SslStream.Write(headerBytes, 0, headerBytes.Length);

                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = stream.Read(buffer, 0, buffer.Length);
                                if (bytesRead > 0)
                                {
                                    _SslStream.Write(buffer, 0, bytesRead);
                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        _SslStream.Flush();
                    }
                }
                finally
                {
                    _WriteLock.Release();
                }

                return(true);
            }
            catch (Exception e)
            {
                Log(Environment.NewLine +
                    "MessageWrite exception encountered:" +
                    Environment.NewLine +
                    e.ToString() +
                    Environment.NewLine);

                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        private async Task <bool> MessageWriteAsync(byte[] data)
        {
            bool disconnectDetected = false;
            long dataLen            = 0;

            if (data != null)
            {
                dataLen = data.Length;
            }

            try
            {
                #region Check-if-Connected

                if (_Client == null)
                {
                    Log("MessageWriteAsync client is null");
                    disconnectDetected = true;
                    return(false);
                }

                #endregion

                #region Send-Message

                WatsonMessage msg         = new WatsonMessage(data, Debug);
                byte[]        headerBytes = msg.ToHeaderBytes(dataLen);

                if (Debug)
                {
                    Log(msg.ToString());
                }

                await _SendLock.WaitAsync();

                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        NetworkStream ns = _Client.GetStream();
                        await ns.WriteAsync(headerBytes, 0, headerBytes.Length);

                        if (data != null && data.Length > 0)
                        {
                            await ns.WriteAsync(data, 0, data.Length);
                        }
                        await ns.FlushAsync();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        await _Ssl.WriteAsync(headerBytes, 0, headerBytes.Length);

                        if (data != null && data.Length > 0)
                        {
                            await _Ssl.WriteAsync(data, 0, data.Length);
                        }
                        await _Ssl.FlushAsync();
                    }
                    else
                    {
                        throw new ArgumentException("Unknown mode: " + _Mode.ToString());
                    }
                }
                finally
                {
                    _SendLock.Release();
                }

                return(true);

                #endregion
            }
            catch (ObjectDisposedException ObjDispInner)
            {
                Log("*** MessageWriteAsync server disconnected (obj disposed exception): " + ObjDispInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (SocketException SockInner)
            {
                Log("*** MessageWriteAsync server disconnected (socket exception): " + SockInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (InvalidOperationException InvOpInner)
            {
                Log("*** MessageWriteAsync server disconnected (invalid operation exception): " + InvOpInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (IOException IOInner)
            {
                Log("*** MessageWriteAsync server disconnected (IO exception): " + IOInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (Exception e)
            {
                LogException("MessageWriteAsync", e);
                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 7
0
        private bool MessageWrite(long contentLength, Stream stream)
        {
            if (contentLength < 0)
            {
                throw new ArgumentException("Content length must be zero or greater bytes.");
            }
            if (contentLength > 0)
            {
                if (stream == null || !stream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            bool disconnectDetected = false;

            try
            {
                #region Check-if-Connected

                if (_Client == null)
                {
                    Log("MessageWrite client is null");
                    disconnectDetected = true;
                    return(false);
                }

                #endregion

                #region Send-Message

                WatsonMessage msg         = new WatsonMessage(contentLength, stream, Debug);
                byte[]        headerBytes = msg.ToHeaderBytes(contentLength);

                int    bytesRead      = 0;
                long   bytesRemaining = contentLength;
                byte[] buffer         = new byte[_ReadStreamBufferSize];

                _SendLock.Wait();
                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        NetworkStream ns = _Client.GetStream();
                        ns.Write(headerBytes, 0, headerBytes.Length);

                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = stream.Read(buffer, 0, buffer.Length);
                                if (bytesRead > 0)
                                {
                                    ns.Write(buffer, 0, bytesRead);
                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        ns.Flush();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        _Ssl.Write(headerBytes, 0, headerBytes.Length);

                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = stream.Read(buffer, 0, buffer.Length);
                                if (bytesRead > 0)
                                {
                                    _Ssl.Write(buffer, 0, bytesRead);
                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        _Ssl.Flush();
                    }
                    else
                    {
                        throw new ArgumentException("Unknown mode: " + _Mode.ToString());
                    }
                }
                finally
                {
                    _SendLock.Release();
                }

                return(true);

                #endregion
            }
            catch (ObjectDisposedException ObjDispInner)
            {
                Log("*** MessageWrite server disconnected (obj disposed exception): " + ObjDispInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (SocketException SockInner)
            {
                Log("*** MessageWrite server disconnected (socket exception): " + SockInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (InvalidOperationException InvOpInner)
            {
                Log("*** MessageWrite server disconnected (invalid operation exception): " + InvOpInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (IOException IOInner)
            {
                Log("*** MessageWrite server disconnected (IO exception): " + IOInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (Exception e)
            {
                LogException("MessageWrite", e);
                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 8
0
        private bool MessageWrite(WatsonMessage msg)
        {
            bool disconnectDetected = false;
            long dataLen            = 0;

            if (msg.Data != null)
            {
                dataLen = msg.Data.Length;
            }

            try
            {
                #region Check-if-Connected

                if (_Client == null)
                {
                    Log("MessageWrite client is null");
                    disconnectDetected = true;
                    return(false);
                }

                #endregion

                #region Send-Message

                byte[] headerBytes = msg.ToHeaderBytes(dataLen);

                _SendLock.Wait();
                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        NetworkStream ns = _Client.GetStream();
                        ns.Write(headerBytes, 0, headerBytes.Length);
                        if (msg.Data != null && msg.Data.Length > 0)
                        {
                            ns.Write(msg.Data, 0, msg.Data.Length);
                        }
                        ns.Flush();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        _Ssl.Write(headerBytes, 0, headerBytes.Length);
                        if (msg.Data != null && msg.Data.Length > 0)
                        {
                            _Ssl.Write(msg.Data, 0, msg.Data.Length);
                        }
                        _Ssl.Flush();
                    }
                    else
                    {
                        throw new ArgumentException("Unknown mode: " + _Mode.ToString());
                    }

                    string logMessage = "MessageWrite sent " + Encoding.UTF8.GetString(headerBytes);
                    if (msg.Data != null && msg.Data.Length > 0)
                    {
                        logMessage += Encoding.UTF8.GetString(msg.Data);
                    }
                    Log(logMessage);
                }
                finally
                {
                    _SendLock.Release();
                }

                return(true);

                #endregion
            }
            catch (ObjectDisposedException ObjDispInner)
            {
                Log("*** MessageWrite server disconnected (obj disposed exception): " + ObjDispInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (SocketException SockInner)
            {
                Log("*** MessageWrite server disconnected (socket exception): " + SockInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (InvalidOperationException InvOpInner)
            {
                Log("*** MessageWrite server disconnected (invalid operation exception): " + InvOpInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (IOException IOInner)
            {
                Log("*** MessageWrite server disconnected (IO exception): " + IOInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (Exception e)
            {
                Log(Common.SerializeJson(e));
                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 9
0
        private async Task <bool> MessageWriteAsync(ClientMetadata client, WatsonMessage msg, long contentLength, Stream stream)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            if (contentLength > 0)
            {
                if (stream == null || !stream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            byte[] headerBytes = msg.ToHeaderBytes(contentLength);

            int  bytesRead      = 0;
            long bytesRemaining = contentLength;

            byte[] buffer = new byte[_ReadStreamBufferSize];

            client.WriteLock.Wait(1);

            try
            {
                if (_Mode == Mode.Tcp)
                {
                    await client.NetworkStream.WriteAsync(headerBytes, 0, headerBytes.Length);

                    if (contentLength > 0)
                    {
                        while (bytesRemaining > 0)
                        {
                            bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                            if (bytesRead > 0)
                            {
                                await client.NetworkStream.WriteAsync(buffer, 0, bytesRead);

                                bytesRemaining -= bytesRead;
                            }
                        }
                    }

                    await client.NetworkStream.FlushAsync();
                }
                else if (_Mode == Mode.Ssl)
                {
                    await client.SslStream.WriteAsync(headerBytes, 0, headerBytes.Length);

                    if (contentLength > 0)
                    {
                        while (bytesRemaining > 0)
                        {
                            bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                            if (bytesRead > 0)
                            {
                                await client.SslStream.WriteAsync(buffer, 0, bytesRead);

                                bytesRemaining -= bytesRead;
                            }
                        }
                    }

                    await client.SslStream.FlushAsync();
                }

                return(true);
            }
            catch (Exception e)
            {
                Log("*** MessageWriteAsync " + client.IpPort + " disconnected due to exception: " + e.Message);
                return(false);
            }
            finally
            {
                client.WriteLock.Release();
            }
        }
Ejemplo n.º 10
0
        private async Task <bool> MessageWriteAsync(Dictionary <object, object> metadata, long contentLength, Stream stream)
        {
            if (!Connected)
            {
                return(false);
            }
            if (contentLength < 0)
            {
                throw new ArgumentException("Content length must be zero or greater bytes.");
            }
            if (contentLength > 0)
            {
                if (stream == null || !stream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            bool disconnectDetected = false;

            try
            {
                if (_Client == null || !_Client.Connected)
                {
                    disconnectDetected = true;
                    return(false);
                }

                WatsonMessage msg         = new WatsonMessage(metadata, contentLength, stream, Debug);
                byte[]        headerBytes = msg.ToHeaderBytes(contentLength);

                int    bytesRead      = 0;
                long   bytesRemaining = contentLength;
                byte[] buffer         = new byte[_ReadStreamBufferSize];

                await _WriteLock.WaitAsync();

                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        // write headers
                        await _TcpStream.WriteAsync(headerBytes, 0, headerBytes.Length);

                        // write metadata
                        if (msg.MetadataBytes != null && msg.MetadataBytes.Length > 0)
                        {
                            await _TcpStream.WriteAsync(msg.MetadataBytes, 0, msg.MetadataBytes.Length);
                        }

                        // write data
                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                                if (bytesRead > 0)
                                {
                                    await _TcpStream.WriteAsync(buffer, 0, bytesRead);

                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        await _TcpStream.FlushAsync();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        // write headers
                        await _SslStream.WriteAsync(headerBytes, 0, headerBytes.Length);

                        // write metadata
                        if (msg.MetadataBytes != null && msg.MetadataBytes.Length > 0)
                        {
                            await _SslStream.WriteAsync(msg.MetadataBytes, 0, msg.MetadataBytes.Length);
                        }

                        // write data
                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                                if (bytesRead > 0)
                                {
                                    await _SslStream.WriteAsync(buffer, 0, bytesRead);

                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        await _SslStream.FlushAsync();
                    }
                    else
                    {
                        throw new ArgumentException("Unknown mode: " + _Mode.ToString());
                    }
                }
                finally
                {
                    _WriteLock.Release();
                }

                return(true);
            }
            catch (Exception e)
            {
                Log(Environment.NewLine +
                    "MessageWrite exception encountered:" +
                    Environment.NewLine +
                    e.ToString() +
                    Environment.NewLine);

                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 11
0
        private async Task <bool> MessageWriteAsync(long contentLength, Stream stream)
        {
            if (contentLength < 0)
            {
                throw new ArgumentException("Content length must be zero or greater bytes.");
            }
            if (contentLength > 0)
            {
                if (stream == null || !stream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            bool disconnectDetected = false;

            try
            {
                if (_Client == null)
                {
                    Log("MessageWriteAsync client is null");
                    disconnectDetected = true;
                    return(false);
                }

                WatsonMessage msg         = new WatsonMessage(contentLength, stream, Debug);
                byte[]        headerBytes = msg.ToHeaderBytes(contentLength);

                int    bytesRead      = 0;
                long   bytesRemaining = contentLength;
                byte[] buffer         = new byte[_ReadStreamBufferSize];

                await _WriteLock.WaitAsync();

                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        await _TcpStream.WriteAsync(headerBytes, 0, headerBytes.Length);

                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                                if (bytesRead > 0)
                                {
                                    await _TcpStream.WriteAsync(buffer, 0, bytesRead);

                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        await _TcpStream.FlushAsync();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        await _SslStream.WriteAsync(headerBytes, 0, headerBytes.Length);

                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                                if (bytesRead > 0)
                                {
                                    await _SslStream.WriteAsync(buffer, 0, bytesRead);

                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        await _SslStream.FlushAsync();
                    }
                    else
                    {
                        throw new ArgumentException("Unknown mode: " + _Mode.ToString());
                    }
                }
                finally
                {
                    _WriteLock.Release();
                }

                string logMessage = "MessageWriteAsync sent " + Encoding.UTF8.GetString(headerBytes);
                Log(logMessage);
                return(true);
            }
            catch (ObjectDisposedException ObjDispInner)
            {
                Log("*** MessageWriteAsync server disconnected (obj disposed exception): " + ObjDispInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (SocketException SockInner)
            {
                Log("*** MessageWriteAsync server disconnected (socket exception): " + SockInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (InvalidOperationException InvOpInner)
            {
                Log("*** MessageWriteAsync server disconnected (invalid operation exception): " + InvOpInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (IOException IOInner)
            {
                Log("*** MessageWriteAsync server disconnected (IO exception): " + IOInner.Message);
                disconnectDetected = true;
                return(false);
            }
            catch (Exception e)
            {
                LogException("MessageWriteAsync", e);
                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 12
0
        private bool MessageWrite(Dictionary <object, object> metadata, long contentLength, Stream stream)
        {
            if (contentLength < 0)
            {
                throw new ArgumentException("Content length must be zero or greater bytes.");
            }
            if (contentLength > 0)
            {
                if (stream == null || !stream.CanRead)
                {
                    throw new ArgumentException("Cannot read from supplied stream.");
                }
            }

            bool disconnectDetected = false;

            try
            {
                if (_Client == null ||
                    !_Client.Connected)
                {
                    disconnectDetected = true;
                    return(false);
                }

                WatsonMessage msg         = new WatsonMessage(metadata, contentLength, stream, Debug);
                byte[]        headerBytes = msg.ToHeaderBytes(contentLength);

                int    bytesRead      = 0;
                long   bytesRemaining = contentLength;
                byte[] buffer         = new byte[_ReadStreamBufferSize];

                _WriteLock.Wait(1);

                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        // write headers
                        _TcpStream.Write(headerBytes, 0, headerBytes.Length);

                        // write metadata
                        if (msg.MetadataBytes != null && msg.MetadataBytes.Length > 0)
                        {
                            _TcpStream.Write(msg.MetadataBytes, 0, msg.MetadataBytes.Length);
                        }

                        // write data
                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = stream.Read(buffer, 0, buffer.Length);
                                if (bytesRead > 0)
                                {
                                    _TcpStream.Write(buffer, 0, bytesRead);
                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        _TcpStream.Flush();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        // write headers
                        _SslStream.Write(headerBytes, 0, headerBytes.Length);

                        // write metadata
                        if (msg.MetadataBytes != null && msg.MetadataBytes.Length > 0)
                        {
                            _SslStream.Write(msg.MetadataBytes, 0, msg.MetadataBytes.Length);
                        }

                        // write data
                        if (contentLength > 0)
                        {
                            while (bytesRemaining > 0)
                            {
                                bytesRead = stream.Read(buffer, 0, buffer.Length);
                                if (bytesRead > 0)
                                {
                                    _SslStream.Write(buffer, 0, bytesRead);
                                    bytesRemaining -= bytesRead;
                                }
                            }
                        }

                        _SslStream.Flush();
                    }
                }
                finally
                {
                    _WriteLock.Release();
                }

                _Stats.SentMessages += 1;
                _Stats.SentBytes    += contentLength;
                return(true);
            }
            catch (Exception e)
            {
                Logger?.Invoke(
                    "[WatsonTcpClient] Message write exception: " +
                    Environment.NewLine +
                    e.ToString() +
                    Environment.NewLine);

                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }
Ejemplo n.º 13
0
        private bool MessageWrite(WatsonMessage msg)
        {
            bool disconnectDetected = false;
            long dataLen            = 0;

            if (msg.Data != null)
            {
                dataLen = msg.Data.Length;
            }

            try
            {
                if (_Client == null ||
                    !_Client.Connected)
                {
                    disconnectDetected = true;
                    return(false);
                }

                byte[] headerBytes = msg.ToHeaderBytes(dataLen);

                _WriteLock.Wait(1);

                try
                {
                    if (_Mode == Mode.Tcp)
                    {
                        // write headers
                        _TcpStream.Write(headerBytes, 0, headerBytes.Length);

                        // write metadata
                        if (msg.MetadataBytes != null && msg.MetadataBytes.Length > 0)
                        {
                            _TcpStream.Write(msg.MetadataBytes, 0, msg.MetadataBytes.Length);
                        }

                        // write data
                        if (msg.Data != null && msg.Data.Length > 0)
                        {
                            _TcpStream.Write(msg.Data, 0, msg.Data.Length);
                        }

                        _TcpStream.Flush();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        // write headers
                        _SslStream.Write(headerBytes, 0, headerBytes.Length);

                        // write metadata
                        if (msg.MetadataBytes != null && msg.MetadataBytes.Length > 0)
                        {
                            _SslStream.Write(msg.MetadataBytes, 0, msg.MetadataBytes.Length);
                        }

                        // write data
                        if (msg.Data != null && msg.Data.Length > 0)
                        {
                            _SslStream.Write(msg.Data, 0, msg.Data.Length);
                        }

                        _SslStream.Flush();
                    }
                }
                finally
                {
                    _WriteLock.Release();
                }

                _Stats.SentMessages += 1;
                _Stats.SentBytes    += dataLen;
                return(true);
            }
            catch (Exception e)
            {
                Logger?.Invoke(
                    "[WatsonTcpClient] Message write exception: " +
                    Environment.NewLine +
                    e.ToString() +
                    Environment.NewLine);

                disconnectDetected = true;
                return(false);
            }
            finally
            {
                if (disconnectDetected)
                {
                    Connected = false;
                    Dispose();
                }
            }
        }