コード例 #1
0
        internal async Task ProcessMessage(IRpcMessage message)
        {
            try
            {
                switch (message.Type)
                {
                case RpcMessageType.Notification:
                    var notification = (RpcNotification)message;

                    if (string.IsNullOrWhiteSpace(notification.Method))
                    {
                        break;
                    }

                    if (notification.Method == "report")
                    {
                        var trade = notification.Payload.ToObject <HitbtcOrderV2>();

                        ArbitragePair pair;
                        if (_pairs.TryGetValue(trade.symbol, out pair))
                        {
                            SocketTradeHandler notificationHandler;
                            if (_handlers.TryGetValue(trade.symbol, out notificationHandler))
                            {
                                await notificationHandler(this, pair, new Match(trade));
                            }
                        }
                    }
                    break;

                case RpcMessageType.Request:
                    throw new Exception("not handling requests");

                case RpcMessageType.Response:
                    var response = (RpcResponse)message;

                    TaskCompletionSource <JToken> completion;
                    if (!_requests.TryGetValue(response.Id, out completion))
                    {
                        throw new Exception("Unknown request id");
                    }

                    if (response.Error != null)
                    {
                        completion.SetException(new RpcException(response.Error));
                        return;
                    }

                    completion.SetResult(response.Result);
                    break;

                default:
                    throw new NotSupportedException(string.Format("Unsupported message type '{0}'", message.Type));
                }
            }
            catch (Exception e)
            {
                logger.Error(e);
            }
        }
コード例 #2
0
        internal async Task ProcessMessage(IRpcMessage message)
        {
            try
            {
                switch (message.Type)
                {
                case RpcMessageType.Notification:
                    var notification = (RpcNotification)message;

                    if (string.IsNullOrWhiteSpace(notification.Method))
                    {
                        break;
                    }

                    RpcNotificationHandler notificationHandler;
                    if (!_handlers.TryGetValue(notification.Method, out notificationHandler))
                    {
                        throw new Exception(string.Format("Unknown method '{0}'", notification.Method));
                    }

                    await notificationHandler(this, notification.Payload);

                    break;

                case RpcMessageType.Request:
                    throw new Exception("not handling requests request");

                case RpcMessageType.Response:
                    var response = (RpcResponse)message;

                    TaskCompletionSource <JToken> completion;
                    if (!_requests.TryGetValue(response.Id, out completion))
                    {
                        throw new Exception("Unknown request id");
                    }

                    if (response.Error != null)
                    {
                        completion.SetException(new RpcException(response.Error));
                        return;
                    }

                    completion.SetResult(response.Result);
                    break;

                default:
                    throw new NotSupportedException(string.Format("Unsupported message type '{0}'", message.Type));
                }
            }
            catch (Exception e)
            {
                DispatchError(e);
            }
        }
コード例 #3
0
ファイル: RpcMessage.cs プロジェクト: Rohansi/Rohmote
        public static string Write(IRpcMessage message)
        {
            string type;
            switch (message.Type)
            {
                case RpcMessageType.Request:
                    type = "req";
                    break;
                case RpcMessageType.Response:
                    type = "res";
                    break;
                default:
                    throw new NotSupportedException(string.Format("Unsupported message type '{0}'", message.Type));
            }

            var data = type + JsonConvert.SerializeObject(message);
            return data;
        }
コード例 #4
0
        public static string Write(IRpcMessage message)
        {
            var data = JsonConvert.SerializeObject(message);

            return(data);
        }
コード例 #5
0
ファイル: RpcProcessor.cs プロジェクト: Rohansi/Rohmote
        internal async Task ProcessMessage(IRpcMessage message)
        {
            try
            {
                switch (message.Type)
                {
                    case RpcMessageType.Request:
                        var request = (RpcRequest)message;

                        try
                        {
                            RpcHandler handler;
                            if (!_handlers.TryGetValue(request.Method, out handler))
                                throw new Exception(string.Format("Unknown method '{0}'", request.Method));

                            var result = await handler(this, request.Parameters);

                            Send(new RpcResponse
                            {
                                Id = request.Id,
                                Result = result,
                                Error = null
                            });
                        }
                        catch (Exception e)
                        {
                            DispatchError(e);

                            Send(new RpcResponse
                            {
                                Id = request.Id,
                                Result = null,
                                Error = ErrorString(e)
                            });
                        }
                        break;

                    case RpcMessageType.Response:
                        var response = (RpcResponse)message;

                        TaskCompletionSource<JToken> completion;
                        if (!_requests.TryGetValue(response.Id, out completion))
                            throw new Exception("Unknown request id");

                        if (response.Error != null)
                        {
                            completion.SetException(new RpcException(response.Error));
                            return;
                        }

                        completion.SetResult(response.Result);
                        break;

                    default:
                        throw new NotSupportedException(string.Format("Unsupported message type '{0}'", message.Type));
                }
            }
            catch (Exception e)
            {
                DispatchError(e);
            }
        }