コード例 #1
0
        private void ProcessData(byte[] bytes)
        {
            var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

            try
            {
                var json = JObject.Parse(data);
                ApplyFilters(json);
                ProcessJson(json);
                _parsedMessages++;
            }
            catch (Exception ex)
            {
                var jex1 = LogErrors.LogException(string.Format("Invalid JSON: {0}", data), ex);
                if (jex1 != null)
                {
                    ProcessJson(jex1);
                }

                var msg = string.Format("Bad JSON: {0}", data);
                LogManager.GetCurrentClassLogger().Warn(msg, ex);

                _parseErrors++;
            }
        }
コード例 #2
0
        private void HandleNewClient(object client)
        {
            var tcpClient = (TcpClient)client;

            try
            {
                NetworkStream clientStream = tcpClient.GetStream();
                using (var stream = new StreamReader(clientStream))
                {
                    //assume a continuous stream of JSON objects
                    using (var reader = new JsonTextReader(stream)
                    {
                        SupportMultipleContent = true
                    })
                    {
                        while (reader.Read())
                        {
                            if (CancelToken.IsCancellationRequested)
                            {
                                break;
                            }
                            try
                            {
                                JObject json = JObject.Load(reader);
                                ApplyFilters(json);
                                ProcessJson(json);
                                Interlocked.Increment(ref _receivedMessages);
                            }
                            catch (Exception ex)
                            {
                                var jex1 = LogErrors.LogException("Bad Json", ex);
                                if (jex1 != null)
                                {
                                    ProcessJson(jex1);
                                }

                                LogManager.GetCurrentClassLogger().Warn(ex);
                                Interlocked.Increment(ref _errorCount);
                            }
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error(ex);
            }

            tcpClient.Close();
            Finished();
        }