/// <summary> /// Push socks5 Udp代理 /// </summary> /// <param name="controlClient"></param> /// <param name="clientPort"></param> /// <param name="localIpe"></param> /// <exception cref="System.Net.TunnelStateMissingException"></exception> private void PushUdpClient(TcpClient controlClient, int clientPort, out IPEndPoint serverIpe) { Contract.Requires(_runType != null && controlClient.Connected); var localIpe = new IPEndPoint(((IPEndPoint)controlClient.Client.LocalEndPoint).Address, clientPort); var proxyClient = xHttpHandler.CreateUdpClient(); serverIpe = new IPEndPoint(((IPEndPoint)controlClient.Client.RemoteEndPoint).Address, ((IPEndPoint)proxyClient.Client.LocalEndPoint).Port); var state = new UdpClientState() { LocalEndPoint = localIpe, Client = proxyClient }; if (!_udpClients.TryAdd(controlClient, state)) { throw new TunnelStateMissingException("Udp ProxySock handle invalid") { Client = controlClient.Client }; } this.PushTcpClient(controlClient, new IPEndPoint(IPAddress.None, IPEndPoint.MinPort)); //监听连接状态 controlClient.Client.BeginReceive(xHttpHandler.EmptyBuffer, 0, 0, SocketFlags.None, this.DisconnectCallbak, controlClient); TaskHelper.Factory.StartNew(this.UdpDirectSend, controlClient); TaskHelper.Factory.StartNew(this.UdpDirectReceive, controlClient); }
public static byte[] ReceiveEx(this UdpClient client, ManualResetEvent cancelEvent) { AutoResetEvent connectEvent = new AutoResetEvent(false); var state = new UdpClientState() { Client = client, ConnectEvent = connectEvent }; client.BeginReceive(ReceiveCallback, state); if (WaitHandle.WaitAny(new WaitHandle[] { connectEvent, cancelEvent }) == 1) { client.Close(); return(new byte[0]); } return(state.Buffer); }
private void StopListenForServer() { // close the listener // this is needed to start a new search if (m_AdvertisingListeningClientLAN != null) { m_AdvertisingListeningClientLAN.u.Close(); m_AdvertisingListeningClientLAN = null; } Debug.Log("Finder advertising listener closed"); }
private void StopFindServer() { if (m_BroadcastClientLAN != null) { m_BroadcastClientLAN.u.Close(); m_BroadcastClientLAN = null; } }
private void ListenForServer(int _ListenPort) { System.Diagnostics.Debug.Assert(m_AdvertisingListeningClientLAN == null); // open a listening port on a random port // to receive a response back from server // using 0 doesn't seem to work reliably // so we'll just do it ourselves IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, _ListenPort); UdpClient uc1 = new UdpClient(ep1); UdpClientState ucs1 = new UdpClientState(ep1, uc1); uc1.BeginReceive(new System.AsyncCallback(ListenForServerCallback), ucs1); m_AdvertisingListeningClientLAN = ucs1; Debug.Log("Finder advertising listener opened on port " + _ListenPort); }
private void FindServer(int _ListenPort, int _BroadcastPort) { System.Diagnostics.Debug.Assert(m_BroadcastClientLAN == null); // open a broadcast on known port and // send own broadcast listener port to the LAN IPEndPoint ep2 = new IPEndPoint(IPAddress.Broadcast, _BroadcastPort); UdpClient uc2 = new UdpClient(); // Important! // this is disabled by default // so we have to enable it uc2.EnableBroadcast = true; UdpClientState ucs2 = new UdpClientState(ep2, uc2); byte[] sendBytes = System.BitConverter.GetBytes(_ListenPort); uc2.BeginSend(sendBytes, sendBytes.Length, ep2, new System.AsyncCallback(FindServerCallback), ucs2); m_BroadcastClientLAN = ucs2; Debug.Log("Find server message sent on broadcast port " + _BroadcastPort); }
private void StopRespondClient() { if (m_AdvertisingClientLAN != null) { m_AdvertisingClientLAN.u.Close(); m_AdvertisingClientLAN = null; } }
private void StopListenForClients() { if (m_BroadcastListeningClientLAN != null) { m_BroadcastListeningClientLAN.u.Close(); m_BroadcastListeningClientLAN = null; } Debug.Log("Server listening for clients stopped."); }
private void RespondClient(IPAddress _ClientAddress, int _ClientPort, byte[] _DataToSend) { System.Diagnostics.Debug.Assert(m_AdvertisingClientLAN == null); IPEndPoint ep2 = new IPEndPoint(_ClientAddress, _ClientPort); UdpClient uc2 = new UdpClient(); //@FIXME: doesn't need to pass EndPoint in constructor? UdpClientState ucs2 = new UdpClientState(ep2, uc2); uc2.BeginSend(_DataToSend, _DataToSend.Length, ep2, new System.AsyncCallback(RespondClientCallback), ucs2); m_AdvertisingClientLAN = ucs2; }
//specific to LAN private void ListenForClients(int _ListenPort) { System.Diagnostics.Debug.Assert(m_BroadcastListeningClientLAN == null); // open a listening port on known port // to listen for any clients IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, _ListenPort); UdpClient uc1 = new UdpClient(ep1); UdpClientState ucs1 = new UdpClientState(ep1, uc1); uc1.BeginReceive(new System.AsyncCallback(ListenForClientsCallback), ucs1); m_BroadcastListeningClientLAN = ucs1; Debug.Log("Server listening for clients on port: " + _ListenPort); }