Esempio n. 1
0
 internal T Deserialize <T>(WebSocketMessage message)
 {
     using (var valueStream = message.GetValueStream())
     {
         if (valueStream != null)
         {
             using (var reader = new StreamReader(valueStream))
                 using (var jsonReader = new JsonTextReader(reader))
                 {
                     return(_serializer.Deserialize <T>(jsonReader));
                 }
         }
     }
     return(default(T));
 }
        protected override void OnMessage(MessageEventArgs e)
        {
            WebSocketMessage message = new WebSocketMessage(e.RawData);

            try
            {
                var user = AuthenticationService.FindUser(AuthenticationSource.IpAddress, Context?.UserEndPoint.Address.ToString());
                if (user == null)
                {
                    throw new UnauthorizedAccessException($"Access from {Context?.UserEndPoint.Address} not allowed");
                }
                Thread.CurrentPrincipal = new GenericPrincipal(user, new string[0]);

                if (message.MessageType == WebSocketMessage.WebSocketMessageType.RootQuery)
                {
                    _sendResponse(message, InitialObject);
                }
                else // method of particular object
                {
                    IDto objectToInvoke = _referenceResolver.ResolveReference(message.DtoGuid);
                    if (objectToInvoke != null)
                    {
                        if (message.MessageType == WebSocketMessage.WebSocketMessageType.Query ||
                            message.MessageType == WebSocketMessage.WebSocketMessageType.Invoke)
                        {
                            Type       objectToInvokeType = objectToInvoke.GetType();
                            MethodInfo methodToInvoke     = objectToInvokeType.GetMethods()
                                                            .FirstOrDefault(m => m.Name == message.MemberName &&
                                                                            m.GetParameters().Length == message.ValueCount);
                            if (methodToInvoke != null)
                            {
                                var             parameters       = DeserializeDto <WebSocketMessageArrayValue>(message.GetValueStream());
                                ParameterInfo[] methodParameters = methodToInvoke.GetParameters();
                                for (int i = 0; i < methodParameters.Length; i++)
                                {
                                    MethodParametersAlignment.AlignType(ref parameters.Value[i],
                                                                        methodParameters[i].ParameterType);
                                }
                                object response = methodToInvoke.Invoke(objectToInvoke,
                                                                        BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null,
                                                                        parameters.Value, null);
                                if (message.MessageType == WebSocketMessage.WebSocketMessageType.Query)
                                {
                                    _sendResponse(message, response);
                                }
                            }
                            else
                            {
                                throw new ApplicationException(
                                          $"Server: unknown method: {objectToInvoke}:{message.MemberName}");
                            }
                        }
                        else if (message.MessageType == WebSocketMessage.WebSocketMessageType.Get ||
                                 message.MessageType == WebSocketMessage.WebSocketMessageType.Set)
                        {
                            PropertyInfo property = objectToInvoke.GetType().GetProperty(message.MemberName);
                            if (property != null)
                            {
                                if (message.MessageType == WebSocketMessage.WebSocketMessageType.Get &&
                                    property.CanRead)
                                {
                                    object response = property.GetValue(objectToInvoke, null);
                                    _sendResponse(message, response);
                                }
                                else // Set
                                {
                                    if (property.CanWrite)
                                    {
                                        var parameter = DeserializeDto <object>(message.GetValueStream());
                                        MethodParametersAlignment.AlignType(ref parameter, property.PropertyType);
                                        property.SetValue(objectToInvoke, parameter, null);
                                    }
                                    else
                                    {
                                        throw new ApplicationException(
                                                  $"Server: not writable property: {objectToInvoke}:{message.MemberName}");
                                    }
                                }
                            }
                            else
                            {
                                throw new ApplicationException(
                                          $"Server: unknown property: {objectToInvoke}:{message.MemberName}");
                            }
                        }
                        else if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventAdd ||
                                 message.MessageType == WebSocketMessage.WebSocketMessageType.EventRemove)
                        {
                            EventInfo ei = objectToInvoke.GetType().GetEvent(message.MemberName);
                            if (ei != null)
                            {
                                if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventAdd)
                                {
                                    _addDelegate(objectToInvoke, ei);
                                }
                                else if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventRemove)
                                {
                                    _removeDelegate(objectToInvoke, ei);
                                }
                            }
                            else
                            {
                                throw new ApplicationException(
                                          $"Server: unknown event: {objectToInvoke}:{message.MemberName}");
                            }
                        }
                    }
                    else
                    {
                        _sendResponse(message, null);
                        throw new ApplicationException($"Server: unknown DTO: {message.DtoGuid} on {message}");
                    }
                }
            }
            catch (Exception ex)
            {
                message.MessageType = WebSocketMessage.WebSocketMessageType.Exception;
                //SerializeDto(message, ex);
                //Send(message.Serialize());
                Logger.Error(ex);
                Debug.WriteLine(ex);
            }
        }