Example #1
0
        private PersistentResponse GetResponse(MessageResult result)
        {
            // Do a single sweep through the results to process commands and extract values
            var messageValues = ProcessResults(result.Messages);

            var response = new PersistentResponse
            {
                MessageId  = result.LastMessageId,
                Messages   = messageValues,
                Disconnect = _disconnected,
                Aborted    = _aborted,
                TimedOut   = result.TimedOut
            };

            PopulateResponseState(response);

            _trace.Source.TraceInformation("Connection: Connection '{0}' received {1} messages, last id {2}", _connectionId, result.Messages.Count, result.LastMessageId);
            Debug.WriteLine("Connection: Connection '{0}' received {1} messages, last id {2}", _connectionId, result.Messages.Count, result.LastMessageId);
            Debug.WriteLine("Connection: Messages");
            Debug.WriteLine(_serializer.Stringify(result.Messages));
            Debug.WriteLine("Connection: Response");
            Debug.WriteLine(_serializer.Stringify(response));

            return(response);
        }
Example #2
0
        public virtual Task Send(PersistentResponse response)
        {
            var data = _jsonSerializer.Stringify(response);

            OnSending(data);

            return(Context.Response.WriteAsync(data));
        }
Example #3
0
        public virtual Task Send(PersistentResponse response)
        {
            HeartBeat.MarkConnection(this);
            var data = _jsonSerializer.Stringify(response);

            OnSending(data);
            return(Context.Response.WriteAsync(data));
        }
Example #4
0
 public Task Send(object value)
 {
     return(TaskAsyncHelper.FromMethod((connection, data) => connection.Send(data),
                                       _webSocketConnection,
                                       _serializer.Stringify(value))
            .Catch());
 }
Example #5
0
        public virtual void Send(object value)
        {
            var payload = _jsonSerializer.Stringify(value);

            OnSending(payload);
            Context.Response.Write(payload);
            Context.Response.Flush();
        }
Example #6
0
        public Task SendCommand(ServerCommand command)
        {
            // Store where the message originated from
            command.ServerId = _serverIdManager.ServerId;

            // Send the command to the all servers
            return(_messageBus.Publish(_serverIdManager.ServerId, ServerSignal, _serializer.Stringify(command)));
        }
 public DefaultContractsGenerator(IJsonSerializer serializer, IPayloadDescriptorProvider payloadProvider, IMethodDescriptorProvider methodProvider, IHubDescriptorProvider hubProvider)
 {
     _serializer = serializer;
     _payloadProvider = payloadProvider;
     _methodProvider = methodProvider;
     _hubProvider = hubProvider;
     _generatedContracts = new Lazy<JRaw>(() => new JRaw(_serializer.Stringify(CreateContractsCache(_serializer, _payloadProvider, _methodProvider, _hubProvider))));
 }
Example #8
0
 /// <summary>
 /// Serializes the specified object to a JSON string.
 /// </summary>
 /// <param name="serializer">The serializer</param>
 /// <param name="value">The object to serailize.</param>
 /// <returns>A JSON string representation of the object.</returns>
 public static string Stringify(this IJsonSerializer serializer, object value)
 {
     using (var writer = new StringWriter())
     {
         serializer.Stringify(value, writer);
         return(writer.ToString());
     }
 }
Example #9
0
 private Task ProcessNegotiationRequest(HostContext context)
 {
     context.Response.ContentType = Json.MimeType;
     return(context.Response.WriteAsync(_jsonSerializer.Stringify(new
     {
         Url = context.Request.Path.Replace("/negotiate", ""),
         ConnectionId = _connectionIdFactory.CreateConnectionId(context.Request),
         TryWebSockets = context.SupportsWebSockets()
     })));
 }
 private Task ProcessNegotiationRequest(HttpContextBase context)
 {
     context.Response.ContentType = Json.MimeType;
     return(context.Response.WriteAsync(_jsonSerializer.Stringify(new
     {
         Url = VirtualPathUtility.ToAbsolute(context.Request.AppRelativeCurrentExecutionFilePath.Replace("/negotiate", "")),
         ConnectionId = _connectionIdFactory.CreateConnectionId(context),
         TryWebSockets = ClientShouldTryWebSockets
     })));
 }
