Exemple #1
0
        public void Connect(IConnection connection, IGameMatch gameMatch, Authentication autentization, int leaguesID, int roundsID)
        {
            if (gameMatch != null) // gameMatch is returned from "server"
            {
                try
                {
                    gameMatch.SetNetworkConnection(connection);
                }
                catch (InvalidStateException e)
                {
                    MessageBoxShow("Error in setting up network game occured: " + e.Message);
                }
            }
            else // "client" doesn't have GameDeskControl opened by default
            {
                string questXml = this.getRequestOnServer("/remote/GetLeague/" + leaguesID + "/" + roundsID);

                if (questXml != "error" && questXml != "")
                {

                    OpeningMode openingMode = (roundsID == -1) ? OpeningMode.League : OpeningMode.Round;
                    Quest q = new Quest(openingMode, questXml, this /*gameServerCommunication*/);
                    IGameMatch gm = this.QuestSelected(leaguesID, roundsID, q, GameMode.TwoPlayers, connection);
                }
                else
                {
                    consolePane.ErrorMessage(ErrorMessageSeverity.Medium, "MainProgram", "Server returned empty response. The problem is propably at server-side.");
                    MessageBoxShow("The league cannot be opened. A problem is probably on the side of server.");
                    connection.CloseConnection();
                }
            }
        }
