/// <summary> /// 运行时注册监听事件 /// </summary> /// <param name="CheckType"></param> /// <param name="action"></param> public void AddEvent(ListenTypeEnum CheckType, System.Action <Transform> action, bool InvokeOnceOnAddListener = false) { if (action == null) { return; } switch (CheckType) { case ListenTypeEnum.Position: PositionListener.RegisterEvent(action); break; case ListenTypeEnum.Rotation: RotationListener.RegisterEvent(action); break; case ListenTypeEnum.Scale: ScaleListener.RegisterEvent(action); break; default: Debug.LogError("AddEvent Fail... Not Define"); break; } if (InvokeOnceOnAddListener) { action(Target); } }
public async Task <Result <Socket> > ConnectAsync(int port = 0, string address = null, int timeoutMs = 3000, int maxCycles = -1) { Socket clientSocket = new Socket(_addressFamily, _socketType, _protocolType); var serverPort = ((port > 0) ? port : CliServDefaults.DfltPort); string _ip = address; if (String.IsNullOrEmpty(_ip)) { var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); var ipAddress = ipHostInfo.AddressList .Select(ip => ip) .FirstOrDefault(ip => ip.AddressFamily == _addressFamily); address = ipAddress.ToString(); } //else //{ // return Result.Fail<Socket>("Empty IP Address"); //} ListenTypeEnum lType = (maxCycles > 0 ? ListenTypeEnum.ListenTypeCycle : ListenTypeEnum.ListenTypeDelay); //System.Threading.Thread.Sleep(1000); try { var connectResult = await clientSocket.ConnectWithTimeoutAsync( address, serverPort, lType, timeoutMs) .ConfigureAwait(false); if (connectResult.Success) { System.Diagnostics.Debug.WriteLine("We're good. Returning Socket."); // Notify caller of connection OnConnect?.Invoke(connectResult.Value); } else { Console.WriteLine("Connection Error: " + connectResult.Error); } return(connectResult); } catch (TimeoutException t) { System.Console.WriteLine("Timeout Exception: " + t.Message); return(Result.Fail <Socket>("Connection Timeout." + t.Message)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("We failed to connect."); return(Result.Fail <Socket>("Connection Failure: " + e.Message)); } }
public static async Task <Result <Socket> > ConnectWithTimeoutAsync( this Socket socket, string remoteIpAddress, int port, ListenTypeEnum listenType = ListenTypeEnum.ListenTypeDelay, int timeoutMs = -1) { int cycles = 0; bool sendException = false; do { ++cycles; //Console.WriteLine("Trying to Connect {0}", cycles); System.Diagnostics.Debug.WriteLine("Trying to Connect {0}", cycles); if (listenType == ListenTypeEnum.ListenTypeDelay || (cycles > MaxCycles)) { sendException = true; } try { System.Threading.Thread.Sleep(1000); var connectTask = Task.Factory.FromAsync( socket.BeginConnect, socket.EndConnect, remoteIpAddress, port, null); if (connectTask == await Task.WhenAny(connectTask, Task.Delay(timeoutMs)).ConfigureAwait(false)) { await connectTask.ConfigureAwait(false); } else { if (sendException) { throw new TimeoutException(); } } // We're connected here. Break out of loop and send OK. break; } catch (SocketException ex) { if (sendException) { return(Result.Fail <Socket>($"{ex.Message} ({ex.GetType()})")); } } catch (TimeoutException ex) { if (sendException) { return(Result.Fail <Socket>($"{ex.Message} ({ex.GetType()})")); } } } while (cycles <= MaxCycles); if (!socket.Connected) { System.Diagnostics.Debug.WriteLine("Returning Fail."); return(Result.Fail <Socket>("There was a connection problem.")); } System.Diagnostics.Debug.WriteLine("Returning OK."); return(Result.Ok(socket)); }