Пример #1
0
    void SendToPipe()
    {
        byte[] buffer = ASCIIEncoding.ASCII.GetBytes ("done");
        System.IO.Pipes.NamedPipeClientStream clientStream = new System.IO.Pipes.NamedPipeClientStream ("mypipe");
        clientStream.Connect (System.TimeSpan.MaxValue.Seconds);
        clientStream.WaitForPipeDrain();
        clientStream.Write (buffer, 0, buffer.Length);

        clientStream.Flush ();
        clientStream.Dispose();
        clientStream.Close();
    }
Пример #2
0
        private static void Main(string[] args)
        {
            try
            {
                if (args.Length > 1)
                {
                    var pipeGuid = args[0];

                    NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipeGuid, PipeDirection.InOut);

                    pipeClient.Connect();

                    var dlls = args.Skip(2).ToArray();

                    var nunit3ConsoleExePath = Encoding.UTF8.GetString(Convert.FromBase64String(args[1]));

                    Write(pipeClient, Discover(dlls, pipeClient, nunit3ConsoleExePath));

                    pipeClient.WaitForPipeDrain();

                    pipeClient.Close();
                }
            }
            catch (Exception ex)
            {
                var sb = new StringBuilder();

                sb.AppendLine(ex.Message);
                sb.AppendLine(ex.StackTrace);

                foreach (var arg in args)
                    sb.AppendLine(arg);

                string fileName = DateTime.Now.ToString("YYYY-MM-DD hh:mm:ss");

                Guid parsedGuid;

                if (args.Length > 0 && Guid.TryParse(args[0], out parsedGuid))
                    fileName = parsedGuid.ToString();

                try
                {
                    File.WriteAllText(fileName, sb.ToString());
                }
                catch (Exception) { }

                Console.WriteLine(sb.ToString());
            }
        }
Пример #3
0
        public static bool SendMessage(Command cmd)
        {
            try
            {
                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", PipeServer.SERVERNAME, PipeDirection.InOut))
                {
                    // Connect to the pipe or wait until the pipe is available.
                    Log.Write("Attempting to connect to pipe: " + PipeServer.SERVERNAME);
                    pipeClient.Connect(1000);
                    Log.Write("Connected to pipe.");

                    StreamWriter sw = new StreamWriter(pipeClient);
                    {
                        sw.AutoFlush = true;

                        sw.WriteLine(cmd.ToString());
                        pipeClient.WaitForPipeDrain();

                        Log.Write("Pipe Message Sent: " + cmd.ToString());
                    }

                    StreamReader sr = new StreamReader(pipeClient);
                    {
                        string temp;
                        while ((temp = sr.ReadLine()) != null)
                        {
                            Log.Write("Pipe Message Received: " + temp);

                            if (temp == "done")
                                return true;
                            else if (temp == "failed")
                                return false;
                        }
                    }

                    sw.Close();
                    sr.Close();
                }
            }
            catch (Exception exc)
            {
                Log.Write(exc.ToString(), true);
                return false;
            }

            return false;
        }
Пример #4
0
 private void pipeWorker (object args)
 {
     try
     {
         string value = args.ToString();
         _pipeStream = new NamedPipeClientStream( ".", _pipeName, PipeDirection.Out );
         _pipeStream.Connect();
         byte[] messageBytes = Encoding.UTF8.GetBytes( value );
         _pipeStream.Write( messageBytes, 0, messageBytes.Length );
         _pipeStream.WaitForPipeDrain();
         _pipeStream.Close();
         Thread.Sleep( 5000 );
         _sendingFinished.Set();
     }
     catch
     {
     }
 }