Exemple #2
0
        public void Connect(IConnection connection, IGameMatch gameMatch, Authentication autentization, int leaguesID, int roundsID)
        {
            DebuggerIX.WriteLine(DebuggerTag.Net, "Connected", "Auth: " + autentization.Name + " " + autentization.IP);
            DebuggerIX.WriteLine(DebuggerTag.Net, "Connected", "Game = {LeaguesID: " + leaguesID + "; RoundsID: " + roundsID + "}");

            if (conn1 == null)
            {
                conn1 = connection;
            }
            else
            {
                conn2 = connection;
                runTests();
            }
        }
        public InitConnection(int leaguesID, int roundsID, IProfileRepository profileRepository, 
            IErrorMessagesPresenter errorPresenter, IConnectionRelayer connectionRelayer,
            IGameMatch gameMatch)
        {
            InitializeComponent();
            this.DataContext = this;
            IpAddress = "Automatic";
            this.leaguesID = leaguesID;
            this.roundsID = roundsID;
            this.profile = profileRepository;
            this.errorPresenter = errorPresenter;
            this.connectionRelayer = connectionRelayer;
            this.gameMatch = gameMatch;

            auth = new Authentication(profileRepository.Username, profileRepository.IPAddress);
        }
        public ConnectionDialog(string ipAddress, int port, int leaguesID, int roundsID, 
            IProfileRepository profileRepository, IErrorMessagesPresenter errorPresenter, 
            IConnectionRelayer connectionRelayer, IGameMatch gameMatch)
        {
            InitializeComponent();
            this.DataContext = this;
            this.leaguesID = leaguesID;
            this.roundsID = roundsID;
            this.profile = profileRepository;
            this.errorPresenter = errorPresenter;
            this.connectionRelayer = connectionRelayer;
            this.port = port;
            this.ipAddress = ipAddress;
            this.gameMatch = gameMatch;
            endTheThread = 0;

            auth = new Authentication(profileRepository.Username, profileRepository.IPAddress);
        }
 public ThreadServerParam(bool automaticFirstConnect, Authentication auth)
 {
     this.Authentication = auth;
     this.AutomaticFirstConnect = automaticFirstConnect;
 }
        private void runServer(object obj)
        {
            ThreadServerParam param = obj as ThreadServerParam;

            string role = "Server";
            NetworkServer n = new NetworkServer(this.ipAddress, this.port);

            int maxPlayersToWaitFor = MAX_PLAYERS_TO_WAIT_FOR;
            int i = 1;
            AddAutentizationDelegate addPlayer = new AddAutentizationDelegate(AddPlayer);
            AddMessageDelegate addMessage = new AddMessageDelegate(AddMessage);
            ConnectionEstablished threadStoppedDel = new ConnectionEstablished(threadStopped);

            if (this.ipAddress == "Automatic")
            {
                Dispatcher.Invoke(addMessage, "Listening on port: " + this.port);
            }
            else
            {
                Dispatcher.Invoke(addMessage, "Listening on: " + this.ipAddress + ":" + this.port);
            }

            bool isAutenticated = false;

            try
            {
                while (i <= maxPlayersToWaitFor && endTheThread != 1)
                {
                    isAutenticated = false;
                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization # " + i + " invoked");
                    Dispatcher.Invoke(addMessage, "Attempt to receive connection #" + i);

                    try
                    {
                        n.InitializeConnection();
                    }
                    catch (InvalidStateException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "InvalidState: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Error:" + e.Message);
                        n.CloseConnection();
                    }
                    catch (TimeoutException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "TimeOut: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Timeout for attempt #" + i);
                        n.CloseConnection();
                    }

                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization = " + n.IsInitialized);

                    if (n.IsInitialized == true)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'Autentization'");
                        n.SendAsync(NetworkMessageType.Authentication, param.Authentication);
                        n.AllSentHandle.WaitOne(); // wait until all is sent
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "'Autentization' sent");
                        Dispatcher.Invoke(addMessage, "Handshake sent");

                        bool disconnect = false;
                        Authentication auth = null;

                        while (isAutenticated == false && disconnect == false)
                        {
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Start async receiving");
                            n.ReceiveAsync();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Waiting for a message to be received");
                            n.ReceivedMessageHandle.WaitOne(5000);

                            NetworkMessageType messageType = n.GetReceivedMessageType();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Received message: " + messageType);

                            if (messageType == NetworkMessageType.Authentication)
                            {
                                auth = (Authentication)n.GetReceivedMessageFromQueue();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role,
                                    string.Format("'Autentization' message details: {0}, {1}", auth.Name, auth.IP));

                                Dispatcher.Invoke(addPlayer, auth);
                                Dispatcher.Invoke(addMessage, "Autentization message received");

                                if (param.AutomaticFirstConnect)
                                {
                                    Dispatcher.Invoke(addMessage, "Accepting game opponent.");
                                    isAutenticated = true;

                                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'StartGame' message");
                                    n.SendAsync(NetworkMessageType.StartGame);
                                    n.AllSentHandle.WaitOne();
                                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "'StartGame' sent");
                                }
                            }
                            else if (messageType == NetworkMessageType.DisconnectRequest)
                            {
                                DisconnectRequest dr = (DisconnectRequest)n.GetReceivedMessageFromQueue();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequest' was sent by the other side in: " +
                                    dr.DateTime);

                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'DisconnectRequestConfirmation' message");
                                n.SendAsync(NetworkMessageType.DisconnectRequestConfirmation);
                                n.AllSentHandle.WaitOne();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequestConfirmation' sent");
                                disconnect = true;
                                Dispatcher.Invoke(addMessage, "Send request to end handshake");
                            }
                        }

                        if (disconnect)
                        {
                            n.CloseConnection();
                            Dispatcher.Invoke(addMessage, "End of initial handshake");
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Connection closed.");
                        }

                        if (isAutenticated)
                        {
                            Dispatcher.Invoke(new EstablishConnectionDelegate(establishConnection),
                                new object[] { n, auth});
                            break; // from outer while cyclus
                        }
                    }

                    i++;
                }

                if (isAutenticated == false)
                {
                    Dispatcher.Invoke(addMessage, "All attempts tried. Click 'listen' to begin again.");
                }
            }
            catch (ThreadInterruptedException)
            {
                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Thread interrupted.");
                n.CloseConnection();
            }
            finally
            {
                if (isAutenticated == false)
                {
                    n.CloseConnection();
                }

                Dispatcher.Invoke(addMessage, "Network connection searching was disabled.");
                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Finished");
                Dispatcher.Invoke(threadStoppedDel, new object[] {isAutenticated});
            }
        }
 private void establishConnection(IConnection connection, Authentication autentization)
 {
     if (connectionRelayer == null)
     {
         errorMessage(ErrorMessageSeverity.Medium,
             "Connection is ready but it cannot be sent to main program. Error is unrecoverable.");
         MessageBox.Show("Connection is ready but it cannot be sent to main program.");
     }
     else
     {
         connectionRelayer.Connect(connection, this.gameMatch, autentization, leaguesID, roundsID);
     }
 }
 private void AddPlayer(Authentication autentization)
 {
     if (!ConnectingPlayers.Contains(autentization))
     {
         ConnectingPlayers.Add(autentization);
     }
 }
 public ThreadClientParam(Authentication auth)
 {
     this.Authentication = auth;
 }
