Sync Invoke Remoting IOCP Async Invoke

Source: Internet
Author: User
/* 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, ServiceMo De); 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 IRemo TingIoCP {void BeginSyncWaitFor (string command); void SendCommand (string command) ;}/ * csc/r: Share. dll Server. cs */namespace Microshaoft {using System; using System. threading; using System. runtime. interopServices; using Microsoft. win32.SafeHandles; using Microshaoft. win32; public class IocpHandler <TSessionData, TIocpData> {public delegate void DataReceivedEventHandler (IocpHandler <TSessionData, TIocpData> sender, TIocpData iocp); public event upload DataReceived; private bool _ running = false; private SafeFileHandle _ completionPort; public void CompleteHandle () {_ running = false; _ completionPort. dispose ();} public void SendData (TIocpData data) {GCHandle gch = GCHandle. alloc (data); unsafe {NativeMethods. postQueuedCompletionStatus (_ completionPort, (uint) sizeof (IntPtr), IntPtr. zero, (IntPtr) gch) ;}} private TSessionData _ sessionData; public TSessionData SessionData {get {return _ sessionData ;}} public void BeginSyncWait (TSessionData session) {_ sessionData = session; _ completionPort = NativeMethods. createIoCompletionPort (new IntPtr (-1), IntPtr. zero, IntPtr. zero, 1); if (! _ CompletionPort. isInvalid) {_ running = true; // Thread thread = new Thread (Run); // thread. start (); Run () ;}} private void Run () {while (_ running) {uint r = 0; IntPtr PerHandleData; IntPtr lpOverlapped; NativeMethods. getQueuedCompletionStatus (_ completionPort, out r, out PerHandleData, out lpOverlapped, 0 xffffffff); Console. writeLine ("GetQueuedCompletionStatus... "); if (r> 0) {GCHandle Gch = GCHandle. FromIntPtr (lpOverlapped); TIocpData data = (TIocpData) gch. Target; gch. Free (); if (DataReceived! = Null) {DataReceived (this, data) ;}}}} namespace Microshaoft. remotingObjects. server {using System; using System. runtime. interopServices; using Microshaoft; public class IocpProcessor {private IocpHandler <UserSessionData, IocpData> _ iocpHandler; public IocpProcessor () {_ Signature = new signature <UserSessionData, IocpData>. dataReceived + = new IocpHandler <UserSessionData, IocpData>. dataReceivedEventHandler (_ iocpHandler_DataReceived);} public void BeginSyncWaitFor (string command) {UserSessionData session = new UserSessionData (); session. completeCommand = command; _ iocpHandler. beginSyncWait (session);} public void SendData (IocpData data) {_ iocpHandler. sendData (data);} void _ iocpHandler_DataReceived (IocpHandler <UserSessionData, IocpData> sender, IocpData data ){ If (sender. sessionData. completeCommand = data. text) {sender. completeHandle () ;}} public class UserSessionData {public string UserName; public string CompleteCommand;} // [StructLayout (LayoutKind. sequential)] public class IocpData {public string Text ;}} namespace Microshaoft. remotingObjects. server {using System; using System. threading; using System. collections; using System. serviceProcess; u Sing 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. win32; using Microshaoft. remotingObjects; public class ServiceHost: ServiceBase {public static readonly string serviceName = "RemotingSyncIocpAsycInv OkeService "; private static IocpProcessor _ IocpProcessor; public static IocpProcessor {get {return _ IocpProcessor;} static void Main (string [] args) {ServiceHost service = new ServiceHost (); int l = 0; bool needFreeConsole = 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 (); _ IocpProcessor = new IocpProcessor (); RemotingHelper. startRemoting <RemotingIoCP> ("iocp", 8080, WellKnownObjectMode. singleCall); Console. writeLine ("Server ., press Enter key to exit. ") ;}} [RunInstallerAttribute (true)] public class ProjectInstaller: Installer {private ServiceInstaller serviceInstaller; private ServiceProcessInstaller processInstaller; publ Ic 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. serviceName; Installers. add (serviceInstaller); I Nstallers. add (processInstaller) ;}} namespace Microshaoft. win32 {using System; using Microsoft. win32.SafeHandles; using System. runtime. interopServices; public static class NativeMethods {[DllImport ("kernel32.dll")] public static extern bool AllocConsole (); [DllImport ("kernel32.dll")] public static extern bool FreeConsole (); [DllImport ("kernel32.dll", CharSet = CharSet. auto, SetLas TError = true)] public static extern SafeFileHandle CreateIoCompletionPort (IntPtr FileHandle, IntPtr ExistingCompletionPort, IntPtr CompletionKey, uint partition); [DllImport ("kernel32.dll", CharSet = CharSet. auto, SetLastError = true)] public static extern bool GetQueuedCompletionStatus (SafeFileHandle CompletionPort, out uint lpNumberOfBytesTransferred, out IntP Tr lpCompletionKey, out IntPtr lpOverlapped, uint dwMilliseconds); [DllImport ("Kernel32", CharSet = CharSet. auto)] public static extern bool PostQueuedCompletionStatus (SafeFileHandle CompletionPort, uint dwNumberOfBytesTransferred, IntPtr dwCompletionKey, IntPtr lpOverlapped);} namespace Microshaoft. remotingObjects {using System; using Microshaoft. remotingObjects. Share; using Microshaoft. remotingObjects. server; public class RemotingIoCP: externalbyrefobject, IRemotingIoCP {public void BeginSyncWaitFor (string command) {ServiceHost. iocpProcessor. beginSyncWaitFor (command);} public void SendCommand (string command) {IocpData data = new IocpData (); data. text = command; ServiceHost. iocpProcessor. sendData (data) ;}}/ * csc/r: Share. dll Client. cs */namespace Cons OleApplication {using System; using System. threading; 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 () {IRemotingIoCP _ proxy = RemotingHelper. getRemotingLocalClientProxyObject <IRemo TingIoCP> ("tcp: // 127.0.0.1: 8080/iocp"); string s; while (s = Console. ReadLine ())! = "Q") {Console. writeLine ("BeginSyncWaitFor: {0} at {1} blocking... ", s, DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fffff "); _ proxy. beginSyncWaitFor (s); Console. writeLine ("EndSyncWaitFor: {0} at {1}", s, DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fffff ") ;}}}/ * csc/r: Share. dll ControlerClient. cs */namespace ConsoleApplication {using System; using System. threading; using Microshaoft; using Mic Roshaoft. remotingObjects. share; public class Class1 {static void Main (string [] args) {Console. title = "Controler Client"; Class1 a = new Class1 ();. run (); Console. writeLine (Environment. version. toString (); Console. readLine ();} public void Run () {IRemotingIoCP _ proxy = RemotingHelper. getRemotingLocalClientProxyObject <IRemotingIoCP> ("tcp: // 127.0.0.1: 8080/iocp"); string s; while (s = Console. re AdLine ())! = "Q") {_ proxy. sendCommand (s); Console. writeLine ("SendCommand end: [{0}] at {1}", s, DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fffff "));}}}}

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.