/// <summary>
        /// Handle messages from client to host - FYI: SlideshowPage handles the events fired from here
        /// </summary>
        private async Task <bool> HandleMessageFromClientAsync(ValueSet message, ValueSet responseMessage)
        {
            if (!message.ContainsKey("type"))
            {
                responseMessage.Add("error", "type not found");
                return(false);
            }

            var args = new SlideshowMessageReceivedEventArgs()
            {
                Message         = message,
                ResponseMessage = responseMessage,
                QueryType       = (SlideshowMessageTypeEnum)Enum.Parse(typeof(SlideshowMessageTypeEnum), (string)message["type"])
            };

            var taskCompletionSource = new TaskCompletionSource <object>();
            var dispatcher           = CoreApplication.MainView.CoreWindow.Dispatcher;

            var t = dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                ReceivedMessageFromClient?.Invoke(this, args);
                taskCompletionSource.SetResult(null);
            });

            await taskCompletionSource.Task.ConfigureAwait(false);

            return(ReceivedMessageFromClient != null);
        }
示例#2
0
        private bool MessageReceived(string ipPort, byte[] data)
        {
            string receiveData;

            if (_key == null)
            {
                receiveData = Encoding.UTF8.GetString(data);
            }
            else
            {
                byte[] decryptedData = ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key);
                if (decryptedData == null)
                {
                    return(false);
                }
                receiveData = Encoding.UTF8.GetString(decryptedData);
            }

            var message = JsonConvert.DeserializeObject <MessageModel>(receiveData);

            string[] split     = ipPort.Split(':');
            string   ipAddress = split[0];
            int      port      = Convert.ToInt32(split[1]);

            if (message != null)
            {
                ClientInfo client;
                client.ipAddress           = IPAddress.Parse(ipAddress);
                client.remoteEndPoint      = port;
                client.ipAndRemoteEndPoint = ipPort;
                client.pcName = message.PcName;

                //Bloklanların içinde varmı?
                var resultBlocked = (from x in BlockedIpList where x == client.ipAddress select x).FirstOrDefault();
                if (resultBlocked != null)
                {
                    MessageModel blockedMessage = new MessageModel();
                    blockedMessage.Type = Type.Blocked;
                    string json = JsonConvert.SerializeObject(blockedMessage);

                    if (_key == null)
                    {
                        _server.Send(client.ipAndRemoteEndPoint, Encoding.UTF8.GetBytes(json));
                    }
                    else
                    {
                        _server.Send(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
                    }

                    return(true);
                }

                if (message.Type == Type.ClientLogon)
                {
                    lock (_lockOfClientList)
                    {
                        _clientList.Add(client);

                        MessageModel acceptMessage = new MessageModel();
                        acceptMessage.Type = Type.AcceptLogon;
                        string json = JsonConvert.SerializeObject(acceptMessage);

                        if (_key == null)
                        {
                            bool status = _server.Send(client.ipAndRemoteEndPoint, Encoding.UTF8.GetBytes(json));
                            if (status)
                            {
                                ConnectedClient?.Invoke(client);
                            }
                        }
                        else
                        {
                            bool status = _server.Send(client.ipAndRemoteEndPoint, ExtensionMethods.Encrypt(json, _key));
                            if (status)
                            {
                                ConnectedClient?.Invoke(client);
                            }
                        }
                    }
                }

                if (message.Type == Type.Message)
                {
                    ReceivedMessageFromClient?.Invoke(client, message.Message);
                }
            }

            return(true);
        }