Ejemplo n.º 1
0
        private bool TryHandleOneProtocol(NetBuffer stream, uint connId)
        {
            ProtocolHeader head = ProtocolHeader.Deserialize(stream);

            if (head == null)
            {
                return(false);
            }

            if (stream.Length < head.dataSize + ProtocolHeader.Length)
            {
                return(false);
            }

            if (m_recvBufferTemp.Capacity < head.dataSize)
            {
                MyLogger.LogError(TAG, "OnRecv() no enough buffer! connId:{0}, dataSize:{1}, RecvBuffCapacity:{2}",
                                  connId.ToString(), head.dataSize, m_recvBufferTemp.Capacity);

                return(false);
            }

            stream.ReadBytes(m_recvBufferTemp.GetBytes(), 0, head.dataSize);
            m_recvBufferTemp.AddLength(head.dataSize, 0);

            ushort sum = SGFEncoder.CheckSum(m_recvBufferTemp.GetBytes(), head.dataSize);

            if (sum != head.checksum)
            {
                MyLogger.LogError(TAG, "OnRecv() CheckSum failed! connId:{0}, dataSize:{1}, RecvBuffSize:{2}",
                                  connId.ToString(), head.dataSize, m_recvBufferTemp.Length);

                //reset conncetion
                stream.Clear();
                return(false);
            }

            object ptlObj = DeserializeProtocol(head.pid, m_recvBufferTemp.GetBytes(), m_recvBufferTemp.Length);

            if (ptlObj != null)
            {
                DispatchProtocol(head.pid, head.index, ptlObj);
            }
            else
            {
                m_recvBufferTemp.Position = 0;
                byte[] buffer = m_recvBufferTemp.ReadBytes(head.dataSize);
                DispatchProtocol(head.pid, head.index, buffer);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public void Send(uint pid, object req, uint connId, Type rspType,
                         ProtocolListener listener, ErrorListener errorListener)
        {
            //regist rspType
            if (rspType != null)
            {
                Register(pid + 1, rspType);
            }

            //record listner
            uint index = 0;

            if (listener != null || errorListener != null)
            {
                index = ++m_index;

                PTLListenerHelper helper = new PTLListenerHelper();
                helper.listener      = listener;
                helper.errorListener = errorListener;
                helper.timestamp     = UnityEngine.Time.realtimeSinceStartup;
                helper.pid           = pid + 1;//this is a rule
                helper.index         = index;

                m_lstRspListener.Add(helper);
            }

            //serialize protocol
            byte[] dataBuffer = SerializeProtocol(req);

            //protocol header
            ProtocolHeader head = new ProtocolHeader();

            head.pid      = pid;
            head.index    = index;
            head.dataSize = dataBuffer.Length;
            head.checksum = SGFEncoder.CheckSum(dataBuffer, dataBuffer.Length);

            //serilize protocol header
            m_sendBufferTemp.Position = 0;
            head.Serialize(m_sendBufferTemp);

            //combine buffer of the protocol
            m_sendBufferTemp.AddBytes(dataBuffer);

            ConnectManager.Instance.Send(connId, m_sendBufferTemp.GetBytes(), m_sendBufferTemp.Length);
        }