示例#1
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);
            }
        }
示例#2
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(CancellationToken token)
        {
            // {"len":0,"s":"Normal"}\r\n\r\n
            byte[] headerBytes = new byte[24];

            try
            {
                #region Read-Headers

                await _DataStream.ReadAsync(headerBytes, 0, 24, token).ConfigureAwait(false);

                byte[] headerBuffer = new byte[1];

                while (true)
                {
                    byte[] endCheck = headerBytes.Skip(headerBytes.Length - 4).Take(4).ToArray();

                    if ((int)endCheck[3] == 0 &&
                        (int)endCheck[2] == 0 &&
                        (int)endCheck[1] == 0 &&
                        (int)endCheck[0] == 0)
                    {
                        _Logger?.Invoke(_Header + "null header data, peer disconnect detected");
                        return(false);
                    }

                    if ((int)endCheck[3] == 10 &&
                        (int)endCheck[2] == 13 &&
                        (int)endCheck[1] == 10 &&
                        (int)endCheck[0] == 13)
                    {
                        _Logger?.Invoke(_Header + "found header demarcation");
                        break;
                    }

                    await _DataStream.ReadAsync(headerBuffer, 0, 1, token).ConfigureAwait(false);

                    headerBytes = WatsonCommon.AppendBytes(headerBytes, headerBuffer);
                }

                WatsonMessage msg = SerializationHelper.DeserializeJson <WatsonMessage>(Encoding.UTF8.GetString(headerBytes));
                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;

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

                #endregion

                return(true);
            }
            catch (TaskCanceledException)
            {
                _Logger?.Invoke(_Header + "message read canceled");
                return(false);
            }
            catch (OperationCanceledException)
            {
                _Logger?.Invoke(_Header + "message read canceled");
                return(false);
            }
            catch (ObjectDisposedException)
            {
                _Logger?.Invoke(_Header + "socket disposed");
                return(false);
            }
            catch (IOException)
            {
                _Logger?.Invoke(_Header + "non-graceful termination by peer");
                return(false);
            }
            catch (Exception e)
            {
                _Logger?.Invoke(_Header + "exception encountered: " +
                                Environment.NewLine +
                                "Header bytes: " + BitConverter.ToString(headerBytes).Replace("-", String.Empty) +
                                Environment.NewLine +
                                "Exception: " + SerializationHelper.SerializeJson(e, true) +
                                Environment.NewLine);
                return(false);
            }
        }
示例#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

                // {"len":0,"s":"Normal"}\r\n\r\n
                byte[] headerBytes = new byte[24];
                await _DataStream.ReadAsync(headerBytes, 0, 24);

                byte[] headerBuffer = new byte[1];

                while (true)
                {
                    byte[] endCheck = headerBytes.Skip(headerBytes.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 + "found header demarcation");
                        break;
                    }

                    await _DataStream.ReadAsync(headerBuffer, 0, 1);

                    headerBytes = WatsonCommon.AppendBytes(headerBytes, headerBuffer);
                }

                WatsonMessage msg = SerializationHelper.DeserializeJson <WatsonMessage>(Encoding.UTF8.GetString(headerBytes));
                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;

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

                #endregion

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