示例#1
0
        /// <summary>
        /// 接收到消息
        /// </summary>
        /// <param name="ms"></param>
        void OnReceivedMessage(byte[] message)
        {
            ByteBuffer buffer = new ByteBuffer(message);
            //放在lua中解析pbid
            int pbId = buffer.ReadShort();

            if (pbId == 10000)
            {
                if (ConnectObserver.inReconnect)
                {
                    ConnectObserver.DispatchEvent(Protocal.Reconnected, new ByteBuffer());
                    if (reconnectAction != null)
                    {
                        reconnectAction();
                    }
                }
                heartBeatService.resetTimeout();
                heartBeatService.pingUpdate();
            }
            else
            {
                buffer.SeekToBegin();
                ConnectObserver.DispatchEvent(Protocal.Message, buffer);
                heartBeatService.resetTimeout();
            }
        }
示例#2
0
        internal void pingUpdate()
        {
            int        protocal = Protocal.PingUpdate;
            ByteBuffer buffer   = new ByteBuffer();

            buffer.WriteShort((ushort)protocal);
            ConnectObserver.DispatchEvent(protocal, buffer);
        }
示例#3
0
 public void UpdateHeartBeat2Server(float deltaTime)
 {
     if (heartBeatService != null && client != null && client.Connected)
     {
         if (heartBeatService.updateHeartBeat2Server(deltaTime))
         {
             ConnectObserver.DispatchEvent(Protocal.HeartBeat2Server, new ByteBuffer());
         }
     }
 }
示例#4
0
        /// <summary>
        /// 主动从服务器断开连接
        /// </summary>
        public void DisconnectFromServer(int protocal)
        {
            StopHeartBeat();
            Close();
            if (protocal == 0)
            {
                protocal = Protocal.KickOut;
            }
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteShort((ushort)protocal);
            ConnectObserver.DispatchEvent(protocal, buffer);
        }
示例#5
0
 /// <summary>
 /// 连接上服务器
 /// </summary>
 void OnConnect(IAsyncResult asr)
 {
     try
     {
         outStream = client.GetStream();
         client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null);
         if (!ConnectObserver.inReconnect)
         {
             ConnectObserver.DispatchEvent(Protocal.Connect, new ByteBuffer());
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         OnDisconnected(DisType.Exception, e.Message);
     }
 }
示例#6
0
        /// <summary>
        /// 丢失链接
        /// </summary>
        void OnDisconnected(DisType dis, string msg)
        {
            if (client != null)
            {
                Close();   //关掉客户端链接
                int protocal = dis == DisType.Exception ?
                               Protocal.Exception : Protocal.Disconnect;

                ByteBuffer buffer = new ByteBuffer();
                buffer.WriteShort((ushort)protocal);
                ConnectObserver.DispatchEvent(protocal, buffer);
                if (ConnectObserver.inReconnect)
                {
                    ConnectObserver.inReconnect = false;
                }
                ConsoleView.LogWarning(string.Format("Connection was closed:>{0} Distype:>{1}", msg, dis));
            }
        }