private void CreateAsyncSocket(Socket socket, JobProcessor jobProcessor, IPacketAnalyzer packetAnalyzer) { this.asyncSocket = this.CreateAsyncSocket(socket, packetAnalyzer); this.crypto = ((packetAnalyzer == null) ? null : packetAnalyzer.CryptoTransform); if (jobProcessor == null) { this.asyncSocket.PacketReceive += delegate(object sender, EventArgs <ArraySegment <byte> > e) { this.OnPacketReceive(e); }; this.asyncSocket.SocketException += delegate(object sender, EventArgs <Exception> e) { this.OnExceptionOccur(e); }; this.asyncSocket.SocketClose += delegate(object sender, EventArgs e) { this.active = false; this.OnDisconnected(e); }; return; } this.asyncSocket.PacketReceive += delegate(object sender, EventArgs <ArraySegment <byte> > e) { jobProcessor.Enqueue(Job.Create <EventArgs <ArraySegment <byte> > >(new Action <EventArgs <ArraySegment <byte> > >(this.OnPacketReceive), e)); }; this.asyncSocket.SocketException += delegate(object sender, EventArgs <Exception> e) { jobProcessor.Enqueue(Job.Create <EventArgs <Exception> >(new Action <EventArgs <Exception> >(this.OnExceptionOccur), e)); }; this.asyncSocket.SocketClose += delegate(object sender, EventArgs e) { this.active = false; jobProcessor.Enqueue(Job.Create <EventArgs>(new Action <EventArgs>(this.OnDisconnected), e)); }; }
public void FindLocation(Location key, Action <IPEndPoint> callback) { JobProcessor currentProcessor = JobProcessor.Current; this.CategoryLookUp.FindLocation(key.Category, delegate(IPEndPoint endpoint) { if (endpoint == null) { Log <LookUp> .Logger.ErrorFormat("Cannot find category {0}", key.Category); currentProcessor.Enqueue(Job.Create <IPEndPoint>(callback, null)); return; } this.Service.ConnectToIP(endpoint, delegate(Peer peer) { SelectEntity selectop = new SelectEntity { ID = key.ID, Category = key.Category }; selectop.OnComplete += delegate(Operation op) { switch (selectop.Result) { case SelectEntity.ResultCode.Ok: currentProcessor.Enqueue(Job.Create <IPEndPoint>(callback, endpoint)); return; case SelectEntity.ResultCode.Redirect: this.IDLookUp.FindLocation(selectop.RedirectServiceID, callback); return; default: currentProcessor.Enqueue(Job.Create <IPEndPoint>(callback, null)); return; } }; selectop.OnFail += delegate(Operation op) { Log <LookUp> .Logger.ErrorFormat("SelectEntity failed : [{0}], [{1}], {2}, {3}", new object[] { selectop.Result, key, key.ID, key.Category }); currentProcessor.Enqueue(Job.Create <IPEndPoint>(callback, null)); }; this.Service.RequestOperation(peer, selectop); }); }); }
public static void StartService() { try { BService service = new BService(); JobProcessor jobProcessor = new JobProcessor(); jobProcessor.ExceptionOccur += delegate(object sender, EventArgs <Exception> e) { Log <BService> .Logger.Error("StartService", e.Value); }; service.Initialize(jobProcessor); jobProcessor.Start(); Register registerop = new Register(typeof(BService)); registerop.OnComplete += delegate(Operation op) { Log <BService> .Logger.DebugFormat("{0} registered on {1} : port {2}", registerop.Category, registerop.ID, registerop.Port); service.ID = registerop.ID; service.Suffix = registerop.Suffix; service.Start(registerop.Port); }; registerop.OnFail += delegate(Operation op) { Log <BService> .Logger.ErrorFormat("cannot find LocationService or register failed : {0}", registerop.Category); service.Dispose(); }; jobProcessor.Enqueue(Job.Create <string, Register>(new Action <string, Register>(service.RequestOperation), typeof(UnifiedNetwork.LocationService.LookUp).FullName, registerop)); } catch (Exception ex) { Log <BService> .Logger.Error("StartService", ex); } }
public static void Call <T>(Func <T> func, Action <T> onComplete, T exceptionCode) { JobProcessor mainThread = JobProcessor.Current; JobProcessor jobThread = new JobProcessor(); jobThread.ExceptionOccur += delegate(object s, EventArgs <Exception> e) { Log <GuildService> .Logger.Fatal("Exception occurred in AsyncFunc Thread", e.Value); }; jobThread.Start(); jobThread.Enqueue(Job.Create(delegate { T funcResult; try { funcResult = func(); } catch (Exception ex) { Log <GuildService> .Logger.Error("Exception occurred in Async function call", ex); funcResult = exceptionCode; } mainThread.Enqueue(Job.Create(delegate { if (onComplete != null) { onComplete(funcResult); } jobThread.Stop(false); })); })); }
public void Initialize <T>(JobProcessor thread, IDictionary <Type, int> typeConverters) where T : ITcpServer, new() { GlobalContext.Properties["Category"] = this.Category; this.Thread = thread; thread.Enqueue(Job.Create(delegate { System.Threading.Thread.CurrentThread.Name = this.Category; })); //VEH.Install(); this.Thread.ExceptionFilter += this.Thread_ExceptionFilter; this.Thread.ExceptionOccur += this.Thread_ExceptionOccur; this.acceptor = new Acceptor((default(T) == null) ? Activator.CreateInstance <T>() : default(T), thread); this.acceptor.OnAccepted += this.acceptor_OnAccepted; if (typeConverters != null) { this.acceptor.RegisterPipeMessageGroup(typeConverters); } this.acceptor.RegisterPipeMessageGroup(UnifiedNetwork.Cooperation.Message.TypeConverters); if (this.Initializing != null) { this.Initializing(this, EventArgs.Empty); } if (Settings.Default.PrintPerformanceInfo) { new PerformanceTest(this).RegisterPerformanceTestJob(); } this.RegisterMessage(Operations.TypeConverters); this.RegisterProcessor(typeof(RequestLookUpInfo), (Operation x) => new RequsetLookUpInfoProcessor(this, x as RequestLookUpInfo)); this.RegisterProcessor(typeof(RequestUnderingList), (Operation x) => new RequsetUnderingListProcessor(this, x as RequestUnderingList)); this.RegisterProcessor(typeof(RequestOperationTimeReport), (Operation x) => new RequestOperationTimeReportProcessor(this, x as RequestOperationTimeReport)); this.RegisterProcessor(typeof(EnableOperationTimeReport), (Operation x) => new EnableOperationTimeReportProcessor(this, x as EnableOperationTimeReport)); this.RegisterProcessor(typeof(RequestShutDownEntity), (Operation x) => new RequestShutDownEntityProcessor(this, x as RequestShutDownEntity)); }
private void EndConnect(IAsyncResult ar) { object[] array = (object[])ar.AsyncState; Socket socket = (Socket)array[0]; JobProcessor jobProcessor = (JobProcessor)array[1]; IPacketAnalyzer packetAnalyzer = (IPacketAnalyzer)array[2]; if (jobProcessor != null && !jobProcessor.IsInThread()) { jobProcessor.Enqueue(Job.Create <IAsyncResult>(new Action <IAsyncResult>(this.EndConnect), ar)); return; } try { socket.EndConnect(ar); this.CreateAsyncSocket(socket, jobProcessor, packetAnalyzer); this.OnConnectionSucceed(EventArgs.Empty); this.asyncSocket.Activate(); } catch (Exception value) { this.active = false; this.OnConnectionFail(new EventArgs <Exception>(value)); } }
public Test() { Service1 service = new Service1(); Service2 service2 = new Service2(); JobProcessor jobProcessor = new JobProcessor(); jobProcessor.ExceptionOccur += this.thread_ExceptionOccur; jobProcessor.Start(); service.Initialize(jobProcessor, TestOperations.TypeConverters); service2.Initialize(jobProcessor, TestOperations.TypeConverters); service.LookUp.AddLocation(new ServiceInfo { ID = 1, FullName = typeof(Service2).FullName, EndPoint = new IPEndPoint(IPAddress.Loopback, 10001) }); service2.LookUp.AddLocation(new ServiceInfo { ID = 2, FullName = typeof(Service1).FullName, EndPoint = new IPEndPoint(IPAddress.Loopback, 10000) }); service.Start(10000); service2.Start(10001); TestOp t1 = new TestOp(100); t1.OnComplete += delegate(Operation op) { Console.WriteLine("result of t1 : {0}", t1.OutValue); }; TestOp t2 = new TestOp(200); t2.OnComplete += delegate(Operation op) { Console.WriteLine("result of t2 : {0}", t2.OutValue); }; jobProcessor.Enqueue(Job.Create <string, TestOp>(new Action <string, TestOp>(service.RequestOperation), typeof(Service2).FullName, t1)); jobProcessor.Enqueue(Job.Create <string, TestOp>(new Action <string, TestOp>(service2.RequestOperation), typeof(Service1).FullName, t2)); }
public static void StartService() { AService service = new AService(); JobProcessor thread = new JobProcessor(); thread.ExceptionOccur += delegate(object sender, EventArgs <Exception> e) { Log <AService> .Logger.Error("StartService", e.Value); }; service.Initialize(thread); thread.Start(); Register registerop = new Register(typeof(AService)); registerop.OnComplete += delegate(Operation op) { Log <AService> .Logger.DebugFormat("{0} registered on {1} : port {2}", registerop.Category, registerop.ID, registerop.Port); service.ID = registerop.ID; service.Suffix = registerop.Suffix; service.Start(registerop.Port); Plus1 op2 = new Plus1 { Input = 5 }; op2.OnComplete += delegate(Operation x) { Log <AService> .Logger.DebugFormat("result : {0}", op2.Output); }; op2.OnComplete += delegate(Operation x) { Scheduler.Schedule(thread, Job.Create(delegate { service.RequestOperation("B", op2); }), 5000); }; op2.OnFail += delegate(Operation x) { Log <AService> .Logger.Debug("failed"); }; service.RequestOperation("B", op2); }; registerop.OnFail += delegate(Operation op) { Log <AService> .Logger.ErrorFormat("cannot find LocationService or register failed : {0}", registerop.Category); service.Dispose(); }; thread.Enqueue(Job.Create <string, Register>(new Action <string, Register>(service.RequestOperation), typeof(LocationService).FullName, registerop)); }
public static void Schedule(JobProcessor thread, IJob job, int interval, bool instant) { ScheduleRepeater @object = new ScheduleRepeater { thread = thread, job = job, interval = interval }; if (instant) { thread.Enqueue(Job.Create(new Action(@object.Repeat))); return; } Scheduler.Schedule(thread, Job.Create(new Action(@object.Repeat)), interval); }
public static void StartService(string ip, string portstr, Service service, bool bUsePerfLog) { int port = int.Parse(portstr); IPAddress ipaddress = null; foreach (IPAddress ipaddress2 in Dns.GetHostAddresses(ip)) { if (ipaddress2.AddressFamily == AddressFamily.InterNetwork) { ipaddress = ipaddress2; break; } } if (ipaddress == null) { Log <ServiceInvoker> .Logger.ErrorFormat("cannot resolve IPv4 address for hostname [{0}]", ip); return; } IPEndPoint arg = new IPEndPoint(ipaddress, port); if (FeatureMatrix.IsEnable("FeatureMatrixSyncService")) { FeatureMatrix.OverrideFeature(EventLoader.GetStartEventList()); } Type typeFromHandle = typeof(UpdateFeatureMatrix); Func <Operation, OperationProcessor> func = (Operation op) => new UpdateFeatureMatrixProcessor(service, op as UpdateFeatureMatrix); MethodInfo method = service.GetType().GetMethod("RegisterProcessor", BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(service, new object[] { typeFromHandle, func }); JobProcessor jobProcessor = new JobProcessor(); service.Initialize(jobProcessor); UnifiedNetwork.LocationService.LookUp @object = new UnifiedNetwork.LocationService.LookUp(service); jobProcessor.Start(); service.AddBootStep(); jobProcessor.Enqueue(Job.Create <IPEndPoint>(new Action <IPEndPoint>(@object.StartService), arg)); if (bUsePerfLog) { PerformanceLogger performanceLogger = new PerformanceLogger(service, 300000); performanceLogger.Start(); } }
private void StartPartitionMove(int curCount) { if (this.startCount != curCount) { return; } foreach (MockClient mockClient in this.clients.Values) { JobProcessor boundThread = this.thread; if (mockClient.Player.BoundThread != null) { boundThread = mockClient.Player.BoundThread; } boundThread.Enqueue(Job.Create(new Action(mockClient.PartitionMove))); } Scheduler.Schedule(this.thread, Job.Create <int>(new Action <int>(this.StartPartitionMove), curCount), (int)(this.PartitionMoveRatio * 1000.0)); }
protected void RequestOperation(IPEndPoint location, Operation op) { if (location == null) { JobProcessor.Current.Enqueue(Job.Create(new Action(op.IssueFailEvent))); return; } JobProcessor currentProcessor = JobProcessor.Current; this.ConnectToIP(location, delegate(Peer peer) { if (peer == null) { currentProcessor.Enqueue(Job.Create(new Action(op.IssueFailEvent))); return; } this.RequestOperation(peer, op); }); }
private void OnPacketReceive(object sender, EventArgs <ArraySegment <byte> > e) { //Console.WriteLine("Received packet"); jp.Enqueue(Job.Create(() => { Packet p = new Packet(e.Value); if (firstMsg) { firstMsg = false; Console.WriteLine("Received TypeConverter"); ProcessTypeConverter(p); } else { Console.WriteLine("Received {0}", classNames[p.CategoryId]); mf.Handle(p, this); } })); }
public static void StartService(string ip, string portstr) { int port = int.Parse(portstr); IPAddress[] hostAddresses = Dns.GetHostAddresses(ip); if (hostAddresses.Length == 0) { Log <Service> .Logger.ErrorFormat("cannot find host [{0}]", ip); return; } IPAddress address = hostAddresses[new Random().Next(hostAddresses.Length - 1)]; IPEndPoint arg = new IPEndPoint(address, port); ReportService reportService = new ReportService(); JobProcessor jobProcessor = new JobProcessor(); reportService.Initialize(jobProcessor, (int)Settings.Default.ReportServiceListenPort); UnifiedNetwork.LocationService.LookUp @object = new UnifiedNetwork.LocationService.LookUp(reportService); jobProcessor.Start(); jobProcessor.Enqueue(Job.Create <IPEndPoint>(new Action <IPEndPoint>(@object.StartService), arg)); }
public static void StartService() { try { AService service = new AService(); JobProcessor thread = new JobProcessor(); thread.ExceptionOccur += delegate(object sender, EventArgs <Exception> e) { Log <AService> .Logger.Error("StartService", e.Value); }; service.Initialize(thread); thread.Start(); Register registerop = new Register(typeof(AService)); registerop.OnComplete += delegate(Operation op) { Log <AService> .Logger.DebugFormat("{0} registered on {1} : port {2}", registerop.Category, registerop.ID, registerop.Port); service.ID = registerop.ID; service.Suffix = registerop.Suffix; service.Start(registerop.Port); Action <int> testRequest = null; Entity entity = new Entity(5L, "A"); IEntityProxy[] connections = new IEntityProxy[5]; for (int j = 0; j < 1; j++) { connections[j] = service.Connect(entity, new Location { ID = (long)j, Category = "B" }); } testRequest = delegate(int i) { TestOp op2 = new TestOp { Param = i }; op2.OnComplete += delegate(Operation op3) { Log <AService> .Logger.DebugFormat("result : {0}", op2.ResultMessage.Value); Scheduler.Schedule(thread, Job.Create <int>(testRequest, i + 1), 1000); }; op2.OnFail += delegate(Operation op3) { Log <AService> .Logger.DebugFormat("TestOp Failed...", new object[0]); }; connections[0].RequestOperation(op2); }; Scheduler.Schedule(thread, Job.Create <int>(testRequest, 0), 1000); }; registerop.OnFail += delegate(Operation op) { Log <AService> .Logger.ErrorFormat("cannot find LocationService or register failed : {0}", registerop.Category); service.Dispose(); }; thread.Enqueue(Job.Create <string, Register>(new Action <string, Register>(service.RequestOperation), typeof(UnifiedNetwork.LocationService.LookUp).FullName, registerop)); } catch (Exception ex) { Log <AService> .Logger.Error("StartService", ex); } }