Guest User

Untitled

a guest
May 12th, 2014
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7.  
  8. using System.Linq;
  9.  
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12.  
  13. public class ElationNetwork : MonoBehaviour
  14. {
  15.  
  16.     public enum Modes { Client, Server, Dedicated };
  17.     private Modes _Mode = Modes.Client;
  18.  
  19.     private string serverHost = "";
  20.     public string ServerHost { get { return serverHost; } }
  21.  
  22.     private int serverPort = 37659;
  23.     public int ServerPort { get { return serverPort; } }
  24.  
  25.     #region Stats
  26.     private int _Ping = 0;
  27.     private int _RPCIn = 0;
  28.     private int _RPCOut = 0;
  29.     private int _RPCAcks = 0;
  30.     private int _NetworkBytesIn = 0;
  31.     private int _NetworkBytesOut = 0;
  32.  
  33.     public string Ping { get { return "Ping: " + _Ping.ToString(); } }
  34.     public string RPCIn { get { return "RPCIn: " + _RPCIn.ToString(); } }
  35.     public string RPCOut { get { return "RPCOut: " + _RPCOut.ToString(); } }
  36.     public string RPCAcks { get { return "RPCAcks: " + _RPCAcks.ToString(); } }
  37.     public string NetworkBytesIn { get { return "Network In (bps): " + _NetworkBytesIn.ToString(); } }
  38.     public string NetworkBytesOut { get { return "Network Out (bps): " + _NetworkBytesOut.ToString(); } }
  39.     public string Mode { get { return "Network Mode: " + _Mode.ToString(); } }
  40.     #endregion
  41.  
  42.     #region GUI
  43.     public NoesisGUIPanel gui;
  44.     private Noesis.Grid root;
  45.  
  46.     private Noesis.TextBlock tbRPC_In;
  47.     private Noesis.TextBlock tbRPC_Out;
  48.     private Noesis.TextBlock tbRPC_Acks;
  49.     private Noesis.TextBlock tbPing;
  50.     private Noesis.TextBlock tbNetworkIn;
  51.     private Noesis.TextBlock tbNetworkOut;
  52.     private Noesis.TextBlock tbMode;
  53.  
  54.     private Noesis.TextBox txtServerHost;
  55.     private Noesis.Button btnConnect;
  56.     private Noesis.Button btnHost;
  57.     #endregion
  58.  
  59.     // Use this for initialization
  60.     void Start()
  61.     {
  62.         StartCoroutine(InitGUI());
  63.     }
  64.  
  65.     private IEnumerator InitGUI()
  66.     {
  67.         yield return new WaitForSeconds(1.0f);
  68.         #region Init GUI Variables
  69.         if (gui)
  70.         {
  71.             root = gui.GetRoot<Noesis.Grid>();
  72.             Noesis.StackPanel spStats = root.FindName<Noesis.StackPanel>("spNetworkStats");
  73.             if (spStats != null)
  74.             {
  75.                 Debug.Log("Found spStats");
  76.                 Noesis.UIElementCollection statLabels = spStats.GetChildren();
  77.                 for (uint i = 0; i < statLabels.Count(); i++)
  78.                 {
  79.                     //Debug.Log("Name: " + statLabels.Get(i).As<Noesis.FrameworkElement>().name));
  80.                 }
  81.                  // TODO -- Sigh.. Why doesn't this work?
  82.                 //tbRPC_In = spStats.FindName<Noesis.TextBlock>("nsRPCIn");
  83.                 tbRPC_In = spStats.GetChildren().Get(0).As<Noesis.TextBlock>();
  84.                 //tbRPC_Out = spStats.FindName<Noesis.TextBlock>("nsRPCOut");
  85.                 tbRPC_Out = spStats.GetChildren().Get(1).As<Noesis.TextBlock>();
  86.                 //tbRPC_Acks = spStats.FindName<Noesis.TextBlock>("nsRPCAcks");
  87.                 tbRPC_Acks = spStats.GetChildren().Get(2).As<Noesis.TextBlock>();
  88.                 //tbNetworkIn = spStats.FindName<Noesis.TextBlock>("nsNetworkIn");
  89.                 tbNetworkIn = spStats.GetChildren().Get(3).As<Noesis.TextBlock>();
  90.                 //tbNetworkOut = spStats.FindName<Noesis.TextBlock>("nsNetworkOut");
  91.                 tbNetworkOut = spStats.GetChildren().Get(4).As<Noesis.TextBlock>();
  92.                 //tbPing = spStats.FindName<Noesis.TextBlock>("nsPing");
  93.                 tbPing = spStats.GetChildren().Get(5).As<Noesis.TextBlock>();
  94.                 tbMode = spStats.GetChildren().Get(6).As<Noesis.TextBlock>();
  95.                 Noesis.StackPanel spStatus = root.FindName<Noesis.StackPanel>("spNetworkStatus");
  96.                 if (spStatus != null)
  97.                 {
  98.                     Debug.Log("Found the Network Status Panel!");
  99.                     Noesis.FrameworkElement fe = Noesis.LogicalTreeHelper.FindName(spStatus, "btnConnect");
  100.                     if (fe != null)
  101.                     {
  102.                         Debug.Log("Found the connect button!");
  103.                     }
  104.                     Noesis.UIElementCollection uiec = spStats.GetChildren();
  105.                     txtServerHost = spStats.FindName<Noesis.TextBox>("txtServerHost");
  106.                     //btnConnect = (Noesis.Button)Noesis.LogicalTreeHelper.FindName(spStatus, "btnConnect");
  107.                     btnConnect = spStats.FindName<Noesis.Button>("btnConnect");
  108.                     btnHost = spStats.FindName<Noesis.Button>("btnHost");
  109.  
  110.                     btnConnect.Click += new Noesis.BaseButton.ClickDelegate(btnConnect_Click);
  111.                     btnHost.Click += new Noesis.BaseButton.ClickDelegate(btnHost_Click);
  112.                 }
  113.  
  114.                 UpdateGUI();
  115.             }
  116.            
  117.         }
  118.         #endregion
  119.  
  120.     }
  121.  
  122.     void btnHost_Click(Noesis.BaseComponent sender, Noesis.RoutedEventArgs e)
  123.     {
  124.         //throw new System.NotImplementedException();
  125.         txtServerHost.SetText("127.0.0.1");
  126.         this._Mode = Modes.Server;
  127.     }
  128.  
  129.     void btnConnect_Click(Noesis.BaseComponent sender, Noesis.RoutedEventArgs e)
  130.     {
  131.         //throw new System.NotImplementedException();
  132.     }
  133.  
  134.     // Update is called once per frame
  135.     void Update()
  136.     {
  137.         _Ping += 1;
  138.         //if (tbPing != null) tbPing.SetText(this.Ping);
  139.         UpdateGUI();
  140.     }
  141.  
  142.     void UpdateGUI()
  143.     {
  144.         if (tbRPC_In != null) tbRPC_In.SetText(this.RPCIn);
  145.         if (tbRPC_Out != null) tbRPC_Out.SetText(this.RPCOut);
  146.         if (tbRPC_Acks != null) tbRPC_Acks.SetText(this.RPCAcks);
  147.         if (tbNetworkIn != null) tbNetworkIn.SetTag(this.NetworkBytesIn);
  148.         if (tbNetworkOut != null) tbNetworkOut.SetTag(this.NetworkBytesOut);
  149.         if (tbPing != null) tbPing.SetText(this.Ping);
  150.         if (tbMode != null) tbMode.SetText(this.Mode);
  151.     }
  152. }
RAW Paste Data