Exemple #10
0
        private void runClient(object obj)
        {
            ThreadClientParam param = obj as ThreadClientParam;

            string role = "Client";
            NetworkClient n = new NetworkClient(this.ipAddress, this.port);
            ConnectionEstablished threadStoppedDel = new ConnectionEstablished(threadStopped);

            int i = 1;
            AddMessageDelegate addMessage = new AddMessageDelegate(AddMessage);
            Dispatcher.Invoke(addMessage, "Connecting to: " + this.ipAddress + ":" + this.port);

            bool startGameMessageReceived = false;
            bool wasGameStarted = false;
            int maxNumberOfConnectionAttempts = 3;

            try
            {
                while (i <= maxNumberOfConnectionAttempts && endTheThread != 1)
                {
                    startGameMessageReceived = false;
                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization # " + i + " invoked");
                    Dispatcher.Invoke(addMessage, "Attempt to receive connection #" + i);

                    try
                    {
                        n.InitializeConnection();
                    }
                    catch (InvalidStateException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "InvalidState: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Error:" + e.Message);
                        continue;
                    }
                    catch (TimeoutException e)
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "TimeOut: " + e.Message);
                        Dispatcher.Invoke(addMessage, "Timeout for attempt #" + i);
                        continue;
                    }

                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "Initialization = " + n.IsInitialized);

                    if (n.IsInitialized == false)
                    {
                        Dispatcher.Invoke(addMessage, n.ErrorMessage);
                    }
                    else
                    {
                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'Autentization'");
                        n.SendAsync(NetworkMessageType.Authentication, param.Authentication);

                        try
                        {
                            n.AllSentHandle.WaitOne(3000); // wait until all is sent
                        }
                        catch (TimeoutException)
                        {
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Timeout");
                            Dispatcher.Invoke(addMessage, "Timeout");
                            continue;
                        }

                        DebuggerIX.WriteLine(DebuggerTag.Net, role, "'Autentization' sent");
                        Dispatcher.Invoke(addMessage, "Handshake sent");

                        bool disconnect = false;
                        Authentication auth = null;
                        int noneMessageNo = 0;

                        while (startGameMessageReceived == false && disconnect == false && n.IsInitialized == true)
                        {
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Start async receiving");
                            n.ReceiveAsync();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Waiting for a message to be received");

                            try
                            {
                                n.ReceivedMessageHandle.WaitOne(2500);
                            }
                            catch (TimeoutException)
                            {
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Timeout");
                                Dispatcher.Invoke(addMessage, "Timeout");
                                break;
                            }

                            NetworkMessageType messageType = n.GetReceivedMessageType();
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Received message: " + messageType);
                            object message = null;

                            if (messageType != NetworkMessageType.None)
                            {
                                noneMessageNo = 0;
                                message = n.GetReceivedMessageFromQueue();
                            }

                            if (messageType == NetworkMessageType.Authentication)
                            {
                                auth = (Authentication)message;
                                DebuggerIX.WriteLine(DebuggerTag.Net, role,
                                    string.Format("'Autentization' message details: {0}, {1}", auth.Name, auth.IP));

                                Dispatcher.Invoke(addMessage, "Autentization message received");

                            }
                            else if (messageType == NetworkMessageType.DisconnectRequest)
                            {
                                DisconnectRequest dr = (DisconnectRequest)message;
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequest' was sent by the other side in: " +
                                    dr.DateTime);

                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Sending 'DisconnectRequestConfirmation' message");
                                n.SendAsync(NetworkMessageType.DisconnectRequestConfirmation);
                                n.AllSentHandle.WaitOne();
                                DebuggerIX.WriteLine(DebuggerTag.Net, role, "'DisconnectRequestConfirmation' sent");
                                disconnect = true;
                                Dispatcher.Invoke(addMessage, "Send request to end handshake");

                            }
                            else if (messageType == NetworkMessageType.StartGame)
                            {
                                startGameMessageReceived = true;
                            }
                            else if (messageType == NetworkMessageType.None)
                            {
                                noneMessageNo++;

                                if (noneMessageNo > 2)
                                {
                                    Dispatcher.Invoke(addMessage, "No message was received in three attempts in a row. Giving up.");
                                    break;
                                }
                                else
                                {
                                    Dispatcher.Invoke(addMessage, "No message was received.");
                                }
                            }
                            else
                            {
                                Dispatcher.Invoke(addMessage, "Warning: Unknown message received.");
                            }
                        }

                        if (n.IsInitialized == false)
                        {
                            Dispatcher.Invoke(addMessage, "Connection lost.");
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Connection lost.");
                        }

                        if (disconnect)
                        {
                            n.CloseConnection();
                            Dispatcher.Invoke(addMessage, "End of initial handshake");
                            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Connection closed.");
                        }

                        if (startGameMessageReceived && auth != null)
                        {
                            wasGameStarted = true;
                            Dispatcher.Invoke(addMessage, "Game is about to start.");
                            Dispatcher.Invoke(new EstablishConnectionDelegate(establishConnection),
                                new object[] { n, auth });
                            break; // from outer while cyclus
                        }
                    }

                    i++;
                }

                if (wasGameStarted == false)
                {
                    DebuggerIX.WriteLine(DebuggerTag.Net, role, "All attempts tried. Click 'Again' to begin again.");
                    Dispatcher.Invoke(addMessage, "All attempts tried. Click 'Again' to begin again.");
                }
                else
                {
                    Dispatcher.BeginInvoke(threadStoppedDel, new object[] { wasGameStarted });
                }
            }
            catch (ThreadInterruptedException e)
            {
                n.CloseConnection();
                DebuggerIX.WriteLine(DebuggerTag.Net, role, "Thread interrupted. Message: " + e.Message);
                Dispatcher.Invoke(addMessage, "Connection attempts aborted by user request.");
            }
            finally
            {

                if (wasGameStarted == false)
                {
                    n.CloseConnection();
                    Dispatcher.Invoke(addMessage, "Network connection searching was disabled.");
                }
            }
            DebuggerIX.WriteLine(DebuggerTag.Net, role, "Finished");
        }