/// <summary> /// 自己管理生命周期的对象池例子 /// </summary> private void CustomPoolExample() { Log.L(""); Log.L("————这个例子讲述如何使用自己管理生命周期的对象池————"); Log.L("创建一个自己管理生命周期的对象池"); MyPool pool = new MyPool(); Log.L("从对象池中取一个对象"); var _obj = pool.Get(); Log.L($"取出的对象类型为 {_obj.GetType()}\n"); Log.L("将对象归还到对象池"); pool.Set(_obj); Log.L("再从对象池中取一个对象"); _obj = pool.Get(); }
/// <summary>异步发出请求,并接收响应</summary> /// <param name="url">地址</param> /// <param name="data">POST数据</param> /// <returns></returns> public async Task <Byte[]> SendAsync(String url, Byte[] data) { var client = _Pool.Get(); try { // 发出请求 var rs = await SendAsync(url, data, client); if (rs == null || rs.Count == 0) { return(null); } return(rs.ToArray()); } finally { _Pool.Put(client); } }
private void NormalTest() { Log.L("Create a auto create pool"); MyPool pool = new MyPool(); Log.L("Get an instance from pool"); var _obj = pool.Get(); Log.L($"the type of instance is {_obj.GetType()}"); Log.L("Let's put the instance to pool"); pool.Set(_obj); }
BulletScript OnBulletSpawned(BulletObject bullet, string bulletName) { // Get a GameObject from the pool // bulletName and bullet can help you identify the bullet you want // TODO: Your pool here GameObject go = MyPool.Get(); // Make sure this GameObject has a BulletScript and return it // BulletScript can be added on the fly, there is no special parameter to pass BulletScript bulletScript = go.GetComponent <BulletScript>(); if (bulletScript == null) { bulletScript = go.AddComponent <BulletScript>(); } return(bulletScript); }
/// <summary>打开客户端</summary> public virtual Boolean Open() { if (Active) { return(true); } lock (Root) { if (Active) { return(true); } var ss = Servers; if (ss == null || ss.Count == 0) { throw new ArgumentNullException(nameof(Servers), "未指定服务端地址"); } if (Pool == null) { Pool = new MyPool { Host = this } } ; if (Encoder == null) { Encoder = new JsonEncoder(); } //if (Encoder == null) Encoder = new BinaryEncoder(); if (Handler == null) { Handler = new ApiHandler { Host = this } } ; if (StatInvoke == null) { StatInvoke = new PerfCounter(); } if (StatProcess == null) { StatProcess = new PerfCounter(); } if (StatSend == null) { StatSend = new PerfCounter(); } if (StatReceive == null) { StatReceive = new PerfCounter(); } Encoder.Log = EncoderLog; var ct = Pool.Get(); try { // 打开网络连接 if (!ct.Open()) { return(false); } } finally { Pool.Put(ct); } ShowService(); var ms = StatPeriod * 1000; if (ms > 0) { _Timer = new TimerX(DoWork, null, ms, ms) { Async = true } } ; return(Active = true); } }