Ejemplo n.º 1
0
        //处理业务逻辑的接口
        private void HandleLogincPackage(BufferEntity buffer)
        {
            if (buffer.sn <= handleSN)
            {
                Debug.Log($"已经处理过的消息了,序号:{buffer.sn}");
                return;
            }

            if (buffer.sn - handleSN > 1)
            {
                if (waitHandle.TryAdd(buffer.sn, buffer))
                {
                    Debug.Log($"错序的报文,进行缓存,序号是:{buffer.sn}");
                }
                return;
            }

            handleSN = buffer.sn;
            if (dispatchNetEvent != null)
            {
                Debug.Log("分发消息给游戏模块...");
                dispatchNetEvent(buffer);
            }
            BufferEntity nextBuffer;

            if (waitHandle.TryRemove(handleSN + 1, out nextBuffer))
            {
                HandleLogincPackage(nextBuffer);
            }
        }
Ejemplo n.º 2
0
        internal static BufferEntity CreqateAndSendPackage(BufferEntity request, IMessage message)
        {
            Debug.Log("会话ID:" + request.session);
            UClient client = GameManager.uSocket.GetClient(request.session);

            return(CreqateAndSendPackage(client, request.messageID, message));
        }
Ejemplo n.º 3
0
        //处理消息的接口
        async Task Handle()
        {
            while (!ct.IsCancellationRequested)
            {
                if (awaitHandle.Count > 0)
                {
                    UdpReceiveResult data;
                    if (awaitHandle.TryDequeue(out data))
                    {
                        BufferEntity bufferEntity = new BufferEntity(data.RemoteEndPoint, data.Buffer);
                        if (bufferEntity.isFull)
                        {
                            //会话ID来进行查询
                            if (bufferEntity.session == 0)
                            {
                                //客户端 还未建立连接 分配会话ID
                                sessionID += 1;
                                //创建客户端 给这个客户端分配这个会话ID
                                bufferEntity.session = sessionID;
                                CreateUClient(bufferEntity);
                                Debug.Log($"创建客户端,会话ID是{sessionID}");
                            }

                            UClient targetClient;
                            //获取到客户端
                            if (clients.TryGetValue(bufferEntity.session, out targetClient))
                            {
                                targetClient.Handle(bufferEntity);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        //创建客户端建立虚拟连接的接口
        void CreateUClient(BufferEntity buffer)
        {
            UClient client;

            if (!clients.TryGetValue(buffer.session, out client))
            {
                client = new UClient(this, buffer.endPoint, 0, 0, buffer.session, dispatchNetEvent);
                clients.TryAdd(buffer.session, client);
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 创建并且发送报文
        /// </summary>
        /// <param name="uClient"></param>
        /// <param name="messageID"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static BufferEntity CreqateAndSendPackage(UClient uClient, int messageID, IMessage message)
        {
            //如果处于连接状态
            if (uClient.isConnect)
            {
                //打印protobuf 按json格式
                Debug.Log(messageID, message);

                BufferEntity bufferEntity = new BufferEntity(uClient.endPoint, uClient.session, 0, 0, MessageType.Login.GetHashCode(),
                                                             messageID, ProtobufHelper.ToBytes(message));

                uClient.Send(bufferEntity);
                return(bufferEntity);
            }
            return(null);
        }
Ejemplo n.º 7
0
        //发送的接口
        public void Send(BufferEntity package)
        {
            if (isConnect == false)
            {
                return;
            }

            package.time = TimeHelper.Now();
            sendSN      += 1;//发送序号+1
            package.sn   = sendSN;

            //序列化
            package.Encoder(false);
            uSocket.Send(package.buffer, endPoint);
            if (session != 0)
            {
                //已经发送的数据
                sendPackage.TryAdd(package.sn, package);
            }
        }
Ejemplo n.º 8
0
        internal void Handle(BufferEntity buffer)
        {
            //要移除掉已经发送的BufferEntity
            //int sn = buffer.sn;

            switch (buffer.messageType)
            {
            case 0:    //ACK确认报文
                BufferEntity buff;
                if (sendPackage.TryRemove(buffer.sn, out buff))
                {
                    Debug.Log($"报文已确认,序号:{buffer.sn}");
                }
                else
                {
                    Debug.Log($"要确认的报文不存在,序号:{buffer.sn}");
                }
                break;

            case 1:    //业务报文

                //if (buffer.sn!=1)
                //{
                //    return;//超时重发的测试代码
                //}
                BufferEntity ackPackage = new BufferEntity(buffer);
                uSocket.SendACK(ackPackage, endPoint);

                Debug.Log("收到的是业务报文!");
                //再进行处理业务报文
                HandleLogincPackage(buffer);
                break;

            default:
                break;
            }
        }
Ejemplo n.º 9
0
 //发送ACK消息的接口
 public void SendACK(BufferEntity ackPackage, IPEndPoint endPoint)
 {
     Debug.Log("回复客户端收到消息了!");
     Send(ackPackage.buffer, endPoint);
 }