Пример #5
0
		static void Main(string[] args)
		{
			try
			{
				if (args.Length > 1)
				{
					NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", args[0], PipeDirection.InOut);
					pipeClient.Connect();
					Discover(args, pipeClient);

					pipeClient.WaitForPipeDrain();

					pipeClient.Close();
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}
Пример #6
0
        public static void RunProperMode(Command command)
        {
            if (command.ContainsCommandArg("/Uninstall")) { // Are we in uninstall-mode?
                UninstallHelper uninstallHelper = new UninstallHelper();
                uninstallHelper.RunUninstall();
            } else if (command.ContainsCommandArg("-silentinstall")) {
                Globals.InPortableMode = command["-portable"].ToBool();
                Globals.IsSilentInstall = true;
                NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "www.pmuniverse.net-installer", PipeDirection.InOut, PipeOptions.None, System.Security.Principal.TokenImpersonationLevel.Impersonation);
                pipeClient.Connect();
                Pipes.StreamString clientStream = new Pipes.StreamString(pipeClient);
                Globals.SilentInstallCommunicationPipeStream = clientStream;
                Globals.SilentInstallCommunicationPipe = pipeClient;
                installWaitEvent = new ManualResetEvent(false);
                Installer installer = new Installer(command);
                installer.InstallComplete += new EventHandler(installer_InstallComplete);
                installer.Install(clientStream);

                installWaitEvent.WaitOne();

                clientStream.WriteString("[InstallComplete]");

                pipeClient.WaitForPipeDrain();
                pipeClient.Close();

                System.Environment.Exit(0);
            } else {
                // There are no special command line arguments, run the installer in install mode
                // Let's check if we elevated ourselves
                if (!Globals.CommandLine.ContainsCommandArg("/skipwelcome") || !VistaSecurity.IsAdmin()) {
                    // Show the welcome page
                    PageManager.ActivePage = new Pages.pgeWelcome();
                } else {
                    // Show the license page
                    PageManager.ActivePage = new Pages.pgeLicense();
                }
            }
        }
Пример #7
0
        /// <summary>
        /// check if given exe already running or not
        /// </summary>
        /// <returns>returns true if already running</returns>
        public static bool IfAlreadyRunningDoSwitch()
        {
            string strLoc = Assembly.GetExecutingAssembly().Location;
            FileSystemInfo fileInfo = new FileInfo(strLoc);
            string sExeName = fileInfo.Name;
            bool bCreatedNew = false;

            _mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
            if (bCreatedNew)
            {
                _mutex.ReleaseMutex();
                _PipeThread = new Thread(new System.Threading.ThreadStart(PipeThread));
                _PipeThread.Name = "Pipe";
                _PipeThread.Start();

                Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            }
            else
            {
                using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(NamedPipeName))
                {
                    // The connect function will indefinitely wait for the pipe to become available
                    // If that is not acceptable specify a maximum waiting time (in ms)
                    pipeStream.Connect();

                    using (StreamWriter sw = new StreamWriter(pipeStream))
                    {
                        string[] Args = Environment.GetCommandLineArgs();

                        sw.AutoFlush = true;
                        sw.WriteLine(Args.Length >= 2 ? Args[1] : string.Empty);
                        pipeStream.WaitForPipeDrain();
                    }
                }
            }

            return !bCreatedNew;
        }
Пример #8
0
 private void Init()
 {
     if (PipeExists(PIPENAME)) {
         using (NamedPipeClientStream client = new NamedPipeClientStream(PIPENAME)) {
             try {
                 client.Connect(2000);
                 client.Write(SHOW, 0, SHOW.Length);
                 client.WaitForPipeDrain();
             }
             catch {
             }
         }
         Environment.Exit(1);
     }
     using (NamedPipeServerStream server = new NamedPipeServerStream(PIPENAME)) {
         while (true) {
             server.WaitForConnection();
             byte[] data = new byte[255];
             int count = server.Read(data, 0, data.Length);
             string msg = Encoding.ASCII.GetString(data, 0, count).ToUpper();
             if (msg.Equals("SHOW")) {
                 main.Invoke(new MethodInvoker(delegate
                 {
                     main.Show();
                     main.WindowState = FormWindowState.Normal;
                 }));
             }
             else if (msg.Equals("BREAK")) {
                 break;
             }
             else if (msg.Equals("EXIT")) {
                 Environment.Exit(0);
             }
             server.Disconnect();
         }
     }
 }
