예제 #1
0
        // Receive data from Socket Server
        private async void ReceiveData(IAsyncResult ar)
        {
            Socket socket = (Socket)ar.AsyncState;

            if (socket.Connected)
            {
                int received = socket.EndReceive(ar);
                // Set server socket
                Connection.Instance.ServerSocket = socket;
                byte[] dataBuf = new byte[received];
                Array.Copy(receivedBuffer, dataBuf, received);
                await Task.Delay(50);

                // MovieCommand from Server
                MobileMovieManager.DAL.Models.Enums.MovieCommandEnum.MovieCommand movieCommand = BinaryConverter.BinaryToCommandEnum(dataBuf);

                // Listen again
                Connection.Instance.ClientSocket.ClientSocket.BeginReceive(receivedBuffer, 0, receivedBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), Connection.Instance.ClientSocket.ClientSocket);

                // Close Client
                if (movieCommand == MobileMovieManager.DAL.Models.Enums.MovieCommandEnum.MovieCommand.CloseClient)
                {
                    //Device.BeginInvokeOnMainThread(async () =>
                    //    await App.Current.MainPage.Navigation.PushModalAsync(new MoviesListPage(movies))
                    //);
                }
                else
                {
                    LoggerService.Instance.Loggers.Add(new Logger {
                        Id = LoggerService.Instance.LastId + 1, Message = "Adat érkezett a szervertől", Sender = Sender.SocketClient, LogDate = DateTime.Now
                    });
                }
            }
            else
            {
                LoggerService.Instance.Loggers.Add(new Logger {
                    Id = LoggerService.Instance.LastId + 1, Message = "Nem sikerült kapcsolódni a szerverhez!", Sender = Sender.SocketClient, LogDate = DateTime.Now
                });
            }
        }
예제 #2
0
        // Receive data
        private void ReceiveCallback(IAsyncResult ar)
        {
            Socket socket = ar.AsyncState as Socket;

            if (socket.Connected)
            {
                int received;
                try
                {
                    received = socket.EndReceive(ar);
                }
                catch (Exception ex)
                {
                    // Close connection
                    CloseClientConnection("Probléma az adatok fogadása közben..." + Environment.NewLine + ex.Message, socket.RemoteEndPoint);

                    return;
                }

                // Get received movie object or movie command
                if (received != 0)
                {
                    byte[] dataBuf = new byte[received];
                    Array.Copy(buffer, dataBuf, received);

                    // Client sent a movie
                    if (received != 194)
                    {
                        MovieViewModel movieViewModel = MovieMap.MapToMovieViewModel(BinaryConverter.BinaryToMovieObject(dataBuf));

                        LoggerService.Instance.Loggers.Add(new Logger {
                            Id = LoggerService.Instance.LastId + 1, Message = "Kliens kiválasztott egy filmet ---\n\t" + movieViewModel.Title, Sender = Sender.SocketServer, LogDate = DateTime.Now
                        });

                        connection.ClientSockets.FirstOrDefault(c => c.ClientSocket == socket).IsPlayMovie = true;

                        // Start playing movie
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            ((ModernWindow)Application.Current.MainWindow).Hide();
                            new PlayerWindow(movieViewModel).Show();
                        });


                        // Listen again (Receive data)
                        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
                    }
                    // Client sent a movie command
                    else
                    {
                        MovieCommand commandEnum = BinaryConverter.BinaryToCommandEnum(dataBuf);

                        if (commandEnum != MovieCommand.CloseClient)
                        {
                            // Process movie command
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                PlayerWindow playerWindow = Application.Current.Windows.OfType <PlayerWindow>().FirstOrDefault();
                                switch (commandEnum)
                                {
                                case MovieCommand.StartPauseMovie:
                                    playerWindow.PlayPause();
                                    break;

                                case MovieCommand.StopMovie:
                                    playerWindow.Stop();
                                    break;

                                case MovieCommand.RewindMovie:
                                    playerWindow.Rewind();
                                    break;

                                case MovieCommand.ForwardMovie:
                                    playerWindow.Forward();
                                    break;

                                case MovieCommand.SoundPlus:
                                    playerWindow.SoundPlus();
                                    break;

                                case MovieCommand.SoundMinus:
                                    playerWindow.SoundMinus();
                                    break;

                                case MovieCommand.Mute:
                                    playerWindow.MuteOnOff();
                                    break;

                                case MovieCommand.FullScreen:
                                    playerWindow.SetFullScreen();
                                    break;

                                case MovieCommand.ShutdownPc:
                                    playerWindow.SetPcOption(PCOption.Shutdown);
                                    break;

                                case MovieCommand.Restart:
                                    playerWindow.SetPcOption(PCOption.Restart);
                                    break;

                                case MovieCommand.Hibernate:
                                    playerWindow.SetPcOption(PCOption.Hibernate);
                                    break;

                                case MovieCommand.LogOff:
                                    playerWindow.SetPcOption(PCOption.LogOff);
                                    break;

                                case MovieCommand.Sleep:
                                    playerWindow.SetPcOption(PCOption.Sleep);
                                    break;

                                case MovieCommand.Lock:
                                    playerWindow.SetPcOption(PCOption.Lock);
                                    break;

                                case MovieCommand.CloseMovie:
                                    playerWindow.CloseWindow();
                                    break;
                                }
                            });

                            // Listen again (Receive data)
                            socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
                        }
                        else
                        {
                            CloseClientConnection("A kliens bezárta a kapcsolatot", socket.RemoteEndPoint);
                        }
                    }
                }
                else
                {
                    CloseClientConnection("Nem érhető el a kliens", socket.RemoteEndPoint);
                }
            }
            else
            {
                CloseClientConnection("Nem érhető el a kliens", socket.RemoteEndPoint);
            }
        }