public void PointToPointPerfTest() { Channel <int> channel = new Channel <int>(); IFiber bus = new PoolFiber(); bus.Start(); int max = 5000000; AutoResetEvent reset = new AutoResetEvent(false); Action <int> onMsg = delegate(int count) { if (count == max) { reset.Set(); } }; channel.Subscribe(bus, onMsg); using (new PerfTimer(max)) { for (int i = 0; i <= max; i++) { channel.Publish(i); } Assert.IsTrue(reset.WaitOne(30000, false)); } }
public void SynchronousRequestWithMultipleReplies() { IFiber responder = new PoolFiber(); responder.Start(); var countChannel = new RequestReplyChannel <string, int>(); var allSent = new AutoResetEvent(false); Action <IRequest <string, int> > onRequest = delegate(IRequest <string, int> req) { for (var i = 0; i <= 5; i++) { req.SendReply(i); } allSent.Set(); }; countChannel.Subscribe(responder, onRequest); var response = countChannel.SendRequest("hello"); int result; using (response) { for (var i = 0; i < 5; i++) { Assert.IsTrue(response.Receive(10000, out result)); Assert.AreEqual(result, i); } allSent.WaitOne(10000, false); } Assert.IsTrue(response.Receive(30000, out result)); Assert.AreEqual(5, result); Assert.IsFalse(response.Receive(30000, out result)); }
public void Run() { MmoWorld world; MmoWorldCache.Instance.Clear(); MmoWorldCache.Instance.TryCreate("TestWorld", (new[] { 0f, 0f }).ToVector(), (new[] { 299f, 299f }).ToVector(), (new[] { 10f, 10f }).ToVector(), out world); List<Client> clients = SetupClients(world); Stopwatch t = Stopwatch.StartNew(); using (var fiber = new PoolFiber(new FailSafeBatchExecutor())) { fiber.Start(); fiber.ScheduleOnInterval(() => PrintStatsPerSecond(t), 1000, 1000); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); log.Info("move completed"); Assert.AreEqual(0, Client.Exceptions, "Exceptions occured at exit"); DisconnectClients(clients); } }
public void InOrderExecution() { PoolFiber fiber = new PoolFiber(new DefaultThreadPool(), new BatchExecutor()); fiber.Start(); int count = 0; AutoResetEvent reset = new AutoResetEvent(false); List <int> result = new List <int>(); Command command = delegate { result.Add(count++); if (count == 100) { reset.Set(); } }; for (int i = 0; i < 100; i++) { fiber.Enqueue(command); } Assert.IsTrue(reset.WaitOne(10000, false)); Assert.AreEqual(100, count); }
public void SingleConsumerWithException() { var exec = new StubExecutor(); var one = new PoolFiber(new DefaultThreadPool(), exec); one.Start(); var reset = new AutoResetEvent(false); using (one) { var channel = new QueueChannel <int>(); Action <int> onMsg = delegate(int num) { if (num == 0) { throw new Exception(); } reset.Set(); }; channel.Subscribe(one, onMsg); channel.Publish(0); channel.Publish(1); Assert.IsTrue(reset.WaitOne(10000, false)); Assert.AreEqual(1, exec.failed.Count); } }
public void SingleConsumer() { var one = new PoolFiber(); one.Start(); var oneConsumed = 0; var reset = new AutoResetEvent(false); using (one) { var channel = new QueueChannel <int>(); Action <int> onMsg = delegate { oneConsumed++; if (oneConsumed == 20) { reset.Set(); } }; channel.Subscribe(one, onMsg); for (var i = 0; i < 20; i++) { channel.Publish(i); } Assert.IsTrue(reset.WaitOne(10000, false)); } }
public IPluginFiber CreateFiber() { var fiber = new PoolFiber(); fiber.Start(); return(new PluginFiber(fiber)); }
public static void Run() { loopFiber = new PoolFiber(); loopFiber.Start(); updateLoop = loopFiber.ScheduleOnInterval(Update, 0, 1000); }
public void RunForTime() { WorldCache.Instance.Clear(); World world; WorldCache.Instance.TryCreate("TestWorld", new BoundingBox(new Vector(0f, 0f), new Vector(400f, 400f)), new Vector(40f, 40f), out world); List <Client> clients = SetupClients(world); Stopwatch t = Stopwatch.StartNew(); using (var fiber = new PoolFiber(new FailSafeBatchExecutor())) { fiber.Start(); fiber.ScheduleOnInterval(() => PrintStatsPerSecond(t), 1000, 1000); while (t.ElapsedMilliseconds < 10000) { MoveClients(clients); PrintStats(t); } } DisconnectClients(clients); }
public void Run() { World world; WorldCache.Instance.Clear(); WorldCache.Instance.TryCreate("TestWorld", new BoundingBox(new Vector(0f, 0f), new Vector(400f, 400f)), new Vector(40f, 40f), out world); List <Client> clients = SetupClients(world); Stopwatch t = Stopwatch.StartNew(); using (var fiber = new PoolFiber(new FailSafeBatchExecutor())) { fiber.Start(); fiber.ScheduleOnInterval(() => PrintStatsPerSecond(t), 1000, 1000); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); MoveClients(clients); PrintStats(t); log.Info("move completed"); Assert.AreEqual(0, Client.Exceptions, "Exceptions occured at exit"); DisconnectClients(clients); } }
public MonitorRequestHandler(PhotonCloudApp application) { fiber = new PoolFiber(); fiber.Start(); photonCloudApp = application; MonitoringService monitoringService = new MonitoringService(Settings.Default.MonitoringApiEndpoint, 5000); this.MonitoringCache = MonitoringCache.CreateCache(monitoringService); }
public FooForm() { InitializeComponent(); _ServerMessageChannel = new Channel <string>(); _WorkFiber = new PoolFiber(); _FormFiber = new _Fiber = new FormFiber(this, new BatchAndSingleExecutor()); _WorkFiber.Start(); //begin recive messages _FormFiber.Start(); //begin recive messages _WorkFiber.Enqueue(LissenToServer); _ServerMessageChannel.Subscribe(_FormFiber, (x) => textBox1.Text = x); }
public void ExecuteOnlyAfterStart() { PoolFiber fiber = new PoolFiber(); AutoResetEvent reset = new AutoResetEvent(false); fiber.Enqueue(delegate { reset.Set(); }); Assert.IsFalse(reset.WaitOne(1, false)); fiber.Start(); Assert.IsTrue(reset.WaitOne(1000, false)); fiber.Stop(); }
/// <summary> /// Starts this instance to collect and publish counter data. /// </summary> public void Start() { lock (syncRoot) { if (timerControl == null) { Interlocked.Exchange(ref state, 1L); fiber.Start(); timerControl = fiber.ScheduleOnInterval(Publish, MaxRetryCount * 1000, MaxRetryCount * 1000); } } }
public Game(MarsPeer peer) { this.marsPeer = peer; if (poolFiber == null) { poolFiber = new PoolFiber(); poolFiber.Start(); } /*this.poolFiber = new PoolFiber(); * this.poolFiber.Start();*/ }
public void RequestReply() { using (var fiber = new PoolFiber()) { fiber.Start(); var channel = new RequestReplyChannel <string, string>(); channel.Subscribe(fiber, req => req.SendReply("bye")); var reply = channel.SendRequest("hello"); string result; Assert.IsTrue(reply.Receive(10000, out result)); Assert.AreEqual("bye", result); } }
public void PubSubWithPool() { //PoolFiber uses the .NET thread pool by default using (var fiber = new PoolFiber()) { fiber.Start(); var channel = new Channel <string>(); var reset = new AutoResetEvent(false); channel.Subscribe(fiber, delegate { reset.Set(); }); channel.Publish("hello"); Assert.IsTrue(reset.WaitOne(5000, false)); } }
public void SynchronousRequestReply() { var responder = new PoolFiber(); responder.Start(); var timeCheck = new RequestReplyChannel <string, DateTime>(); var now = DateTime.Now; Action <IRequest <string, DateTime> > onRequest = req => req.SendReply(now); timeCheck.Subscribe(responder, onRequest); var response = timeCheck.SendRequest("hello"); DateTime result; Assert.IsTrue(response.Receive(10000, out result)); Assert.AreEqual(result, now); }
public void ShouldIncreasePoolFiberSubscriberCountByOne() { var fiber = new PoolFiber(); fiber.Start(); var channel = new Channel <int>(); Assert.AreEqual(0, fiber.NumSubscriptions); Assert.AreEqual(0, channel.NumSubscribers); channel.Subscribe(fiber, x => { }); Assert.AreEqual(1, fiber.NumSubscriptions); Assert.AreEqual(1, channel.NumSubscribers); fiber.Dispose(); Assert.AreEqual(0, fiber.NumSubscriptions); Assert.AreEqual(0, channel.NumSubscribers); }
public void UnsubscriptionShouldRemoveSubscriber() { var fiber = new PoolFiber(); fiber.Start(); var channel = new Channel <int>(); Assert.AreEqual(0, fiber.NumSubscriptions); Assert.AreEqual(0, channel.NumSubscribers); var unsubscriber = channel.Subscribe(fiber, x => { }); Assert.AreEqual(1, fiber.NumSubscriptions); Assert.AreEqual(1, channel.NumSubscribers); unsubscriber.Dispose(); Assert.AreEqual(0, fiber.NumSubscriptions); Assert.AreEqual(0, channel.NumSubscribers); }
public void BasicPubSubWithPoolQueue() { IFiber queue = new PoolFiber(); queue.Start(); Channel <string> hello = new Channel <string>(); Channel <string> hello2 = new Channel <string>(); AutoResetEvent reset = new AutoResetEvent(false); Action <string> receiveHello = delegate(string str) { Assert.AreEqual("hello", str); reset.Set(); }; hello.Subscribe(queue, receiveHello); hello2.Subscribe(queue, receiveHello); Assert.IsTrue(hello.Publish("hello")); Assert.IsTrue(reset.WaitOne(10000, false)); queue.Dispose(); }
public static void Main(string[] args) { Setup(); fiber.Start(); //fiber.ScheduleOnInterval() for (int i = 0; i < Settings.NumGames; i++) { fiber.Enqueue(StartGame); } Console.WriteLine("[{0}] Press Return to End", Process.GetCurrentProcess().Id); Console.ReadLine(); ///log.InfoFormat("Stopping {0} clients", clients.Count); fiber.Enqueue(StopGames); // wait for stop to complete resetEvent.WaitOne(); Console.ReadLine(); }
public void Multiple() { var queues = new List <IFiber>(); var receiveCount = 0; var reset = new AutoResetEvent(false); var channel = new QueueChannel <int>(); var messageCount = 100; var updateLock = new object(); for (var i = 0; i < 5; i++) { Action <int> onReceive = delegate { Thread.Sleep(15); lock (updateLock) { receiveCount++; if (receiveCount == messageCount) { reset.Set(); } } }; var fiber = new PoolFiber(); fiber.Start(); queues.Add(fiber); channel.Subscribe(fiber, onReceive); } for (var i = 0; i < messageCount; i++) { channel.Publish(i); } Assert.IsTrue(reset.WaitOne(10000, false)); queues.ForEach(delegate(IFiber q) { q.Dispose(); }); }
private Nami() { Fiber = new PoolFiber(); Fiber.Start(); }
public void RunForTime() { MmoWorldCache.Instance.Clear(); MmoWorld world; MmoWorldCache.Instance.TryCreate("TestWorld", (new[] { 0f, 0f }).ToVector(), (new[] { 299f, 299f }).ToVector(), (new[] { 10f, 10f }).ToVector(), out world); List<Client> clients = SetupClients(world); Stopwatch t = Stopwatch.StartNew(); using (var fiber = new PoolFiber(new FailSafeBatchExecutor())) { fiber.Start(); fiber.ScheduleOnInterval(() => PrintStatsPerSecond(t), 1000, 1000); while (t.ElapsedMilliseconds < 10000) { MoveClients(clients); PrintStats(t); } } DisconnectClients(clients); }
private void StartService() { ServiceFiber.Start(); ServiceFiber.Enqueue(Service); }
private void StartExecute() { ExecuteFiber.Start(); }
private static void Main(string[] args) { var now = DateTime.Now; Log.Info($"Start at {now:HH:mm:ss}"); /*Nami.Delay(20000).Do(() => * { * foreach (var s in new[] {1, 2, 3}) * { * var ss = s; * Nami.Delay(2000).Do(() => * { * Loop(ss, 0); * }); * } * });*/ Nami.Every(450).Milliseconds().Times(2).Do(() => { PrintData("Every 450 Milliseconds", DateTime.Now); }); Nami.Every(1).Seconds().Times(3).Do(() => { PrintData("Every 1 Seconds Times 3", DateTime.Now); }); Nami.Every(10).Minutes().Do(() => { PrintData("Every 10 Minutes", DateTime.Now); }); Nami.Every(10).Minutes().AfterExecuteTask().Do(() => { PrintData("Every 10 Minutes and AfterExecuteTask(didn't work)", DateTime.Now); Thread.Sleep(4 * 60 * 1000); }); Nami.Every(600).Seconds().AfterExecuteTask().Do(() => { PrintData("Every 600 Seconds and sleep 4 Minutes", DateTime.Now); Thread.Sleep(4 * 60 * 1000); }); Nami.Delay(4000).Times(4).Do(() => { PrintData("Delay 4000 ms Times 4", DateTime.Now); }); now = now.AddSeconds(17).AddMilliseconds(100); Nami.EveryMonday().At(now.Hour, now.Minute, now.Second).Do(() => { PrintData("Monday", DateTime.Now); }); Nami.EveryTuesday().At(now.Hour, now.Minute, now.Second).Do(() => { PrintData("Tuesday", DateTime.Now); }); Nami.EveryWednesday().At(now.Hour, now.Minute, now.Second) .Do(() => { PrintData("Wednesday", DateTime.Now); }); Nami.EveryThursday().At(now.Hour, now.Minute, now.Second) .Do(() => { PrintData("Thursday", DateTime.Now); }); Nami.EveryFriday().At(now.Hour, now.Minute, now.Second).Do(() => { PrintData("Friday", DateTime.Now); }); Nami.EverySaturday().At(now.Hour, now.Minute, now.Second) .Do(() => { PrintData("Saturday", DateTime.Now); }); Nami.EverySunday().At(now.Hour, now.Minute, now.Second).Do(() => { PrintData("Sunday", DateTime.Now); }); now = now.AddSeconds(1); Nami.Every(1).Hours().At(now.Hour, now.Minute, now.Second).Do(() => { PrintData("Every 1 Hours", DateTime.Now); }); now = now.AddSeconds(1); Nami.Every(1).Days().At(now.Hour, now.Minute, now.Second) .Do(() => { PrintData("Every 1 Days", DateTime.Now); }); now = now.AddSeconds(1); Nami.Everyday().At(now.Hour, now.Minute, now.Second).Do(() => { PrintData("Everyday", DateTime.Now); }); Console.ReadKey(); Nami.EverySunday().At(6, 0, 0).Do(() => { PrintData("EverySunday().Days().At(6,0,0) ", DateTime.Now); }); Nami.EveryMonday().At(6, 0, 0).Do(() => { PrintData("EveryMonday().Days().At(6,0,0) ", DateTime.Now); }); Nami.EveryMonday().At(12, 0, 0).Do(() => { PrintData("EveryMonday().Days().At(12,0,0) ", DateTime.Now); }); Nami.EveryTuesday().At(6, 0, 0).Do(() => { PrintData("EveryMonday().Days().At(6,0,0) ", DateTime.Now); }); Nami.EveryTuesday().At(12, 0, 0) .Do(() => { PrintData("EveryMonday().Days().At(12,0,0) ", DateTime.Now); }); Nami.Everyday().At(6, 0, 0).Do(() => { PrintData("Everyday().At(6,0,0) ", DateTime.Now); }); Nami.Every(1).Days().Do(() => { PrintData("Every(1).Days() ", DateTime.Now); }); Nami.Every(1).Days().At(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second + 2).Do(() => { PrintData("Every(1).Days().At ", DateTime.Now); }); Nami.Every(2).Days().At(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second + 2).Do(() => { PrintData("Every(2).Days().At ", DateTime.Now); }); Nami.Every(1).Hours().At(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second + 2).Do(() => { PrintData("Every Hours ", DateTime.Now); }); Nami.Every(2).Hours().At(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second + 2).Do(() => { PrintData("Every 2 Hours ", DateTime.Now); }); Nami.Every(1).Minutes().Do(() => { PrintData("Every(1).Minutes()", DateTime.Now); }); Nami.Every(2).Minutes().Do(() => { PrintData("Every(2).Minutes()", DateTime.Now); }); Nami.Delay(1).Seconds().Times(5).Do(() => { Log.Info($"Nami.Delay(1).Seconds() {DateTime.Now:HH:mm:ss}"); }); var d = Nami.Delay(1).Seconds().Times(5).Do(() => { PrintData(" Dispose ", DateTime.Now); }); d.Dispose(); Nami.Delay(30).Seconds().Times(5).Do(() => { PrintData("Delay(30) 5 times", DateTime.Now); }); Nami.RightNow().Times(3).Do(() => { PrintData("RightNow 3 times", DateTime.Now); }); Nami.RightNow().Do(() => { PrintData("Just RightNow ", DateTime.Now); }); Nami.Delay(1000).Milliseconds().Do(() => { PrintData($"Just Delay(1000) execute:{DateTime.Now:HH:mm:ss.fff}", DateTime.Now); }); Nami.Every(60000).Milliseconds().Do(() => { PrintData("Just Every(60000).Milliseconds() ", DateTime.Now); }); Nami.Everyday().At(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second + 5) .Do(() => { PrintData("Everyday ", DateTime.Now); }); Console.ReadKey(); IFiber pool = new PoolFiber(); IFiber thread = new ThreadFiber(); pool.Start(); thread.Start(); pool.ScheduleOnInterval(() => { PrintData("pool ", DateTime.Now); }, 0, 150000); var td = thread.ScheduleOnInterval(() => { PrintData("thread", DateTime.Now); }, 0, 100000); thread.ScheduleOnInterval(() => { PrintData("thread ten second", DateTime.Now); }, 0, 100000); for (var i = 0; i < 100; i++) { var i1 = i; thread.Enqueue(() => { PrintData($"thread {i1}", DateTime.Now); }); } pool.Schedule(() => { Console.WriteLine($"td Dispose"); td.Dispose(); for (var i = 0; i < 10; i++) { thread.Enqueue(() => { PrintData("Schedule start thread ", DateTime.Now); }); } }, 20000); Nami.Every(10).Seconds().AfterExecuteTask().Do(() => { RunSleepCron("Af", 10, 600); }); Nami.Every(10).Seconds().AfterExecuteTask().Do(() => { RunSleepCron("Af", 10, 600); }); Nami.Every(10).Seconds().BeforeExecuteTask().Do(() => { RunSleepCron("Be", 10, 0); }); Nami.Every(10).Seconds().BeforeExecuteTask().Do(() => { RunSleepCron("Be", 10, 0); }); Nami.Every(100).Seconds().Do(() => { thread.Enqueue(() => { PrintData(" Nami.Every(1).Seconds().Do ", DateTime.Now); }); }); Nami.Every(150).Seconds().Do(() => { PrintData("Nami ", DateTime.Now); }); Nami.Every(1).Hours().At(0, 02, 0).Do(() => { PrintData("Hours 2", DateTime.Now); }); Nami.Delay(1500).Do(() => { PrintData("Delay ", DateTime.Now); }); Nami.EveryTuesday().At(14, 13, 40).Do(() => { PrintData("Nami.EveryTuesday().At(n, n, n) ", DateTime.Now); }); Nami.Every(1).Minutes().Do(() => { PrintData("Nami.Every(1).Minutes()", DateTime.Now); }); Nami.Every(2).Minutes().At(0, 0, 15).Do(() => { PrintData("Nami.Every(2).Minutes().At(0,0,15)", DateTime.Now); }); Nami.Delay(1000).Do(() => { PrintData("Delay ", DateTime.Now); }); Nami.Delay(2500).Do(() => { PrintData("Delay ", DateTime.Now); }); Nami.Delay(3500).Do(() => { PrintData("Delay ", DateTime.Now); }); Nami.Delay(4500).Do(() => { PrintData("Delay ", DateTime.Now); }); //thread.Dispose(); Console.ReadKey(); }
/// <summary> /// Start executing callbacks. /// </summary> public void Start() { fiber.Start(); running = true; }
public GameRoom(int id) { this.id = id; ExecutionFiber = new PoolFiber(); ExecutionFiber.Start(); }
/// <summary> /// The main entry point for the application. /// </summary> /// <param name="args"> /// The args. /// </param> private static void Main(params string[] args) { LogManager.SetLoggerFactory(ExitGames.Logging.Log4Net.Log4NetLoggerFactory.Instance); Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory); // Set logfile name and application name variables FileInfo configFileInfo; try { // IOException if another process already writes to the file if (File.Exists(@"log\" + firstGameName + ".log")) { File.Delete(@"log\" + firstGameName + ".log"); } GlobalContext.Properties["LogName"] = firstGameName + ".log"; configFileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config")); } catch (IOException) { GlobalContext.Properties["LogName"] = baseGameName + ".log"; // just write to console for now configFileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4netConsole.config")); } XmlConfigurator.ConfigureAndWatch(configFileInfo); AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WindowsCounters.Initialize(); int index = 0; if (args.Length > index) { Settings.TestCase = args[index]; } index++; if (args.Length > index) { Settings.NumGames = byte.Parse(args[index]); } index++; if (args.Length > index) { Settings.NumClientsPerGame = byte.Parse(args[index]); } index++; if (args.Length > index) { Settings.ServerAddress = args[index]; } index++; if (args.Length > index) { Settings.FlushInterval = int.Parse(args[index]); } index++; if (args.Length > index) { Settings.SendReliableData = bool.Parse(args[index]); } index++; if (args.Length > index) { Settings.ReliableDataSendInterval = int.Parse(args[index]); } index++; if (args.Length > index) { Settings.SendUnreliableData = bool.Parse(args[index]); } index++; if (args.Length > index) { Settings.UnreliableDataSendInterval = int.Parse(args[index]); } Console.WriteLine("Test case: " + Settings.TestCase); Console.WriteLine("Settings: {0} games per process, {1} players per game, game server at {2}", Settings.NumGames, Settings.NumClientsPerGame, Settings.ServerAddress); if (Settings.SendReliableData) { Console.WriteLine("Sending reliable operation every {0} ms", Settings.ReliableDataSendInterval); } if (Settings.SendUnreliableData) { Console.WriteLine("Sending unreliable operation every {0} ms", Settings.UnreliableDataSendInterval); } Console.WriteLine(); stopped = false; fiber.Start(); counterLoggingFiber.Start(); counterLoggingFiber.ScheduleOnInterval(PrintCounter, Settings.LogCounterInterval, Settings.LogCounterInterval); try { log.InfoFormat("Starting {0} games with {1} players", Settings.NumGames, Settings.NumClientsPerGame); for (int g = 0; g < Settings.NumGames; g++) { fiber.Enqueue(StartGame); } ////log.InfoFormat("{0} clients running", clients.Count); Console.WriteLine("[{0}] Press Return to End", Process.GetCurrentProcess().Id); Console.ReadLine(); ////log.InfoFormat("Stopping {0} clients", clients.Count); StopGames(); fiber.Stop(); // wait for stop to complete resetEvent.WaitOne(); } catch (Exception e) { log.Error(e); } finally { log.InfoFormat("[{0}] Stopped", Process.GetCurrentProcess().Id); } }