コード例 #1
0
        public void OnMsg(byte[] msg)
        {
            Console.WriteLine("receive from server!");
            Console.WriteLine(msg.Length);
            BinParser par = new BinParser(msg, 0);

            Console.WriteLine(par.Int());
            Console.WriteLine(par.String());
        }
コード例 #2
0
 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         if (!this.closing)
         {
             int n = this.tcpsocket.EndReceive(ar);
             this.ping.ResetConnState();
             if (n > 0)
             {
                 this.rcvbuf.AddDataLen(n);
                 while (this.rcvbuf.count > TcpBuffer.PCK_MIN_SIZE)
                 {
                     BinParser parser = new BinParser(this.rcvbuf.buf, this.rcvbuf.offset);
                     int       head   = parser.Int();
                     if (head != TcpBuffer.PCK_HEADER)
                     {
                         this.rcvbuf.Clear();
                         break;
                     }
                     int length = (int)parser.Short();
                     if (length > this.rcvbuf.count)
                     {
                         break;
                     }
                     byte[] tmpbuf = new byte[length];
                     Buffer.BlockCopy(this.rcvbuf.buf, this.rcvbuf.offset + TcpBuffer.PCK_MIN_SIZE, tmpbuf, 0, length);
                     this.rcvbuf.DeleteData(length + TcpBuffer.PCK_MIN_SIZE);
                     this.OnReceive(tmpbuf); // just put in a msg queue, not whole logic!
                 }
                 this.rcvbuf.Reset();
                 this.PostReceive(); // the reason why you need a msg queue!
             }
             else
             {
                 this.OnError(errors.New("empty data!!"));
                 this.Close();
             }
         }
     }
     catch (Exception e)
     {
         this.OnError(errors.New(e.Message));
     }
 }