예제 #1
0
파일: PipeHost.cs 프로젝트: safino9/priv10
        public void AddListener()
        {
            PipeListener serverPipe = new PipeListener(Name);

            serverPipes.Add(serverPipe);

            serverPipe.DataReceived += (sndr, data) =>
            {
                //mDispatcher.BeginInvoke(new Action(() => {

                EMessageTypes type  = EMessageTypes.eCall;
                int           seqID = 0;
                string        func  = null;
                List <byte[]> args  = PipeListener.ParsePacket(data, ref type, ref seqID, ref func);

                List <byte[]> ret = null;
                if (func == "InitSession")
                {
                    int SessionId = BitConverter.ToInt32(args[0], 0);

                    bool Duplicate = mDispatcher.Invoke(new Func <bool>(() => {
                        return(CountSessions(SessionId) > 0);
                    }));

                    ret = new List <byte[]>();

                    ret.Add(BitConverter.GetBytes(Duplicate));

                    serverPipe.SessionID = SessionId;
                }
                else if (args != null)
                {
                    ret = Process(func, args);
                }

                if (ret != null)
                {
                    serverPipe.SendPacket(type, seqID, func, ret);
                }
                //}));
            };

            serverPipe.Connected += (sndr, args) =>
            {
                mDispatcher.BeginInvoke(new Action(() => {
                    // if we used a listener allocate a replacement one
                    AddListener();
                }));
            };

            serverPipe.PipeClosed += (sndr, args) =>
            {
                mDispatcher.BeginInvoke(new Action(() => {
                    serverPipes.Remove(serverPipe);
                }));
            };
        }