Esempio n. 1
0
 internal Response Response(Package pack)
 {
     return (p) =>
     {
         p.Sequeue = pack.Sequeue;
         this.SendImpl(p,false);
     };
 }
Esempio n. 2
0
        public PackageResponse Send(Package pack)
        {
            lock (sequeueLock)
            {
                if (this.sequeue == 0)
                {
                    this.sequeue += 2;
                }
                pack.Sequeue = this.sequeue;
                this.sequeue += 2;
            }

            AutoResetEvent set = new AutoResetEvent(false);
            PackageResponse r = new PackageResponse(set);
            recv.AddRequest(pack.Sequeue,r.Response);
            this.SendImpl(pack,true);
            return r;
        }
 internal void Response(Package pack)
 {
     this.pack = pack;
         set.Set();
 }
Esempio n. 4
0
        private void SendImpl(Package pack,bool isRequest)
        {
            //int size = pack.size();
            //byte[] bs = new byte[size];
            byte[] bs = pack.Write();
            byte[] tmpBs = new byte[2 * bs.Length + 3 + 18];	//14
            int pos = 0;
            //将包中的数据写入数组
            tmpBs[0] = 0xC0;
            tmpBs[1] = pack.Type;
            if (isRequest)
            {
                tmpBs[2] = 0;
            }
            else
            {
                tmpBs[2] = 1;
            }

            ByteUtils.WriteLong(tmpBs, (long)pack.Sequeue, 3);
            pos = 11;

            for (int n = 0; n < bs.Length; n++)
            {				//解析FF、FA
                if ((byte)bs[n] == 0xC0)
                {
                    tmpBs[pos] = 0xDB;
                    pos++;
                    tmpBs[pos] = 0xDC;
                    pos++;
                }
                else if ((byte)bs[n] == 0xDB)
                {
                    tmpBs[pos] = 0xDB;
                    pos++;
                    tmpBs[pos] = 0xDD;
                    pos++;
                }
                else
                {
                    tmpBs[pos] = bs[n];
                    pos++;
                }
            }
            tmpBs[pos] = 0xC0;

            lock (this.SendPackageLock)
            {
                this.Socket.Send(tmpBs, pos + 1, SocketFlags.None);
            }
        }
        private void CommunicateListenerImpl(Package package)
        {
            //if (r != null)
            //{
            //    package.State = PackageState.RESPONSE;
            //}
            //else
            //{
            //    package.State = PackageState.REQUEST;
            //}
                bool isResponse = false;
                Response sr = this.session.Response(package);
            if (this.listener != null)
            {
                //try {
                if (package.State == PackageState.REQUEST)
                {
                    listener(this.session, package, p => { isResponse = true; sr(p); });
                }
                else
                {
                    listener(this.session, package, null);
                }
                //}
                //finally
                //{

                //}
            }
            if (package.State == PackageState.RESPONSE)
            {
                Response r = sequeues[package.Sequeue];
                sequeues[package.Sequeue] = null;
                if (r != null)
                {
                    r(package);
                }
            }
            else
            {
                if (!isResponse)
                {
                    sr(new ErrorPackage());
                }
            }
        }
 private void CommunicateListener(Package package)
 {
     //采用线程池技术
     new Thread(() => { CommunicateListenerImpl(package); }).Start();
 }