Example #1
0
        private void SendConnectionAlivenessVerificationCallback(IAsyncResult ar)
        {
            SocketError sockErr = new SocketError();

            try
            {
                // End the sending.
                int bytesSent = _clientSocket.EndSend(ar, out sockErr);
                if (!sockErr.Equals(SocketError.Success) && !sockErr.Equals(SocketError.WouldBlock))
                {
                    _mainWinWMCaller.CancelStringCommand();
                    // Close the connection with the server. The disconnect function calls the CloseClient, the Remove server, and the removeProcessesFromApplication.
                    _mainWinWMCaller.DisconnectFromServer(this.ServerIpAddress);

                    System.Windows.Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        // Open the Error Window
                        Views.ErrorWindowView errView         = new Views.ErrorWindowView();
                        ErrorWindowViewModel errWindViewModel = new ErrorWindowViewModel("Server " + this.ServerIpAddress.ToString() + " disconnesso.");
                        errWindViewModel.ClosingRequest      += errView.Close;
                        errView.DataContext = errWindViewModel;
                        errView.Show();
                    });
                }
            }
            catch (Exception e)
            {
                // Check if the socket error is not WouldBlock (that is not a proper error):
                // in this case return without taking action
                if (sockErr.Equals(SocketError.WouldBlock))
                {
                    Console.WriteLine(e.ToString());
                }
                else
                {
                    _mainWinWMCaller.CancelStringCommand();
                    Console.WriteLine(e.ToString());
                    // Close the connection with the server. The disconnect function calls the CloseClient, the Remove server, and the removeProcessesFromApplication.
                    _mainWinWMCaller.DisconnectFromServer(this.ServerIpAddress);

                    System.Windows.Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        // Open the Error Window
                        Views.ErrorWindowView errView         = new Views.ErrorWindowView();
                        ErrorWindowViewModel errWindViewModel = new ErrorWindowViewModel("Server " + this.ServerIpAddress.ToString() + " disconnesso.");
                        errWindViewModel.ClosingRequest      += errView.Close;
                        errView.DataContext = errWindViewModel;
                        errView.Show();
                    });
                }
            }
        }
Example #2
0
        private void Connect()
        {
            if (ServersStorage.ServerSocket != null)
            {
                ServersStorage.SelectedServer = null;
                if (ServersStorage.ServerSocket.Connected)
                {
                    ServersStorage.ServerSocket.Close();
                }
                ServersStorage.ServerSocket = null;
            }
            SocketError      result      = SocketError.Fault;
            Socket           _socket     = null;
            ManualResetEvent _clientDone = new ManualResetEvent(false);

            DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);

            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

            socketEventArg.RemoteEndPoint = hostEntry;

            socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
            {
                result = e.SocketError;

                if (!result.Equals(SocketError.Success))
                {
                    Dispatcher.BeginInvoke(delegate() { MessageBox.Show("Failed to connect"); });
                }
                else
                {
                    Dispatcher.BeginInvoke(delegate() { NavigationService.GoBack(); });
                }
                _clientDone.Set();
            });

            _clientDone.Reset();

            _socket.ConnectAsync(socketEventArg);

            _clientDone.WaitOne(2000);

            if (_socket != null && _socket.Connected)
            {
                SocketAsyncEventArgs toBeSent = new SocketAsyncEventArgs();
                toBeSent.RemoteEndPoint = _socket.RemoteEndPoint;
                toBeSent.UserToken      = null;
                byte[] payload = IntroductionMessage.GetMessage();
                toBeSent.SetBuffer(payload, 0, payload.Length);
                _socket.SendAsync(toBeSent);
                ServersStorage.ServerSocket   = _socket;
                ServersStorage.SelectedServer = new ServerData(host, hostName, portNumber);
            }
        }
Example #3
0
        public static void ReestabilishConnection()
        {
            if (SelectedServer != null && ServerSocket != null && !ServerSocket.Connected)
            {
                Debug.WriteLine("Reestabilishing connection");
                SocketError      result      = SocketError.Fault;
                Socket           _socket     = null;
                ManualResetEvent _clientDone = new ManualResetEvent(false);

                DnsEndPoint hostEntry = new DnsEndPoint(SelectedServer.Address, SelectedServer.Port);

                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                socketEventArg.RemoteEndPoint = hostEntry;

                socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
                {
                    result = e.SocketError;

                    if (!result.Equals(SocketError.Success) && OnConnectionFailed != null)
                    {
                        OnConnectionFailed();
                    }
                    _clientDone.Set();
                });

                _clientDone.Reset();

                _socket.ConnectAsync(socketEventArg);

                _clientDone.WaitOne(2000);

                if (_socket != null && _socket.Connected)
                {
                    SocketAsyncEventArgs toBeSent = new SocketAsyncEventArgs();
                    toBeSent.RemoteEndPoint = _socket.RemoteEndPoint;
                    toBeSent.UserToken      = null;
                    byte[] payload = IntroductionMessage.GetMessage();
                    toBeSent.SetBuffer(payload, 0, payload.Length);
                    _socket.SendAsync(toBeSent);
                    ServersStorage.ServerSocket = _socket;
                }
            }
        }