Ejemplo n.º 1
0
        protected virtual void OnReceive(StratumClient client, PooledArraySegment <byte> data)
        {
            // get off of LibUV event-loop-thread immediately
            Task.Run(async() =>
            {
                using (data)
                {
                    JsonRpcRequest request = null;

                    try
                    {
                        // de-serialize
                        _logger.Verbose($"[{client.ConnectionId}] Received request data:\n {StratumConstants.Encoding.GetString(data.Array, 0, data.Size).FormatJson()}");
                        request = client.DeserializeRequest(data);

                        // dispatch
                        if (request != null)
                        {
                            _logger.Debug($"[{client.ConnectionId}] Dispatching request '{request.Method}' [{request.Id}]");
                            await _pool.OnRequestAsync(client, new Timestamped <JsonRpcRequest>(request, MasterClock.Now));
                        }
                        else
                        {
                            _logger.Verbose($"[{client.ConnectionId}] Unable to deserialize request");
                        }
                    }
                    catch (JsonReaderException jsonEx)
                    {
                        // junk received (no valid json)
                        _logger.Error($"[{client.ConnectionId}] Connection json error state: {jsonEx.Message}");
                    }

                    catch (Exception ex)
                    {
                        if (request != null)
                        {
                            _logger.Error(ex, $"[{client.ConnectionId}] Error processing request {request.Method} [{request.Id}]");
                        }
                    }
                }
            });
        }