public virtual void Send <I, O>(string service, string method, I payload, ClientTransportCallback <O> callback, C ctx)
        {
            try {
                if (!Ready)
                {
                    throw new Exception("WebSocketTransport is not ready.");
                }

                var req = new WebSocketRequestMessageJson(WebSocketMessageKind.RpcRequest);
                req.ID      = GetRandomMessageId();
                req.Service = service;
                req.Method  = method;
                req.Data    = _marshaller.Marshal(payload);

                var record = new WSRequest();
                _requests.Add(req.ID, record);
                record.Timer   = new Timer();
                record.Success = msg => {
                    O data;
                    try {
                        data = _marshaller.Unmarshal <O>(msg.Data);
                    }
                    catch (Exception ex) {
                        callback.Failure(new TransportMarshallingException("Unexpected exception occured during unmarshalling.", ex));
                        return;
                    }
                    callback.Success(data);
                };

                record.Failure = (ex) => {
                    callback.Failure(new TransportException("Request failed. ", ex));
                };
                record.Timer.Interval = DefaultTimeoutInterval * 1000;
                record.Timer.Elapsed += (sender, e) => {
                    HandleRPCFailure(req.ID, new Exception("Request timed out."));
                };

                var serialized = _marshaller.Marshal(req);
                _logger.Logf(LogLevel.Trace, "WebSocketTransport: Outgoing message: \n{0}", serialized);

                _ws.SendAsync(serialized, success => {
                    if (success)
                    {
                        return;
                    }
                    HandleRPCFailure(req.ID, new Exception("Sending request failed."));
                });
                record.Timer.Start();
            }
            catch (Exception ex)
            {
                callback.Failure(
                    new TransportException("Unexpected exception occured during async request.", ex)
                    );
            }
        }
示例#2
0
        public virtual void Send <I, O>(string service, string method, I payload, ClientTransportCallback <O> callback, C ctx)
        {
            try {
                var request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", _endpoint, service, method));
                request.Timeout = _timeout * 1000;
                request.Method  = payload == null ? "GET" : "POST";
                if (_headers != null)
                {
                    foreach (var key in _headers.Keys)
                    {
                        request.Headers.Add(key, _headers[key]);
                    }
                }

                if (_auth != null)
                {
                    request.Headers.Add("Authorization", _auth.ToValue());
                }

                var state = new RequestState <O>(request, callback, null);
                if (payload == null)
                {
                    request.BeginGetResponse(ProcessResponse <O>, state);
                }
                else
                {
                    var data = _marshaller.Marshal <I>(payload);
                    state.JsonString    = data;
                    request.ContentType = "application/json";
                    request.BeginGetRequestStream(ProcessStreamRequest <O>, state);
                }
            }
            catch (Exception ex)
            {
                callback.Failure(
                    new TransportException("Unexpected exception occured during async request.", ex)
                    );
            }
        }
示例#3
0
        public virtual void Send <I, O>(string service, string method, I payload, ClientTransportCallback <O> callback, C ctx)
        {
            try {
                var request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", _endpoint, service, method));
                request.Timeout = _timeout * 1000;
                request.Method  = payload == null ? "GET" : "POST";
                if (_headers != null)
                {
                    foreach (var key in _headers.Keys)
                    {
                        request.Headers.Add(key, _headers[key]);
                    }
                }

                if (_auth != null)
                {
                    request.Headers.Add("Authorization", _auth.ToValue());
                }

                if (payload != null)
                {
                    var data = _marshaller.Marshal <I>(payload);
                    if (data == null)
                    {
                        throw new TransportException("HttpTransport only supports Marshallers which return a string.");
                    }

                    request.Method        = "POST";
                    request.ContentType   = "application/json";
                    request.ContentLength = data.Length;

                    using (var stream = request.GetRequestStream()) {
                        stream.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
                    }
                }

                using (var response = (HttpWebResponse)request.GetResponse()) {
                    using (var respStream = response.GetResponseStream()) {
                        using (var reader = new StreamReader(respStream, Encoding.UTF8)) {
                            string jsonString = reader.ReadToEnd();
                            if (string.IsNullOrEmpty(jsonString))
                            {
                                throw new TransportException("Empty Response");
                            }

                            O data;
                            try {
                                data = _marshaller.Unmarshal <O>(jsonString);
                            } catch (Exception ex) {
                                callback.Failure(
                                    new TransportMarshallingException("Unexpected exception occuted while unmarshalling response.", ex)
                                    );
                                return;
                            }

                            callback.Success(data);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                callback.Failure(
                    new TransportException("Unexpected exception occured during async request.", ex)
                    );
            }
        }