Example #1
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            bb.ReaderIndex(us.getProtocal().HeaderLen());
            short cmd = bb.ReadShort();

            byte[] bs = bb.GetRaw();
            Statics.SetXor(bs, bb.ReaderIndex());
            MemoryStream stream  = new MemoryStream(bs, bb.ReaderIndex(), bb.ReadableBytes());
            object       obj     = ProtoBuf.Serializer.NonGeneric.Deserialize(MessageQueueHandler.GetProtocolType(cmd), stream);
            FieldInfo    success = obj.GetType().GetField("success");

            if (success != null)
            {
                if ((bool)success.GetValue(obj) == true)
                {
                    MessageQueueHandler.PushQueue(cmd, obj);
                }
                else
                {
                    FieldInfo info = obj.GetType().GetField("info");
                    if (info != null)
                    {
                        Debug.LogWarning("下行出错, cmd=" + cmd + ", type=" + MessageQueueHandler.GetProtocolType(cmd).ToString());
                        MessageQueueHandler.PushError(info.GetValue(obj).ToString());
                    }
                }
            }
        }
Example #2
0
 public override void OnMessage(USocket us,ByteBuf bb)
 {
     Console.WriteLine ("收到数据:");
     bb.ReaderIndex (2);
     string s=bb.ReadUTF8 ();
     Console.WriteLine (s);
     bb.ReaderIndex (0);
     us.Send (bb);//原样返回给服务器,服务器是一个echo,也会原样返回
 }
Example #3
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            Console.WriteLine("收到数据:");
            bb.ReaderIndex(2);
            string s = bb.ReadUTF8();

            Console.WriteLine(s);
            bb.ReaderIndex(0);
            us.Send(bb);             //原样返回给服务器,服务器是一个echo,也会原样返回
        }
Example #4
0
 /**
  * 接收数据
  */
 private void receive()
 {
     while (this.status == STATUS_CONNECTED)
     {
         if (clientSocket.Poll(-1, SelectMode.SelectRead))
         {
             try
             {
                 if (asyc)//异步收取
                 {
                     clientSocket.BeginReceive(buf.GetRaw(), 0, buf.GetRaw().Length, SocketFlags.None, new AsyncCallback(onRecieved), clientSocket);
                 }
                 else //同步收取
                 {
                     int len = clientSocket.Receive(buf.GetRaw());
                     if (len > 0)
                     {
                         buf.ReaderIndex(0);
                         buf.WriterIndex(len);
                         while (true)
                         {
                             ByteBuf frame = this.protocal.TranslateFrame(buf);
                             if (frame != null)
                             {
                                 this.listner.OnMessage(this, frame);
                             }
                             else
                             {
                                 break;
                             }
                         }
                     }
                     else
                     {
                         this.Close(true);
                     }
                 }
             }
             catch (Exception e)
             {
                 this.status = STATUS_CLOSED;
                 this.listner.OnError(this, e.StackTrace + e.Message);
                 this.Close(true);
             }
         }
         else
         {
             this.Close(true);
         }
     }
     this.listner.OnClose(this, serverClose);
 }
Example #5
0
        public override void OnMessage(USocket us,ByteBuf bb)
        {
            Console.WriteLine ("收到数据:");
            bb.ReaderIndex (us.getProtocal().HeaderLen());

            int cmd = bb.ReadShort();
            MemoryStream stream = new MemoryStream(bb.GetRaw(),bb.ReaderIndex(),bb.ReadableBytes());
            AuthResponse response= ProtoBuf.Serializer.Deserialize<AuthResponse>(stream);

            Console.WriteLine (response.pid);
            Console.WriteLine(response.info);
            Console.WriteLine(response.success);
        }
Example #6
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            Console.WriteLine("收到数据:");
            bb.ReaderIndex(us.getProtocal().HeaderLen());

            int          cmd      = bb.ReadShort();
            MemoryStream stream   = new MemoryStream(bb.GetRaw(), bb.ReaderIndex(), bb.ReadableBytes());
            AuthResponse response = ProtoBuf.Serializer.Deserialize <AuthResponse>(stream);

            Console.WriteLine(response.pid);
            Console.WriteLine(response.info);
            Console.WriteLine(response.success);
        }
Example #7
0
		public override  void OnMessage(USocket us,ByteBuf bb)
		{
			Console.WriteLine ("收到数据:");
			bb.ReaderIndex (us.getProtocal().HeaderLen());

            int cmd = bb.ReadShort();
            Type t=null;
            MemoryStream stream = new MemoryStream(bb.GetRaw(),bb.ReaderIndex(),bb.ReadableBytes());
            object response=ProtoBuf.Serializer.NonGeneric.Deserialize(t,stream);
            /**
			Console.WriteLine (response.pid);
            Console.WriteLine(response.info);
            Console.WriteLine(response.success);
            */
		}
