Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1.  public void StartHost() {
  2.         try
  3.         {
  4.             ResetTimer(); //To close the connection for timeouts
  5.             HostingThread = new Thread(HostGame);
  6.             HostingThread.Start();
  7.             TimerThread = new Thread(Timeout);
  8.             TimerThread.Start();
  9.         }
  10.         catch { }
  11.     }
  12.  
  13. void HostGame() {
  14.         bool ConnectedTo = false;
  15.         listener = new TcpListener(IPAddress.Any, port);
  16.         listener.Start();
  17.         print("Listener Setup");
  18.         client = listener.AcceptTcpClient();
  19.         print("Listener accepting");
  20.         netS = client.GetStream();
  21.         print("Getting stream");
  22.         sr = new StreamReader(client.GetStream());
  23.         sw = new StreamWriter(client.GetStream());
  24.         //TimerThread.Start();
  25.  
  26.         if (AwaitConnection()) {
  27.             string team = gameManager.playerTeam.ToString();
  28.  
  29.             while (!ConnectedTo)
  30.             {
  31.                 sw.WriteLine("DEPLOY " + team);
  32.                 sw.Flush();
  33.                 if (netS.DataAvailable)
  34.                 {
  35.                     ResetTimer();
  36.                     string request = sr.ReadLine();
  37.                     print(request);
  38.                     string[] tokens = request.Split(' ');
  39.                     if (tokens[0] == "CONNECTED")
  40.                     {
  41.                         ConnectedTo = true;
  42.  
  43.  
  44.                         GetComponent<MenuNavigation>().MultiplayerStart();
  45.  
  46.                     }
  47.                 }
  48.             }
  49.             //gameObject.GetComponent<GameManager>().SetTeam(team);
  50.            
  51.             while (hosting) {
  52.  
  53.                 try {
  54.                     if (netS.DataAvailable) {
  55.                         ResetTimer();
  56.                         string request = sr.ReadLine();
  57.                         print(request);
  58.                         string[] tokens = request.Split(' ');
  59.  
  60.                         if (tokens[0] == "PING")
  61.                         {
  62.                             PingReply();
  63.                         }
  64.                         else if (tokens[0] == "PONG")
  65.                         {
  66.                             //Timer Already set
  67.                         }
  68.                         else if (tokens[0] == "LOSS")
  69.                         {
  70.  
  71.                         }
  72.                        
  73.                         else if (tokens[0] == "MOVE")
  74.                         {
  75.                             gameManager.opponentEndedTurn = true;
  76.                             int index = 0;
  77.                             #region movement
  78.                             if (gameManager.playerTeam == Team.Attacker) {
  79.                                 foreach (FireTeam fT in gameManager.Defenders) {
  80.                                     char[] array;
  81.                                     array = tokens[(index * 4) + 2].ToCharArray();
  82.                                     fT.position[0] = array[0];
  83.  
  84.                                     array = tokens[(index * 4) + 3].ToCharArray();
  85.                                     fT.position[1] = array[0];
  86.  
  87.                                     array = tokens[index * 4 + 4].ToCharArray();
  88.                                     fT.facing = (DirectionFacing)array[0];
  89.  
  90.                                     index++;
  91.                                 }
  92.                             }
  93.  
  94.                             else if (gameManager.playerTeam == Team.Defender)
  95.                             {
  96.                                 foreach (FireTeam fT in gameManager.Attackers)
  97.                                 {
  98.                                     char[] array;
  99.                                     array = tokens[(index * 4) + 2].ToCharArray();
  100.                                     fT.position[0] = array[0];
  101.  
  102.                                     array = tokens[(index * 4) + 3].ToCharArray();
  103.                                     fT.position[1] = array[0];
  104.  
  105.                                     array = tokens[index * 4 + 4].ToCharArray();
  106.                                     fT.facing = (DirectionFacing)array[0];
  107.  
  108.                                     index++;
  109.                                 }
  110.                             }
  111.  
  112.                             #endregion
  113.                         }
  114.                         else if (tokens[0] == "CHAT") {
  115.                             string message = "";
  116.                             for (int i = 1; i < tokens.Length; i++) {
  117.                                 message += tokens[i] + " ";
  118.                             }
  119.                             GetComponent<MenuNavigation>().DisplayChatMessage(message);
  120.                         }
  121.                     }
  122.                    
  123.                 } catch {
  124.  
  125.                 }
  126.             }
  127.         }
  128.  
  129.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement