SHARE
TWEET

GameSparks Client Proxy

a guest May 25th, 2018 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //in main "Awake" method:
  2.  
  3. //Set proxy address
  4. ProxyAddress.addressServerEndPoint = ...
  5.  
  6. GameSparks.RT.Connection.ProxyTool.TcpProxy = TcpProxy;
  7. GameSparks.RT.Connection.ProxyTool.UdpProxy = UdpProxy;
  8.  
  9. private IPEndPoint TcpProxy(IPEndPoint endPoint)
  10. {
  11.     return ProxyAddress.GetTcp(endPoint);
  12. }
  13.  
  14. private IPEndPoint UdpProxy(IPEndPoint endPoint)
  15. {
  16.     // Like userID + matchID to avoid collisions
  17.     var id = ...
  18.  
  19.     return ProxyAddress.GetUdp(id, endPoint);
  20. }
  21.  
  22. public static class ProxyAddress
  23. {
  24.     public static IPEndPoint addressServerEndPoint;
  25.  
  26.     public static byte tryCount = 20;
  27.  
  28.     public static ushort retryDelayMs = 100;
  29.  
  30.     private class Context
  31.     {
  32.         internal readonly UdpClient socket;
  33.  
  34.         internal readonly string request;
  35.  
  36.         internal volatile string result;
  37.  
  38.         internal Context(UdpClient socket, string request)
  39.         {
  40.             this.socket = socket;
  41.             this.request = request;
  42.         }
  43.     }
  44.  
  45.     public static IPEndPoint GetTcp(IPEndPoint address)
  46.     {
  47.         return Get("tcp", null, address);
  48.     }
  49.  
  50.     public static IPEndPoint GetUdp(string id, IPEndPoint address)
  51.     {
  52.         return Get("udp", id, address);
  53.     }
  54.  
  55.     private static IPEndPoint Get(string protocol, string id, IPEndPoint address)
  56.     {
  57.         var request = protocol + address;
  58.         if (!string.IsNullOrEmpty(id)) request += ':' + id;
  59.  
  60.         var context = new Context(new UdpClient(addressServerEndPoint.Address.ToString(), addressServerEndPoint.Port), request);
  61.  
  62.         context.socket.BeginReceive(OnDataReceive, context);
  63.  
  64.         var bytes = Encoding.ASCII.GetBytes(context.request);
  65.         for (var i = 0; i < tryCount; i++)
  66.         {
  67.             context.socket.Send(bytes, bytes.Length);
  68.  
  69.             Thread.Sleep(retryDelayMs);
  70.  
  71.             if (context.result != null)
  72.             {
  73.                 context.socket.Close();
  74.  
  75.                 var args = context.result.Split(':');
  76.  
  77.                 return new IPEndPoint(IPAddress.Parse(args[0]), ushort.Parse(args[1]));
  78.             }
  79.         }
  80.  
  81.         return new IPEndPoint(0, 0);
  82.     }
  83.  
  84.     private static void OnDataReceive(IAsyncResult result)
  85.     {
  86.         var context = (Context) result.AsyncState;
  87.  
  88.         if (context.result == null)
  89.         {
  90.             try
  91.             {
  92.                 var remoteEndPoint = new IPEndPoint(0, 0);
  93.                 var data = context.socket.EndReceive(result, ref remoteEndPoint);
  94.  
  95.                 if (data != null && data.Length > 4)
  96.                 {
  97.                     var responseString = Encoding.ASCII.GetString(data);
  98.  
  99.                     var response = responseString.Split(new[] {"/"}, StringSplitOptions.RemoveEmptyEntries);
  100.  
  101.                     if (response.Length == 2 && response[0] == context.request)
  102.                     {
  103.                         if (ushort.TryParse(response[1], out var port))
  104.                         {
  105.                             context.result = remoteEndPoint.Address + ":" + port;
  106.  
  107.                             return;
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.             catch
  113.             {
  114.                 return;
  115.             }
  116.         }
  117.  
  118.         try
  119.         {
  120.             context.socket.BeginReceive(OnDataReceive, context);
  121.         }
  122.         catch
  123.         {
  124.             //Ignore all
  125.         }
  126.     }
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top