Example #8
0
 /**
  * 异步收取信息
  */
 private void onRecieved(IAsyncResult ar)
 {
     try
     {
         Socket so  = (Socket)ar.AsyncState;
         int    len = so.EndReceive(ar);
         if (len > 0)
         {
             buf.ReaderIndex(0);
             buf.WriterIndex(len);
             while (true)
             {
                 ByteBuf frame = this.protocol.TranslateFrame(buf);
                 if (frame != null)
                 {
                     this.listener.OnMessage(this, frame);
                 }
                 else
                 {
                     break;
                 }
             }
             this.receive();
         }
         else
         {
             this.Close(true);
         }
     }
     catch (Exception e)
     {
         this.Close(true);
     }
 }
Example #9
0
        /**
         * 发送成功的回调
         */
        private void sended(IAsyncResult ar)
        {
            ByteBuf bb = (ByteBuf)ar.AsyncState;

            bb.ReaderIndex(bb.WriterIndex());
            this.clientSocket.EndSend(ar);
        }
Example #10
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            Console.WriteLine("收到数据:");
            bb.ReaderIndex(us.getProtocal().HeaderLen());

            int          cmd      = bb.ReadShort();
            Type         t        = null;
            MemoryStream stream   = new MemoryStream(bb.GetRaw(), bb.ReaderIndex(), bb.ReadableBytes());
            object       response = ProtoBuf.Serializer.NonGeneric.Deserialize(t, stream);

            /**
             *          Console.WriteLine (response.pid);
             * Console.WriteLine(response.info);
             * Console.WriteLine(response.success);
             */
        }
Example #11
0
        /**
         * 封包
         **/
        public override void End()
        {
            ByteBuf bb     = payload;
            int     reader = bb.ReaderIndex();
            int     writer = bb.WriterIndex();
            int     l      = writer - reader - 4; //数据长度

            bb.WriterIndex(reader);
            bb.WriteInt(l);
            bb.WriterIndex(writer);
            end = true;
        }
Example #12
0
        /**
         * 封包
         **/
        public virtual void End()
        {
            ByteBuf bb     = payload;
            int     reader = bb.ReaderIndex();
            int     writer = bb.WriterIndex();
            int     l      = writer - reader - 2; //数据长度

            bb.WriterIndex(reader);
            bb.WriteShort((short)l);
            bb.WriterIndex(writer);
            end = true;
        }
Example #13
0
 /**
  *发送
  */
 public IAsyncResult Send(ByteBuf buf)
 {
     try
     {
         byte[]       msg       = buf.GetRaw();
         IAsyncResult asyncSend = clientSocket.BeginSend(msg, buf.ReaderIndex(), buf.ReadableBytes(), SocketFlags.None, sended, buf);
         return(asyncSend);
     }
     catch (Exception e)
     {
         return(null);
     }
 }
Example #14
0
 /**
  * 封包
  */
 public override void End()
 {
     if (!this.end)
     {
         ByteBuf bb     = payload;
         int     reader = bb.ReaderIndex();
         int     writer = bb.WriterIndex();
         int     l      = writer - reader;  //数据长度
         ByteBuf tar    = new ByteBuf(l + 5);
         WriteVarint32(tar, l);
         tar.WriteBytes(bb);
         payload  = tar;
         this.end = true;
     }
 }
Example #15
0
        /**
         * 接收数据
         */
        private void received()
        {
            ByteBuf buf = new ByteBuf(4096);

            while (true)
            {
                if (!clientSocket.Connected)
                {
                    this.status = STATUS_CLOSED;
                    this.listner.OnClose(this, true);
                    break;
                }
                try
                {
                    int size = clientSocket.Receive(buf.GetRaw());
                    if (size <= 0)
                    {
                        this.status = STATUS_CLOSED;
                        clientSocket.Close();
                        this.listner.OnClose(this, true);
                        break;
                    }
                    buf.ReaderIndex(0);
                    buf.WriterIndex(size);
                    while (true)
                    {
                        ByteBuf frame = this.protocal.TranslateFrame(buf);
                        if (frame != null)
                        {
                            this.listner.OnMessage(this, frame);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    this.status = STATUS_CLOSED;
                    this.listner.OnError(this, e.Message);
                    break;
                }
            }
        }
Example #16
0
 /**
  *发送
  */
 public void Send(Frame frame)
 {
     try
     {
         if (this.status == STATUS_CONNECTED)
         {
             this.sending++;
             ByteBuf bb = frame.GetData();
             SocketAsyncEventArgs arg = new SocketAsyncEventArgs();
             arg.SetBuffer(bb.GetRaw(), bb.ReaderIndex(), bb.ReadableBytes());
             arg.UserToken  = bb;
             arg.Completed += new EventHandler <SocketAsyncEventArgs>(OnSend);
             this.clientSocket.SendAsync(arg);
         }
     }
     catch (Exception ex)
     {
         this.Close(false);
     }
 }
Example #17
0
 /**
  *发送
  */
 public IAsyncResult Send(ByteBuf buf)
 {
     try
     {
         byte[] msg=buf.GetRaw();
         IAsyncResult asyncSend = clientSocket.BeginSend (msg,buf.ReaderIndex(),buf.ReadableBytes(),SocketFlags.None,sended,buf);
         return asyncSend;
     }
     catch
     {
         return null;
     }
 }