Exemple #1
0
 internal static byte[] ReadMessageDataAsync(WatsonMessage msg, int bufferLen)
 {
     if (msg == null)
     {
         throw new ArgumentNullException(nameof(msg));
     }
     if (msg.ContentLength == 0)
     {
         return(new byte[0]);
     }
     return(WatsonCommon.ReadFromStreamAsync(msg.DataStream, msg.ContentLength, bufferLen));
 }
Exemple #2
0
        internal static async Task <byte[]> ReadMessageDataAsync(WatsonMessage msg, int bufferLen)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }
            if (msg.ContentLength == 0)
            {
                return(new byte[0]);
            }

            byte[]       msgData = null;
            MemoryStream ms      = new MemoryStream();

            if (msg.Compression == CompressionType.None)
            {
                msgData = await WatsonCommon.ReadFromStreamAsync(msg.DataStream, msg.ContentLength, bufferLen);
            }
            else if (msg.Compression == CompressionType.Deflate)
            {
                using (DeflateStream ds = new DeflateStream(msg.DataStream, CompressionMode.Decompress, true))
                {
                    msgData = WatsonCommon.ReadStreamFully(ds);
                }
            }
            else if (msg.Compression == CompressionType.Gzip)
            {
                using (GZipStream gs = new GZipStream(msg.DataStream, CompressionMode.Decompress, true))
                {
                    msgData = WatsonCommon.ReadStreamFully(gs);
                }
            }
            else
            {
                throw new InvalidOperationException("Unknown compression type: " + msg.Compression.ToString());
            }

            return(msgData);
        }
Exemple #3
0
        /// <summary>
        /// Build the Message object from data that awaits in a NetworkStream or SslStream.
        /// </summary>
        /// <returns>True if successful.</returns>
        internal async Task <bool> BuildFromStream()
        {
            try
            {
                #region Read-Headers

                byte[] buffer = new byte[0];

                while (true)
                {
                    byte[] data = await WatsonCommon.ReadFromStreamAsync(_DataStream, 1, _ReadStreamBuffer);

                    if (data != null && data.Length == 1)
                    {
                        buffer = WatsonCommon.AppendBytes(buffer, data);
                        if (buffer.Length >= 4)
                        {
                            byte[] endCheck = buffer.Skip(buffer.Length - 4).Take(4).ToArray();
                            if ((int)endCheck[3] == 10 &&
                                (int)endCheck[2] == 13 &&
                                (int)endCheck[1] == 10 &&
                                (int)endCheck[0] == 13)
                            {
                                _Logger?.Invoke(_Header + "ReadHeaders found header demarcation");
                                break;
                            }
                        }
                    }
                }

                WatsonMessage msg = SerializationHelper.DeserializeJson <WatsonMessage>(Encoding.UTF8.GetString(buffer));
                ContentLength    = msg.ContentLength;
                PresharedKey     = msg.PresharedKey;
                Status           = msg.Status;
                Metadata         = msg.Metadata;
                SyncRequest      = msg.SyncRequest;
                SyncResponse     = msg.SyncResponse;
                SenderTimestamp  = msg.SenderTimestamp;
                Expiration       = msg.Expiration;
                ConversationGuid = msg.ConversationGuid;
                Compression      = msg.Compression;

                _Logger?.Invoke(_Header + "BuildFromStream header processing complete" + Environment.NewLine + Encoding.UTF8.GetString(buffer).Trim());

                #endregion

                return(true);
            }
            catch (IOException)
            {
                _Logger?.Invoke(_Header + "BuildStream IOexception, disconnect assumed");
                return(false);
            }
            catch (SocketException)
            {
                _Logger?.Invoke(_Header + "BuildStream SocketException, disconnect assumed");
                return(false);
            }
            catch (ObjectDisposedException)
            {
                _Logger?.Invoke(_Header + "BuildStream ObjectDisposedException, disconnect assumed");
                return(false);
            }
            catch (Exception e)
            {
                _Logger?.Invoke(_Header + "BuildStream exception: " +
                                Environment.NewLine +
                                SerializationHelper.SerializeJson(e, true) +
                                Environment.NewLine);
                return(false);
            }
        }