Esempio n. 1
0
        //处理业务报文的逻辑
        void HandleLogincPackage(BufferEntity buffer)
        {
            if (buffer.sn <= handleSN)
            {
                return;
            }

            //已经收到的报文是错序的
            if (buffer.sn - handleSN > 1)
            {
                if (waitHandle.TryAdd(buffer.sn, buffer))
                {
                    Debug.Log($"收到错序的报文:{buffer.sn}");
                }
                return;
            }

            //更新已处理的报文
            handleSN = buffer.sn;
            if (handleAction != null)
            {
                //派发 分发给游戏模块去处理
                handleAction(buffer);
            }

            //检测缓存的数据 有没有包含下一条可以处理的数据
            BufferEntity nextBuffer;

            if (waitHandle.TryRemove(handleSN + 1, out nextBuffer))
            {
                //这里是判断缓冲区有没有存在下一条数据
                HandleLogincPackage(nextBuffer);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 创建并且发送报文
        /// </summary>
        /// <param name="messageID"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static BufferEntity CreateAndSendPackage(int messageID, IMessage message)
        {
            JsonHelper.Log(messageID, message);

            //Debug.Log($"报文ID:{messageID}\n包体{JsonHelper.SerializeObject(message)}");
            BufferEntity buffer = new BufferEntity(USocket.local.endPoint, USocket.local.sessionID, 0, 0, MessageType.Login.GetHashCode(),
                                                   messageID, ProtobufHelper.ToBytes(message));

            USocket.local.Send(buffer);
            return(buffer);
        }
Esempio n. 3
0
        /// <summary>
        /// 创建一个ACK报文的实体
        /// </summary>
        /// <param name="package">收到的报文实体</param>
        public BufferEntity(BufferEntity package)
        {
            protoSize        = 0;
            this.endPoint    = package.endPoint;
            this.session     = package.session;
            this.sn          = package.sn;
            this.moduleID    = package.moduleID;
            this.time        = 0; //
            this.messageType = 0; //
            this.messageID   = package.messageID;

            buffer = Encoder(true);
        }
Esempio n. 4
0
 //Update去进行调用
 public void Handle()
 {
     if (awaitHandle.Count > 0)
     {
         UdpReceiveResult data;
         if (awaitHandle.TryDequeue(out data))
         {
             //反序列化
             BufferEntity bufferEntity = new BufferEntity(data.RemoteEndPoint, data.Buffer);
             if (bufferEntity.isFull)
             {
                 Debug.Log($"处理消息,id:{bufferEntity.messageID},序号:{bufferEntity.sn}");
                 //处理业务逻辑
                 local.Handle(bufferEntity);
             }
         }
     }
 }
Esempio n. 5
0
        //发送的接口
        public void Send(BufferEntity package)
        {
            package.time = TimeHelper.Now(); //暂时先等于0
            sendSN      += 1;                //
            package.sn   = sendSN;

            package.Encoder(false);
            if (sessionID != 0)
            {
                //缓存起来 因为可能需要重发
                sendPackage.TryAdd(sendSN, package);
            }
            else
            {
                //还没跟服务器建立连接的 所以不需要进行缓存
            }
            uSocket.Send(package.buffer, endPoint);
        }
Esempio n. 6
0
        //处理消息 :按照报文的序号 进行顺序处理  如果是收到超过当前顺序+1的报文 先进行缓存
        public void Handle(BufferEntity buffer)
        {
            if (this.sessionID == 0 && buffer.session != 0)
            {
                Debug.Log($"服务发送给我们的会话ID是:{buffer.session}");
                this.sessionID = buffer.session;
            }
            //else
            //{
            //    if (buffer.session==this.sessionID)
            //    {

            //    }
            //}

            switch (buffer.messageType)
            {
            case 0:    //ACK确认报文
                BufferEntity bufferEntity;
                if (sendPackage.TryRemove(buffer.sn, out bufferEntity))
                {
                    Debug.Log($"收到ACK确认报文,序号是:{buffer.sn}");
                }
                break;

            case 1:                        //业务报文
                BufferEntity ackPacka = new BufferEntity(buffer);
                uSocket.SendACK(ackPacka); //先告诉服务器 我已经收到这个报文

                //再来处理业务报文
                HandleLogincPackage(buffer);
                break;

            default:
                break;
            }
        }
Esempio n. 7
0
 //发送ACK报文 解包后 马上调用
 public void SendACK(BufferEntity bufferEntity)
 {
     Send(bufferEntity.buffer, server);
 }