Esempio n. 1
0
        private async Task <bool> MessageWriteAsync(ClientMetadata client, WatsonMessage msg)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            byte[] msgBytes = msg.ToBytes();

            await _SendLock.WaitAsync();

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

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

                    await client.SslStream.FlushAsync();
                }
                else
                {
                    throw new ArgumentException("Unknown mode: " + _Mode.ToString());
                }

                return(true);
            }
            catch (Exception)
            {
                Log("*** MessageWriteAsync " + client.IpPort + " disconnected due to exception");
                return(false);
            }
            finally
            {
                _SendLock.Release();
            }
        }
Esempio n. 2
0
        private async Task <bool> MessageWriteAsync(WatsonMessage msg)
        {
            bool disconnectDetected = false;

            try
            {
                #region Check-if-Connected

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

                #endregion

                #region Send-Message

                byte[] msgBytes = msg.ToBytes();

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

                await _SendLock.WaitAsync();

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

                        await _Client.GetStream().FlushAsync();
                    }
                    else if (_Mode == Mode.Ssl)
                    {
                        await _Ssl.WriteAsync(msgBytes, 0, msgBytes.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();
                }
            }
        }