Sync Invoke Asyc Process Remoting Service

Source: Internet
Author: User

// ================================================ ========================================================== ========================================================== =====/* Csc/r: share. dll Server. cs */namespace Microshaoft. remotingObjects. server {using System; using System. threading; using System. collections. concurrent; using Microshaoft; public class SyncInvokeAsyncProcessor {private ConcurrentDictionary <string, SessionWithData <string, DateTi Me> _ sessions = new ConcurrentDictionary <string, SessionWithData <string, DateTime> (); public string SyncInvokeWaitFor (string sessionID, string completeCommand) {var sessionData = _ sessions. getOrAdd (sessionID, key => {return new SessionWithData <string, DateTime> () {SessionID = sessionID, CompleteCommandWaits = new ConcurrentDictionary <string, concurrentBag <WaitWithData <DateTime >> ()};}); Var waits = sessionData. completeCommandWaits; var completeCommandWaits = waits. getOrAdd (completeCommand, key =>{ return new ConcurrentBag <WaitWithData <DateTime >>() ;}); var wwd = new WaitWithData <DateTime> () {Wait = new AutoResetEvent (false)}; completeCommandWaits. add (wwd); EventWaitHandle wait = wwd. wait; wait. waitOne (); return wwd. data. toString ("yyyy-MM-dd HH: mm: ss. fffff ");} public voi D SendCommand (string sessionID, string commandText) {SessionWithData <string, DateTime> session; if (_ sessions. tryGetValue (sessionID, out session) {ConcurrentBag <WaitWithData <DateTime> waits; if (session. completeCommandWaits. tryGetValue (commandText, out waits) {while (! Waits. isEmpty) {WaitWithData <DateTime> wwd; if (waits. tryTake (out wwd) {wwd. data = DateTime. now; wwd. wait. set () ;}}}}} public class SessionWithData <TSessionData, TConnectionData> {public string SessionID; public ConcurrentDictionary <string, ConcurrentBag <WaitWithData <TConnectionData> CompleteCommandWaits; public TSessionData Data;} public class WaitWithData <T> {public EventWaitHandle Wait; public T Data ;}} namespace Microshaoft. remotingObjects. server {using System; using System. threading; using System. collections; using System. serviceProcess; using System. componentModel; using System. security. principal; using System. configuration. install; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. tcp; using Microshaoft; using Microshaoft. win32; using Microshaoft. remotingObjects; public class ServiceHost: ServiceBase {public static readonly string serviceName = "Hangzhou"; private static SyncInvokeAsyncProcessor _ processor; public static SyncInvokeAsyncProcessor Processor {get {return _ processor ;}} static void Main (string [] args) {ServiceHost service = new ServiceHost (); int l = 0; bool need FreeConsole = false; if (args! = Null) {l = args. length;} if (l> 0) {if (args [0]. toLower () = "/console") {needFreeConsole = true; NativeMethods. allocConsole (); Console. title = "Server... "; Console. writeLine ("Alloc Console... "); Console. writeLine ("Current User Identity: {0}", WindowsIdentity. getCurrent (). name); Console. writeLine (". net Framework version: {0} ", Environment. version. toString ();} Console. title = "Server ";// You cannot run the Console as a service. writeLine ("Console"); service. onStart (null); Console. readLine (); return;} Console. writeLine ("Service"); ServiceBase. run (service); if (needFreeConsole) {Console. writeLine ("Free Console... "); NativeMethods. freeConsole () ;}} public ServiceHost () {CanPauseAndContinue = true; ServiceName = ServiceHost. serviceName;} protected override void OnStart (string [] args) {Console. writeLine (Environment. version. toString (); _ processor = new SyncInvokeAsyncProcessor (); RemotingHelper. startRemoting <RemotingSyncInvokerAsyncHandler> ("SyncInvokeAsyncHandler", 8080, WellKnownObjectMode. singleCall); Console. writeLine ("Server ., press Enter key to exit. ") ;}} [RunInstallerAttribute (true)] public class ProjectInstaller: Installer {private ServiceInstaller serviceInstaller; private Se RviceProcessInstaller processInstaller; public ProjectInstaller () {processInstaller = new ServiceProcessInstaller (); serviceInstaller = new ServiceInstaller (); // Service will run under system account processInstaller. account = ServiceAccount. localSystem; // Service will have Start Type of Manual serviceInstaller. startType = ServiceStartMode. manual; serviceInstaller. serviceName = ServiceHost. serv IceName; Installers. add (serviceInstaller); Installers. add (processInstaller) ;}} namespace Microshaoft. remotingObjects {using System; using Microshaoft. remotingObjects. share; using Microshaoft. remotingObjects. server; public class RemotingSyncInvokerAsyncHandler: externalbyrefobject, IRemotingSyncInvokerAsyncHandler {public string SyncInvokeWaitFor (string user, string completeCo Mmand) {return ServiceHost. processor. syncInvokeWaitFor (user, completeCommand);} public void SendCommand (string user, string command) {ServiceHost. processor. sendCommand (user, command) ;}} namespace Microshaoft. win32 {using System; using System. runtime. interopServices; public static class NativeMethods {[DllImport ("kernel32.dll")] public static extern bool AllocConsole (); [DllImport ("ke Rnel32.dll ")] public static extern bool FreeConsole ();}} // ================================================ ========================================================== ========================================================== ================================================ ========================================================== ========================================================== =========/* csc/r: share. dll Client. cs */namespace ConsoleApplication {using System; using System. thread Ing; using Microshaoft. remotingObjects. share; public class Class1 {static void Main (string [] args) {Console. title = "Client"; Class1 a = new Class1 ();. run (); Console. writeLine (Environment. version. toString (); Console. readLine ();} public void Run () {IRemotingSyncInvokerAsyncHandler _ proxy = RemotingHelper. getRemotingLocalClientProxyObject <IRemotingSyncInvokerAsyncHandler> (" Tcp: // 127.0.0.1: 8080/SyncInvokeAsyncHandler "); string s; while (s = Console. ReadLine ())! = "Q") {string [] a = s. split (''); Console. writeLine ("BeginSyncWaitFor: {0} at {1} blocking... ", s, DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fffff "); string r = _ proxy. syncInvokeWaitFor (a [0], a [1]); Console. writeLine (r); Console. writeLine ("EndSyncWaitFor: {0} at {1}", s, DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fffff "));}}}} // ================================================ ========================================================== ========================================================== ================================================ ========================================================== ========================================================== =========/* csc/t: library Share. cs */namespace Microshaoft {using System; using System. collections; using System. collections. generic; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. tcp; using System. runtime. serialization. formatters; public static class RemotingHelper {public static void StartRemoting (Type RemotingType, string Url, int Port, WellKnownObjectMode ServiceMode) {BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider (); provider. typeFilterLevel = TypeFilterLevel. full; IDictionary ht = new Hashtable (); ht ["port"] = Port; TcpChannel tc = new TcpChannel (ht, null, provider); ChannelServices. registerChannel (tc, false); RemotingConfiguration. registerWellKnownServiceType (RemotingType, Url, ServiceMode); Console. writeLine ("Remoting Object Started... ");} public static void StartRemoting <T> (string Url, int Port, WellKnownObjectMode) {StartRemoting (typeof (T), Url, Port, Mode );} public static T GetRemotingLocalClientProxyObject <T> (string Url) {return (T) Activator. getObject (typeof (T), Url) ;}} namespace Microshaoft. remotingObjects. share {using System; public interface IRemotingSyncInvokerAsyncHandler {string SyncInvokeWaitFor (string user, string command); void SendCommand (string user, string command );}} // ================================================ ========================================================== ========================================================== ====

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.