Example #11
0
        public virtual Task Send(object value)
        {
            var payload = _jsonSerializer.Stringify(value);

            if (Sending != null)
            {
                Sending(payload);
            }
            _context.Response.ContentType = Json.MimeType;
            return(_context.Response.WriteAsync(payload));
        }
        public virtual void Send(object value)
        {
            var payload = _jsonSerializer.Stringify(value);

            if (Sending != null)
            {
                Sending(payload);
            }
            _context.Response.ContentType = Json.MimeType;
            _context.Response.Write(payload);
        }
Example #13
0
 private Task ProcessNegotiationRequest(HostContext context)
 {
     context.Response.ContentType = Json.MimeType;
     return(context.Response.EndAsync(_jsonSerializer.Stringify(new
     {
         Url = context.Request.Url.LocalPath.Replace("/negotiate", ""),
         ConnectionId = _connectionIdFactory.CreateConnectionId(context.Request),
         TryWebSockets = context.SupportsWebSockets(),
         WebSocketServerUrl = context.WebSocketServerUrl(),
         ProtocolVersion = "1.0"
     })));
 }
Example #14
0
        private Message CreateMessage(string key, object value)
        {
            var command = value as Command;
            var message = new Message(_connectionId, key, _serializer.Stringify(value));

            if (command != null)
            {
                // Set the command id
                message.CommandId  = command.Id;
                message.WaitForAck = command.WaitForAck;
            }

            return(message);
        }
Example #15
0
        public virtual Task Send(object value)
        {
            var payload = _jsonSerializer.Stringify(value);

            if (IsJsonp)
            {
                payload = Json.CreateJsonpCallback(JsonpCallback, payload);
            }

            if (Sending != null)
            {
                Sending(payload);
            }

            Context.Response.ContentType = IsJsonp ? Json.JsonpMimeType : Json.MimeType;
            return(Context.Response.EndAsync(payload));
        }
Example #16
0
        private Task ProcessNegotiationRequest(HostContext context)
        {
            var payload = new
            {
                Url                = context.Request.Url.LocalPath.Replace("/negotiate", ""),
                ConnectionId       = _connectionIdPrefixGenerator.GenerateConnectionIdPrefix(context.Request) + Guid.NewGuid().ToString("d"),
                TryWebSockets      = _transportManager.SupportsTransport(WebSocketsTransportName) && context.SupportsWebSockets(),
                WebSocketServerUrl = context.WebSocketServerUrl(),
                ProtocolVersion    = "1.0"
            };

            if (!String.IsNullOrEmpty(context.Request.QueryString["callback"]))
            {
                return(ProcessJsonpNegotiationRequest(context, payload));
            }

            context.Response.ContentType = Json.MimeType;
            return(context.Response.EndAsync(_jsonSerializer.Stringify(payload)));
        }
Example #17
0
        public virtual Task Send(object value)
        {
            Context.Response.ContentType = IsJsonp ? Json.JsonpMimeType : Json.MimeType;

            if (IsJsonp)
            {
                OutputWriter.Write(JsonpCallback);
                OutputWriter.Write("(");
            }

            _jsonSerializer.Stringify(value, OutputWriter);

            if (IsJsonp)
            {
                OutputWriter.Write(");");
            }

            OutputWriter.Flush();
            return(Context.Response.EndAsync());
        }
