public void SendToRemote()
        {
            if (HaveUnsentMessages)
            {
                RemoteCommunicator.Send(outgoingStream);

                // Reset the stream as the content have been read and sent
                outgoingStream.Position = 0;
            }
        }
Exemplo n.º 2
0
        public IRemoteCommunicatorPrx createCommunicator(Dictionary <string, string> props, Current current)
        {
            //
            // Prepare the property set using the given properties.
            //
            ILogger?logger = null;
            string? value;
            int     nullLogger;

            if (props.TryGetValue("NullLogger", out value) && int.TryParse(value, out nullLogger) && nullLogger > 0)
            {
                logger = new NullLogger();
            }

            //
            // Initialize a new communicator.
            //
            var communicator = new Communicator(props, logger: logger);

            //
            // Install a custom admin facet.
            //
            try
            {
                var testFacet = new TestFacet();
                communicator.AddAdminFacet(testFacet, "TestFacet");
            }
            catch (System.ArgumentException)
            {
            }

            //
            // The RemoteCommunicator servant also implements PropertiesAdminUpdateCallback.
            // Set the callback on the admin facet.
            //
            var servant   = new RemoteCommunicator(communicator);
            var propFacet = communicator.FindAdminFacet("Properties");

            if (propFacet != null)
            {
                var admin = (INativePropertiesAdmin)propFacet;
                Debug.Assert(admin != null);
                admin.AddUpdateCallback(servant.updated);
            }

            return(current.Adapter.Add(servant, IRemoteCommunicatorPrx.Factory));
        }
Exemplo n.º 3
0
        public IRemoteCommunicatorPrx createCommunicator(Dictionary <string, string> props, Current current)
        {
            //
            // Prepare the property set using the given properties.
            //
            ILogger?logger = null;

            if (props.TryGetValue("NullLogger", out string?value) &&
                int.TryParse(value, out int nullLogger) && nullLogger > 0)
            {
                logger = new NullLogger();
            }

            //
            // Initialize a new communicator.
            //
            var communicator = new Communicator(props, logger: logger);

            //
            // Install a custom admin facet.
            //
            try
            {
                var testFacet = new TestFacet();
                communicator.AddAdminFacet("TestFacet", testFacet);
            }
            catch (System.ArgumentException)
            {
            }

            // The RemoteCommunicator servant also implements PropertiesAdminUpdateCallback.
            var servant   = new RemoteCommunicator(communicator);
            var propFacet = communicator.FindAdminFacet("Properties");

            if (propFacet is IPropertiesAdmin admin)
            {
                admin.Updated += (_, updates) => servant.Updated(updates);
            }

            return(current.Adapter.AddWithUUID(servant, IRemoteCommunicatorPrx.Factory));
        }
        public RemoteDispatcherProxy(IDispatcher localDispatcher, ISerializer serializer, IRemoteCommunicator remoteCommunicator)
        {
            this.LocalDispatcher    = localDispatcher;
            this.Serializer         = serializer;
            this.RemoteCommunicator = remoteCommunicator;


            messageBuffer      = new List <IMessage> [2];
            messageBuffer[0]   = new List <IMessage>();
            messageBuffer[1]   = new List <IMessage>();
            messageBufferIndex = 0;

            messageWaiting    = false;
            messageBufferLock = new object();

            outgoingStream = new MemoryStream();

            LocalDispatcher.RegisterListenerForAll(this);
            LocalDispatcher.AddToForwardIgnoreList(this);
            RemoteCommunicator.RegisterReceiver(ReceiveRemoteMessage);
        }
Exemplo n.º 5
0
 public void Run(string[] commandLine)
 {
     InternalCommandLine = new ReadOnlyCollection <string>(commandLine);
     if (!IsSingleInstance)
     {
         DoApplicationModel();
     }
     else
     {
         var applicationInstanceId = GetApplicationInstanceID();
         m_MemoryMappedID = applicationInstanceId + "Map";
         var name1 = applicationInstanceId + "Event";
         var name2 = applicationInstanceId + "Event2";
         RunNextInstanceDelegate = OnStartupNextInstanceMarshallingAdaptor;
         new SecurityPermission(SecurityPermissionFlag.ControlPrincipal).Assert();
         var name3           = WindowsIdentity.GetCurrent().Name;
         var ChannelIsSecure = (uint)Operators.CompareString(name3, "", false) > 0U;
         CodeAccessPermission.RevertAssert();
         bool createdNew1;
         if (ChannelIsSecure)
         {
             var rule = new EventWaitHandleAccessRule(name3, EventWaitHandleRights.FullControl,
                                                      AccessControlType.Allow);
             var eventSecurity1 = new EventWaitHandleSecurity();
             new SecurityPermission(SecurityPermissionFlag.ControlPrincipal).Assert();
             eventSecurity1.AddAccessRule(rule);
             CodeAccessPermission.RevertAssert();
             m_FirstInstanceSemaphore = new EventWaitHandle(false, EventResetMode.ManualReset, name1,
                                                            out createdNew1, eventSecurity1);
             var num1  = 0;
             var num2  = 0;
             var name4 = name2;
             var flag  = false;
             // ISSUE: explicit reference operation
             // ISSUE: variable of a reference type
             var createdNew2    = flag;
             var eventSecurity2 = eventSecurity1;
             m_MessageRecievedSemaphore = new EventWaitHandle(num1 != 0, (EventResetMode)num2, name4,
                                                              out createdNew2, eventSecurity2);
         }
         else
         {
             m_FirstInstanceSemaphore = new EventWaitHandle(false, EventResetMode.ManualReset, name1,
                                                            out createdNew1);
             m_MessageRecievedSemaphore = new EventWaitHandle(false, EventResetMode.AutoReset, name2);
         }
         if (createdNew1)
         {
             try
             {
                 var tcpServerChannel   = (TcpServerChannel)RegisterChannel(ChannelType.Server, ChannelIsSecure);
                 var remoteCommunicator = new RemoteCommunicator(this, m_MessageRecievedSemaphore);
                 var str = applicationInstanceId + ".rem";
                 new SecurityPermission(SecurityPermissionFlag.RemotingConfiguration).Assert();
                 var URI = str;
                 RemotingServices.Marshal(remoteCommunicator, URI);
                 CodeAccessPermission.RevertAssert();
                 var objectUri = str;
                 WriteUrlToMemoryMappedFile(tcpServerChannel.GetUrlsForUri(objectUri)[0]);
                 m_FirstInstanceSemaphore.Set();
                 DoApplicationModel();
             }
             catch (Exception ex)
             {
                 //MessageBox.Show(ex.Message);
             }
             finally
             {
                 if (m_MessageRecievedSemaphore != null)
                 {
                     m_MessageRecievedSemaphore.Close();
                 }
                 if (m_FirstInstanceSemaphore != null)
                 {
                     m_FirstInstanceSemaphore.Close();
                 }
                 if (m_FirstInstanceMemoryMappedFileHandle != null && !m_FirstInstanceMemoryMappedFileHandle.IsInvalid)
                 {
                     m_FirstInstanceMemoryMappedFileHandle.Close();
                 }
             }
         }
         else
         {
             if (!m_FirstInstanceSemaphore.WaitOne(2500, false))
             {
                 throw new CantStartSingleInstanceException();
             }
             RegisterChannel(ChannelType.Client, ChannelIsSecure);
             var url = ReadUrlFromMemoryMappedFile();
             if (url == null)
             {
                 throw new CantStartSingleInstanceException();
             }
             var remoteCommunicator =
                 (RemoteCommunicator)RemotingServices.Connect(typeof(RemoteCommunicator), url);
             var permissionSet      = new PermissionSet(PermissionState.None);
             var securityPermission =
                 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode |
                                        SecurityPermissionFlag.SerializationFormatter |
                                        SecurityPermissionFlag.ControlPrincipal);
             permissionSet.AddPermission(securityPermission);
             var dnsPermission = new DnsPermission(PermissionState.Unrestricted);
             permissionSet.AddPermission(dnsPermission);
             var socketPermission = new SocketPermission(NetworkAccess.Connect, TransportType.Tcp, "127.0.0.1",
                                                         -1);
             permissionSet.AddPermission(socketPermission);
             var environmentPermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME");
             permissionSet.AddPermission(environmentPermission);
             permissionSet.Assert();
             var commandLineArgs = CommandLineArgs;
             remoteCommunicator.RunNextInstance(commandLineArgs);
             PermissionSet.RevertAssert();
             if (!m_MessageRecievedSemaphore.WaitOne(2500, false))
             {
                 throw new CantStartSingleInstanceException();
             }
         }
     }
 }