Example #1
0
 public void getMessageFromPlayerClient(Messages.GameMessages message,List<object> data, playerClient client)
 {
     switch (message)
     {
         case Messages.GameMessages.matchCreated:
             broadcast(Messages.GameMessages.gamesUpdated);
             break;
         case Messages.GameMessages.gamesUpdated:
             broadcast(Messages.GameMessages.gamesUpdated);
             break;
         case Messages.GameMessages.SendActiveMatches:
             broadcast(Messages.GameMessages.SendActiveMatches);
             break;
         case Messages.GameMessages.JoinMatch:
             String gameName = (String)data[0];
             Match m = this.getMatch(gameName);
             if (m != null)
                 m.Player2.PlayerName = client.playerName;
             broadcast(Messages.GameMessages.gamesUpdated);
             break;
         default:
             break;
     }
     updateServerForm();
 }
Example #2
0
        public void listenForConnections(TcpListener serverSocket)
        {
            TcpClient clientSocket = null;

            while (!formClosed)
            {
                byte[] bytesFrom = new byte[10025], bytesTo = new byte[10025];
                try
                {
                    //Call this method in a separate thread so that the loop doesn't get blocked by acceptTcpClient call
                    //Thread socketThread = new Thread(() => (new accepter(acceptTcpClient)).Invoke(ref clientSocket, serverSocket));
                    //socketThread.IsBackground = true;

                    acceptTcpClient(ref clientSocket, serverSocket);
                    //If server is open and we havent obtained a connection sleep this thread 
                    while (!formClosed && clientSocket == null)
                        Thread.Sleep(500);

                    if (formClosed)
                        serverThread.Abort();
                    else if (clientSocket != null)
                    {                        
                        bytesFrom = new byte[10025];
                        string dataFromPlayer = null;

                        dataFromPlayer = getData(clientSocket);

                        //Check if player with same name exists
                        if (playerSocketList.FindIndex(n => n.Key.Equals(dataFromPlayer)) != -1)
                        {
                            sendData(Messages.GameMessages.playerExists.ToString(), clientSocket);
                            String ack = getData(clientSocket);
                            sendFeedback("listenForConnections","Player Exists");

                            clientSocket.Client.Disconnect(true);
                            clientSocket = new TcpClient();
                        }
                        else
                        {
                            sendFeedback("listenForConnections", "New Player:"+dataFromPlayer);
                            sendData(Messages.GameMessages.connected.ToString(), clientSocket);

                            //Add to list using name as key
                            //Start listening to player
                            playerClient client = new playerClient(this, clientSocket, dataFromPlayer);
                            playerSocketList.Add(new KeyValuePair<string, playerClient>(dataFromPlayer,                                                                                             client));
                            Console.WriteLine(dataFromPlayer);
                            client.FeedbackWriter = this.FeedbackWriter;
                            client.start();

                            //Add name to player list, use thread safe way
                            updateServerForm();
                        }
                    }//End socket if
                }//End Try
                catch (Exception ex)
                {
                    sendFeedback("listenForConnections", "Exception:" + ex.InnerException) ;
                    Console.WriteLine(ex.ToString());
                }
            }//End while

            if (clientSocket != null)
                clientSocket.Close();
        }