Example #18
0
        internal static void PopulateResponseState(PersistentResponse response,
                                                   DiffSet <string> groupSet,
                                                   IJsonSerializer serializer,
                                                   IProtectedData protectedData,
                                                   string connectionId)
        {
            bool anyChanges = groupSet.DetectChanges();

            if (anyChanges)
            {
                // Create a protected payload of the sorted list
                IEnumerable <string> groups = groupSet.GetSnapshot();

                // Remove group prefixes before any thing goes over the wire
                string groupsString = connectionId + ':' + serializer.Stringify(PrefixHelper.RemoveGroupPrefixes(groups));;

                // The groups token
                response.GroupsToken = protectedData.Protect(groupsString, Purposes.Groups);
            }
        }
Example #19
0
        public override Task ProcessRequestAsync(HttpContext context)
        {
            Task task        = null;
            var  contextBase = new HttpContextWrapper(context);

            if (IsNegotiationRequest(context.Request))
            {
                context.Response.ContentType = Json.MimeType;
                context.Response.Write(_jsonSerializer.Stringify(new
                {
                    Url          = VirtualPathUtility.ToAbsolute(context.Request.AppRelativeCurrentExecutionFilePath.Replace("/negotiate", "")),
                    ConnectionId = _connectionIdFactory.CreateConnectionId(contextBase)
                }));
            }
            else
            {
                _transport = GetTransport(contextBase);

                string connectionId = _transport.ConnectionId;

                // If there's no connection id then this is a bad request
                if (String.IsNullOrEmpty(connectionId))
                {
                    throw new InvalidOperationException("Protocol error: Missing connection id.");
                }

                IEnumerable <string> groups = GetGroups(contextBase);

                Connection = CreateConnection(connectionId, groups, contextBase);

                // Wire up the events we need
                _transport.Connected += () =>
                {
                    task = OnConnectedAsync(contextBase, connectionId);
                };

                _transport.Received += (data) =>
                {
                    task = OnReceivedAsync(connectionId, data);
                };

                _transport.Error += (e) =>
                {
                    task = OnErrorAsync(e);
                };

                _transport.Disconnected += () =>
                {
                    task = OnDisconnectAsync(connectionId);
                };

                Func <Task> processRequestTask = _transport.ProcessRequest(Connection);

                if (processRequestTask != null)
                {
                    if (task != null)
                    {
                        return(task.Success(_ => processRequestTask()).Unwrap());
                    }
                    return(processRequestTask());
                }
            }

            return(task ?? TaskAsyncHelper.Empty);
        }
Example #20
0
        internal static void PopulateResponseState(PersistentResponse response,
                                                   DiffSet<string> groupSet,
                                                   IJsonSerializer serializer,
                                                   IProtectedData protectedData,
                                                   string connectionId)
        {
            bool anyChanges = groupSet.DetectChanges();

            if (anyChanges)
            {
                // Create a protected payload of the sorted list
                IEnumerable<string> groups = groupSet.GetSnapshot();

                // Remove group prefixes before any thing goes over the wire
                string groupsString = connectionId + ':' + serializer.Stringify(PrefixHelper.RemoveGroupPrefixes(groups)); ;

                // The groups token
                response.GroupsToken = protectedData.Protect(groupsString, Purposes.Groups);
            }
        }
Example #21
0
        internal static void PopulateResponseState(PersistentResponse response, 
                                                   DiffSet<string> groupSet, 
                                                   IJsonSerializer serializer, 
                                                   IProtectedData protectedData)
        {
            DiffPair<string> groupDiff = groupSet.GetDiff();

            if (groupDiff.AnyChanges)
            {
                // Create a protected payload of the sorted list
                IEnumerable<string> groups = groupSet.GetSnapshot();

                // No groups so do nothing
                if (groups.Any())
                {
                    // Remove group prefixes before any thing goes over the wire
                    string groupsString = serializer.Stringify(PrefixHelper.RemoveGroupPrefixes(groups));

                    // The groups token
                    response.GroupsToken = protectedData.Protect(groupsString, Purposes.Groups);
                }
            }
        }
Example #22
0
 public override string ToString()
 {
     return(_serializer.Stringify(_value));
 }