Пример #9
0
        public static void NotifYUser(Settings settings, SettingsPrinter printer, List<InkLevel> inkLevel)
        {
            string title = "Printer Ink is at low level";
            string body = "";
            //body += title + "\n\r\n\r";
            body += "Printer Details:\n\r";
            body += printer.Name + "\n\r";

            foreach (var level in inkLevel)
            {
                if (level.Value <= printer.Level)
                {
                    body += string.Format("{0} - {1:00%}\n\r", level.Name, level.Value);
                }
            }
            body += "\n\r\n\r*****";

            try
            {
                var pipeClient = new NamedPipeClientStream("InkMonPipe");

                pipeClient.Connect(3000);

                if (pipeClient.IsConnected)
                {
                    var buf = Encoding.ASCII.GetBytes(body);
                    pipeClient.Write(buf, 0, buf.Length);
                    pipeClient.WaitForPipeDrain();
                    pipeClient.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #10
0
        protected override void OnStartup(StartupEventArgs e)
        {
            if (e.Args == null)
                _exeArguments = new string[0];
            else
                _exeArguments = e.Args;

            AppDomain.CurrentDomain.UnhandledException += UncaughtThreadException;
            DispatcherUnhandledException += UncaughtUiThreadException;

            using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
                _pipeName = String.Format("DayZeroLauncher_{{{0}}}_Instance", identity.User);

            bool secondInstance = false;
            using (var pipeConn = new NamedPipeClientStream(".", _pipeName, PipeDirection.Out))
            {
                try
                {
                    const int timeoutMs = 100;
                    pipeConn.Connect(timeoutMs);
                    pipeConn.ReadMode = PipeTransmissionMode.Message;

                    string queryString = GetQueryParams();
                    if (!string.IsNullOrEmpty(queryString))
                    {
                        byte[] bytesToWrite = Encoding.UTF8.GetBytes(queryString);
                        pipeConn.Write(bytesToWrite, 0, bytesToWrite.Length);
                        pipeConn.WaitForPipeDrain();
                    }
                    secondInstance = true;

                    pipeConn.Close();
                }
                catch (TimeoutException)
                {
                }
            }

            if (secondInstance) //already sent message to pipe
            {
                Shutdown();
                return;
            }

            //we are the only app, start the server
            StartPipeServer();

            LocalMachineInfo.Current.Update();
            base.OnStartup(e);
        }
Пример #11
0
        public static Task<bool> SendMessageAsync(object message, string pipeName, bool wait = true)
        {
            return Task.Run<bool>(new Func<bool>(() =>
               {
                   try
                   {
                       using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.None, TokenImpersonationLevel.None))
                       {
                           using (MemoryStream ms = new MemoryStream())
                           {
                               if (wait)
                               {
                                   int i = 0;
                                   for (; i != 20; i++)
                                   {
                                       if (!NamedPipeDoesNotExist(pipeName)) break;
                                       Thread.Sleep(50);
                                   }
                                   if (i == 20) return false;
                               }
                               if (NamedPipeDoesNotExist(pipeName)) return false;

                               pipeClient.Connect(10);

                               BinaryFormatter bf = new BinaryFormatter();

                               bf.Serialize(ms, message);
                               ms.Seek(0, SeekOrigin.Begin);
                               ms.CopyTo(pipeClient);
                               pipeClient.Flush();

                               pipeClient.WaitForPipeDrain();
                           }
                       }
                       return true;
                   }
                   catch (Exception e)
                   {
                       Logging.LogException(e);
                       return false;
                   }
               }));
        }
Пример #12
0
        public static void HandleCommandLine (int connectTimeoutMs = 2500) {
            var commandLineArgs = Environment.GetCommandLineArgs();
            if ((commandLineArgs.Length == 3) && (commandLineArgs[1] == "--buildSolution")) {
                try {
                    var jss = new JavaScriptSerializer {
                        MaxJsonLength = 1024 * 1024 * 64
                    };

                    var pipeId = commandLineArgs[2];

                    using (var pipe = new NamedPipeClientStream(pipeId)) {
                        pipe.Connect(connectTimeoutMs);

                        using (var sr = new StreamReader(pipe))
                        using (var sw = new StreamWriter(pipe)) {
                            var argsJson = sr.ReadLine();
                            var argsDict = jss.Deserialize<Dictionary<string, object>>(argsJson);

                            var buildResult = Build(
                                (string)argsDict["solutionFile"],
                                (string)argsDict["buildConfiguration"],
                                (string)argsDict["buildPlatform"],
                                (string)argsDict["buildTarget"],
                                (string)argsDict["logVerbosity"],
                                true
                            );

                            var resultJson = jss.Serialize(buildResult);

                            sw.WriteLine(resultJson);
                            sw.Flush();
                            pipe.Flush();
                            pipe.WaitForPipeDrain();
                        }
                    }
                } catch (Exception exc) {
                    Console.Error.WriteLine(exc.ToString());
                    Environment.Exit(1);
                }

                Environment.Exit(0);
            }
        }
Пример #13
0
        static void Main(string[] args)
        {
            // get application GUID as defined in AssemblyInfo.cs
            string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

            // unique id for global mutex - Global prefix means it is global to the machine
            string mutexId = string.Format("Global\\{{{0}}}", appGuid);

            using (var mutex = new Mutex(false, mutexId))
            {
                // edited by Jeremy Wiebe to add example of setting up security for multi-user usage
                // edited by 'Marc' to work also on localized systems (don't use just "Everyone")
                var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
                var securitySettings = new MutexSecurity();
                securitySettings.AddAccessRule(allowEveryoneRule);
                mutex.SetAccessControl(securitySettings);

                // edited by acidzombie24
                var hasHandle = false;
                try
                {
                    try
                    {
                        // note, you may want to time out here instead of waiting forever
                        // edited by acidzombie24
                        // mutex.WaitOne(Timeout.Infinite, false);
                        hasHandle = mutex.WaitOne(500, false);
                        if (hasHandle == false)
                        {
                            if (args.Count() == 1)
                            {
                                if (args[0] == "update")
                                {
                                    //TODO: send message to pipe
                                    NamedPipeClientStream pipe = new NamedPipeClientStream(".", "NXTLibTesterGUI_forceupdatepipe", PipeDirection.Out);
                                    StreamWriter sw = new StreamWriter(pipe);
                                    pipe.Connect();
                                    sw.AutoFlush = true;
                                    sw.WriteLine("Force Update Now!");
                                    pipe.WaitForPipeDrain();
                                    pipe.Dispose();
                                }
                                return;
                            }

                            MessageBox.Show("Application already running!  Close the other instance and try again.",
                                   "Application Running", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    catch (AbandonedMutexException)
                    {
                        // Log the fact the mutex was abandoned in another process, it will still get aquired
                        hasHandle = true;
                    }

                    FindNXT.updatewaiting = false;
                    if (args.Count() == 1)
                    {
                        if (args[0] == "update")
                        {
                            FindNXT.updatewaiting = true;
                        }
                    }

                    // Perform your work here.
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new FindNXT());
                }
                finally
                {
                    // edited by acidzombie24, added if statemnet
                    if (hasHandle)
                        mutex.ReleaseMutex();
                }
            }
        }
Пример #14
0
        private void QvxCommandWorker()
        {
            try
            {
                if (pipeName == null) return;

                object state = new object();
                object connection = null;

                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut))
                {
                    var buf = new byte[4];
                    var buf2 = new byte[4];
                    Int32 count = 0;
                    Int32 datalength = 0;
                    pipeClient.Connect(1000);
                    while (pipeClient.IsConnected)
                    {
                        try
                        {
                            #region Get QvxRequest
                            var iar = pipeClient.BeginRead(buf, 0, 4, null, state);
                            while (!iar.IsCompleted) Thread.Sleep(1);
                            count = pipeClient.EndRead(iar);
                            if (count != 4) throw new Exception("Invalid Count Length");
                            buf2[0] = buf[3];
                            buf2[1] = buf[2];
                            buf2[2] = buf[1];
                            buf2[3] = buf[0];
                            datalength = BitConverter.ToInt32(buf2, 0);
                            var data = new byte[datalength];
                            count = pipeClient.Read(data, 0, datalength);
                            if (count != datalength) throw new Exception("Invalid Data Length");

                            var sdata = ASCIIEncoding.ASCII.GetString(data);
                            sdata = sdata.Replace("\0", "");
                            QvxRequest request;
                            try
                            {
                                request = QvxRequest.Deserialize(sdata);

                            }
                            catch (Exception ex)
                            {
                                logger.Error(ex);
                                throw ex;
                            }
                            request.QVWindow = QVWindow;
                            request.Connection = connection;
                            #endregion

                            #region Handle QvxRequets
                            QvxReply result = null;
                            if (HandleQvxRequest != null)
                                result = HandleQvxRequest(request);

                            if (result == null)
                                result = new QvxReply() { Result = QvxResult.QVX_UNKNOWN_ERROR };
                            #endregion

                            #region Send QvxReply
                            sdata = "    " + result.Serialize() + "\0";
                            datalength = sdata.Length - 4;
                            buf2 = ASCIIEncoding.ASCII.GetBytes(sdata);
                            buf = BitConverter.GetBytes(datalength);
                            buf2[0] = buf[3];
                            buf2[1] = buf[2];
                            buf2[2] = buf[1];
                            buf2[3] = buf[0];
                            pipeClient.Write(buf2, 0, buf2.Length);
                            pipeClient.WaitForPipeDrain();
                            #endregion

                            #region Handle result States
                            if (result.Terminate)
                                close = true;
                            if (result.Connection != null)
                                connection = result.Connection;
                            if (result.SetConnectionNULL)
                                connection = null;
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            Thread.Sleep(500);
                            close = true;
                        }

                        if (close)
                        {
                            close = false;
                            pipeClient.Close();
                        }

                        Thread.Sleep(5);
                    }
                }
                running = false;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
Пример #15
0
    public static async Task ClientPInvokeChecks()
    {
        using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.None, 4096, 4096))
        {
            using (NamedPipeClientStream client = new NamedPipeClientStream(".", "foo", PipeDirection.Out))
            {
                Task serverTask = DoServerOperationsAsync(server);
                client.Connect();

                Assert.False(client.CanRead);
                Assert.False(client.CanSeek);
                Assert.False(client.CanTimeout);
                Assert.True(client.CanWrite);
                Assert.False(client.IsAsync);
                Assert.True(client.IsConnected);
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Assert.Equal(0, client.OutBufferSize);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    Assert.True(client.OutBufferSize > 0);
                }
                else
                {
                    Assert.Throws<PlatformNotSupportedException>(() => client.OutBufferSize);
                }
                Assert.Equal(PipeTransmissionMode.Byte, client.ReadMode);
                Assert.NotNull(client.SafePipeHandle);
                Assert.Equal(PipeTransmissionMode.Byte, client.TransmissionMode);

                client.Write(new byte[] { 123 }, 0, 1);
                await client.WriteAsync(new byte[] { 124 }, 0, 1);
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    client.WaitForPipeDrain();
                }
                else
                {
                    Assert.Throws<PlatformNotSupportedException>(() => client.WaitForPipeDrain());
                }
                client.Flush();

                await serverTask;
            }
        }

        using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.Out))
        {
            using (NamedPipeClientStream client = new NamedPipeClientStream(".", "foo", PipeDirection.In))
            {
                Task serverTask = DoServerOperationsAsync(server);
                client.Connect();

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Assert.Equal(0, client.InBufferSize);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    Assert.True(client.InBufferSize > 0);
                }
                else
                {
                    Assert.Throws<PlatformNotSupportedException>(() => client.InBufferSize);
                }
                byte[] readData = new byte[] { 0, 1 };
                Assert.Equal(1, client.Read(readData, 0, 1));
                Assert.Equal(1, client.ReadAsync(readData, 1, 1).Result);
                Assert.Equal(123, readData[0]);
                Assert.Equal(124, readData[1]);

                await serverTask;
            }
        }
    }
Пример #16
0
        private Response GetResponse(Request request)
        {
            _pipeClient = CreateClientPipe();
            TryConnect(_pipeClient);

            _pipeClient.WriteByte((byte)request);
            _pipeClient.WriteByte((byte)_pos);
            _pipeClient.WaitForPipeDrain();

            Response response = (Response)_pipeClient.ReadByte();
            return response;
        }
Пример #17
0
 public void Send(byte[] message)
 {
     using (
         var pipeClientStream = new NamedPipeClientStream(
             ".", this.hookServerChannelName, PipeDirection.Out, PipeOptions.None))
     {
         pipeClientStream.Connect();
         pipeClientStream.Write(message, 0, message.Length);
         try
         {
             pipeClientStream.WaitForPipeDrain();
         }
         catch
         {
             Debug.WriteLine("pipe bandaid!");
         }
     }
 }
Пример #18
0
        private void Send(string message)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(message))
                    return;

                using (var pipeStream = new NamedPipeClientStream
                    (".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous))
                {
                    pipeStream.Connect(Timeout*1000);

                    var buffer = Encoding.UTF8.GetBytes(message);
                    pipeStream.Write(buffer, 0, buffer.Length);
                    pipeStream.Flush();
                    pipeStream.WaitForPipeDrain();
                }
            }
            catch (Exception e)
            {
                Log.WarnFormat("An exception occurred while trying to send '{0}' to pipe '{1}': {2}", Message, PipeName, e.Message);
            }
        }
Пример #19
0
        private string ReadWriteMessage(NamedPipeClientStream pipeClient, string message)
        {
            try
            {
                ChannelStream channelStream = new ChannelStream(pipeClient);

                channelStream.Send(message);

                pipeClient.WaitForPipeDrain();

                string response = channelStream.Receive();

                pipeClient.Close();

                return response;
            }
            catch
            {
                return null;
            }
        }
Пример #20
0
        public MainForm(string wzToLoad, bool usingPipes, bool firstrun)
        {
            InitializeComponent();
            encryptionBox.Items.Add(HaRepacker.Properties.Resources.EncTypeGMS);
            encryptionBox.Items.Add(HaRepacker.Properties.Resources.EncTypeMSEA);
            encryptionBox.Items.Add(HaRepacker.Properties.Resources.EncTypeNone);
            #if DEBUG
            debugToolStripMenuItem.Visible = true;
            #endif
            WindowState = ApplicationSettings.Maximized ? FormWindowState.Maximized : FormWindowState.Normal;
            Size = ApplicationSettings.WindowSize;

            if (usingPipes)
            {
                try
                {
                    Program.pipe = new NamedPipeServerStream(Program.pipeName, PipeDirection.In);
                    Program.pipeThread = new Thread(new ThreadStart(PipeServer));
                    Program.pipeThread.IsBackground = true;
                    Program.pipeThread.Start();
                }
                catch (IOException)
                {
                    if (wzToLoad != null)
                    {
                        try
                        {
                            NamedPipeClientStream clientPipe = new NamedPipeClientStream(".", Program.pipeName, PipeDirection.Out);
                            clientPipe.Connect(0);
                            StreamWriter sw = new StreamWriter(clientPipe);
                            sw.WriteLine(wzToLoad);
                            clientPipe.WaitForPipeDrain();
                            sw.Close();
                            Environment.Exit(0);
                        }
                        catch (TimeoutException)
                        {
                        }
                    }
                }
            }
            if (wzToLoad != null && File.Exists(wzToLoad))
            {
                short version;
                WzMapleVersion encVersion = WzTool.DetectMapleVersion(wzToLoad, out version);
                encryptionBox.SelectedIndex = (int)encVersion;
                LoadWzFileThreadSafe(wzToLoad, MainPanel, false);
            }
            WzNode.ContextMenuBuilder = new WzNode.ContextMenuBuilderDelegate(new ContextMenuManager(MainPanel.UndoRedoMan).CreateMenu);
        }
