Skip to content

Instantly share code, notes, and snippets.

@TimVosch
Last active April 30, 2016 15:26
Show Gist options
  • Save TimVosch/dd0c8dc30d3767b5a47bcd703f58485a to your computer and use it in GitHub Desktop.
Save TimVosch/dd0c8dc30d3767b5a47bcd703f58485a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ClashRoyaleProxy
{
class UdpProxy
{
private const int PORT = 9339;
private const int MAX_CONNECTIONS = 100;
private static Dictionary<IPEndPoint, BattleClient> battleClientPool = new Dictionary<IPEndPoint, BattleClient>();
public static void Start()
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 9339);
UdpClient Listener = new UdpClient(endPoint);
Logger.Log("[UDP] Successfully bound socket to " + endPoint.Address + "!", LogType.INFO);
Logger.Log("[UDP] Listening for incoming connections..", LogType.INFO);
Listener.BeginReceive(onReceive, Listener);
}
public static void Stop()
{
}
private static void onReceive(IAsyncResult result)
{
var Listener = (UdpClient)result.AsyncState;
var clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
var ReceivedBytes = Listener.EndReceive(result, ref clientEndPoint);
if (!battleClientPool.ContainsKey(clientEndPoint))
{
battleClientPool.Add(clientEndPoint, new BattleClient(clientEndPoint));
Logger.Log("[UDP] New client: " + clientEndPoint.ToString(), LogType.INFO);
}
BattleClient battleClient = battleClientPool[clientEndPoint];
battleClient.parseIncomingPacket(ReceivedBytes);
Listener.BeginReceive(onReceive, Listener);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment