Example #1
0
        /// <summary>
        /// 클라이언트 생성하기
        /// </summary>
        /// <param name="pipeName">파이프명</param>
        /// <param name="pipeDirection">파이프 방향</param>
        /// <returns>명명 파이프</returns>
        public static NamedPipe CreateClient(string pipeName, PipeDirection pipeDirection)
        {
            NamedPipe pipe = new NamedPipe(pipeName, pipeDirection);

            FileAccess fileAccess = GetFileAccess(pipeDirection);

            pipe.pipeHandle = CreateFile
                              (
                pipe.pipeName,
                (uint)fileAccess,
                0,
                IntPtr.Zero,
                OPEN_EXISTING,
                FILE_FLAG_OVERLAPPED,
                IntPtr.Zero
                              );

            if (pipe.pipeHandle.IsInvalid == true)
            {
                Console.WriteLine($"fail to connect for {GetLastError()}");
                pipe.Dispose();

                return(null);
            }

            pipe.pipeStream = new FileStream(pipe.pipeHandle, FileAccess.ReadWrite, (int)BUFFER_SIZE, true);

            return(pipe);
        }
Example #2
0
        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 연결 대기하기 - WaitForConnection()

        /// <summary>
        /// 연결 대기하기
        /// </summary>
        /// <returns>자식 파이프</returns>
        public NamedPipe WaitForConnection()
        {
            if (string.IsNullOrEmpty(this.pipeName) == true || this.exitEventHandle == null)
            {
                Trace.WriteLine("파이프 서비스 종료");

                return(null);
            }

            NamedPipe clientPipe = new NamedPipe();

            clientPipe.pipeName      = this.pipeName;
            clientPipe.pipeDirection = this.pipeDirection;

            SECURITY_ATTRIBUTE securityAttribute = GetSecurityAttribute();

            clientPipe.pipeHandle = CreateNamedPipe
                                    (
                clientPipe.pipeName,
                (uint)(this.pipeDirection | PipeDirection.FILE_FLAG_OVERLAPPED),
                PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
                255,
                BUFFER_SIZE,
                BUFFER_SIZE,
                0,
                ref securityAttribute
                                    );

            Marshal.FreeHGlobal(securityAttribute.SecurityDescriptor);

            if (clientPipe.pipeHandle.IsInvalid)
            {
                Trace.WriteLine("무효한 파이프 연결");

                clientPipe.Dispose();

                return(null);
            }

            NativeOverlapped nativeOverlapped = new NativeOverlapped();

            SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.WorldSid, null);

            EventWaitHandleSecurity eventHandleSecurity = new EventWaitHandleSecurity();

            bool created;

            eventHandleSecurity.AddAccessRule(new EventWaitHandleAccessRule(securityIdentifier, EventWaitHandleRights.FullControl, AccessControlType.Allow));
            eventHandleSecurity.AddAccessRule(new EventWaitHandleAccessRule(securityIdentifier, EventWaitHandleRights.ChangePermissions, AccessControlType.Deny));

            using (EventWaitHandle eventHandle = new EventWaitHandle(false, EventResetMode.ManualReset, null, out created, eventHandleSecurity))
            {
                nativeOverlapped.EventHandle = eventHandle.SafeWaitHandle.DangerousGetHandle();

                ConnectNamedPipe(clientPipe.pipeHandle, ref nativeOverlapped);

                int lastError = Marshal.GetLastWin32Error();

                if (lastError == 997)
                {
                    int completeCode = 0;

                    IntPtr[] waitHandleArray = new IntPtr[]
                    {
                        eventHandle.SafeWaitHandle.DangerousGetHandle(),
                        this.exitEventHandle.SafeWaitHandle.DangerousGetHandle()
                    };

                    completeCode = WaitForMultipleObjects(waitHandleArray.Length, waitHandleArray, false, -1);

                    if (completeCode == 0)
                    {
                    }
                    else if (completeCode == 1)
                    {
                        Trace.WriteLine("파이프 서비스 종료 (리소스 해제)");

                        clientPipe.Dispose();

                        return(null);
                    }
                }
                else if (lastError != 535)
                {
                    Trace.WriteLine("파이프 연결 에러 : " + lastError);

                    clientPipe.Dispose();

                    return(null);
                }
                else
                {
                    Debug.WriteLine("연결되었습니다.");
                }
            }

            clientPipe.needToDisconnect = true;

            FileAccess fileAccess = GetFileAccess(clientPipe.pipeDirection);

            clientPipe.pipeStream = new FileStream(clientPipe.pipeHandle, fileAccess, (int)BUFFER_SIZE, true);

            if (clientPipe.pipeStream == null)
            {
                Trace.WriteLine("파이프 파일 스트림이 null 입니다.");

                clientPipe.Dispose();

                return(null);
            }

            return(clientPipe);
        }
Example #3
0
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 서버 생성하기 - CreateServer(pipeName, pipeDirection)

        /// <summary>
        /// 서버 생성하기
        /// </summary>
        /// <param name="pipeName">파이프명</param>
        /// <param name="pipeDirection">파이프 방향</param>
        /// <returns>명명 파이프</returns>
        public static NamedPipe CreateServer(string pipeName, PipeDirection pipeDirection)
        {
            NamedPipe pipe = new NamedPipe(pipeName, pipeDirection);

            return(pipe);
        }
Example #4
0
        static void Main(string[] args)
        {
            using (NamedPipe pipe = NamedPipe.CreateClient("callmonitor", PipeDirection.PIPE_ACCESS_INBOUND))
            {
                if (pipe != null && pipe.IsConnected == true)
                {
                    Console.WriteLine("***********************************************");
                    Console.WriteLine("*                                             *");
                    Console.WriteLine("*        Unbelievable SimpleDisplay!          *");
                    Console.WriteLine("*                                             *");
                    Console.WriteLine("***********************************************");

                    //var targets = new Dictionary<uint, int>();
                    var bOn     = false;
                    var nCount  = 0;
                    var gateway = new Gateway();

                    bool isRun = true;
                    while (isRun)
                    {
                        var cmd = pipe.ReadString();

                        if (string.IsNullOrEmpty(cmd))
                        {
                            Thread.Sleep(100);
                            continue;
                        }

                        //Console.WriteLine($"cmd: {cmd}");
                        var lines = cmd.Split('\n');
                        for (int i = 0; i != lines.Length; i++)
                        {
                            var line = lines[i];
                            if (string.IsNullOrEmpty(line))
                            {
                                continue;
                            }

                            //Console.WriteLine($"line: {line}");
                            int  depth;
                            uint tid;
                            int  pos;
                            int  dir;
                            getInfo(line, out depth, out tid, out pos, out dir);
                            //if (targets.Count == 0)
                            if (!bOn)
                            {
                                if (depth != 0)
                                {
                                    continue;
                                }

                                var callee = line.Substring(pos + 2);    // overhead for "[Enter] "
                                if (!gateway.Vaild(callee))
                                {
                                    continue;
                                }

                                //Console.WriteLine($"vaild callee: {callee}");
                                //targets.Add(tid, 0);
                                bOn = true;
                            }

                            if (bOn)
                            {
                                //if (!targets.TryGetValue(tid, out int count))
                                //    continue;

                                if (dir == 0)
                                {
                                    //count++;
                                    nCount++;
                                    //Console.WriteLine($"[E count: {nCount.ToString()}");
                                }
                                else
                                {
                                    //if (--count == 0)
                                    if (--nCount == 0)
                                    {
                                        bOn = false;
                                        //targets.Remove(tid);
                                    }
                                    //Console.WriteLine($"[L count: {nCount.ToString()}");
                                }

                                Console.WriteLine(line);
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("fail to connected...");
                }
            }

            Console.WriteLine("Press key to exit...");
            Console.Read();
        }