Пример #21
0
        private void QvxDataWorker()
        {
            try
            {
                if (pipeName == null) return;

                logger.Info("Start :" + pipeName);

                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.Out))
                {
                    pipeClient.Connect(1000);

                    if (pipeClient.IsConnected)
                    {
                        if (DataClientDeliverData != null)
                        {
                            var thread = new Thread(
                                new ThreadStart(() => {
                                    DataClientDeliverData(this);
                                    this.Close();
                                }));
                            thread.IsBackground = false;
                            thread.Name = "DataClientDeliverDataThread";
                            thread.Start();
                        }
                    }

                    while (pipeClient.IsConnected)
                    {
                        byte[] buffer = null;
                        List<byte[]> sendList = null;
                        lock (lockQueue)
                        {
                            if (SendQueue.Count > 0)
                            {
                                sendList = SendQueue;
                                SendQueue = new List<byte[]>();
                            }
                        }
                        if (sendList != null)
                        {
                            int reqSize = 0;
                            for (int i = 0; i < sendList.Count; i++)
                                reqSize += sendList[i].Length;

                            if (reqSize > 0)
                            {
                                buffer = new byte[reqSize];
                                reqSize = 0;
                                for (int i = 0; i < sendList.Count; i++)
                                {
                                    sendList[i].CopyTo(buffer, reqSize);
                                    reqSize += sendList[i].Length;
                                }
                            }
                        }

                        if (buffer != null)
                        {
                            int index = 0;
                            int tosend = buffer.Length;
                            int sendnow = 0;
                            while (tosend > 0)
                            {
                                sendnow = tosend;

                                if ((sendnow > pipeClient.OutBufferSize) && (pipeClient.OutBufferSize > 0))
                                    sendnow = pipeClient.OutBufferSize;

                                pipeClient.Write(buffer, index, sendnow);

                                index += sendnow;
                                tosend -= sendnow;
                                pipeClient.WaitForPipeDrain();
                            }
                        }
                        if (close & (buffer == null))
                        {
                            logger.Info("Close :" + pipeName);
                            pipeClient.Close();
                            close = false;
                        }

                        Thread.Sleep(5);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException("Exceptions:" + pipeName, ex);
            }
        }
Пример #22
0
    public static void ClientPInvokeChecks()
    {
        using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.In))
        {
            using (NamedPipeClientStream client = new NamedPipeClientStream(".", "foo", PipeDirection.Out))
            {
                Task serverTask = DoServerOperationsAsync(server);
                client.Connect();
                Console.WriteLine("client.CanRead = {0}", client.CanRead);
                Console.WriteLine("client.CanSeek = {0}", client.CanSeek);
                Console.WriteLine("client.CanTimeout = {0}", client.CanTimeout);
                Console.WriteLine("client.CanWrite = {0}", client.CanWrite);
                Console.WriteLine("client.IsAsync = {0}", client.IsAsync);
                Console.WriteLine("client.IsConnected = {0}", client.IsConnected);
                Console.WriteLine("client.OutBufferSize = {0}", client.OutBufferSize);
                Console.WriteLine("client.ReadMode = {0}", client.ReadMode);
                Console.WriteLine("client.SafePipeHandle = {0}", client.SafePipeHandle);
                Console.WriteLine("client.TransmissionMode = {0}", client.TransmissionMode);

                client.Write(new byte[] { 123 }, 0, 1);
                client.WriteAsync(new byte[] { 124 }, 0, 1).Wait();
                client.WaitForPipeDrain();
                client.Flush();

                serverTask.Wait();
            }
        }

        using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.Out))
        {
            using (NamedPipeClientStream client = new NamedPipeClientStream(".", "foo", PipeDirection.In))
            {
                Task serverTask = DoServerOperationsAsync(server);
                client.Connect();

                Console.WriteLine("client.InBufferSize = {0}", client.InBufferSize);
                byte[] readData = new byte[] { 0, 1 };
                client.Read(readData, 0, 1);
                client.ReadAsync(readData, 1, 1).Wait();
                Assert.Equal(123, readData[0]);
                Assert.Equal(124, readData[1]);

                serverTask.Wait();
            }
        }
    }